aidev-bkplugin 2.2.2rc50__tar.gz → 2.2.2rc52__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (110) hide show
  1. {aidev_bkplugin-2.2.2rc50 → aidev_bkplugin-2.2.2rc52}/PKG-INFO +2 -2
  2. {aidev_bkplugin-2.2.2rc50 → aidev_bkplugin-2.2.2rc52}/aidev_bkplugin/apps.py +27 -1
  3. {aidev_bkplugin-2.2.2rc50 → aidev_bkplugin-2.2.2rc52}/aidev_bkplugin/services/otel_metrics.py +30 -37
  4. {aidev_bkplugin-2.2.2rc50 → aidev_bkplugin-2.2.2rc52}/aidev_bkplugin/tasks.py +1 -1
  5. {aidev_bkplugin-2.2.2rc50 → aidev_bkplugin-2.2.2rc52}/pyproject.toml +2 -2
  6. {aidev_bkplugin-2.2.2rc50 → aidev_bkplugin-2.2.2rc52}/tests/services/test_otel_metrics.py +43 -24
  7. {aidev_bkplugin-2.2.2rc50 → aidev_bkplugin-2.2.2rc52}/tests/test_apps.py +46 -8
  8. {aidev_bkplugin-2.2.2rc50 → aidev_bkplugin-2.2.2rc52}/tests/test_management_metrics.py +11 -9
  9. {aidev_bkplugin-2.2.2rc50 → aidev_bkplugin-2.2.2rc52}/aidev_bkplugin/__init__.py +0 -0
  10. {aidev_bkplugin-2.2.2rc50 → aidev_bkplugin-2.2.2rc52}/aidev_bkplugin/constants.py +0 -0
  11. {aidev_bkplugin-2.2.2rc50 → aidev_bkplugin-2.2.2rc52}/aidev_bkplugin/enums.py +0 -0
  12. {aidev_bkplugin-2.2.2rc50 → aidev_bkplugin-2.2.2rc52}/aidev_bkplugin/management/__init__.py +0 -0
  13. {aidev_bkplugin-2.2.2rc50 → aidev_bkplugin-2.2.2rc52}/aidev_bkplugin/management/commands/__init__.py +0 -0
  14. {aidev_bkplugin-2.2.2rc50 → aidev_bkplugin-2.2.2rc52}/aidev_bkplugin/management/commands/upgrade_sessions.py +0 -0
  15. {aidev_bkplugin-2.2.2rc50 → aidev_bkplugin-2.2.2rc52}/aidev_bkplugin/migrations/0001_initial.py +0 -0
  16. {aidev_bkplugin-2.2.2rc50 → aidev_bkplugin-2.2.2rc52}/aidev_bkplugin/migrations/0002_event_delivery.py +0 -0
  17. {aidev_bkplugin-2.2.2rc50 → aidev_bkplugin-2.2.2rc52}/aidev_bkplugin/migrations/__init__.py +0 -0
  18. {aidev_bkplugin-2.2.2rc50 → aidev_bkplugin-2.2.2rc52}/aidev_bkplugin/models.py +0 -0
  19. {aidev_bkplugin-2.2.2rc50 → aidev_bkplugin-2.2.2rc52}/aidev_bkplugin/openapi/__init__.py +0 -0
  20. {aidev_bkplugin-2.2.2rc50 → aidev_bkplugin-2.2.2rc52}/aidev_bkplugin/openapi/urls.py +0 -0
  21. {aidev_bkplugin-2.2.2rc50 → aidev_bkplugin-2.2.2rc52}/aidev_bkplugin/openapi/views.py +0 -0
  22. {aidev_bkplugin-2.2.2rc50 → aidev_bkplugin-2.2.2rc52}/aidev_bkplugin/packages/__init__.py +0 -0
  23. {aidev_bkplugin-2.2.2rc50 → aidev_bkplugin-2.2.2rc52}/aidev_bkplugin/packages/apigw/__init__.py +0 -0
  24. {aidev_bkplugin-2.2.2rc50 → aidev_bkplugin-2.2.2rc52}/aidev_bkplugin/packages/apigw/permissions.py +0 -0
  25. {aidev_bkplugin-2.2.2rc50 → aidev_bkplugin-2.2.2rc52}/aidev_bkplugin/packages/checkpoint/__init__.py +0 -0
  26. {aidev_bkplugin-2.2.2rc50 → aidev_bkplugin-2.2.2rc52}/aidev_bkplugin/packages/checkpoint/bk_django_saver.py +0 -0
  27. {aidev_bkplugin-2.2.2rc50 → aidev_bkplugin-2.2.2rc52}/aidev_bkplugin/packages/drf/__init__.py +0 -0
  28. {aidev_bkplugin-2.2.2rc50 → aidev_bkplugin-2.2.2rc52}/aidev_bkplugin/packages/drf/authentication.py +0 -0
  29. {aidev_bkplugin-2.2.2rc50 → aidev_bkplugin-2.2.2rc52}/aidev_bkplugin/packages/drf/decorators.py +0 -0
  30. {aidev_bkplugin-2.2.2rc50 → aidev_bkplugin-2.2.2rc52}/aidev_bkplugin/packages/drf/exception.py +0 -0
  31. {aidev_bkplugin-2.2.2rc50 → aidev_bkplugin-2.2.2rc52}/aidev_bkplugin/packages/drf/renderers.py +0 -0
  32. {aidev_bkplugin-2.2.2rc50 → aidev_bkplugin-2.2.2rc52}/aidev_bkplugin/permissions.py +0 -0
  33. {aidev_bkplugin-2.2.2rc50 → aidev_bkplugin-2.2.2rc52}/aidev_bkplugin/private/__init__.py +0 -0
  34. {aidev_bkplugin-2.2.2rc50 → aidev_bkplugin-2.2.2rc52}/aidev_bkplugin/private/permissions.py +0 -0
  35. {aidev_bkplugin-2.2.2rc50 → aidev_bkplugin-2.2.2rc52}/aidev_bkplugin/private/urls.py +0 -0
  36. {aidev_bkplugin-2.2.2rc50 → aidev_bkplugin-2.2.2rc52}/aidev_bkplugin/private/views.py +0 -0
  37. {aidev_bkplugin-2.2.2rc50 → aidev_bkplugin-2.2.2rc52}/aidev_bkplugin/serializers/__init__.py +0 -0
  38. {aidev_bkplugin-2.2.2rc50 → aidev_bkplugin-2.2.2rc52}/aidev_bkplugin/serializers/agent.py +0 -0
  39. {aidev_bkplugin-2.2.2rc50 → aidev_bkplugin-2.2.2rc52}/aidev_bkplugin/serializers/chat_completion.py +0 -0
  40. {aidev_bkplugin-2.2.2rc50 → aidev_bkplugin-2.2.2rc52}/aidev_bkplugin/serializers/llm.py +0 -0
  41. {aidev_bkplugin-2.2.2rc50 → aidev_bkplugin-2.2.2rc52}/aidev_bkplugin/serializers/user_operation.py +0 -0
  42. {aidev_bkplugin-2.2.2rc50 → aidev_bkplugin-2.2.2rc52}/aidev_bkplugin/services/__init__.py +0 -0
  43. {aidev_bkplugin-2.2.2rc50 → aidev_bkplugin-2.2.2rc52}/aidev_bkplugin/services/agent_bkplugin.py +0 -0
  44. {aidev_bkplugin-2.2.2rc50 → aidev_bkplugin-2.2.2rc52}/aidev_bkplugin/services/agent_builder.py +0 -0
  45. {aidev_bkplugin-2.2.2rc50 → aidev_bkplugin-2.2.2rc52}/aidev_bkplugin/services/agent_config.py +0 -0
  46. {aidev_bkplugin-2.2.2rc50 → aidev_bkplugin-2.2.2rc52}/aidev_bkplugin/services/agent_execution.py +0 -0
  47. {aidev_bkplugin-2.2.2rc50 → aidev_bkplugin-2.2.2rc52}/aidev_bkplugin/services/agent_helpers.py +0 -0
  48. {aidev_bkplugin-2.2.2rc50 → aidev_bkplugin-2.2.2rc52}/aidev_bkplugin/services/agent_session.py +0 -0
  49. {aidev_bkplugin-2.2.2rc50 → aidev_bkplugin-2.2.2rc52}/aidev_bkplugin/services/approval_resume.py +0 -0
  50. {aidev_bkplugin-2.2.2rc50 → aidev_bkplugin-2.2.2rc52}/aidev_bkplugin/services/chat_tracing.py +0 -0
  51. {aidev_bkplugin-2.2.2rc50 → aidev_bkplugin-2.2.2rc52}/aidev_bkplugin/services/database_event_bus.py +0 -0
  52. {aidev_bkplugin-2.2.2rc50 → aidev_bkplugin-2.2.2rc52}/aidev_bkplugin/services/event_resource_manager.py +0 -0
  53. {aidev_bkplugin-2.2.2rc50 → aidev_bkplugin-2.2.2rc52}/aidev_bkplugin/services/event_tracing.py +0 -0
  54. {aidev_bkplugin-2.2.2rc50 → aidev_bkplugin-2.2.2rc52}/aidev_bkplugin/services/execution.py +0 -0
  55. {aidev_bkplugin-2.2.2rc50 → aidev_bkplugin-2.2.2rc52}/aidev_bkplugin/services/llm.py +0 -0
  56. {aidev_bkplugin-2.2.2rc50 → aidev_bkplugin-2.2.2rc52}/aidev_bkplugin/services/metric_runtime.py +0 -0
  57. {aidev_bkplugin-2.2.2rc50 → aidev_bkplugin-2.2.2rc52}/aidev_bkplugin/services/user_operation.py +0 -0
  58. {aidev_bkplugin-2.2.2rc50 → aidev_bkplugin-2.2.2rc52}/aidev_bkplugin/settings.py +0 -0
  59. {aidev_bkplugin-2.2.2rc50 → aidev_bkplugin-2.2.2rc52}/aidev_bkplugin/urls.py +0 -0
  60. {aidev_bkplugin-2.2.2rc50 → aidev_bkplugin-2.2.2rc52}/aidev_bkplugin/utils.py +0 -0
  61. {aidev_bkplugin-2.2.2rc50 → aidev_bkplugin-2.2.2rc52}/aidev_bkplugin/views/__init__.py +0 -0
  62. {aidev_bkplugin-2.2.2rc50 → aidev_bkplugin-2.2.2rc52}/aidev_bkplugin/views/agent.py +0 -0
  63. {aidev_bkplugin-2.2.2rc50 → aidev_bkplugin-2.2.2rc52}/aidev_bkplugin/views/base.py +0 -0
  64. {aidev_bkplugin-2.2.2rc50 → aidev_bkplugin-2.2.2rc52}/aidev_bkplugin/views/chat.py +0 -0
  65. {aidev_bkplugin-2.2.2rc50 → aidev_bkplugin-2.2.2rc52}/aidev_bkplugin/views/chat_group.py +0 -0
  66. {aidev_bkplugin-2.2.2rc50 → aidev_bkplugin-2.2.2rc52}/aidev_bkplugin/views/flow_agent.py +0 -0
  67. {aidev_bkplugin-2.2.2rc50 → aidev_bkplugin-2.2.2rc52}/aidev_bkplugin/views/llm.py +0 -0
  68. {aidev_bkplugin-2.2.2rc50 → aidev_bkplugin-2.2.2rc52}/aidev_bkplugin/views/session.py +0 -0
  69. {aidev_bkplugin-2.2.2rc50 → aidev_bkplugin-2.2.2rc52}/aidev_bkplugin/views/user_operation.py +0 -0
  70. {aidev_bkplugin-2.2.2rc50 → aidev_bkplugin-2.2.2rc52}/readme.md +0 -0
  71. {aidev_bkplugin-2.2.2rc50 → aidev_bkplugin-2.2.2rc52}/tests/__init__.py +0 -0
  72. {aidev_bkplugin-2.2.2rc50 → aidev_bkplugin-2.2.2rc52}/tests/apps.py +0 -0
  73. {aidev_bkplugin-2.2.2rc50 → aidev_bkplugin-2.2.2rc52}/tests/conftest.py +0 -0
  74. {aidev_bkplugin-2.2.2rc50 → aidev_bkplugin-2.2.2rc52}/tests/database_events/__init__.py +0 -0
  75. {aidev_bkplugin-2.2.2rc50 → aidev_bkplugin-2.2.2rc52}/tests/database_events/conftest.py +0 -0
  76. {aidev_bkplugin-2.2.2rc50 → aidev_bkplugin-2.2.2rc52}/tests/database_events/test_bus.py +0 -0
  77. {aidev_bkplugin-2.2.2rc50 → aidev_bkplugin-2.2.2rc52}/tests/database_events/test_resource_manager.py +0 -0
  78. {aidev_bkplugin-2.2.2rc50 → aidev_bkplugin-2.2.2rc52}/tests/database_events/test_tracing.py +0 -0
  79. {aidev_bkplugin-2.2.2rc50 → aidev_bkplugin-2.2.2rc52}/tests/packages/__init__.py +0 -0
  80. {aidev_bkplugin-2.2.2rc50 → aidev_bkplugin-2.2.2rc52}/tests/packages/drf/test_renderers_trace.py +0 -0
  81. {aidev_bkplugin-2.2.2rc50 → aidev_bkplugin-2.2.2rc52}/tests/packages/test_bk_django_saver.py +0 -0
  82. {aidev_bkplugin-2.2.2rc50 → aidev_bkplugin-2.2.2rc52}/tests/packages/test_bk_django_saver_async.py +0 -0
  83. {aidev_bkplugin-2.2.2rc50 → aidev_bkplugin-2.2.2rc52}/tests/private/__init__.py +0 -0
  84. {aidev_bkplugin-2.2.2rc50 → aidev_bkplugin-2.2.2rc52}/tests/private/test_private_views.py +0 -0
  85. {aidev_bkplugin-2.2.2rc50 → aidev_bkplugin-2.2.2rc52}/tests/services/__init__.py +0 -0
  86. {aidev_bkplugin-2.2.2rc50 → aidev_bkplugin-2.2.2rc52}/tests/services/agent/__init__.py +0 -0
  87. {aidev_bkplugin-2.2.2rc50 → aidev_bkplugin-2.2.2rc52}/tests/services/agent/conftest.py +0 -0
  88. {aidev_bkplugin-2.2.2rc50 → aidev_bkplugin-2.2.2rc52}/tests/services/agent/test_agent_config_fetcher.py +0 -0
  89. {aidev_bkplugin-2.2.2rc50 → aidev_bkplugin-2.2.2rc52}/tests/services/agent/test_builder.py +0 -0
  90. {aidev_bkplugin-2.2.2rc50 → aidev_bkplugin-2.2.2rc52}/tests/services/agent/test_session_manager.py +0 -0
  91. {aidev_bkplugin-2.2.2rc50 → aidev_bkplugin-2.2.2rc52}/tests/services/test_agent_completion.py +0 -0
  92. {aidev_bkplugin-2.2.2rc50 → aidev_bkplugin-2.2.2rc52}/tests/services/test_agui_handler.py +0 -0
  93. {aidev_bkplugin-2.2.2rc50 → aidev_bkplugin-2.2.2rc52}/tests/services/test_approval_resume.py +0 -0
  94. {aidev_bkplugin-2.2.2rc50 → aidev_bkplugin-2.2.2rc52}/tests/services/test_approval_resume_tracing.py +0 -0
  95. {aidev_bkplugin-2.2.2rc50 → aidev_bkplugin-2.2.2rc52}/tests/services/test_bkplugin.py +0 -0
  96. {aidev_bkplugin-2.2.2rc50 → aidev_bkplugin-2.2.2rc52}/tests/services/test_execution.py +0 -0
  97. {aidev_bkplugin-2.2.2rc50 → aidev_bkplugin-2.2.2rc52}/tests/services/test_llm.py +0 -0
  98. {aidev_bkplugin-2.2.2rc50 → aidev_bkplugin-2.2.2rc52}/tests/services/test_resume_consumer.py +0 -0
  99. {aidev_bkplugin-2.2.2rc50 → aidev_bkplugin-2.2.2rc52}/tests/services/test_thread_chat_completion.py +0 -0
  100. {aidev_bkplugin-2.2.2rc50 → aidev_bkplugin-2.2.2rc52}/tests/services/test_user_operation.py +0 -0
  101. {aidev_bkplugin-2.2.2rc50 → aidev_bkplugin-2.2.2rc52}/tests/settings.py +0 -0
  102. {aidev_bkplugin-2.2.2rc50 → aidev_bkplugin-2.2.2rc52}/tests/test_constants.py +0 -0
  103. {aidev_bkplugin-2.2.2rc50 → aidev_bkplugin-2.2.2rc52}/tests/views/__init__.py +0 -0
  104. {aidev_bkplugin-2.2.2rc50 → aidev_bkplugin-2.2.2rc52}/tests/views/test_agent_view.py +0 -0
  105. {aidev_bkplugin-2.2.2rc50 → aidev_bkplugin-2.2.2rc52}/tests/views/test_chat_agent_code_propagation.py +0 -0
  106. {aidev_bkplugin-2.2.2rc50 → aidev_bkplugin-2.2.2rc52}/tests/views/test_chat_completion_serializer.py +0 -0
  107. {aidev_bkplugin-2.2.2rc50 → aidev_bkplugin-2.2.2rc52}/tests/views/test_chat_flow_agent.py +0 -0
  108. {aidev_bkplugin-2.2.2rc50 → aidev_bkplugin-2.2.2rc52}/tests/views/test_chat_stream_status.py +0 -0
  109. {aidev_bkplugin-2.2.2rc50 → aidev_bkplugin-2.2.2rc52}/tests/views/test_session_pv_files_views.py +0 -0
  110. {aidev_bkplugin-2.2.2rc50 → aidev_bkplugin-2.2.2rc52}/tests/views/test_session_view.py +0 -0
@@ -1,9 +1,9 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: aidev-bkplugin
3
- Version: 2.2.2rc50
3
+ Version: 2.2.2rc52
4
4
  Summary: AiDev Agent Bkplugin builtin functions
5
5
  Requires-Python: <3.12,>=3.11
6
- Requires-Dist: aidev-agent>=2.2.2rc50
6
+ Requires-Dist: aidev-agent>=2.2.2rc52
7
7
  Requires-Dist: blueapps<5.0,>=4.15.1
8
8
  Requires-Dist: django<5.0,>=4.2.28
9
9
  Provides-Extra: opentelemetry
@@ -62,6 +62,25 @@ except ImportError:
62
62
  logger = logging.getLogger(__name__)
63
63
 
64
64
 
65
+ def _deduplicate_otel_endpoints(endpoints: list[dict]) -> list[dict]:
66
+ """Keep the highest-priority config for each effective OTLP destination."""
67
+ deduplicated = []
68
+ seen = set()
69
+ for endpoint in endpoints:
70
+ exporter_type = endpoint.get("exporter_type")
71
+ exporter_type = getattr(exporter_type, "value", exporter_type)
72
+ identity = (
73
+ str(endpoint.get("url", "")).rstrip("/"),
74
+ str(exporter_type),
75
+ str(endpoint.get("token", "")),
76
+ )
77
+ if identity in seen:
78
+ continue
79
+ seen.add(identity)
80
+ deduplicated.append(endpoint)
81
+ return deduplicated
82
+
83
+
65
84
  def _is_one_off_management_command() -> bool:
66
85
  if not sys.argv or Path(sys.argv[0]).name not in {"manage.py", "django-admin", "django-admin.py"}:
67
86
  return False
@@ -129,6 +148,13 @@ def init_bk_aidev_agent_otel() -> None:
129
148
  endpoints.extend(get_otel_endpoint_by_agent_info(agent_info=agent_info))
130
149
  # 3. 从 OTEL_GRPC_URL 和 OTEL_BK_DATA_TOKEN 获取单地址
131
150
  endpoints.extend(get_otel_endpoint_by_env())
151
+ deduplicated_endpoints = _deduplicate_otel_endpoints(endpoints)
152
+ if len(deduplicated_endpoints) != len(endpoints):
153
+ logger.info(
154
+ "[aidev_bkplugin] removed %d duplicate OpenTelemetry endpoint(s)",
155
+ len(endpoints) - len(deduplicated_endpoints),
156
+ )
157
+ endpoints = deduplicated_endpoints
132
158
 
133
159
  otel_config.otel_endpoints = endpoints
134
160
  if configure_metric_identity is None or BkPluginMetricService is None or MetricExportSettings is None:
@@ -149,7 +175,7 @@ def init_bk_aidev_agent_otel() -> None:
149
175
  agent_info.get("agent_name"),
150
176
  agent_info.get("agent_sdk_version"),
151
177
  )
152
- if metric_settings.export_via_celery:
178
+ if metric_settings.has_bkm_config:
153
179
  metric_service = BkPluginMetricService(
154
180
  service_name=otel_config.service_name,
155
181
  endpoints=endpoints,
@@ -46,6 +46,7 @@ from .metric_runtime import RetryableMetricPushError
46
46
  logger = logging.getLogger(__name__)
47
47
 
48
48
  DEFAULT_METRIC_EXPORT_INTERVAL_MILLIS = 10_000
49
+ DEFAULT_METRIC_TASK_TTL_SECONDS = 3600
49
50
  BKM_PUSH_MODE_CELERY = "celery"
50
51
  BKM_PUSH_MODE_DIRECT = "direct"
51
52
  BKM_PUSH_MODES = frozenset({BKM_PUSH_MODE_CELERY, BKM_PUSH_MODE_DIRECT})
@@ -79,59 +80,52 @@ class MetricExportSettings:
79
80
  enabled: bool
80
81
  export_interval_millis: int = DEFAULT_METRIC_EXPORT_INTERVAL_MILLIS
81
82
  export_timeout_millis: int = 30000
82
- export_via_celery: bool = True
83
83
  bkm_push_mode: str = BKM_PUSH_MODE_CELERY
84
84
  bkm_data_id: int | None = None
85
85
  bkm_access_token: str = ""
86
86
  bkm_push_url: str = ""
87
87
  bkm_target: str = ""
88
- task_ttl_seconds: int = agent_settings.BKAI_AGENT_METRICS_TASK_TTL_SECONDS
88
+ task_ttl_seconds: int = DEFAULT_METRIC_TASK_TTL_SECONDS
89
+
90
+ @property
91
+ def has_bkm_config(self) -> bool:
92
+ """Whether all credentials required for BKM export are configured."""
93
+ return self.bkm_data_id is not None and bool(self.bkm_access_token and self.bkm_push_url)
89
94
 
90
95
  @classmethod
91
96
  def from_agent_info(cls, agent_info: dict[str, Any] | None, *, default_enabled: bool) -> "MetricExportSettings":
92
97
  otel_info = (agent_info or {}).get("otel_info") or {}
93
98
  metrics_info = otel_info.get("metrics") or {}
94
- interval = metrics_info.get(
95
- "export_interval_millis",
96
- otel_info.get(
97
- "metric_export_interval_millis",
98
- agent_settings.BKAI_AGENT_METRICS_EXPORT_INTERVAL_MILLIS,
99
- ),
100
- )
99
+ interval = agent_settings.BKAI_AGENT_METRICS_EXPORT_INTERVAL_MILLIS
100
+ if interval is None:
101
+ interval = metrics_info.get(
102
+ "export_interval_millis",
103
+ otel_info.get("metric_export_interval_millis", DEFAULT_METRIC_EXPORT_INTERVAL_MILLIS),
104
+ )
101
105
  timeout = metrics_info.get(
102
106
  "export_timeout_millis",
103
107
  otel_info.get("metric_export_timeout_millis", 30000),
104
108
  )
105
- task_ttl_seconds = metrics_info.get(
106
- "task_ttl_seconds",
107
- agent_settings.BKAI_AGENT_METRICS_TASK_TTL_SECONDS,
108
- )
109
- export_via_celery = metrics_info.get(
110
- "export_via_celery",
111
- otel_info.get("metric_export_via_celery"),
112
- )
113
- bkm_push_mode = (
114
- str(
115
- metrics_info.get(
116
- "push_mode",
117
- otel_info.get("metric_push_mode", agent_settings.BKAI_AGENT_METRICS_PUSH_MODE),
118
- )
109
+ task_ttl_seconds = agent_settings.BKAI_AGENT_METRICS_TASK_TTL_SECONDS
110
+ if task_ttl_seconds is None:
111
+ task_ttl_seconds = metrics_info.get("task_ttl_seconds", DEFAULT_METRIC_TASK_TTL_SECONDS)
112
+ bkm_push_mode = agent_settings.BKAI_AGENT_METRICS_PUSH_MODE
113
+ if bkm_push_mode is None:
114
+ bkm_push_mode = metrics_info.get(
115
+ "push_mode",
116
+ otel_info.get("metric_push_mode", BKM_PUSH_MODE_CELERY),
119
117
  )
120
- .strip()
121
- .lower()
122
- )
118
+ bkm_push_mode = str(bkm_push_mode).strip().lower()
123
119
  if bkm_push_mode not in BKM_PUSH_MODES:
124
120
  raise ValueError(
125
121
  f"Unsupported metric push mode: {bkm_push_mode!r}; expected one of {sorted(BKM_PUSH_MODES)}"
126
122
  )
127
- data_id = metrics_info.get("agent_data_id", agent_settings.BKAI_AGENT_METRICS_DATA_ID)
128
- access_token = metrics_info.get("agent_access_token", agent_settings.BKAI_AGENT_METRICS_TOKEN)
129
- push_url = metrics_info.get("agent_push_url", agent_settings.BKAI_AGENT_METRICS_HOST)
123
+ data_id = agent_settings.BKAI_AGENT_METRICS_DATA_ID or metrics_info.get("agent_data_id")
124
+ access_token = agent_settings.BKAI_AGENT_METRICS_TOKEN or metrics_info.get("agent_access_token", "")
125
+ push_url = agent_settings.BKAI_AGENT_METRICS_HOST or metrics_info.get("agent_push_url", "")
130
126
  push_url = _normalize_bkm_push_url(push_url)
131
- target = metrics_info.get("agent_target", agent_settings.BKAI_AGENT_METRICS_TARGET)
127
+ target = agent_settings.BKAI_AGENT_METRICS_TARGET or metrics_info.get("agent_target", "")
132
128
  has_bkm_config = data_id not in (None, "") and bool(access_token and push_url)
133
- if export_via_celery is None:
134
- export_via_celery = has_bkm_config
135
129
  environment_enabled = agent_settings.BKAI_AGENT_ENABLE_METRICS
136
130
  enabled = (
137
131
  environment_enabled
@@ -140,10 +134,9 @@ class MetricExportSettings:
140
134
  )
141
135
  return cls(
142
136
  enabled=_as_bool(enabled),
143
- export_interval_millis=max(1000, int(interval)),
137
+ export_interval_millis=max(10_000, int(interval)),
144
138
  export_timeout_millis=max(1000, int(timeout)),
145
139
  task_ttl_seconds=max(1, int(task_ttl_seconds)),
146
- export_via_celery=_as_bool(export_via_celery),
147
140
  bkm_push_mode=bkm_push_mode,
148
141
  bkm_data_id=int(data_id) if data_id not in (None, "") else None,
149
142
  bkm_access_token=str(access_token or ""),
@@ -328,7 +321,7 @@ class CeleryMetricExporter(BkmMetricExporter):
328
321
  endpoint_key: str,
329
322
  target: str,
330
323
  enqueue: Callable[[str, str, int, int], Any],
331
- task_ttl_seconds: int = agent_settings.BKAI_AGENT_METRICS_TASK_TTL_SECONDS,
324
+ task_ttl_seconds: int = DEFAULT_METRIC_TASK_TTL_SECONDS,
332
325
  ) -> None:
333
326
  super().__init__(endpoint_key, target)
334
327
  self.task_ttl_seconds = task_ttl_seconds
@@ -374,7 +367,7 @@ class BkPluginMetricService:
374
367
  logger.info("[aidev_bkplugin] metric export disabled")
375
368
  return False
376
369
  readers = []
377
- if self.settings.export_via_celery:
370
+ if self.settings.has_bkm_config:
378
371
  if self.settings.bkm_push_mode == BKM_PUSH_MODE_CELERY and self.enqueue_bkm_metrics is None:
379
372
  logger.warning("[aidev_bkplugin] BKM metric export enabled but Celery enqueue is unavailable")
380
373
  return False
@@ -410,7 +403,7 @@ class BkPluginMetricService:
410
403
  self.provider.shutdown()
411
404
  self.provider = None
412
405
  return False
413
- transport = f"bkm_{self.settings.bkm_push_mode}" if self.settings.export_via_celery else "otlp_direct"
406
+ transport = f"bkm_{self.settings.bkm_push_mode}" if self.settings.has_bkm_config else "otlp_direct"
414
407
  logger.info(
415
408
  "[aidev_bkplugin] metric export started with %d reader(s), transport=%s",
416
409
  len(readers),
@@ -19,7 +19,7 @@ logger = logging.getLogger(__name__)
19
19
  @shared_task(
20
20
  name="aidev_bkplugin.push_bkm_metrics",
21
21
  ignore_result=True,
22
- queue=agent_settings.BKAI_AGENT_TASK,
22
+ queue=agent_settings.BKAI_AGENT_QUEUE_METRIC,
23
23
  autoretry_for=(RetryableMetricPushError,),
24
24
  retry_backoff=True,
25
25
  retry_jitter=True,
@@ -1,11 +1,11 @@
1
1
  [project]
2
2
  name = "aidev-bkplugin"
3
- version = "2.2.2rc50"
3
+ version = "2.2.2rc52"
4
4
  description = "AiDev Agent Bkplugin builtin functions"
5
5
  readme = "readme.md"
6
6
  requires-python = "<3.12,>=3.11"
7
7
  dependencies = [
8
- "aidev-agent>=2.2.2rc50",
8
+ "aidev-agent>=2.2.2rc52",
9
9
  "blueapps>=4.15.1, <5.0",
10
10
  "django>=4.2.28,<5.0",
11
11
  ]
@@ -50,13 +50,13 @@ def isolate_agent_metric_settings(monkeypatch):
50
50
 
51
51
  defaults = {
52
52
  "BKAI_AGENT_ENABLE_METRICS": None,
53
- "BKAI_AGENT_METRICS_EXPORT_INTERVAL_MILLIS": 10_000,
54
- "BKAI_AGENT_METRICS_PUSH_MODE": BKM_PUSH_MODE_CELERY,
53
+ "BKAI_AGENT_METRICS_EXPORT_INTERVAL_MILLIS": None,
54
+ "BKAI_AGENT_METRICS_PUSH_MODE": None,
55
55
  "BKAI_AGENT_METRICS_DATA_ID": "",
56
56
  "BKAI_AGENT_METRICS_TOKEN": "",
57
57
  "BKAI_AGENT_METRICS_HOST": "",
58
58
  "BKAI_AGENT_METRICS_TARGET": "",
59
- "BKAI_AGENT_METRICS_TASK_TTL_SECONDS": 3600,
59
+ "BKAI_AGENT_METRICS_TASK_TTL_SECONDS": None,
60
60
  }
61
61
  for name, value in defaults.items():
62
62
  monkeypatch.setattr(otel_metrics.agent_settings, name, value)
@@ -158,28 +158,28 @@ def test_metric_settings_parse_nested_otel_info():
158
158
  )
159
159
 
160
160
  assert settings.enabled is True
161
- assert settings.export_interval_millis == 1500
161
+ assert settings.export_interval_millis == 10_000
162
162
  assert settings.export_timeout_millis == 7000
163
163
  assert settings.task_ttl_seconds == 120
164
- assert settings.export_via_celery is True
164
+ assert settings.has_bkm_config is True
165
165
  assert settings.bkm_data_id == 1001
166
166
  assert settings.bkm_access_token == "metric-secret"
167
167
  assert settings.bkm_push_url == "http://proxy:10205/v2/push/"
168
168
  assert settings.bkm_target == "127.0.0.1"
169
169
 
170
170
 
171
- def test_metric_settings_prefer_platform_interval_over_agent_setting(monkeypatch):
171
+ def test_metric_settings_prefer_agent_interval_over_platform_setting(monkeypatch):
172
172
  from aidev_bkplugin.services import otel_metrics
173
173
 
174
- monkeypatch.setattr(otel_metrics.agent_settings, "BKAI_AGENT_METRICS_EXPORT_INTERVAL_MILLIS", 2500)
175
- agent_info = {"otel_info": {"metrics": {"export_interval_millis": 1500}}}
174
+ monkeypatch.setattr(otel_metrics.agent_settings, "BKAI_AGENT_METRICS_EXPORT_INTERVAL_MILLIS", 25_000)
175
+ agent_info = {"otel_info": {"metrics": {"export_interval_millis": 15_000}}}
176
176
 
177
177
  settings = MetricExportSettings.from_agent_info(agent_info, default_enabled=False)
178
178
 
179
- assert settings.export_interval_millis == 1500
179
+ assert settings.export_interval_millis == 25_000
180
180
 
181
181
 
182
- def test_metric_settings_prefer_platform_push_mode_over_agent_setting(monkeypatch):
182
+ def test_metric_settings_prefer_agent_push_mode_over_platform_setting(monkeypatch):
183
183
  from aidev_bkplugin.services import otel_metrics
184
184
 
185
185
  monkeypatch.setattr(otel_metrics.agent_settings, "BKAI_AGENT_METRICS_PUSH_MODE", BKM_PUSH_MODE_DIRECT)
@@ -187,7 +187,18 @@ def test_metric_settings_prefer_platform_push_mode_over_agent_setting(monkeypatc
187
187
 
188
188
  settings = MetricExportSettings.from_agent_info(agent_info, default_enabled=False)
189
189
 
190
- assert settings.bkm_push_mode == BKM_PUSH_MODE_CELERY
190
+ assert settings.bkm_push_mode == BKM_PUSH_MODE_DIRECT
191
+
192
+
193
+ def test_metric_settings_prefer_agent_task_ttl_over_platform_setting(monkeypatch):
194
+ from aidev_bkplugin.services import otel_metrics
195
+
196
+ monkeypatch.setattr(otel_metrics.agent_settings, "BKAI_AGENT_METRICS_TASK_TTL_SECONDS", 1800)
197
+ agent_info = {"otel_info": {"metrics": {"task_ttl_seconds": 120}}}
198
+
199
+ settings = MetricExportSettings.from_agent_info(agent_info, default_enabled=False)
200
+
201
+ assert settings.task_ttl_seconds == 1800
191
202
 
192
203
 
193
204
  def test_metric_settings_reject_unknown_push_mode():
@@ -204,7 +215,7 @@ def test_metric_settings_use_local_environment_fallback(monkeypatch):
204
215
  monkeypatch.setattr(otel_metrics.agent_settings, "BKAI_AGENT_METRICS_TOKEN", "local-secret")
205
216
  monkeypatch.setattr(otel_metrics.agent_settings, "BKAI_AGENT_METRICS_HOST", "local-proxy")
206
217
  monkeypatch.setattr(otel_metrics.agent_settings, "BKAI_AGENT_METRICS_TARGET", "local-target")
207
- monkeypatch.setattr(otel_metrics.agent_settings, "BKAI_AGENT_METRICS_EXPORT_INTERVAL_MILLIS", 2500)
218
+ monkeypatch.setattr(otel_metrics.agent_settings, "BKAI_AGENT_METRICS_EXPORT_INTERVAL_MILLIS", 25_000)
208
219
  monkeypatch.setattr(otel_metrics.agent_settings, "BKAI_AGENT_METRICS_PUSH_MODE", BKM_PUSH_MODE_DIRECT)
209
220
  monkeypatch.setattr(otel_metrics.agent_settings, "BKAI_AGENT_METRICS_TASK_TTL_SECONDS", 1800)
210
221
 
@@ -215,8 +226,8 @@ def test_metric_settings_use_local_environment_fallback(monkeypatch):
215
226
  assert settings.bkm_access_token == "local-secret"
216
227
  assert settings.bkm_push_url == "http://local-proxy:10205/v2/push/"
217
228
  assert settings.bkm_target == "local-target"
218
- assert settings.export_via_celery is True
219
- assert settings.export_interval_millis == 2500
229
+ assert settings.has_bkm_config is True
230
+ assert settings.export_interval_millis == 25_000
220
231
  assert settings.bkm_push_mode == BKM_PUSH_MODE_DIRECT
221
232
  assert settings.task_ttl_seconds == 1800
222
233
 
@@ -225,7 +236,10 @@ def test_metric_settings_keep_direct_otlp_transport_without_bkm_config():
225
236
  settings = MetricExportSettings.from_agent_info({}, default_enabled=True)
226
237
 
227
238
  assert settings.enabled is True
228
- assert settings.export_via_celery is False
239
+ assert settings.export_interval_millis == 10_000
240
+ assert settings.bkm_push_mode == BKM_PUSH_MODE_CELERY
241
+ assert settings.task_ttl_seconds == 3600
242
+ assert settings.has_bkm_config is False
229
243
 
230
244
 
231
245
  def test_metric_settings_do_not_reuse_trace_credentials(monkeypatch):
@@ -251,10 +265,13 @@ def test_normalize_bkm_push_url(configured, expected):
251
265
  assert _normalize_bkm_push_url(configured) == expected
252
266
 
253
267
 
254
- def test_metric_settings_prefer_agent_info_over_local_environment(monkeypatch):
255
- monkeypatch.setenv("BKAI_AGENT_METRICS_DATA_ID", "1002")
256
- monkeypatch.setenv("BKAI_AGENT_METRICS_TOKEN", "local-secret")
257
- monkeypatch.setenv("BKAI_AGENT_METRICS_HOST", "local-proxy")
268
+ def test_metric_settings_prefer_local_bkm_setting_over_agent_info(monkeypatch):
269
+ from aidev_bkplugin.services import otel_metrics
270
+
271
+ monkeypatch.setattr(otel_metrics.agent_settings, "BKAI_AGENT_METRICS_DATA_ID", "1002")
272
+ monkeypatch.setattr(otel_metrics.agent_settings, "BKAI_AGENT_METRICS_TOKEN", "local-secret")
273
+ monkeypatch.setattr(otel_metrics.agent_settings, "BKAI_AGENT_METRICS_HOST", "local-proxy")
274
+ monkeypatch.setattr(otel_metrics.agent_settings, "BKAI_AGENT_METRICS_TARGET", "local-target")
258
275
 
259
276
  settings = MetricExportSettings.from_agent_info(
260
277
  {
@@ -263,15 +280,17 @@ def test_metric_settings_prefer_agent_info_over_local_environment(monkeypatch):
263
280
  "agent_data_id": 1003,
264
281
  "agent_access_token": "platform-secret",
265
282
  "agent_push_url": "http://platform-proxy:10205/v2/push/",
283
+ "agent_target": "platform-target",
266
284
  }
267
285
  }
268
286
  },
269
287
  default_enabled=False,
270
288
  )
271
289
 
272
- assert settings.bkm_data_id == 1003
273
- assert settings.bkm_access_token == "platform-secret"
274
- assert settings.bkm_push_url == "http://platform-proxy:10205/v2/push/"
290
+ assert settings.bkm_data_id == 1002
291
+ assert settings.bkm_access_token == "local-secret"
292
+ assert settings.bkm_push_url == "http://local-proxy:10205/v2/push/"
293
+ assert settings.bkm_target == "local-target"
275
294
 
276
295
 
277
296
  def test_metric_settings_parse_false_string_safely():
@@ -563,8 +582,8 @@ def test_celery_task_exports_through_process_local_metric_service(mocker):
563
582
  service.push_bkm.assert_called_once_with("endpoint-fingerprint", "payload")
564
583
  assert tasks.push_bkm_metrics_task.shared_task_options["autoretry_for"] == (RetryableMetricPushError,)
565
584
  assert tasks.push_bkm_metrics_task.shared_task_options["max_retries"] == 3
566
- assert tasks.agent_settings.BKAI_AGENT_TASK == "bkai_agent_task"
567
- assert tasks.push_bkm_metrics_task.shared_task_options["queue"] == "bkai_agent_task"
585
+ assert tasks.agent_settings.BKAI_AGENT_QUEUE_METRIC == "bkai_agent_metric"
586
+ assert tasks.push_bkm_metrics_task.shared_task_options["queue"] == "bkai_agent_metric"
568
587
  assert tasks.run_bkplugin_background_agent_task.shared_task_options["queue"] == "bkai_agent_task"
569
588
 
570
589
 
@@ -2,6 +2,7 @@ from types import SimpleNamespace
2
2
 
3
3
  import aidev_agent.packages as aidev_agent_packages
4
4
  import pytest
5
+ from aidev_agent.packages.opentelemetry.utils import ExporterType
5
6
  from aidev_bkplugin import apps
6
7
 
7
8
 
@@ -65,6 +66,40 @@ def _mock_otel_inputs(mocker, agent_info, metric_settings):
65
66
  return otel_config, instrumentor, get_agent_info
66
67
 
67
68
 
69
+ def test_deduplicate_otel_endpoints_keeps_first_effective_destination():
70
+ preferred = {"url": "https://collector.example/api", "token": "token", "exporter_type": ExporterType.GRPC}
71
+ duplicate = {"url": "https://collector.example/api/", "token": "token", "exporter_type": ExporterType.GRPC}
72
+ different_token = {"url": preferred["url"], "token": "other-token", "exporter_type": ExporterType.GRPC}
73
+ different_protocol = {"url": preferred["url"], "token": "token", "exporter_type": ExporterType.HTTP}
74
+
75
+ assert apps._deduplicate_otel_endpoints([preferred, duplicate, different_token, different_protocol]) == [
76
+ preferred,
77
+ different_token,
78
+ different_protocol,
79
+ ]
80
+
81
+
82
+ def test_init_otel_deduplicates_endpoint_sources_before_instrumenting(mocker):
83
+ endpoint = {"url": "https://collector.example/api", "token": "token", "exporter_type": ExporterType.GRPC}
84
+ metric_settings = SimpleNamespace(
85
+ enabled=False,
86
+ has_bkm_config=False,
87
+ export_interval_millis=1500,
88
+ export_timeout_millis=7000,
89
+ )
90
+ otel_config, instrumentor, _ = _mock_otel_inputs(mocker, {}, metric_settings)
91
+ apps.get_otel_endpoint_by_json_str.return_value = [endpoint]
92
+ apps.get_otel_endpoint_by_agent_info.return_value = [{**endpoint}]
93
+ apps.get_otel_endpoint_by_env.return_value = [{**endpoint}]
94
+ mocker.patch.object(apps, "configure_metric_identity")
95
+ mocker.patch.object(apps, "set_metric_service")
96
+
97
+ apps.init_bk_aidev_agent_otel()
98
+
99
+ assert otel_config.otel_endpoints == [endpoint]
100
+ instrumentor.instrument.assert_called_once_with()
101
+
102
+
68
103
  @pytest.mark.parametrize(
69
104
  "argv",
70
105
  [
@@ -113,11 +148,14 @@ def test_management_commands_disable_metrics_and_traces_before_remote_config(moc
113
148
  ],
114
149
  )
115
150
  @pytest.mark.parametrize("enabled", [False, True])
116
- @pytest.mark.parametrize("via_celery", [False, True])
117
- def test_service_entries_preserve_metric_configuration(mocker, monkeypatch, argv, enabled, via_celery):
151
+ @pytest.mark.parametrize("has_bkm_config", [False, True])
152
+ def test_service_entries_preserve_metric_configuration(mocker, monkeypatch, argv, enabled, has_bkm_config):
118
153
  monkeypatch.setattr(apps.sys, "argv", argv)
119
154
  metric_settings = SimpleNamespace(
120
- enabled=enabled, export_via_celery=via_celery, export_interval_millis=1500, export_timeout_millis=7000
155
+ enabled=enabled,
156
+ has_bkm_config=has_bkm_config,
157
+ export_interval_millis=1500,
158
+ export_timeout_millis=7000,
121
159
  )
122
160
  config, instrumentor, _ = _mock_otel_inputs(mocker, {}, metric_settings)
123
161
  config.enable_traces = not enabled
@@ -130,8 +168,8 @@ def test_service_entries_preserve_metric_configuration(mocker, monkeypatch, argv
130
168
 
131
169
  assert config.enable_metrics is enabled
132
170
  assert config.enable_traces is (not enabled)
133
- assert config.metric_provider_managed_externally is (enabled and via_celery)
134
- assert metric_service.called is via_celery
171
+ assert config.metric_provider_managed_externally is (enabled and has_bkm_config)
172
+ assert metric_service.called is has_bkm_config
135
173
  apps.MetricExportSettings.from_agent_info.assert_called_once()
136
174
  instrumentor.instrument.assert_called_once_with()
137
175
 
@@ -142,7 +180,7 @@ def test_init_otel_keeps_direct_metric_export_in_agent_sdk(mocker, monkeypatch):
142
180
  monkeypatch.delattr(aidev_agent_packages, "opentelemetry", raising=False)
143
181
  settings = SimpleNamespace(
144
182
  enabled=True,
145
- export_via_celery=False,
183
+ has_bkm_config=False,
146
184
  export_interval_millis=1500,
147
185
  export_timeout_millis=7000,
148
186
  )
@@ -167,10 +205,10 @@ def test_init_otel_keeps_direct_metric_export_in_agent_sdk(mocker, monkeypatch):
167
205
  instrumentor.instrument.assert_called_once_with()
168
206
 
169
207
 
170
- def test_init_otel_uses_bkplugin_metric_provider_for_celery_export(mocker):
208
+ def test_init_otel_uses_bkplugin_metric_provider_for_bkm_export(mocker):
171
209
  settings = SimpleNamespace(
172
210
  enabled=True,
173
- export_via_celery=True,
211
+ has_bkm_config=True,
174
212
  export_interval_millis=1500,
175
213
  export_timeout_millis=7000,
176
214
  )
@@ -50,13 +50,15 @@ EXIT_PROBE = dedent("""
50
50
  # Keep the real SDK provider lifecycle; avoid unrelated agent wrappers.
51
51
  return SimpleNamespace(instrument=BkAgentOTelService(config).start)
52
52
 
53
- via_celery = sys.argv[1] == "celery"
53
+ use_bkm = sys.argv[1] == "bkm"
54
54
  sys.argv = ["bin/manage.py", sys.argv[2]]
55
- agent_info = {"otel_info": {"metrics": {
56
- "enabled": True, "export_via_celery": via_celery,
57
- "agent_data_id": 1, "agent_access_token": "test-token",
58
- "agent_push_url": "http://collector.example.com/v2/push/",
59
- }}}
55
+ metrics_config = {"enabled": True}
56
+ if use_bkm:
57
+ metrics_config.update({
58
+ "agent_data_id": 1, "agent_access_token": "test-token",
59
+ "agent_push_url": "http://collector.example.com/v2/push/",
60
+ })
61
+ agent_info = {"otel_info": {"metrics": metrics_config}}
60
62
  endpoint = {"url": "http://collector.example.com", "token": "", "exporter_type": ExporterType.HTTP}
61
63
  with (
62
64
  patch.object(apps, "get_otel_endpoint_by_json_str", return_value=[endpoint]),
@@ -67,7 +69,7 @@ EXIT_PROBE = dedent("""
67
69
  patch.object(apps, "enqueue_bkm_metrics_task", lambda *args: None),
68
70
  patch.object(BkAgentOTelService, "_create_metric_exporter", side_effect=lambda *args: SlowExporter()),
69
71
  patch.object(BkAgentOTelService, "_create_trace_exporter", side_effect=lambda *args: SlowSpanExporter()),
70
- patch.object(BkPluginMetricService, "_create_celery_exporter", side_effect=lambda: SlowExporter()),
72
+ patch.object(BkPluginMetricService, "_create_bkm_exporter", side_effect=lambda: SlowExporter()),
71
73
  ):
72
74
  apps.init_bk_aidev_agent_otel()
73
75
  metrics.get_meter("exit-probe").create_counter("probe.count").add(1)
@@ -77,13 +79,13 @@ EXIT_PROBE = dedent("""
77
79
  """)
78
80
 
79
81
 
80
- @pytest.mark.parametrize("transport", ["direct", "celery"])
82
+ @pytest.mark.parametrize("transport", ["otlp", "bkm"])
81
83
  @pytest.mark.parametrize("command", ["migrate", "upgrade_sessions"])
82
84
  def test_management_initialization_exits_without_metric_or_trace_export(transport, command):
83
85
  env = {
84
86
  **os.environ,
85
87
  "DJANGO_SETTINGS_MODULE": "tests.settings",
86
- "PYTHONPATH": os.pathsep.join([str(PLUGIN_ROOT), str(AGENT_ROOT)]),
88
+ "PYTHONPATH": os.pathsep.join([str(PLUGIN_ROOT), str(AGENT_ROOT), os.environ.get("PYTHONPATH", "")]),
87
89
  "BKAI_AGENT_OTEL_ENABLED": "true",
88
90
  "BKAI_AGENT_ENABLE_METRICS": "true",
89
91
  "BKAI_AGENT_ENABLE_TRACES": "true",