aidev-bkplugin 2.2.2rc49__tar.gz → 2.2.2rc50__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.2rc50}/PKG-INFO +2 -2
  2. {aidev_bkplugin-2.2.2rc49 → aidev_bkplugin-2.2.2rc50}/aidev_bkplugin/services/otel_metrics.py +70 -21
  3. {aidev_bkplugin-2.2.2rc49 → aidev_bkplugin-2.2.2rc50}/pyproject.toml +2 -2
  4. {aidev_bkplugin-2.2.2rc49 → aidev_bkplugin-2.2.2rc50}/tests/services/test_otel_metrics.py +95 -2
  5. {aidev_bkplugin-2.2.2rc49 → aidev_bkplugin-2.2.2rc50}/aidev_bkplugin/__init__.py +0 -0
  6. {aidev_bkplugin-2.2.2rc49 → aidev_bkplugin-2.2.2rc50}/aidev_bkplugin/apps.py +0 -0
  7. {aidev_bkplugin-2.2.2rc49 → aidev_bkplugin-2.2.2rc50}/aidev_bkplugin/constants.py +0 -0
  8. {aidev_bkplugin-2.2.2rc49 → aidev_bkplugin-2.2.2rc50}/aidev_bkplugin/enums.py +0 -0
  9. {aidev_bkplugin-2.2.2rc49 → aidev_bkplugin-2.2.2rc50}/aidev_bkplugin/management/__init__.py +0 -0
  10. {aidev_bkplugin-2.2.2rc49 → aidev_bkplugin-2.2.2rc50}/aidev_bkplugin/management/commands/__init__.py +0 -0
  11. {aidev_bkplugin-2.2.2rc49 → aidev_bkplugin-2.2.2rc50}/aidev_bkplugin/management/commands/upgrade_sessions.py +0 -0
  12. {aidev_bkplugin-2.2.2rc49 → aidev_bkplugin-2.2.2rc50}/aidev_bkplugin/migrations/0001_initial.py +0 -0
  13. {aidev_bkplugin-2.2.2rc49 → aidev_bkplugin-2.2.2rc50}/aidev_bkplugin/migrations/0002_event_delivery.py +0 -0
  14. {aidev_bkplugin-2.2.2rc49 → aidev_bkplugin-2.2.2rc50}/aidev_bkplugin/migrations/__init__.py +0 -0
  15. {aidev_bkplugin-2.2.2rc49 → aidev_bkplugin-2.2.2rc50}/aidev_bkplugin/models.py +0 -0
  16. {aidev_bkplugin-2.2.2rc49 → aidev_bkplugin-2.2.2rc50}/aidev_bkplugin/openapi/__init__.py +0 -0
  17. {aidev_bkplugin-2.2.2rc49 → aidev_bkplugin-2.2.2rc50}/aidev_bkplugin/openapi/urls.py +0 -0
  18. {aidev_bkplugin-2.2.2rc49 → aidev_bkplugin-2.2.2rc50}/aidev_bkplugin/openapi/views.py +0 -0
  19. {aidev_bkplugin-2.2.2rc49 → aidev_bkplugin-2.2.2rc50}/aidev_bkplugin/packages/__init__.py +0 -0
  20. {aidev_bkplugin-2.2.2rc49 → aidev_bkplugin-2.2.2rc50}/aidev_bkplugin/packages/apigw/__init__.py +0 -0
  21. {aidev_bkplugin-2.2.2rc49 → aidev_bkplugin-2.2.2rc50}/aidev_bkplugin/packages/apigw/permissions.py +0 -0
  22. {aidev_bkplugin-2.2.2rc49 → aidev_bkplugin-2.2.2rc50}/aidev_bkplugin/packages/checkpoint/__init__.py +0 -0
  23. {aidev_bkplugin-2.2.2rc49 → aidev_bkplugin-2.2.2rc50}/aidev_bkplugin/packages/checkpoint/bk_django_saver.py +0 -0
  24. {aidev_bkplugin-2.2.2rc49 → aidev_bkplugin-2.2.2rc50}/aidev_bkplugin/packages/drf/__init__.py +0 -0
  25. {aidev_bkplugin-2.2.2rc49 → aidev_bkplugin-2.2.2rc50}/aidev_bkplugin/packages/drf/authentication.py +0 -0
  26. {aidev_bkplugin-2.2.2rc49 → aidev_bkplugin-2.2.2rc50}/aidev_bkplugin/packages/drf/decorators.py +0 -0
  27. {aidev_bkplugin-2.2.2rc49 → aidev_bkplugin-2.2.2rc50}/aidev_bkplugin/packages/drf/exception.py +0 -0
  28. {aidev_bkplugin-2.2.2rc49 → aidev_bkplugin-2.2.2rc50}/aidev_bkplugin/packages/drf/renderers.py +0 -0
  29. {aidev_bkplugin-2.2.2rc49 → aidev_bkplugin-2.2.2rc50}/aidev_bkplugin/permissions.py +0 -0
  30. {aidev_bkplugin-2.2.2rc49 → aidev_bkplugin-2.2.2rc50}/aidev_bkplugin/private/__init__.py +0 -0
  31. {aidev_bkplugin-2.2.2rc49 → aidev_bkplugin-2.2.2rc50}/aidev_bkplugin/private/permissions.py +0 -0
  32. {aidev_bkplugin-2.2.2rc49 → aidev_bkplugin-2.2.2rc50}/aidev_bkplugin/private/urls.py +0 -0
  33. {aidev_bkplugin-2.2.2rc49 → aidev_bkplugin-2.2.2rc50}/aidev_bkplugin/private/views.py +0 -0
  34. {aidev_bkplugin-2.2.2rc49 → aidev_bkplugin-2.2.2rc50}/aidev_bkplugin/serializers/__init__.py +0 -0
  35. {aidev_bkplugin-2.2.2rc49 → aidev_bkplugin-2.2.2rc50}/aidev_bkplugin/serializers/agent.py +0 -0
  36. {aidev_bkplugin-2.2.2rc49 → aidev_bkplugin-2.2.2rc50}/aidev_bkplugin/serializers/chat_completion.py +0 -0
  37. {aidev_bkplugin-2.2.2rc49 → aidev_bkplugin-2.2.2rc50}/aidev_bkplugin/serializers/llm.py +0 -0
  38. {aidev_bkplugin-2.2.2rc49 → aidev_bkplugin-2.2.2rc50}/aidev_bkplugin/serializers/user_operation.py +0 -0
  39. {aidev_bkplugin-2.2.2rc49 → aidev_bkplugin-2.2.2rc50}/aidev_bkplugin/services/__init__.py +0 -0
  40. {aidev_bkplugin-2.2.2rc49 → aidev_bkplugin-2.2.2rc50}/aidev_bkplugin/services/agent_bkplugin.py +0 -0
  41. {aidev_bkplugin-2.2.2rc49 → aidev_bkplugin-2.2.2rc50}/aidev_bkplugin/services/agent_builder.py +0 -0
  42. {aidev_bkplugin-2.2.2rc49 → aidev_bkplugin-2.2.2rc50}/aidev_bkplugin/services/agent_config.py +0 -0
  43. {aidev_bkplugin-2.2.2rc49 → aidev_bkplugin-2.2.2rc50}/aidev_bkplugin/services/agent_execution.py +0 -0
  44. {aidev_bkplugin-2.2.2rc49 → aidev_bkplugin-2.2.2rc50}/aidev_bkplugin/services/agent_helpers.py +0 -0
  45. {aidev_bkplugin-2.2.2rc49 → aidev_bkplugin-2.2.2rc50}/aidev_bkplugin/services/agent_session.py +0 -0
  46. {aidev_bkplugin-2.2.2rc49 → aidev_bkplugin-2.2.2rc50}/aidev_bkplugin/services/approval_resume.py +0 -0
  47. {aidev_bkplugin-2.2.2rc49 → aidev_bkplugin-2.2.2rc50}/aidev_bkplugin/services/chat_tracing.py +0 -0
  48. {aidev_bkplugin-2.2.2rc49 → aidev_bkplugin-2.2.2rc50}/aidev_bkplugin/services/database_event_bus.py +0 -0
  49. {aidev_bkplugin-2.2.2rc49 → aidev_bkplugin-2.2.2rc50}/aidev_bkplugin/services/event_resource_manager.py +0 -0
  50. {aidev_bkplugin-2.2.2rc49 → aidev_bkplugin-2.2.2rc50}/aidev_bkplugin/services/event_tracing.py +0 -0
  51. {aidev_bkplugin-2.2.2rc49 → aidev_bkplugin-2.2.2rc50}/aidev_bkplugin/services/execution.py +0 -0
  52. {aidev_bkplugin-2.2.2rc49 → aidev_bkplugin-2.2.2rc50}/aidev_bkplugin/services/llm.py +0 -0
  53. {aidev_bkplugin-2.2.2rc49 → aidev_bkplugin-2.2.2rc50}/aidev_bkplugin/services/metric_runtime.py +0 -0
  54. {aidev_bkplugin-2.2.2rc49 → aidev_bkplugin-2.2.2rc50}/aidev_bkplugin/services/user_operation.py +0 -0
  55. {aidev_bkplugin-2.2.2rc49 → aidev_bkplugin-2.2.2rc50}/aidev_bkplugin/settings.py +0 -0
  56. {aidev_bkplugin-2.2.2rc49 → aidev_bkplugin-2.2.2rc50}/aidev_bkplugin/tasks.py +0 -0
  57. {aidev_bkplugin-2.2.2rc49 → aidev_bkplugin-2.2.2rc50}/aidev_bkplugin/urls.py +0 -0
  58. {aidev_bkplugin-2.2.2rc49 → aidev_bkplugin-2.2.2rc50}/aidev_bkplugin/utils.py +0 -0
  59. {aidev_bkplugin-2.2.2rc49 → aidev_bkplugin-2.2.2rc50}/aidev_bkplugin/views/__init__.py +0 -0
  60. {aidev_bkplugin-2.2.2rc49 → aidev_bkplugin-2.2.2rc50}/aidev_bkplugin/views/agent.py +0 -0
  61. {aidev_bkplugin-2.2.2rc49 → aidev_bkplugin-2.2.2rc50}/aidev_bkplugin/views/base.py +0 -0
  62. {aidev_bkplugin-2.2.2rc49 → aidev_bkplugin-2.2.2rc50}/aidev_bkplugin/views/chat.py +0 -0
  63. {aidev_bkplugin-2.2.2rc49 → aidev_bkplugin-2.2.2rc50}/aidev_bkplugin/views/chat_group.py +0 -0
  64. {aidev_bkplugin-2.2.2rc49 → aidev_bkplugin-2.2.2rc50}/aidev_bkplugin/views/flow_agent.py +0 -0
  65. {aidev_bkplugin-2.2.2rc49 → aidev_bkplugin-2.2.2rc50}/aidev_bkplugin/views/llm.py +0 -0
  66. {aidev_bkplugin-2.2.2rc49 → aidev_bkplugin-2.2.2rc50}/aidev_bkplugin/views/session.py +0 -0
  67. {aidev_bkplugin-2.2.2rc49 → aidev_bkplugin-2.2.2rc50}/aidev_bkplugin/views/user_operation.py +0 -0
  68. {aidev_bkplugin-2.2.2rc49 → aidev_bkplugin-2.2.2rc50}/readme.md +0 -0
  69. {aidev_bkplugin-2.2.2rc49 → aidev_bkplugin-2.2.2rc50}/tests/__init__.py +0 -0
  70. {aidev_bkplugin-2.2.2rc49 → aidev_bkplugin-2.2.2rc50}/tests/apps.py +0 -0
  71. {aidev_bkplugin-2.2.2rc49 → aidev_bkplugin-2.2.2rc50}/tests/conftest.py +0 -0
  72. {aidev_bkplugin-2.2.2rc49 → aidev_bkplugin-2.2.2rc50}/tests/database_events/__init__.py +0 -0
  73. {aidev_bkplugin-2.2.2rc49 → aidev_bkplugin-2.2.2rc50}/tests/database_events/conftest.py +0 -0
  74. {aidev_bkplugin-2.2.2rc49 → aidev_bkplugin-2.2.2rc50}/tests/database_events/test_bus.py +0 -0
  75. {aidev_bkplugin-2.2.2rc49 → aidev_bkplugin-2.2.2rc50}/tests/database_events/test_resource_manager.py +0 -0
  76. {aidev_bkplugin-2.2.2rc49 → aidev_bkplugin-2.2.2rc50}/tests/database_events/test_tracing.py +0 -0
  77. {aidev_bkplugin-2.2.2rc49 → aidev_bkplugin-2.2.2rc50}/tests/packages/__init__.py +0 -0
  78. {aidev_bkplugin-2.2.2rc49 → aidev_bkplugin-2.2.2rc50}/tests/packages/drf/test_renderers_trace.py +0 -0
  79. {aidev_bkplugin-2.2.2rc49 → aidev_bkplugin-2.2.2rc50}/tests/packages/test_bk_django_saver.py +0 -0
  80. {aidev_bkplugin-2.2.2rc49 → aidev_bkplugin-2.2.2rc50}/tests/packages/test_bk_django_saver_async.py +0 -0
  81. {aidev_bkplugin-2.2.2rc49 → aidev_bkplugin-2.2.2rc50}/tests/private/__init__.py +0 -0
  82. {aidev_bkplugin-2.2.2rc49 → aidev_bkplugin-2.2.2rc50}/tests/private/test_private_views.py +0 -0
  83. {aidev_bkplugin-2.2.2rc49 → aidev_bkplugin-2.2.2rc50}/tests/services/__init__.py +0 -0
  84. {aidev_bkplugin-2.2.2rc49 → aidev_bkplugin-2.2.2rc50}/tests/services/agent/__init__.py +0 -0
  85. {aidev_bkplugin-2.2.2rc49 → aidev_bkplugin-2.2.2rc50}/tests/services/agent/conftest.py +0 -0
  86. {aidev_bkplugin-2.2.2rc49 → aidev_bkplugin-2.2.2rc50}/tests/services/agent/test_agent_config_fetcher.py +0 -0
  87. {aidev_bkplugin-2.2.2rc49 → aidev_bkplugin-2.2.2rc50}/tests/services/agent/test_builder.py +0 -0
  88. {aidev_bkplugin-2.2.2rc49 → aidev_bkplugin-2.2.2rc50}/tests/services/agent/test_session_manager.py +0 -0
  89. {aidev_bkplugin-2.2.2rc49 → aidev_bkplugin-2.2.2rc50}/tests/services/test_agent_completion.py +0 -0
  90. {aidev_bkplugin-2.2.2rc49 → aidev_bkplugin-2.2.2rc50}/tests/services/test_agui_handler.py +0 -0
  91. {aidev_bkplugin-2.2.2rc49 → aidev_bkplugin-2.2.2rc50}/tests/services/test_approval_resume.py +0 -0
  92. {aidev_bkplugin-2.2.2rc49 → aidev_bkplugin-2.2.2rc50}/tests/services/test_approval_resume_tracing.py +0 -0
  93. {aidev_bkplugin-2.2.2rc49 → aidev_bkplugin-2.2.2rc50}/tests/services/test_bkplugin.py +0 -0
  94. {aidev_bkplugin-2.2.2rc49 → aidev_bkplugin-2.2.2rc50}/tests/services/test_execution.py +0 -0
  95. {aidev_bkplugin-2.2.2rc49 → aidev_bkplugin-2.2.2rc50}/tests/services/test_llm.py +0 -0
  96. {aidev_bkplugin-2.2.2rc49 → aidev_bkplugin-2.2.2rc50}/tests/services/test_resume_consumer.py +0 -0
  97. {aidev_bkplugin-2.2.2rc49 → aidev_bkplugin-2.2.2rc50}/tests/services/test_thread_chat_completion.py +0 -0
  98. {aidev_bkplugin-2.2.2rc49 → aidev_bkplugin-2.2.2rc50}/tests/services/test_user_operation.py +0 -0
  99. {aidev_bkplugin-2.2.2rc49 → aidev_bkplugin-2.2.2rc50}/tests/settings.py +0 -0
  100. {aidev_bkplugin-2.2.2rc49 → aidev_bkplugin-2.2.2rc50}/tests/test_apps.py +0 -0
  101. {aidev_bkplugin-2.2.2rc49 → aidev_bkplugin-2.2.2rc50}/tests/test_constants.py +0 -0
  102. {aidev_bkplugin-2.2.2rc49 → aidev_bkplugin-2.2.2rc50}/tests/test_management_metrics.py +0 -0
  103. {aidev_bkplugin-2.2.2rc49 → aidev_bkplugin-2.2.2rc50}/tests/views/__init__.py +0 -0
  104. {aidev_bkplugin-2.2.2rc49 → aidev_bkplugin-2.2.2rc50}/tests/views/test_agent_view.py +0 -0
  105. {aidev_bkplugin-2.2.2rc49 → aidev_bkplugin-2.2.2rc50}/tests/views/test_chat_agent_code_propagation.py +0 -0
  106. {aidev_bkplugin-2.2.2rc49 → aidev_bkplugin-2.2.2rc50}/tests/views/test_chat_completion_serializer.py +0 -0
  107. {aidev_bkplugin-2.2.2rc49 → aidev_bkplugin-2.2.2rc50}/tests/views/test_chat_flow_agent.py +0 -0
  108. {aidev_bkplugin-2.2.2rc49 → aidev_bkplugin-2.2.2rc50}/tests/views/test_chat_stream_status.py +0 -0
  109. {aidev_bkplugin-2.2.2rc49 → aidev_bkplugin-2.2.2rc50}/tests/views/test_session_pv_files_views.py +0 -0
  110. {aidev_bkplugin-2.2.2rc49 → aidev_bkplugin-2.2.2rc50}/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.2rc50
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.2rc50
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
@@ -46,6 +46,9 @@ from .metric_runtime import RetryableMetricPushError
46
46
  logger = logging.getLogger(__name__)
47
47
 
48
48
  DEFAULT_METRIC_EXPORT_INTERVAL_MILLIS = 10_000
49
+ BKM_PUSH_MODE_CELERY = "celery"
50
+ BKM_PUSH_MODE_DIRECT = "direct"
51
+ BKM_PUSH_MODES = frozenset({BKM_PUSH_MODE_CELERY, BKM_PUSH_MODE_DIRECT})
49
52
 
50
53
 
51
54
  def _as_bool(value: Any) -> bool:
@@ -77,6 +80,7 @@ class MetricExportSettings:
77
80
  export_interval_millis: int = DEFAULT_METRIC_EXPORT_INTERVAL_MILLIS
78
81
  export_timeout_millis: int = 30000
79
82
  export_via_celery: bool = True
83
+ bkm_push_mode: str = BKM_PUSH_MODE_CELERY
80
84
  bkm_data_id: int | None = None
81
85
  bkm_access_token: str = ""
82
86
  bkm_push_url: str = ""
@@ -89,7 +93,10 @@ class MetricExportSettings:
89
93
  metrics_info = otel_info.get("metrics") or {}
90
94
  interval = metrics_info.get(
91
95
  "export_interval_millis",
92
- otel_info.get("metric_export_interval_millis", DEFAULT_METRIC_EXPORT_INTERVAL_MILLIS),
96
+ otel_info.get(
97
+ "metric_export_interval_millis",
98
+ agent_settings.BKAI_AGENT_METRICS_EXPORT_INTERVAL_MILLIS,
99
+ ),
93
100
  )
94
101
  timeout = metrics_info.get(
95
102
  "export_timeout_millis",
@@ -103,6 +110,20 @@ class MetricExportSettings:
103
110
  "export_via_celery",
104
111
  otel_info.get("metric_export_via_celery"),
105
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
+ )
119
+ )
120
+ .strip()
121
+ .lower()
122
+ )
123
+ if bkm_push_mode not in BKM_PUSH_MODES:
124
+ raise ValueError(
125
+ f"Unsupported metric push mode: {bkm_push_mode!r}; expected one of {sorted(BKM_PUSH_MODES)}"
126
+ )
106
127
  data_id = metrics_info.get("agent_data_id", agent_settings.BKAI_AGENT_METRICS_DATA_ID)
107
128
  access_token = metrics_info.get("agent_access_token", agent_settings.BKAI_AGENT_METRICS_TOKEN)
108
129
  push_url = metrics_info.get("agent_push_url", agent_settings.BKAI_AGENT_METRICS_HOST)
@@ -123,6 +144,7 @@ class MetricExportSettings:
123
144
  export_timeout_millis=max(1000, int(timeout)),
124
145
  task_ttl_seconds=max(1, int(task_ttl_seconds)),
125
146
  export_via_celery=_as_bool(export_via_celery),
147
+ bkm_push_mode=bkm_push_mode,
126
148
  bkm_data_id=int(data_id) if data_id not in (None, "") else None,
127
149
  bkm_access_token=str(access_token or ""),
128
150
  bkm_push_url=str(push_url or ""),
@@ -260,21 +282,13 @@ def _bkm_records(metrics_data: MetricsData, target: str) -> list[dict[str, Any]]
260
282
  return records
261
283
 
262
284
 
263
- class CeleryMetricExporter(MetricExporter):
264
- """Convert one periodic OTel snapshot and delegate its BKM push to Celery."""
285
+ class BkmMetricExporter(MetricExporter):
286
+ """Convert one periodic OTel snapshot and publish it through a selected transport."""
265
287
 
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:
288
+ def __init__(self, endpoint_key: str, target: str) -> None:
273
289
  super().__init__()
274
290
  self.endpoint_key = endpoint_key
275
291
  self.target = target
276
- self.task_ttl_seconds = task_ttl_seconds
277
- self.enqueue = enqueue
278
292
 
279
293
  def export(
280
294
  self,
@@ -287,16 +301,18 @@ class CeleryMetricExporter(MetricExporter):
287
301
  if not records:
288
302
  return MetricExportResult.SUCCESS
289
303
  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)
304
+ self._publish(payload)
292
305
  except Exception: # noqa: BLE001
293
306
  logger.exception(
294
- "[aidev_bkplugin] failed to enqueue metric snapshot for endpoint %s",
307
+ "[aidev_bkplugin] failed to publish metric snapshot for endpoint %s",
295
308
  self.endpoint_key,
296
309
  )
297
310
  return MetricExportResult.FAILURE
298
311
  return MetricExportResult.SUCCESS
299
312
 
313
+ def _publish(self, payload: str) -> None:
314
+ raise NotImplementedError
315
+
300
316
  def force_flush(self, timeout_millis: float = 10000) -> bool:
301
317
  return True
302
318
 
@@ -304,6 +320,36 @@ class CeleryMetricExporter(MetricExporter):
304
320
  return None
305
321
 
306
322
 
323
+ class CeleryMetricExporter(BkmMetricExporter):
324
+ """Delegate one periodic BKM snapshot to Celery."""
325
+
326
+ def __init__(
327
+ self,
328
+ endpoint_key: str,
329
+ target: str,
330
+ enqueue: Callable[[str, str, int, int], Any],
331
+ task_ttl_seconds: int = agent_settings.BKAI_AGENT_METRICS_TASK_TTL_SECONDS,
332
+ ) -> None:
333
+ super().__init__(endpoint_key, target)
334
+ self.task_ttl_seconds = task_ttl_seconds
335
+ self.enqueue = enqueue
336
+
337
+ def _publish(self, payload: str) -> None:
338
+ created_at_millis = time.time_ns() // 1_000_000
339
+ self.enqueue(self.endpoint_key, payload, created_at_millis, self.task_ttl_seconds)
340
+
341
+
342
+ class DirectBkmMetricExporter(BkmMetricExporter):
343
+ """Push one periodic BKM snapshot directly from the metric reader thread."""
344
+
345
+ def __init__(self, endpoint_key: str, target: str, push: Callable[[str, str], Any]) -> None:
346
+ super().__init__(endpoint_key, target)
347
+ self.push = push
348
+
349
+ def _publish(self, payload: str) -> None:
350
+ self.push(self.endpoint_key, payload)
351
+
352
+
307
353
  class BkPluginMetricService:
308
354
  """Own the metric SDK lifecycle; the Agent SDK only calls metric APIs."""
309
355
 
@@ -329,7 +375,7 @@ class BkPluginMetricService:
329
375
  return False
330
376
  readers = []
331
377
  if self.settings.export_via_celery:
332
- if self.enqueue_bkm_metrics is None:
378
+ if self.settings.bkm_push_mode == BKM_PUSH_MODE_CELERY and self.enqueue_bkm_metrics is None:
333
379
  logger.warning("[aidev_bkplugin] BKM metric export enabled but Celery enqueue is unavailable")
334
380
  return False
335
381
  if not self.settings.bkm_data_id or not self.settings.bkm_access_token or not self.settings.bkm_push_url:
@@ -337,7 +383,7 @@ class BkPluginMetricService:
337
383
  return False
338
384
  readers.append(
339
385
  PeriodicExportingMetricReader(
340
- self._create_celery_exporter(),
386
+ self._create_bkm_exporter(),
341
387
  export_interval_millis=self.settings.export_interval_millis,
342
388
  export_timeout_millis=self.settings.export_timeout_millis,
343
389
  )
@@ -364,7 +410,7 @@ class BkPluginMetricService:
364
410
  self.provider.shutdown()
365
411
  self.provider = None
366
412
  return False
367
- transport = "celery" if self.settings.export_via_celery else "direct"
413
+ transport = f"bkm_{self.settings.bkm_push_mode}" if self.settings.export_via_celery else "otlp_direct"
368
414
  logger.info(
369
415
  "[aidev_bkplugin] metric export started with %d reader(s), transport=%s",
370
416
  len(readers),
@@ -372,12 +418,15 @@ class BkPluginMetricService:
372
418
  )
373
419
  return True
374
420
 
375
- def _create_celery_exporter(self) -> CeleryMetricExporter:
421
+ def _create_bkm_exporter(self) -> BkmMetricExporter:
422
+ target = self.settings.bkm_target or socket.gethostname()
423
+ endpoint_key = _bkm_endpoint_key(self.settings)
424
+ if self.settings.bkm_push_mode == BKM_PUSH_MODE_DIRECT:
425
+ return DirectBkmMetricExporter(endpoint_key, target, self.push_bkm)
376
426
  if self.enqueue_bkm_metrics is None:
377
427
  raise RuntimeError("Celery metric enqueue is unavailable")
378
- target = self.settings.bkm_target or socket.gethostname()
379
428
  return CeleryMetricExporter(
380
- endpoint_key=_bkm_endpoint_key(self.settings),
429
+ endpoint_key=endpoint_key,
381
430
  target=target,
382
431
  enqueue=self.enqueue_bkm_metrics,
383
432
  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.2rc50"
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.2rc50",
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,6 +50,8 @@ 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": 10_000,
54
+ "BKAI_AGENT_METRICS_PUSH_MODE": BKM_PUSH_MODE_CELERY,
50
55
  "BKAI_AGENT_METRICS_DATA_ID": "",
51
56
  "BKAI_AGENT_METRICS_TOKEN": "",
52
57
  "BKAI_AGENT_METRICS_HOST": "",
@@ -163,6 +168,35 @@ 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_platform_interval_over_agent_setting(monkeypatch):
172
+ from aidev_bkplugin.services import otel_metrics
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}}}
176
+
177
+ settings = MetricExportSettings.from_agent_info(agent_info, default_enabled=False)
178
+
179
+ assert settings.export_interval_millis == 1500
180
+
181
+
182
+ def test_metric_settings_prefer_platform_push_mode_over_agent_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_CELERY
191
+
192
+
193
+ def test_metric_settings_reject_unknown_push_mode():
194
+ agent_info = {"otel_info": {"metrics": {"push_mode": "unknown"}}}
195
+
196
+ with pytest.raises(ValueError, match="Unsupported metric push mode"):
197
+ MetricExportSettings.from_agent_info(agent_info, default_enabled=False)
198
+
199
+
166
200
  def test_metric_settings_use_local_environment_fallback(monkeypatch):
167
201
  from aidev_bkplugin.services import otel_metrics
168
202
 
@@ -170,6 +204,8 @@ def test_metric_settings_use_local_environment_fallback(monkeypatch):
170
204
  monkeypatch.setattr(otel_metrics.agent_settings, "BKAI_AGENT_METRICS_TOKEN", "local-secret")
171
205
  monkeypatch.setattr(otel_metrics.agent_settings, "BKAI_AGENT_METRICS_HOST", "local-proxy")
172
206
  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)
208
+ monkeypatch.setattr(otel_metrics.agent_settings, "BKAI_AGENT_METRICS_PUSH_MODE", BKM_PUSH_MODE_DIRECT)
173
209
  monkeypatch.setattr(otel_metrics.agent_settings, "BKAI_AGENT_METRICS_TASK_TTL_SECONDS", 1800)
174
210
 
175
211
  settings = MetricExportSettings.from_agent_info({}, default_enabled=False)
@@ -180,7 +216,8 @@ def test_metric_settings_use_local_environment_fallback(monkeypatch):
180
216
  assert settings.bkm_push_url == "http://local-proxy:10205/v2/push/"
181
217
  assert settings.bkm_target == "local-target"
182
218
  assert settings.export_via_celery is True
183
- assert settings.export_interval_millis == 10_000
219
+ assert settings.export_interval_millis == 2500
220
+ assert settings.bkm_push_mode == BKM_PUSH_MODE_DIRECT
184
221
  assert settings.task_ttl_seconds == 1800
185
222
 
186
223
 
@@ -367,7 +404,7 @@ def test_metric_service_uses_credential_free_bkm_endpoint_key():
367
404
  enqueue_bkm_metrics=lambda *_args: None,
368
405
  )
369
406
 
370
- exporter = service._create_celery_exporter()
407
+ exporter = service._create_bkm_exporter()
371
408
 
372
409
  assert isinstance(exporter, CeleryMetricExporter)
373
410
  assert exporter.endpoint_key == _bkm_endpoint_key(settings)
@@ -375,6 +412,62 @@ def test_metric_service_uses_credential_free_bkm_endpoint_key():
375
412
  assert settings.bkm_access_token not in exporter.endpoint_key
376
413
 
377
414
 
415
+ def test_direct_bkm_exporter_pushes_without_celery_metadata(mocker):
416
+ push = mocker.Mock()
417
+
418
+ result = DirectBkmMetricExporter(
419
+ endpoint_key="endpoint-fingerprint",
420
+ target="127.0.0.1",
421
+ push=push,
422
+ ).export(_sample_metrics_data())
423
+
424
+ assert result is MetricExportResult.SUCCESS
425
+ endpoint_key, payload = push.call_args.args
426
+ assert endpoint_key == "endpoint-fingerprint"
427
+ assert "secret" not in payload
428
+ assert json.loads(payload)[0]["target"] == "127.0.0.1"
429
+
430
+
431
+ def test_direct_bkm_exporter_returns_failure_when_push_fails(mocker):
432
+ push = mocker.Mock(side_effect=requests.Timeout)
433
+ exporter = DirectBkmMetricExporter("endpoint-fingerprint", "127.0.0.1", push)
434
+
435
+ result = exporter.export(_sample_metrics_data())
436
+
437
+ assert result is MetricExportResult.FAILURE
438
+
439
+
440
+ def test_metric_service_selects_direct_bkm_exporter_without_celery_enqueue():
441
+ service = BkPluginMetricService(
442
+ service_name="ai-demo",
443
+ endpoints=[],
444
+ agent_info={},
445
+ settings=_bkm_settings(bkm_push_mode=BKM_PUSH_MODE_DIRECT),
446
+ )
447
+
448
+ exporter = service._create_bkm_exporter()
449
+
450
+ assert isinstance(exporter, DirectBkmMetricExporter)
451
+
452
+
453
+ def test_metric_service_starts_direct_bkm_without_celery_enqueue(mocker):
454
+ provider = mocker.Mock()
455
+ meter_provider = mocker.patch("aidev_bkplugin.services.otel_metrics.MeterProvider", return_value=provider)
456
+ reader = mocker.patch("aidev_bkplugin.services.otel_metrics.PeriodicExportingMetricReader")
457
+ mocker.patch("aidev_bkplugin.services.otel_metrics.metrics.set_meter_provider")
458
+ mocker.patch("aidev_bkplugin.services.otel_metrics.metrics.get_meter_provider", return_value=provider)
459
+ service = BkPluginMetricService(
460
+ service_name="ai-demo",
461
+ endpoints=[],
462
+ agent_info={},
463
+ settings=_bkm_settings(bkm_push_mode=BKM_PUSH_MODE_DIRECT),
464
+ )
465
+
466
+ assert service.start() is True
467
+ assert isinstance(reader.call_args.args[0], DirectBkmMetricExporter)
468
+ assert meter_provider.call_count == 1
469
+
470
+
378
471
  def test_metric_service_does_not_enable_incomplete_bkm_export():
379
472
  service = BkPluginMetricService(
380
473
  service_name="ai-demo",