aidev-bkplugin 2.2.2rc49__tar.gz → 2.2.2rc51__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.2rc49 → aidev_bkplugin-2.2.2rc51}/PKG-INFO +2 -2
  2. {aidev_bkplugin-2.2.2rc49 → aidev_bkplugin-2.2.2rc51}/aidev_bkplugin/apps.py +26 -0
  3. {aidev_bkplugin-2.2.2rc49 → aidev_bkplugin-2.2.2rc51}/aidev_bkplugin/services/otel_metrics.py +79 -34
  4. {aidev_bkplugin-2.2.2rc49 → aidev_bkplugin-2.2.2rc51}/pyproject.toml +2 -2
  5. {aidev_bkplugin-2.2.2rc49 → aidev_bkplugin-2.2.2rc51}/tests/services/test_otel_metrics.py +123 -11
  6. {aidev_bkplugin-2.2.2rc49 → aidev_bkplugin-2.2.2rc51}/tests/test_apps.py +35 -0
  7. {aidev_bkplugin-2.2.2rc49 → aidev_bkplugin-2.2.2rc51}/tests/test_management_metrics.py +1 -1
  8. {aidev_bkplugin-2.2.2rc49 → aidev_bkplugin-2.2.2rc51}/aidev_bkplugin/__init__.py +0 -0
  9. {aidev_bkplugin-2.2.2rc49 → aidev_bkplugin-2.2.2rc51}/aidev_bkplugin/constants.py +0 -0
  10. {aidev_bkplugin-2.2.2rc49 → aidev_bkplugin-2.2.2rc51}/aidev_bkplugin/enums.py +0 -0
  11. {aidev_bkplugin-2.2.2rc49 → aidev_bkplugin-2.2.2rc51}/aidev_bkplugin/management/__init__.py +0 -0
  12. {aidev_bkplugin-2.2.2rc49 → aidev_bkplugin-2.2.2rc51}/aidev_bkplugin/management/commands/__init__.py +0 -0
  13. {aidev_bkplugin-2.2.2rc49 → aidev_bkplugin-2.2.2rc51}/aidev_bkplugin/management/commands/upgrade_sessions.py +0 -0
  14. {aidev_bkplugin-2.2.2rc49 → aidev_bkplugin-2.2.2rc51}/aidev_bkplugin/migrations/0001_initial.py +0 -0
  15. {aidev_bkplugin-2.2.2rc49 → aidev_bkplugin-2.2.2rc51}/aidev_bkplugin/migrations/0002_event_delivery.py +0 -0
  16. {aidev_bkplugin-2.2.2rc49 → aidev_bkplugin-2.2.2rc51}/aidev_bkplugin/migrations/__init__.py +0 -0
  17. {aidev_bkplugin-2.2.2rc49 → aidev_bkplugin-2.2.2rc51}/aidev_bkplugin/models.py +0 -0
  18. {aidev_bkplugin-2.2.2rc49 → aidev_bkplugin-2.2.2rc51}/aidev_bkplugin/openapi/__init__.py +0 -0
  19. {aidev_bkplugin-2.2.2rc49 → aidev_bkplugin-2.2.2rc51}/aidev_bkplugin/openapi/urls.py +0 -0
  20. {aidev_bkplugin-2.2.2rc49 → aidev_bkplugin-2.2.2rc51}/aidev_bkplugin/openapi/views.py +0 -0
  21. {aidev_bkplugin-2.2.2rc49 → aidev_bkplugin-2.2.2rc51}/aidev_bkplugin/packages/__init__.py +0 -0
  22. {aidev_bkplugin-2.2.2rc49 → aidev_bkplugin-2.2.2rc51}/aidev_bkplugin/packages/apigw/__init__.py +0 -0
  23. {aidev_bkplugin-2.2.2rc49 → aidev_bkplugin-2.2.2rc51}/aidev_bkplugin/packages/apigw/permissions.py +0 -0
  24. {aidev_bkplugin-2.2.2rc49 → aidev_bkplugin-2.2.2rc51}/aidev_bkplugin/packages/checkpoint/__init__.py +0 -0
  25. {aidev_bkplugin-2.2.2rc49 → aidev_bkplugin-2.2.2rc51}/aidev_bkplugin/packages/checkpoint/bk_django_saver.py +0 -0
  26. {aidev_bkplugin-2.2.2rc49 → aidev_bkplugin-2.2.2rc51}/aidev_bkplugin/packages/drf/__init__.py +0 -0
  27. {aidev_bkplugin-2.2.2rc49 → aidev_bkplugin-2.2.2rc51}/aidev_bkplugin/packages/drf/authentication.py +0 -0
  28. {aidev_bkplugin-2.2.2rc49 → aidev_bkplugin-2.2.2rc51}/aidev_bkplugin/packages/drf/decorators.py +0 -0
  29. {aidev_bkplugin-2.2.2rc49 → aidev_bkplugin-2.2.2rc51}/aidev_bkplugin/packages/drf/exception.py +0 -0
  30. {aidev_bkplugin-2.2.2rc49 → aidev_bkplugin-2.2.2rc51}/aidev_bkplugin/packages/drf/renderers.py +0 -0
  31. {aidev_bkplugin-2.2.2rc49 → aidev_bkplugin-2.2.2rc51}/aidev_bkplugin/permissions.py +0 -0
  32. {aidev_bkplugin-2.2.2rc49 → aidev_bkplugin-2.2.2rc51}/aidev_bkplugin/private/__init__.py +0 -0
  33. {aidev_bkplugin-2.2.2rc49 → aidev_bkplugin-2.2.2rc51}/aidev_bkplugin/private/permissions.py +0 -0
  34. {aidev_bkplugin-2.2.2rc49 → aidev_bkplugin-2.2.2rc51}/aidev_bkplugin/private/urls.py +0 -0
  35. {aidev_bkplugin-2.2.2rc49 → aidev_bkplugin-2.2.2rc51}/aidev_bkplugin/private/views.py +0 -0
  36. {aidev_bkplugin-2.2.2rc49 → aidev_bkplugin-2.2.2rc51}/aidev_bkplugin/serializers/__init__.py +0 -0
  37. {aidev_bkplugin-2.2.2rc49 → aidev_bkplugin-2.2.2rc51}/aidev_bkplugin/serializers/agent.py +0 -0
  38. {aidev_bkplugin-2.2.2rc49 → aidev_bkplugin-2.2.2rc51}/aidev_bkplugin/serializers/chat_completion.py +0 -0
  39. {aidev_bkplugin-2.2.2rc49 → aidev_bkplugin-2.2.2rc51}/aidev_bkplugin/serializers/llm.py +0 -0
  40. {aidev_bkplugin-2.2.2rc49 → aidev_bkplugin-2.2.2rc51}/aidev_bkplugin/serializers/user_operation.py +0 -0
  41. {aidev_bkplugin-2.2.2rc49 → aidev_bkplugin-2.2.2rc51}/aidev_bkplugin/services/__init__.py +0 -0
  42. {aidev_bkplugin-2.2.2rc49 → aidev_bkplugin-2.2.2rc51}/aidev_bkplugin/services/agent_bkplugin.py +0 -0
  43. {aidev_bkplugin-2.2.2rc49 → aidev_bkplugin-2.2.2rc51}/aidev_bkplugin/services/agent_builder.py +0 -0
  44. {aidev_bkplugin-2.2.2rc49 → aidev_bkplugin-2.2.2rc51}/aidev_bkplugin/services/agent_config.py +0 -0
  45. {aidev_bkplugin-2.2.2rc49 → aidev_bkplugin-2.2.2rc51}/aidev_bkplugin/services/agent_execution.py +0 -0
  46. {aidev_bkplugin-2.2.2rc49 → aidev_bkplugin-2.2.2rc51}/aidev_bkplugin/services/agent_helpers.py +0 -0
  47. {aidev_bkplugin-2.2.2rc49 → aidev_bkplugin-2.2.2rc51}/aidev_bkplugin/services/agent_session.py +0 -0
  48. {aidev_bkplugin-2.2.2rc49 → aidev_bkplugin-2.2.2rc51}/aidev_bkplugin/services/approval_resume.py +0 -0
  49. {aidev_bkplugin-2.2.2rc49 → aidev_bkplugin-2.2.2rc51}/aidev_bkplugin/services/chat_tracing.py +0 -0
  50. {aidev_bkplugin-2.2.2rc49 → aidev_bkplugin-2.2.2rc51}/aidev_bkplugin/services/database_event_bus.py +0 -0
  51. {aidev_bkplugin-2.2.2rc49 → aidev_bkplugin-2.2.2rc51}/aidev_bkplugin/services/event_resource_manager.py +0 -0
  52. {aidev_bkplugin-2.2.2rc49 → aidev_bkplugin-2.2.2rc51}/aidev_bkplugin/services/event_tracing.py +0 -0
  53. {aidev_bkplugin-2.2.2rc49 → aidev_bkplugin-2.2.2rc51}/aidev_bkplugin/services/execution.py +0 -0
  54. {aidev_bkplugin-2.2.2rc49 → aidev_bkplugin-2.2.2rc51}/aidev_bkplugin/services/llm.py +0 -0
  55. {aidev_bkplugin-2.2.2rc49 → aidev_bkplugin-2.2.2rc51}/aidev_bkplugin/services/metric_runtime.py +0 -0
  56. {aidev_bkplugin-2.2.2rc49 → aidev_bkplugin-2.2.2rc51}/aidev_bkplugin/services/user_operation.py +0 -0
  57. {aidev_bkplugin-2.2.2rc49 → aidev_bkplugin-2.2.2rc51}/aidev_bkplugin/settings.py +0 -0
  58. {aidev_bkplugin-2.2.2rc49 → aidev_bkplugin-2.2.2rc51}/aidev_bkplugin/tasks.py +0 -0
  59. {aidev_bkplugin-2.2.2rc49 → aidev_bkplugin-2.2.2rc51}/aidev_bkplugin/urls.py +0 -0
  60. {aidev_bkplugin-2.2.2rc49 → aidev_bkplugin-2.2.2rc51}/aidev_bkplugin/utils.py +0 -0
  61. {aidev_bkplugin-2.2.2rc49 → aidev_bkplugin-2.2.2rc51}/aidev_bkplugin/views/__init__.py +0 -0
  62. {aidev_bkplugin-2.2.2rc49 → aidev_bkplugin-2.2.2rc51}/aidev_bkplugin/views/agent.py +0 -0
  63. {aidev_bkplugin-2.2.2rc49 → aidev_bkplugin-2.2.2rc51}/aidev_bkplugin/views/base.py +0 -0
  64. {aidev_bkplugin-2.2.2rc49 → aidev_bkplugin-2.2.2rc51}/aidev_bkplugin/views/chat.py +0 -0
  65. {aidev_bkplugin-2.2.2rc49 → aidev_bkplugin-2.2.2rc51}/aidev_bkplugin/views/chat_group.py +0 -0
  66. {aidev_bkplugin-2.2.2rc49 → aidev_bkplugin-2.2.2rc51}/aidev_bkplugin/views/flow_agent.py +0 -0
  67. {aidev_bkplugin-2.2.2rc49 → aidev_bkplugin-2.2.2rc51}/aidev_bkplugin/views/llm.py +0 -0
  68. {aidev_bkplugin-2.2.2rc49 → aidev_bkplugin-2.2.2rc51}/aidev_bkplugin/views/session.py +0 -0
  69. {aidev_bkplugin-2.2.2rc49 → aidev_bkplugin-2.2.2rc51}/aidev_bkplugin/views/user_operation.py +0 -0
  70. {aidev_bkplugin-2.2.2rc49 → aidev_bkplugin-2.2.2rc51}/readme.md +0 -0
  71. {aidev_bkplugin-2.2.2rc49 → aidev_bkplugin-2.2.2rc51}/tests/__init__.py +0 -0
  72. {aidev_bkplugin-2.2.2rc49 → aidev_bkplugin-2.2.2rc51}/tests/apps.py +0 -0
  73. {aidev_bkplugin-2.2.2rc49 → aidev_bkplugin-2.2.2rc51}/tests/conftest.py +0 -0
  74. {aidev_bkplugin-2.2.2rc49 → aidev_bkplugin-2.2.2rc51}/tests/database_events/__init__.py +0 -0
  75. {aidev_bkplugin-2.2.2rc49 → aidev_bkplugin-2.2.2rc51}/tests/database_events/conftest.py +0 -0
  76. {aidev_bkplugin-2.2.2rc49 → aidev_bkplugin-2.2.2rc51}/tests/database_events/test_bus.py +0 -0
  77. {aidev_bkplugin-2.2.2rc49 → aidev_bkplugin-2.2.2rc51}/tests/database_events/test_resource_manager.py +0 -0
  78. {aidev_bkplugin-2.2.2rc49 → aidev_bkplugin-2.2.2rc51}/tests/database_events/test_tracing.py +0 -0
  79. {aidev_bkplugin-2.2.2rc49 → aidev_bkplugin-2.2.2rc51}/tests/packages/__init__.py +0 -0
  80. {aidev_bkplugin-2.2.2rc49 → aidev_bkplugin-2.2.2rc51}/tests/packages/drf/test_renderers_trace.py +0 -0
  81. {aidev_bkplugin-2.2.2rc49 → aidev_bkplugin-2.2.2rc51}/tests/packages/test_bk_django_saver.py +0 -0
  82. {aidev_bkplugin-2.2.2rc49 → aidev_bkplugin-2.2.2rc51}/tests/packages/test_bk_django_saver_async.py +0 -0
  83. {aidev_bkplugin-2.2.2rc49 → aidev_bkplugin-2.2.2rc51}/tests/private/__init__.py +0 -0
  84. {aidev_bkplugin-2.2.2rc49 → aidev_bkplugin-2.2.2rc51}/tests/private/test_private_views.py +0 -0
  85. {aidev_bkplugin-2.2.2rc49 → aidev_bkplugin-2.2.2rc51}/tests/services/__init__.py +0 -0
  86. {aidev_bkplugin-2.2.2rc49 → aidev_bkplugin-2.2.2rc51}/tests/services/agent/__init__.py +0 -0
  87. {aidev_bkplugin-2.2.2rc49 → aidev_bkplugin-2.2.2rc51}/tests/services/agent/conftest.py +0 -0
  88. {aidev_bkplugin-2.2.2rc49 → aidev_bkplugin-2.2.2rc51}/tests/services/agent/test_agent_config_fetcher.py +0 -0
  89. {aidev_bkplugin-2.2.2rc49 → aidev_bkplugin-2.2.2rc51}/tests/services/agent/test_builder.py +0 -0
  90. {aidev_bkplugin-2.2.2rc49 → aidev_bkplugin-2.2.2rc51}/tests/services/agent/test_session_manager.py +0 -0
  91. {aidev_bkplugin-2.2.2rc49 → aidev_bkplugin-2.2.2rc51}/tests/services/test_agent_completion.py +0 -0
  92. {aidev_bkplugin-2.2.2rc49 → aidev_bkplugin-2.2.2rc51}/tests/services/test_agui_handler.py +0 -0
  93. {aidev_bkplugin-2.2.2rc49 → aidev_bkplugin-2.2.2rc51}/tests/services/test_approval_resume.py +0 -0
  94. {aidev_bkplugin-2.2.2rc49 → aidev_bkplugin-2.2.2rc51}/tests/services/test_approval_resume_tracing.py +0 -0
  95. {aidev_bkplugin-2.2.2rc49 → aidev_bkplugin-2.2.2rc51}/tests/services/test_bkplugin.py +0 -0
  96. {aidev_bkplugin-2.2.2rc49 → aidev_bkplugin-2.2.2rc51}/tests/services/test_execution.py +0 -0
  97. {aidev_bkplugin-2.2.2rc49 → aidev_bkplugin-2.2.2rc51}/tests/services/test_llm.py +0 -0
  98. {aidev_bkplugin-2.2.2rc49 → aidev_bkplugin-2.2.2rc51}/tests/services/test_resume_consumer.py +0 -0
  99. {aidev_bkplugin-2.2.2rc49 → aidev_bkplugin-2.2.2rc51}/tests/services/test_thread_chat_completion.py +0 -0
  100. {aidev_bkplugin-2.2.2rc49 → aidev_bkplugin-2.2.2rc51}/tests/services/test_user_operation.py +0 -0
  101. {aidev_bkplugin-2.2.2rc49 → aidev_bkplugin-2.2.2rc51}/tests/settings.py +0 -0
  102. {aidev_bkplugin-2.2.2rc49 → aidev_bkplugin-2.2.2rc51}/tests/test_constants.py +0 -0
  103. {aidev_bkplugin-2.2.2rc49 → aidev_bkplugin-2.2.2rc51}/tests/views/__init__.py +0 -0
  104. {aidev_bkplugin-2.2.2rc49 → aidev_bkplugin-2.2.2rc51}/tests/views/test_agent_view.py +0 -0
  105. {aidev_bkplugin-2.2.2rc49 → aidev_bkplugin-2.2.2rc51}/tests/views/test_chat_agent_code_propagation.py +0 -0
  106. {aidev_bkplugin-2.2.2rc49 → aidev_bkplugin-2.2.2rc51}/tests/views/test_chat_completion_serializer.py +0 -0
  107. {aidev_bkplugin-2.2.2rc49 → aidev_bkplugin-2.2.2rc51}/tests/views/test_chat_flow_agent.py +0 -0
  108. {aidev_bkplugin-2.2.2rc49 → aidev_bkplugin-2.2.2rc51}/tests/views/test_chat_stream_status.py +0 -0
  109. {aidev_bkplugin-2.2.2rc49 → aidev_bkplugin-2.2.2rc51}/tests/views/test_session_pv_files_views.py +0 -0
  110. {aidev_bkplugin-2.2.2rc49 → aidev_bkplugin-2.2.2rc51}/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.2rc49
3
+ Version: 2.2.2rc51
4
4
  Summary: AiDev Agent Bkplugin builtin functions
5
5
  Requires-Python: <3.12,>=3.11
6
- Requires-Dist: aidev-agent>=2.2.2rc49
6
+ Requires-Dist: aidev-agent>=2.2.2rc51
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:
@@ -46,6 +46,10 @@ 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
50
+ BKM_PUSH_MODE_CELERY = "celery"
51
+ BKM_PUSH_MODE_DIRECT = "direct"
52
+ BKM_PUSH_MODES = frozenset({BKM_PUSH_MODE_CELERY, BKM_PUSH_MODE_DIRECT})
49
53
 
50
54
 
51
55
  def _as_bool(value: Any) -> bool:
@@ -77,37 +81,50 @@ class MetricExportSettings:
77
81
  export_interval_millis: int = DEFAULT_METRIC_EXPORT_INTERVAL_MILLIS
78
82
  export_timeout_millis: int = 30000
79
83
  export_via_celery: bool = True
84
+ bkm_push_mode: str = BKM_PUSH_MODE_CELERY
80
85
  bkm_data_id: int | None = None
81
86
  bkm_access_token: str = ""
82
87
  bkm_push_url: str = ""
83
88
  bkm_target: str = ""
84
- task_ttl_seconds: int = agent_settings.BKAI_AGENT_METRICS_TASK_TTL_SECONDS
89
+ task_ttl_seconds: int = DEFAULT_METRIC_TASK_TTL_SECONDS
85
90
 
86
91
  @classmethod
87
92
  def from_agent_info(cls, agent_info: dict[str, Any] | None, *, default_enabled: bool) -> "MetricExportSettings":
88
93
  otel_info = (agent_info or {}).get("otel_info") or {}
89
94
  metrics_info = otel_info.get("metrics") or {}
90
- interval = metrics_info.get(
91
- "export_interval_millis",
92
- otel_info.get("metric_export_interval_millis", DEFAULT_METRIC_EXPORT_INTERVAL_MILLIS),
93
- )
95
+ interval = agent_settings.BKAI_AGENT_METRICS_EXPORT_INTERVAL_MILLIS
96
+ if interval is None:
97
+ interval = metrics_info.get(
98
+ "export_interval_millis",
99
+ otel_info.get("metric_export_interval_millis", DEFAULT_METRIC_EXPORT_INTERVAL_MILLIS),
100
+ )
94
101
  timeout = metrics_info.get(
95
102
  "export_timeout_millis",
96
103
  otel_info.get("metric_export_timeout_millis", 30000),
97
104
  )
98
- task_ttl_seconds = metrics_info.get(
99
- "task_ttl_seconds",
100
- agent_settings.BKAI_AGENT_METRICS_TASK_TTL_SECONDS,
101
- )
105
+ task_ttl_seconds = agent_settings.BKAI_AGENT_METRICS_TASK_TTL_SECONDS
106
+ if task_ttl_seconds is None:
107
+ task_ttl_seconds = metrics_info.get("task_ttl_seconds", DEFAULT_METRIC_TASK_TTL_SECONDS)
102
108
  export_via_celery = metrics_info.get(
103
109
  "export_via_celery",
104
110
  otel_info.get("metric_export_via_celery"),
105
111
  )
106
- data_id = metrics_info.get("agent_data_id", agent_settings.BKAI_AGENT_METRICS_DATA_ID)
107
- access_token = metrics_info.get("agent_access_token", agent_settings.BKAI_AGENT_METRICS_TOKEN)
108
- push_url = metrics_info.get("agent_push_url", agent_settings.BKAI_AGENT_METRICS_HOST)
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),
117
+ )
118
+ bkm_push_mode = str(bkm_push_mode).strip().lower()
119
+ if bkm_push_mode not in BKM_PUSH_MODES:
120
+ raise ValueError(
121
+ f"Unsupported metric push mode: {bkm_push_mode!r}; expected one of {sorted(BKM_PUSH_MODES)}"
122
+ )
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", "")
109
126
  push_url = _normalize_bkm_push_url(push_url)
110
- 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", "")
111
128
  has_bkm_config = data_id not in (None, "") and bool(access_token and push_url)
112
129
  if export_via_celery is None:
113
130
  export_via_celery = has_bkm_config
@@ -119,10 +136,11 @@ class MetricExportSettings:
119
136
  )
120
137
  return cls(
121
138
  enabled=_as_bool(enabled),
122
- export_interval_millis=max(1000, int(interval)),
139
+ export_interval_millis=max(10_000, int(interval)),
123
140
  export_timeout_millis=max(1000, int(timeout)),
124
141
  task_ttl_seconds=max(1, int(task_ttl_seconds)),
125
142
  export_via_celery=_as_bool(export_via_celery),
143
+ bkm_push_mode=bkm_push_mode,
126
144
  bkm_data_id=int(data_id) if data_id not in (None, "") else None,
127
145
  bkm_access_token=str(access_token or ""),
128
146
  bkm_push_url=str(push_url or ""),
@@ -260,21 +278,13 @@ def _bkm_records(metrics_data: MetricsData, target: str) -> list[dict[str, Any]]
260
278
  return records
261
279
 
262
280
 
263
- class CeleryMetricExporter(MetricExporter):
264
- """Convert one periodic OTel snapshot and delegate its BKM push to Celery."""
281
+ class BkmMetricExporter(MetricExporter):
282
+ """Convert one periodic OTel snapshot and publish it through a selected transport."""
265
283
 
266
- def __init__(
267
- self,
268
- endpoint_key: str,
269
- target: str,
270
- enqueue: Callable[[str, str, int, int], Any],
271
- task_ttl_seconds: int = agent_settings.BKAI_AGENT_METRICS_TASK_TTL_SECONDS,
272
- ) -> None:
284
+ def __init__(self, endpoint_key: str, target: str) -> None:
273
285
  super().__init__()
274
286
  self.endpoint_key = endpoint_key
275
287
  self.target = target
276
- self.task_ttl_seconds = task_ttl_seconds
277
- self.enqueue = enqueue
278
288
 
279
289
  def export(
280
290
  self,
@@ -287,16 +297,18 @@ class CeleryMetricExporter(MetricExporter):
287
297
  if not records:
288
298
  return MetricExportResult.SUCCESS
289
299
  payload = json.dumps(records, ensure_ascii=False, separators=(",", ":"))
290
- created_at_millis = time.time_ns() // 1_000_000
291
- self.enqueue(self.endpoint_key, payload, created_at_millis, self.task_ttl_seconds)
300
+ self._publish(payload)
292
301
  except Exception: # noqa: BLE001
293
302
  logger.exception(
294
- "[aidev_bkplugin] failed to enqueue metric snapshot for endpoint %s",
303
+ "[aidev_bkplugin] failed to publish metric snapshot for endpoint %s",
295
304
  self.endpoint_key,
296
305
  )
297
306
  return MetricExportResult.FAILURE
298
307
  return MetricExportResult.SUCCESS
299
308
 
309
+ def _publish(self, payload: str) -> None:
310
+ raise NotImplementedError
311
+
300
312
  def force_flush(self, timeout_millis: float = 10000) -> bool:
301
313
  return True
302
314
 
@@ -304,6 +316,36 @@ class CeleryMetricExporter(MetricExporter):
304
316
  return None
305
317
 
306
318
 
319
+ class CeleryMetricExporter(BkmMetricExporter):
320
+ """Delegate one periodic BKM snapshot to Celery."""
321
+
322
+ def __init__(
323
+ self,
324
+ endpoint_key: str,
325
+ target: str,
326
+ enqueue: Callable[[str, str, int, int], Any],
327
+ task_ttl_seconds: int = DEFAULT_METRIC_TASK_TTL_SECONDS,
328
+ ) -> None:
329
+ super().__init__(endpoint_key, target)
330
+ self.task_ttl_seconds = task_ttl_seconds
331
+ self.enqueue = enqueue
332
+
333
+ def _publish(self, payload: str) -> None:
334
+ created_at_millis = time.time_ns() // 1_000_000
335
+ self.enqueue(self.endpoint_key, payload, created_at_millis, self.task_ttl_seconds)
336
+
337
+
338
+ class DirectBkmMetricExporter(BkmMetricExporter):
339
+ """Push one periodic BKM snapshot directly from the metric reader thread."""
340
+
341
+ def __init__(self, endpoint_key: str, target: str, push: Callable[[str, str], Any]) -> None:
342
+ super().__init__(endpoint_key, target)
343
+ self.push = push
344
+
345
+ def _publish(self, payload: str) -> None:
346
+ self.push(self.endpoint_key, payload)
347
+
348
+
307
349
  class BkPluginMetricService:
308
350
  """Own the metric SDK lifecycle; the Agent SDK only calls metric APIs."""
309
351
 
@@ -329,7 +371,7 @@ class BkPluginMetricService:
329
371
  return False
330
372
  readers = []
331
373
  if self.settings.export_via_celery:
332
- if self.enqueue_bkm_metrics is None:
374
+ if self.settings.bkm_push_mode == BKM_PUSH_MODE_CELERY and self.enqueue_bkm_metrics is None:
333
375
  logger.warning("[aidev_bkplugin] BKM metric export enabled but Celery enqueue is unavailable")
334
376
  return False
335
377
  if not self.settings.bkm_data_id or not self.settings.bkm_access_token or not self.settings.bkm_push_url:
@@ -337,7 +379,7 @@ class BkPluginMetricService:
337
379
  return False
338
380
  readers.append(
339
381
  PeriodicExportingMetricReader(
340
- self._create_celery_exporter(),
382
+ self._create_bkm_exporter(),
341
383
  export_interval_millis=self.settings.export_interval_millis,
342
384
  export_timeout_millis=self.settings.export_timeout_millis,
343
385
  )
@@ -364,7 +406,7 @@ class BkPluginMetricService:
364
406
  self.provider.shutdown()
365
407
  self.provider = None
366
408
  return False
367
- transport = "celery" if self.settings.export_via_celery else "direct"
409
+ transport = f"bkm_{self.settings.bkm_push_mode}" if self.settings.export_via_celery else "otlp_direct"
368
410
  logger.info(
369
411
  "[aidev_bkplugin] metric export started with %d reader(s), transport=%s",
370
412
  len(readers),
@@ -372,12 +414,15 @@ class BkPluginMetricService:
372
414
  )
373
415
  return True
374
416
 
375
- def _create_celery_exporter(self) -> CeleryMetricExporter:
417
+ def _create_bkm_exporter(self) -> BkmMetricExporter:
418
+ target = self.settings.bkm_target or socket.gethostname()
419
+ endpoint_key = _bkm_endpoint_key(self.settings)
420
+ if self.settings.bkm_push_mode == BKM_PUSH_MODE_DIRECT:
421
+ return DirectBkmMetricExporter(endpoint_key, target, self.push_bkm)
376
422
  if self.enqueue_bkm_metrics is None:
377
423
  raise RuntimeError("Celery metric enqueue is unavailable")
378
- target = self.settings.bkm_target or socket.gethostname()
379
424
  return CeleryMetricExporter(
380
- endpoint_key=_bkm_endpoint_key(self.settings),
425
+ endpoint_key=endpoint_key,
381
426
  target=target,
382
427
  enqueue=self.enqueue_bkm_metrics,
383
428
  task_ttl_seconds=self.settings.task_ttl_seconds,
@@ -1,11 +1,11 @@
1
1
  [project]
2
2
  name = "aidev-bkplugin"
3
- version = "2.2.2rc49"
3
+ version = "2.2.2rc51"
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.2rc49",
8
+ "aidev-agent>=2.2.2rc51",
9
9
  "blueapps>=4.15.1, <5.0",
10
10
  "django>=4.2.28,<5.0",
11
11
  ]
@@ -17,8 +17,11 @@ from aidev_agent.packages.opentelemetry.metrics import (
17
17
  )
18
18
  from aidev_bkplugin.services.metric_runtime import RetryableMetricPushError
19
19
  from aidev_bkplugin.services.otel_metrics import (
20
+ BKM_PUSH_MODE_CELERY,
21
+ BKM_PUSH_MODE_DIRECT,
20
22
  BkPluginMetricService,
21
23
  CeleryMetricExporter,
24
+ DirectBkmMetricExporter,
22
25
  MetricExportSettings,
23
26
  _bkm_endpoint_key,
24
27
  _bkm_metric_name,
@@ -47,11 +50,13 @@ def isolate_agent_metric_settings(monkeypatch):
47
50
 
48
51
  defaults = {
49
52
  "BKAI_AGENT_ENABLE_METRICS": None,
53
+ "BKAI_AGENT_METRICS_EXPORT_INTERVAL_MILLIS": None,
54
+ "BKAI_AGENT_METRICS_PUSH_MODE": None,
50
55
  "BKAI_AGENT_METRICS_DATA_ID": "",
51
56
  "BKAI_AGENT_METRICS_TOKEN": "",
52
57
  "BKAI_AGENT_METRICS_HOST": "",
53
58
  "BKAI_AGENT_METRICS_TARGET": "",
54
- "BKAI_AGENT_METRICS_TASK_TTL_SECONDS": 3600,
59
+ "BKAI_AGENT_METRICS_TASK_TTL_SECONDS": None,
55
60
  }
56
61
  for name, value in defaults.items():
57
62
  monkeypatch.setattr(otel_metrics.agent_settings, name, value)
@@ -153,7 +158,7 @@ def test_metric_settings_parse_nested_otel_info():
153
158
  )
154
159
 
155
160
  assert settings.enabled is True
156
- assert settings.export_interval_millis == 1500
161
+ assert settings.export_interval_millis == 10_000
157
162
  assert settings.export_timeout_millis == 7000
158
163
  assert settings.task_ttl_seconds == 120
159
164
  assert settings.export_via_celery is True
@@ -163,6 +168,46 @@ def test_metric_settings_parse_nested_otel_info():
163
168
  assert settings.bkm_target == "127.0.0.1"
164
169
 
165
170
 
171
+ def test_metric_settings_prefer_agent_interval_over_platform_setting(monkeypatch):
172
+ from aidev_bkplugin.services import otel_metrics
173
+
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
+
177
+ settings = MetricExportSettings.from_agent_info(agent_info, default_enabled=False)
178
+
179
+ assert settings.export_interval_millis == 25_000
180
+
181
+
182
+ def test_metric_settings_prefer_agent_push_mode_over_platform_setting(monkeypatch):
183
+ from aidev_bkplugin.services import otel_metrics
184
+
185
+ monkeypatch.setattr(otel_metrics.agent_settings, "BKAI_AGENT_METRICS_PUSH_MODE", BKM_PUSH_MODE_DIRECT)
186
+ agent_info = {"otel_info": {"metrics": {"push_mode": BKM_PUSH_MODE_CELERY}}}
187
+
188
+ settings = MetricExportSettings.from_agent_info(agent_info, default_enabled=False)
189
+
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
202
+
203
+
204
+ def test_metric_settings_reject_unknown_push_mode():
205
+ agent_info = {"otel_info": {"metrics": {"push_mode": "unknown"}}}
206
+
207
+ with pytest.raises(ValueError, match="Unsupported metric push mode"):
208
+ MetricExportSettings.from_agent_info(agent_info, default_enabled=False)
209
+
210
+
166
211
  def test_metric_settings_use_local_environment_fallback(monkeypatch):
167
212
  from aidev_bkplugin.services import otel_metrics
168
213
 
@@ -170,6 +215,8 @@ def test_metric_settings_use_local_environment_fallback(monkeypatch):
170
215
  monkeypatch.setattr(otel_metrics.agent_settings, "BKAI_AGENT_METRICS_TOKEN", "local-secret")
171
216
  monkeypatch.setattr(otel_metrics.agent_settings, "BKAI_AGENT_METRICS_HOST", "local-proxy")
172
217
  monkeypatch.setattr(otel_metrics.agent_settings, "BKAI_AGENT_METRICS_TARGET", "local-target")
218
+ monkeypatch.setattr(otel_metrics.agent_settings, "BKAI_AGENT_METRICS_EXPORT_INTERVAL_MILLIS", 25_000)
219
+ monkeypatch.setattr(otel_metrics.agent_settings, "BKAI_AGENT_METRICS_PUSH_MODE", BKM_PUSH_MODE_DIRECT)
173
220
  monkeypatch.setattr(otel_metrics.agent_settings, "BKAI_AGENT_METRICS_TASK_TTL_SECONDS", 1800)
174
221
 
175
222
  settings = MetricExportSettings.from_agent_info({}, default_enabled=False)
@@ -180,7 +227,8 @@ def test_metric_settings_use_local_environment_fallback(monkeypatch):
180
227
  assert settings.bkm_push_url == "http://local-proxy:10205/v2/push/"
181
228
  assert settings.bkm_target == "local-target"
182
229
  assert settings.export_via_celery is True
183
- assert settings.export_interval_millis == 10_000
230
+ assert settings.export_interval_millis == 25_000
231
+ assert settings.bkm_push_mode == BKM_PUSH_MODE_DIRECT
184
232
  assert settings.task_ttl_seconds == 1800
185
233
 
186
234
 
@@ -188,6 +236,9 @@ def test_metric_settings_keep_direct_otlp_transport_without_bkm_config():
188
236
  settings = MetricExportSettings.from_agent_info({}, default_enabled=True)
189
237
 
190
238
  assert settings.enabled is True
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
191
242
  assert settings.export_via_celery is False
192
243
 
193
244
 
@@ -214,10 +265,13 @@ def test_normalize_bkm_push_url(configured, expected):
214
265
  assert _normalize_bkm_push_url(configured) == expected
215
266
 
216
267
 
217
- def test_metric_settings_prefer_agent_info_over_local_environment(monkeypatch):
218
- monkeypatch.setenv("BKAI_AGENT_METRICS_DATA_ID", "1002")
219
- monkeypatch.setenv("BKAI_AGENT_METRICS_TOKEN", "local-secret")
220
- 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")
221
275
 
222
276
  settings = MetricExportSettings.from_agent_info(
223
277
  {
@@ -226,15 +280,17 @@ def test_metric_settings_prefer_agent_info_over_local_environment(monkeypatch):
226
280
  "agent_data_id": 1003,
227
281
  "agent_access_token": "platform-secret",
228
282
  "agent_push_url": "http://platform-proxy:10205/v2/push/",
283
+ "agent_target": "platform-target",
229
284
  }
230
285
  }
231
286
  },
232
287
  default_enabled=False,
233
288
  )
234
289
 
235
- assert settings.bkm_data_id == 1003
236
- assert settings.bkm_access_token == "platform-secret"
237
- 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"
238
294
 
239
295
 
240
296
  def test_metric_settings_parse_false_string_safely():
@@ -367,7 +423,7 @@ def test_metric_service_uses_credential_free_bkm_endpoint_key():
367
423
  enqueue_bkm_metrics=lambda *_args: None,
368
424
  )
369
425
 
370
- exporter = service._create_celery_exporter()
426
+ exporter = service._create_bkm_exporter()
371
427
 
372
428
  assert isinstance(exporter, CeleryMetricExporter)
373
429
  assert exporter.endpoint_key == _bkm_endpoint_key(settings)
@@ -375,6 +431,62 @@ def test_metric_service_uses_credential_free_bkm_endpoint_key():
375
431
  assert settings.bkm_access_token not in exporter.endpoint_key
376
432
 
377
433
 
434
+ def test_direct_bkm_exporter_pushes_without_celery_metadata(mocker):
435
+ push = mocker.Mock()
436
+
437
+ result = DirectBkmMetricExporter(
438
+ endpoint_key="endpoint-fingerprint",
439
+ target="127.0.0.1",
440
+ push=push,
441
+ ).export(_sample_metrics_data())
442
+
443
+ assert result is MetricExportResult.SUCCESS
444
+ endpoint_key, payload = push.call_args.args
445
+ assert endpoint_key == "endpoint-fingerprint"
446
+ assert "secret" not in payload
447
+ assert json.loads(payload)[0]["target"] == "127.0.0.1"
448
+
449
+
450
+ def test_direct_bkm_exporter_returns_failure_when_push_fails(mocker):
451
+ push = mocker.Mock(side_effect=requests.Timeout)
452
+ exporter = DirectBkmMetricExporter("endpoint-fingerprint", "127.0.0.1", push)
453
+
454
+ result = exporter.export(_sample_metrics_data())
455
+
456
+ assert result is MetricExportResult.FAILURE
457
+
458
+
459
+ def test_metric_service_selects_direct_bkm_exporter_without_celery_enqueue():
460
+ service = BkPluginMetricService(
461
+ service_name="ai-demo",
462
+ endpoints=[],
463
+ agent_info={},
464
+ settings=_bkm_settings(bkm_push_mode=BKM_PUSH_MODE_DIRECT),
465
+ )
466
+
467
+ exporter = service._create_bkm_exporter()
468
+
469
+ assert isinstance(exporter, DirectBkmMetricExporter)
470
+
471
+
472
+ def test_metric_service_starts_direct_bkm_without_celery_enqueue(mocker):
473
+ provider = mocker.Mock()
474
+ meter_provider = mocker.patch("aidev_bkplugin.services.otel_metrics.MeterProvider", return_value=provider)
475
+ reader = mocker.patch("aidev_bkplugin.services.otel_metrics.PeriodicExportingMetricReader")
476
+ mocker.patch("aidev_bkplugin.services.otel_metrics.metrics.set_meter_provider")
477
+ mocker.patch("aidev_bkplugin.services.otel_metrics.metrics.get_meter_provider", return_value=provider)
478
+ service = BkPluginMetricService(
479
+ service_name="ai-demo",
480
+ endpoints=[],
481
+ agent_info={},
482
+ settings=_bkm_settings(bkm_push_mode=BKM_PUSH_MODE_DIRECT),
483
+ )
484
+
485
+ assert service.start() is True
486
+ assert isinstance(reader.call_args.args[0], DirectBkmMetricExporter)
487
+ assert meter_provider.call_count == 1
488
+
489
+
378
490
  def test_metric_service_does_not_enable_incomplete_bkm_export():
379
491
  service = BkPluginMetricService(
380
492
  service_name="ai-demo",
@@ -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
+ export_via_celery=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
  [
@@ -67,7 +67,7 @@ EXIT_PROBE = dedent("""
67
67
  patch.object(apps, "enqueue_bkm_metrics_task", lambda *args: None),
68
68
  patch.object(BkAgentOTelService, "_create_metric_exporter", side_effect=lambda *args: SlowExporter()),
69
69
  patch.object(BkAgentOTelService, "_create_trace_exporter", side_effect=lambda *args: SlowSpanExporter()),
70
- patch.object(BkPluginMetricService, "_create_celery_exporter", side_effect=lambda: SlowExporter()),
70
+ patch.object(BkPluginMetricService, "_create_bkm_exporter", side_effect=lambda: SlowExporter()),
71
71
  ):
72
72
  apps.init_bk_aidev_agent_otel()
73
73
  metrics.get_meter("exit-probe").create_counter("probe.count").add(1)