aidev-bkplugin 2.2.2rc46__tar.gz → 2.2.2rc49__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.
- {aidev_bkplugin-2.2.2rc46 → aidev_bkplugin-2.2.2rc49}/PKG-INFO +2 -2
- {aidev_bkplugin-2.2.2rc46 → aidev_bkplugin-2.2.2rc49}/aidev_bkplugin/apps.py +3 -3
- {aidev_bkplugin-2.2.2rc46 → aidev_bkplugin-2.2.2rc49}/aidev_bkplugin/services/agent_session.py +10 -1
- {aidev_bkplugin-2.2.2rc46 → aidev_bkplugin-2.2.2rc49}/aidev_bkplugin/services/event_resource_manager.py +2 -3
- {aidev_bkplugin-2.2.2rc46 → aidev_bkplugin-2.2.2rc49}/aidev_bkplugin/services/otel_metrics.py +25 -11
- {aidev_bkplugin-2.2.2rc46 → aidev_bkplugin-2.2.2rc49}/aidev_bkplugin/settings.py +0 -3
- {aidev_bkplugin-2.2.2rc46 → aidev_bkplugin-2.2.2rc49}/aidev_bkplugin/tasks.py +36 -5
- {aidev_bkplugin-2.2.2rc46 → aidev_bkplugin-2.2.2rc49}/pyproject.toml +3 -2
- {aidev_bkplugin-2.2.2rc46 → aidev_bkplugin-2.2.2rc49}/tests/apps.py +0 -7
- aidev_bkplugin-2.2.2rc49/tests/database_events/conftest.py +29 -0
- {aidev_bkplugin-2.2.2rc46 → aidev_bkplugin-2.2.2rc49}/tests/database_events/test_resource_manager.py +3 -20
- aidev_bkplugin-2.2.2rc49/tests/database_events/test_tracing.py +32 -0
- {aidev_bkplugin-2.2.2rc46 → aidev_bkplugin-2.2.2rc49}/tests/services/agent/test_builder.py +4 -2
- {aidev_bkplugin-2.2.2rc46 → aidev_bkplugin-2.2.2rc49}/tests/services/agent/test_session_manager.py +8 -0
- {aidev_bkplugin-2.2.2rc46 → aidev_bkplugin-2.2.2rc49}/tests/services/test_otel_metrics.py +64 -11
- {aidev_bkplugin-2.2.2rc46 → aidev_bkplugin-2.2.2rc49}/tests/test_apps.py +8 -8
- {aidev_bkplugin-2.2.2rc46 → aidev_bkplugin-2.2.2rc49}/tests/test_management_metrics.py +1 -1
- aidev_bkplugin-2.2.2rc46/tests/database_events/conftest.py +0 -100
- aidev_bkplugin-2.2.2rc46/tests/database_events/process_helpers.py +0 -352
- aidev_bkplugin-2.2.2rc46/tests/database_events/test_approval_delivery.py +0 -84
- aidev_bkplugin-2.2.2rc46/tests/database_events/test_cross_process.py +0 -178
- aidev_bkplugin-2.2.2rc46/tests/database_events/test_tracing.py +0 -59
- aidev_bkplugin-2.2.2rc46/tests/database_events/test_wx_delivery.py +0 -68
- {aidev_bkplugin-2.2.2rc46 → aidev_bkplugin-2.2.2rc49}/aidev_bkplugin/__init__.py +0 -0
- {aidev_bkplugin-2.2.2rc46 → aidev_bkplugin-2.2.2rc49}/aidev_bkplugin/constants.py +0 -0
- {aidev_bkplugin-2.2.2rc46 → aidev_bkplugin-2.2.2rc49}/aidev_bkplugin/enums.py +0 -0
- {aidev_bkplugin-2.2.2rc46 → aidev_bkplugin-2.2.2rc49}/aidev_bkplugin/management/__init__.py +0 -0
- {aidev_bkplugin-2.2.2rc46 → aidev_bkplugin-2.2.2rc49}/aidev_bkplugin/management/commands/__init__.py +0 -0
- {aidev_bkplugin-2.2.2rc46 → aidev_bkplugin-2.2.2rc49}/aidev_bkplugin/management/commands/upgrade_sessions.py +0 -0
- {aidev_bkplugin-2.2.2rc46 → aidev_bkplugin-2.2.2rc49}/aidev_bkplugin/migrations/0001_initial.py +0 -0
- {aidev_bkplugin-2.2.2rc46 → aidev_bkplugin-2.2.2rc49}/aidev_bkplugin/migrations/0002_event_delivery.py +0 -0
- {aidev_bkplugin-2.2.2rc46 → aidev_bkplugin-2.2.2rc49}/aidev_bkplugin/migrations/__init__.py +0 -0
- {aidev_bkplugin-2.2.2rc46 → aidev_bkplugin-2.2.2rc49}/aidev_bkplugin/models.py +0 -0
- {aidev_bkplugin-2.2.2rc46 → aidev_bkplugin-2.2.2rc49}/aidev_bkplugin/openapi/__init__.py +0 -0
- {aidev_bkplugin-2.2.2rc46 → aidev_bkplugin-2.2.2rc49}/aidev_bkplugin/openapi/urls.py +0 -0
- {aidev_bkplugin-2.2.2rc46 → aidev_bkplugin-2.2.2rc49}/aidev_bkplugin/openapi/views.py +0 -0
- {aidev_bkplugin-2.2.2rc46 → aidev_bkplugin-2.2.2rc49}/aidev_bkplugin/packages/__init__.py +0 -0
- {aidev_bkplugin-2.2.2rc46 → aidev_bkplugin-2.2.2rc49}/aidev_bkplugin/packages/apigw/__init__.py +0 -0
- {aidev_bkplugin-2.2.2rc46 → aidev_bkplugin-2.2.2rc49}/aidev_bkplugin/packages/apigw/permissions.py +0 -0
- {aidev_bkplugin-2.2.2rc46 → aidev_bkplugin-2.2.2rc49}/aidev_bkplugin/packages/checkpoint/__init__.py +0 -0
- {aidev_bkplugin-2.2.2rc46 → aidev_bkplugin-2.2.2rc49}/aidev_bkplugin/packages/checkpoint/bk_django_saver.py +0 -0
- {aidev_bkplugin-2.2.2rc46 → aidev_bkplugin-2.2.2rc49}/aidev_bkplugin/packages/drf/__init__.py +0 -0
- {aidev_bkplugin-2.2.2rc46 → aidev_bkplugin-2.2.2rc49}/aidev_bkplugin/packages/drf/authentication.py +0 -0
- {aidev_bkplugin-2.2.2rc46 → aidev_bkplugin-2.2.2rc49}/aidev_bkplugin/packages/drf/decorators.py +0 -0
- {aidev_bkplugin-2.2.2rc46 → aidev_bkplugin-2.2.2rc49}/aidev_bkplugin/packages/drf/exception.py +0 -0
- {aidev_bkplugin-2.2.2rc46 → aidev_bkplugin-2.2.2rc49}/aidev_bkplugin/packages/drf/renderers.py +0 -0
- {aidev_bkplugin-2.2.2rc46 → aidev_bkplugin-2.2.2rc49}/aidev_bkplugin/permissions.py +0 -0
- {aidev_bkplugin-2.2.2rc46 → aidev_bkplugin-2.2.2rc49}/aidev_bkplugin/private/__init__.py +0 -0
- {aidev_bkplugin-2.2.2rc46 → aidev_bkplugin-2.2.2rc49}/aidev_bkplugin/private/permissions.py +0 -0
- {aidev_bkplugin-2.2.2rc46 → aidev_bkplugin-2.2.2rc49}/aidev_bkplugin/private/urls.py +0 -0
- {aidev_bkplugin-2.2.2rc46 → aidev_bkplugin-2.2.2rc49}/aidev_bkplugin/private/views.py +0 -0
- {aidev_bkplugin-2.2.2rc46 → aidev_bkplugin-2.2.2rc49}/aidev_bkplugin/serializers/__init__.py +0 -0
- {aidev_bkplugin-2.2.2rc46 → aidev_bkplugin-2.2.2rc49}/aidev_bkplugin/serializers/agent.py +0 -0
- {aidev_bkplugin-2.2.2rc46 → aidev_bkplugin-2.2.2rc49}/aidev_bkplugin/serializers/chat_completion.py +0 -0
- {aidev_bkplugin-2.2.2rc46 → aidev_bkplugin-2.2.2rc49}/aidev_bkplugin/serializers/llm.py +0 -0
- {aidev_bkplugin-2.2.2rc46 → aidev_bkplugin-2.2.2rc49}/aidev_bkplugin/serializers/user_operation.py +0 -0
- {aidev_bkplugin-2.2.2rc46 → aidev_bkplugin-2.2.2rc49}/aidev_bkplugin/services/__init__.py +0 -0
- {aidev_bkplugin-2.2.2rc46 → aidev_bkplugin-2.2.2rc49}/aidev_bkplugin/services/agent_bkplugin.py +0 -0
- {aidev_bkplugin-2.2.2rc46 → aidev_bkplugin-2.2.2rc49}/aidev_bkplugin/services/agent_builder.py +0 -0
- {aidev_bkplugin-2.2.2rc46 → aidev_bkplugin-2.2.2rc49}/aidev_bkplugin/services/agent_config.py +0 -0
- {aidev_bkplugin-2.2.2rc46 → aidev_bkplugin-2.2.2rc49}/aidev_bkplugin/services/agent_execution.py +0 -0
- {aidev_bkplugin-2.2.2rc46 → aidev_bkplugin-2.2.2rc49}/aidev_bkplugin/services/agent_helpers.py +0 -0
- {aidev_bkplugin-2.2.2rc46 → aidev_bkplugin-2.2.2rc49}/aidev_bkplugin/services/approval_resume.py +0 -0
- {aidev_bkplugin-2.2.2rc46 → aidev_bkplugin-2.2.2rc49}/aidev_bkplugin/services/chat_tracing.py +0 -0
- {aidev_bkplugin-2.2.2rc46 → aidev_bkplugin-2.2.2rc49}/aidev_bkplugin/services/database_event_bus.py +0 -0
- {aidev_bkplugin-2.2.2rc46 → aidev_bkplugin-2.2.2rc49}/aidev_bkplugin/services/event_tracing.py +0 -0
- {aidev_bkplugin-2.2.2rc46 → aidev_bkplugin-2.2.2rc49}/aidev_bkplugin/services/execution.py +0 -0
- {aidev_bkplugin-2.2.2rc46 → aidev_bkplugin-2.2.2rc49}/aidev_bkplugin/services/llm.py +0 -0
- {aidev_bkplugin-2.2.2rc46 → aidev_bkplugin-2.2.2rc49}/aidev_bkplugin/services/metric_runtime.py +0 -0
- {aidev_bkplugin-2.2.2rc46 → aidev_bkplugin-2.2.2rc49}/aidev_bkplugin/services/user_operation.py +0 -0
- {aidev_bkplugin-2.2.2rc46 → aidev_bkplugin-2.2.2rc49}/aidev_bkplugin/urls.py +0 -0
- {aidev_bkplugin-2.2.2rc46 → aidev_bkplugin-2.2.2rc49}/aidev_bkplugin/utils.py +0 -0
- {aidev_bkplugin-2.2.2rc46 → aidev_bkplugin-2.2.2rc49}/aidev_bkplugin/views/__init__.py +0 -0
- {aidev_bkplugin-2.2.2rc46 → aidev_bkplugin-2.2.2rc49}/aidev_bkplugin/views/agent.py +0 -0
- {aidev_bkplugin-2.2.2rc46 → aidev_bkplugin-2.2.2rc49}/aidev_bkplugin/views/base.py +0 -0
- {aidev_bkplugin-2.2.2rc46 → aidev_bkplugin-2.2.2rc49}/aidev_bkplugin/views/chat.py +0 -0
- {aidev_bkplugin-2.2.2rc46 → aidev_bkplugin-2.2.2rc49}/aidev_bkplugin/views/chat_group.py +0 -0
- {aidev_bkplugin-2.2.2rc46 → aidev_bkplugin-2.2.2rc49}/aidev_bkplugin/views/flow_agent.py +0 -0
- {aidev_bkplugin-2.2.2rc46 → aidev_bkplugin-2.2.2rc49}/aidev_bkplugin/views/llm.py +0 -0
- {aidev_bkplugin-2.2.2rc46 → aidev_bkplugin-2.2.2rc49}/aidev_bkplugin/views/session.py +0 -0
- {aidev_bkplugin-2.2.2rc46 → aidev_bkplugin-2.2.2rc49}/aidev_bkplugin/views/user_operation.py +0 -0
- {aidev_bkplugin-2.2.2rc46 → aidev_bkplugin-2.2.2rc49}/readme.md +0 -0
- {aidev_bkplugin-2.2.2rc46 → aidev_bkplugin-2.2.2rc49}/tests/__init__.py +0 -0
- {aidev_bkplugin-2.2.2rc46 → aidev_bkplugin-2.2.2rc49}/tests/conftest.py +0 -0
- {aidev_bkplugin-2.2.2rc46 → aidev_bkplugin-2.2.2rc49}/tests/database_events/__init__.py +0 -0
- {aidev_bkplugin-2.2.2rc46 → aidev_bkplugin-2.2.2rc49}/tests/database_events/test_bus.py +0 -0
- {aidev_bkplugin-2.2.2rc46 → aidev_bkplugin-2.2.2rc49}/tests/packages/__init__.py +0 -0
- {aidev_bkplugin-2.2.2rc46 → aidev_bkplugin-2.2.2rc49}/tests/packages/drf/test_renderers_trace.py +0 -0
- {aidev_bkplugin-2.2.2rc46 → aidev_bkplugin-2.2.2rc49}/tests/packages/test_bk_django_saver.py +0 -0
- {aidev_bkplugin-2.2.2rc46 → aidev_bkplugin-2.2.2rc49}/tests/packages/test_bk_django_saver_async.py +0 -0
- {aidev_bkplugin-2.2.2rc46 → aidev_bkplugin-2.2.2rc49}/tests/private/__init__.py +0 -0
- {aidev_bkplugin-2.2.2rc46 → aidev_bkplugin-2.2.2rc49}/tests/private/test_private_views.py +0 -0
- {aidev_bkplugin-2.2.2rc46 → aidev_bkplugin-2.2.2rc49}/tests/services/__init__.py +0 -0
- {aidev_bkplugin-2.2.2rc46 → aidev_bkplugin-2.2.2rc49}/tests/services/agent/__init__.py +0 -0
- {aidev_bkplugin-2.2.2rc46 → aidev_bkplugin-2.2.2rc49}/tests/services/agent/conftest.py +0 -0
- {aidev_bkplugin-2.2.2rc46 → aidev_bkplugin-2.2.2rc49}/tests/services/agent/test_agent_config_fetcher.py +0 -0
- {aidev_bkplugin-2.2.2rc46 → aidev_bkplugin-2.2.2rc49}/tests/services/test_agent_completion.py +0 -0
- {aidev_bkplugin-2.2.2rc46 → aidev_bkplugin-2.2.2rc49}/tests/services/test_agui_handler.py +0 -0
- {aidev_bkplugin-2.2.2rc46 → aidev_bkplugin-2.2.2rc49}/tests/services/test_approval_resume.py +0 -0
- {aidev_bkplugin-2.2.2rc46 → aidev_bkplugin-2.2.2rc49}/tests/services/test_approval_resume_tracing.py +0 -0
- {aidev_bkplugin-2.2.2rc46 → aidev_bkplugin-2.2.2rc49}/tests/services/test_bkplugin.py +0 -0
- {aidev_bkplugin-2.2.2rc46 → aidev_bkplugin-2.2.2rc49}/tests/services/test_execution.py +0 -0
- {aidev_bkplugin-2.2.2rc46 → aidev_bkplugin-2.2.2rc49}/tests/services/test_llm.py +0 -0
- {aidev_bkplugin-2.2.2rc46 → aidev_bkplugin-2.2.2rc49}/tests/services/test_resume_consumer.py +0 -0
- {aidev_bkplugin-2.2.2rc46 → aidev_bkplugin-2.2.2rc49}/tests/services/test_thread_chat_completion.py +0 -0
- {aidev_bkplugin-2.2.2rc46 → aidev_bkplugin-2.2.2rc49}/tests/services/test_user_operation.py +0 -0
- {aidev_bkplugin-2.2.2rc46 → aidev_bkplugin-2.2.2rc49}/tests/settings.py +0 -0
- {aidev_bkplugin-2.2.2rc46 → aidev_bkplugin-2.2.2rc49}/tests/test_constants.py +0 -0
- {aidev_bkplugin-2.2.2rc46 → aidev_bkplugin-2.2.2rc49}/tests/views/__init__.py +0 -0
- {aidev_bkplugin-2.2.2rc46 → aidev_bkplugin-2.2.2rc49}/tests/views/test_agent_view.py +0 -0
- {aidev_bkplugin-2.2.2rc46 → aidev_bkplugin-2.2.2rc49}/tests/views/test_chat_agent_code_propagation.py +0 -0
- {aidev_bkplugin-2.2.2rc46 → aidev_bkplugin-2.2.2rc49}/tests/views/test_chat_completion_serializer.py +0 -0
- {aidev_bkplugin-2.2.2rc46 → aidev_bkplugin-2.2.2rc49}/tests/views/test_chat_flow_agent.py +0 -0
- {aidev_bkplugin-2.2.2rc46 → aidev_bkplugin-2.2.2rc49}/tests/views/test_chat_stream_status.py +0 -0
- {aidev_bkplugin-2.2.2rc46 → aidev_bkplugin-2.2.2rc49}/tests/views/test_session_pv_files_views.py +0 -0
- {aidev_bkplugin-2.2.2rc46 → aidev_bkplugin-2.2.2rc49}/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.
|
|
3
|
+
Version: 2.2.2rc49
|
|
4
4
|
Summary: AiDev Agent Bkplugin builtin functions
|
|
5
5
|
Requires-Python: <3.12,>=3.11
|
|
6
|
-
Requires-Dist: aidev-agent>=2.2.
|
|
6
|
+
Requires-Dist: aidev-agent>=2.2.2rc49
|
|
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
|
|
@@ -45,9 +45,9 @@ except ImportError:
|
|
|
45
45
|
MetricExportSettings = None
|
|
46
46
|
|
|
47
47
|
try:
|
|
48
|
-
from aidev_bkplugin.tasks import
|
|
48
|
+
from aidev_bkplugin.tasks import enqueue_bkm_metrics_task
|
|
49
49
|
except ImportError:
|
|
50
|
-
|
|
50
|
+
enqueue_bkm_metrics_task = None
|
|
51
51
|
|
|
52
52
|
try:
|
|
53
53
|
from opentelemetry.instrumentation.httpx import HTTPXClientInstrumentor
|
|
@@ -155,7 +155,7 @@ def init_bk_aidev_agent_otel() -> None:
|
|
|
155
155
|
endpoints=endpoints,
|
|
156
156
|
agent_info=agent_info,
|
|
157
157
|
settings=metric_settings,
|
|
158
|
-
enqueue_bkm_metrics=
|
|
158
|
+
enqueue_bkm_metrics=enqueue_bkm_metrics_task,
|
|
159
159
|
)
|
|
160
160
|
set_metric_service(metric_service)
|
|
161
161
|
otel_config.enable_metrics = metric_service.start()
|
{aidev_bkplugin-2.2.2rc46 → aidev_bkplugin-2.2.2rc49}/aidev_bkplugin/services/agent_session.py
RENAMED
|
@@ -26,6 +26,7 @@ from ..enums import PluginPollTaskState
|
|
|
26
26
|
logger = getLogger(__name__)
|
|
27
27
|
|
|
28
28
|
STALE_SESSION_THRESHOLD_SECONDS = 1800 # 30 分钟
|
|
29
|
+
DEFAULT_SESSION_NAME = "新会话"
|
|
29
30
|
|
|
30
31
|
|
|
31
32
|
class SessionManager:
|
|
@@ -64,7 +65,7 @@ class SessionManager:
|
|
|
64
65
|
def get_or_create_by_session_code(
|
|
65
66
|
self,
|
|
66
67
|
session_code: str,
|
|
67
|
-
session_name=
|
|
68
|
+
session_name=DEFAULT_SESSION_NAME,
|
|
68
69
|
is_temporary=None,
|
|
69
70
|
channel_type: str = ChannelType.POPUP.value,
|
|
70
71
|
) -> str:
|
|
@@ -147,6 +148,14 @@ class SessionManager:
|
|
|
147
148
|
if (result.get("data") or {}).get("session_name") != session_name:
|
|
148
149
|
raise ValueError("Session title update was not confirmed")
|
|
149
150
|
|
|
151
|
+
def ai_rename(self, session_code: str) -> dict:
|
|
152
|
+
"""按最新 user prompt 调平台生成会话名。"""
|
|
153
|
+
result = self._client().api.rename_chat_session(
|
|
154
|
+
path_params={"session_code": session_code},
|
|
155
|
+
headers=self._user_headers(),
|
|
156
|
+
)
|
|
157
|
+
return result.get("data") or {}
|
|
158
|
+
|
|
150
159
|
def get_flow_info(self, session_code: str) -> dict:
|
|
151
160
|
"""读取 ``session_property.flow_info``(流程智能体执行信息),不存在时返回空 dict。"""
|
|
152
161
|
session_property = self.retrieve_session(session_code).get("session_property") or {}
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"""Per-agent publishing adapter; never replaces the global resource registry."""
|
|
2
2
|
|
|
3
3
|
from ag_ui.core import BaseEvent
|
|
4
|
+
from aidev_agent.config import settings as agent_settings
|
|
4
5
|
from django.db import close_old_connections
|
|
5
6
|
|
|
6
7
|
|
|
@@ -25,9 +26,7 @@ class EventResourceManager:
|
|
|
25
26
|
|
|
26
27
|
|
|
27
28
|
def with_database_events(resource_manager, app_code: str):
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
if getattr(settings, "AIDEV_DATABASE_EVENTS_ENABLED", False) is not True:
|
|
29
|
+
if agent_settings.BKAI_EVENT_DATABASE_ENABLED is not True:
|
|
31
30
|
return resource_manager
|
|
32
31
|
if isinstance(resource_manager, EventResourceManager):
|
|
33
32
|
return resource_manager
|
{aidev_bkplugin-2.2.2rc46 → aidev_bkplugin-2.2.2rc49}/aidev_bkplugin/services/otel_metrics.py
RENAMED
|
@@ -9,11 +9,13 @@ import logging
|
|
|
9
9
|
import os
|
|
10
10
|
import re
|
|
11
11
|
import socket
|
|
12
|
+
import time
|
|
12
13
|
from dataclasses import dataclass
|
|
13
14
|
from typing import Any, Callable
|
|
14
15
|
from urllib.parse import urlparse, urlunparse
|
|
15
16
|
|
|
16
17
|
import requests
|
|
18
|
+
from aidev_agent.config import settings as agent_settings
|
|
17
19
|
from aidev_agent.packages.opentelemetry.metrics import (
|
|
18
20
|
AGENT_ITERATION_HISTOGRAM_BOUNDARIES,
|
|
19
21
|
DURATION_HISTOGRAM_BOUNDARIES,
|
|
@@ -79,6 +81,7 @@ class MetricExportSettings:
|
|
|
79
81
|
bkm_access_token: str = ""
|
|
80
82
|
bkm_push_url: str = ""
|
|
81
83
|
bkm_target: str = ""
|
|
84
|
+
task_ttl_seconds: int = agent_settings.BKAI_AGENT_METRICS_TASK_TTL_SECONDS
|
|
82
85
|
|
|
83
86
|
@classmethod
|
|
84
87
|
def from_agent_info(cls, agent_info: dict[str, Any] | None, *, default_enabled: bool) -> "MetricExportSettings":
|
|
@@ -92,21 +95,23 @@ class MetricExportSettings:
|
|
|
92
95
|
"export_timeout_millis",
|
|
93
96
|
otel_info.get("metric_export_timeout_millis", 30000),
|
|
94
97
|
)
|
|
98
|
+
task_ttl_seconds = metrics_info.get(
|
|
99
|
+
"task_ttl_seconds",
|
|
100
|
+
agent_settings.BKAI_AGENT_METRICS_TASK_TTL_SECONDS,
|
|
101
|
+
)
|
|
95
102
|
export_via_celery = metrics_info.get(
|
|
96
103
|
"export_via_celery",
|
|
97
104
|
otel_info.get("metric_export_via_celery"),
|
|
98
105
|
)
|
|
99
|
-
data_id = metrics_info.get("agent_data_id",
|
|
100
|
-
access_token = metrics_info.get("agent_access_token",
|
|
101
|
-
push_url = metrics_info.get("agent_push_url",
|
|
102
|
-
if not push_url and os.getenv("PROXY_IP"):
|
|
103
|
-
push_url = os.environ["PROXY_IP"]
|
|
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)
|
|
104
109
|
push_url = _normalize_bkm_push_url(push_url)
|
|
105
|
-
target = metrics_info.get("agent_target",
|
|
110
|
+
target = metrics_info.get("agent_target", agent_settings.BKAI_AGENT_METRICS_TARGET)
|
|
106
111
|
has_bkm_config = data_id not in (None, "") and bool(access_token and push_url)
|
|
107
112
|
if export_via_celery is None:
|
|
108
113
|
export_via_celery = has_bkm_config
|
|
109
|
-
environment_enabled =
|
|
114
|
+
environment_enabled = agent_settings.BKAI_AGENT_ENABLE_METRICS
|
|
110
115
|
enabled = (
|
|
111
116
|
environment_enabled
|
|
112
117
|
if environment_enabled is not None
|
|
@@ -116,6 +121,7 @@ class MetricExportSettings:
|
|
|
116
121
|
enabled=_as_bool(enabled),
|
|
117
122
|
export_interval_millis=max(1000, int(interval)),
|
|
118
123
|
export_timeout_millis=max(1000, int(timeout)),
|
|
124
|
+
task_ttl_seconds=max(1, int(task_ttl_seconds)),
|
|
119
125
|
export_via_celery=_as_bool(export_via_celery),
|
|
120
126
|
bkm_data_id=int(data_id) if data_id not in (None, "") else None,
|
|
121
127
|
bkm_access_token=str(access_token or ""),
|
|
@@ -261,11 +267,13 @@ class CeleryMetricExporter(MetricExporter):
|
|
|
261
267
|
self,
|
|
262
268
|
endpoint_key: str,
|
|
263
269
|
target: str,
|
|
264
|
-
enqueue: Callable[[str, str], Any],
|
|
270
|
+
enqueue: Callable[[str, str, int, int], Any],
|
|
271
|
+
task_ttl_seconds: int = agent_settings.BKAI_AGENT_METRICS_TASK_TTL_SECONDS,
|
|
265
272
|
) -> None:
|
|
266
273
|
super().__init__()
|
|
267
274
|
self.endpoint_key = endpoint_key
|
|
268
275
|
self.target = target
|
|
276
|
+
self.task_ttl_seconds = task_ttl_seconds
|
|
269
277
|
self.enqueue = enqueue
|
|
270
278
|
|
|
271
279
|
def export(
|
|
@@ -279,7 +287,8 @@ class CeleryMetricExporter(MetricExporter):
|
|
|
279
287
|
if not records:
|
|
280
288
|
return MetricExportResult.SUCCESS
|
|
281
289
|
payload = json.dumps(records, ensure_ascii=False, separators=(",", ":"))
|
|
282
|
-
|
|
290
|
+
created_at_millis = time.time_ns() // 1_000_000
|
|
291
|
+
self.enqueue(self.endpoint_key, payload, created_at_millis, self.task_ttl_seconds)
|
|
283
292
|
except Exception: # noqa: BLE001
|
|
284
293
|
logger.exception(
|
|
285
294
|
"[aidev_bkplugin] failed to enqueue metric snapshot for endpoint %s",
|
|
@@ -305,7 +314,7 @@ class BkPluginMetricService:
|
|
|
305
314
|
endpoints: list[dict[str, Any]],
|
|
306
315
|
agent_info: dict[str, Any] | None,
|
|
307
316
|
settings: MetricExportSettings,
|
|
308
|
-
enqueue_bkm_metrics: Callable[[str, str], Any] | None = None,
|
|
317
|
+
enqueue_bkm_metrics: Callable[[str, str, int, int], Any] | None = None,
|
|
309
318
|
) -> None:
|
|
310
319
|
self.service_name = service_name
|
|
311
320
|
self.endpoints = endpoints
|
|
@@ -367,7 +376,12 @@ class BkPluginMetricService:
|
|
|
367
376
|
if self.enqueue_bkm_metrics is None:
|
|
368
377
|
raise RuntimeError("Celery metric enqueue is unavailable")
|
|
369
378
|
target = self.settings.bkm_target or socket.gethostname()
|
|
370
|
-
return CeleryMetricExporter(
|
|
379
|
+
return CeleryMetricExporter(
|
|
380
|
+
endpoint_key=_bkm_endpoint_key(self.settings),
|
|
381
|
+
target=target,
|
|
382
|
+
enqueue=self.enqueue_bkm_metrics,
|
|
383
|
+
task_ttl_seconds=self.settings.task_ttl_seconds,
|
|
384
|
+
)
|
|
371
385
|
|
|
372
386
|
def push_bkm(self, endpoint_key: str, payload: str) -> None:
|
|
373
387
|
if endpoint_key != _bkm_endpoint_key(self.settings):
|
|
@@ -11,9 +11,6 @@ AIDEV_AGENT = os.environ.get("AIDEV_AGENT", "aidev_agent.services.common_agent.C
|
|
|
11
11
|
# ``resource_manager.replace_defaults(...)`` 注入到全局工厂;为空则使用 SDK 默认的
|
|
12
12
|
# ``AgentResourceManager``。
|
|
13
13
|
AIDEV_RESOURCE_MANAGER = os.environ.get("AIDEV_RESOURCE_MANAGER", "")
|
|
14
|
-
# Enabled by default; migrate before startup and share the database/app scope across Web and wxbot.
|
|
15
|
-
# Set BKAPP_AIDEV_DATABASE_EVENTS_ENABLED=0 to opt out.
|
|
16
|
-
AIDEV_DATABASE_EVENTS_ENABLED = os.environ.get("BKAPP_AIDEV_DATABASE_EVENTS_ENABLED", "1") == "1"
|
|
17
14
|
AIDEV_AGENT_MAX_WORKERS = int(os.environ.get("BKAPP_AIDEV_AGENT_MAX_WORKERS", 16))
|
|
18
15
|
AIDEV_AGENT_MAX_PENDING = int(os.environ.get("BKAPP_AIDEV_AGENT_MAX_PENDING", 32))
|
|
19
16
|
AIDEV_AGENT_CLEANUP_MAX_WORKERS = int(os.environ.get("BKAPP_AIDEV_AGENT_CLEANUP_MAX_WORKERS", 2))
|
|
@@ -3,36 +3,67 @@
|
|
|
3
3
|
|
|
4
4
|
from __future__ import annotations
|
|
5
5
|
|
|
6
|
+
import logging
|
|
7
|
+
import time
|
|
8
|
+
from typing import Any
|
|
9
|
+
|
|
10
|
+
from aidev_agent.config import settings as agent_settings
|
|
6
11
|
from aidev_agent.enums import AgentType
|
|
7
12
|
from celery import shared_task
|
|
8
13
|
|
|
9
14
|
from .services.metric_runtime import RetryableMetricPushError, get_metric_service
|
|
10
15
|
|
|
11
|
-
|
|
12
|
-
BKPLUGIN_CELERY_QUEUE = "plugin_schedule"
|
|
16
|
+
logger = logging.getLogger(__name__)
|
|
13
17
|
|
|
14
18
|
|
|
15
19
|
@shared_task(
|
|
16
20
|
name="aidev_bkplugin.push_bkm_metrics",
|
|
17
21
|
ignore_result=True,
|
|
18
|
-
queue=
|
|
22
|
+
queue=agent_settings.BKAI_AGENT_TASK,
|
|
19
23
|
autoretry_for=(RetryableMetricPushError,),
|
|
20
24
|
retry_backoff=True,
|
|
21
25
|
retry_jitter=True,
|
|
22
26
|
max_retries=3,
|
|
23
27
|
)
|
|
24
|
-
def push_bkm_metrics_task(
|
|
28
|
+
def push_bkm_metrics_task(
|
|
29
|
+
endpoint_key: str,
|
|
30
|
+
payload: str,
|
|
31
|
+
created_at_millis: int,
|
|
32
|
+
ttl_seconds: int,
|
|
33
|
+
) -> None:
|
|
25
34
|
"""Push one periodic BKM metric snapshot from the Celery worker."""
|
|
35
|
+
age_millis = max(0, time.time_ns() // 1_000_000 - created_at_millis)
|
|
36
|
+
if age_millis > ttl_seconds * 1000:
|
|
37
|
+
logger.warning(
|
|
38
|
+
"[aidev_bkplugin] stale metric snapshot discarded: endpoint=%s age_seconds=%.3f ttl_seconds=%d",
|
|
39
|
+
endpoint_key,
|
|
40
|
+
age_millis / 1000,
|
|
41
|
+
ttl_seconds,
|
|
42
|
+
)
|
|
43
|
+
return
|
|
26
44
|
metric_service = get_metric_service()
|
|
27
45
|
if metric_service is None:
|
|
28
46
|
raise RuntimeError("Metric service is unavailable in the Celery worker")
|
|
29
47
|
metric_service.push_bkm(endpoint_key, payload)
|
|
30
48
|
|
|
31
49
|
|
|
50
|
+
def enqueue_bkm_metrics_task(
|
|
51
|
+
endpoint_key: str,
|
|
52
|
+
payload: str,
|
|
53
|
+
created_at_millis: int,
|
|
54
|
+
ttl_seconds: int,
|
|
55
|
+
) -> Any:
|
|
56
|
+
"""Enqueue a metric snapshot with the same expiry enforced by the worker."""
|
|
57
|
+
return push_bkm_metrics_task.apply_async(
|
|
58
|
+
args=(endpoint_key, payload, created_at_millis, ttl_seconds),
|
|
59
|
+
expires=ttl_seconds,
|
|
60
|
+
)
|
|
61
|
+
|
|
62
|
+
|
|
32
63
|
@shared_task(
|
|
33
64
|
name="aidev_bkplugin.run_background_agent",
|
|
34
65
|
ignore_result=True,
|
|
35
|
-
queue=
|
|
66
|
+
queue=agent_settings.BKAI_AGENT_TASK,
|
|
36
67
|
)
|
|
37
68
|
def run_bkplugin_background_agent_task(
|
|
38
69
|
session_code: str,
|
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
[project]
|
|
2
2
|
name = "aidev-bkplugin"
|
|
3
|
-
version = "2.2.
|
|
3
|
+
version = "2.2.2rc49"
|
|
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.
|
|
8
|
+
"aidev-agent>=2.2.2rc49",
|
|
9
9
|
"blueapps>=4.15.1, <5.0",
|
|
10
10
|
"django>=4.2.28,<5.0",
|
|
11
11
|
]
|
|
@@ -17,6 +17,7 @@ opentelemetry = [
|
|
|
17
17
|
|
|
18
18
|
[dependency-groups]
|
|
19
19
|
dev = [
|
|
20
|
+
"aidev-agent[opentelemetry]>=2.2.2rc28",
|
|
20
21
|
"pytest>=7.4.4,<9",
|
|
21
22
|
"pytest-cov>=4.1.0,<4.2.0",
|
|
22
23
|
"pytest-mock>=3.14.0",
|
|
@@ -14,10 +14,3 @@ class AidevBkpluginTestConfig(AppConfig):
|
|
|
14
14
|
default_auto_field = "django.db.models.BigAutoField"
|
|
15
15
|
name = "aidev_bkplugin"
|
|
16
16
|
label = "aidev_bkplugin"
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
class WxAiBotTestConfig(AppConfig):
|
|
20
|
-
"""Register models only, without starting wxbot services."""
|
|
21
|
-
|
|
22
|
-
name = "aidev_wxbot.wxaibot"
|
|
23
|
-
label = "wxaibot"
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
from types import SimpleNamespace
|
|
2
|
+
|
|
3
|
+
import pytest
|
|
4
|
+
from ag_ui.core import CustomEvent
|
|
5
|
+
from aidev_agent.events import AIDEV_CHAT_RESUME_FINISHED
|
|
6
|
+
from aidev_bkplugin.services.database_event_bus import DatabaseEventBus
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
@pytest.fixture(scope="session")
|
|
10
|
+
def django_db_modify_db_settings(tmp_path_factory):
|
|
11
|
+
from django.conf import settings
|
|
12
|
+
|
|
13
|
+
settings.DATABASES["default"]["TEST"]["NAME"] = str(tmp_path_factory.mktemp("event-db") / "events.sqlite3")
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
@pytest.fixture
|
|
17
|
+
def event_case(db):
|
|
18
|
+
bus = DatabaseEventBus("app")
|
|
19
|
+
subscription = bus.subscribe("wxbot:test", AIDEV_CHAT_RESUME_FINISHED, "session", property={"target": "user"})
|
|
20
|
+
event = CustomEvent(
|
|
21
|
+
name=AIDEV_CHAT_RESUME_FINISHED,
|
|
22
|
+
value={
|
|
23
|
+
"eventId": "event-1",
|
|
24
|
+
"appCode": "app",
|
|
25
|
+
"sessionCode": "session",
|
|
26
|
+
"events": [],
|
|
27
|
+
},
|
|
28
|
+
)
|
|
29
|
+
return SimpleNamespace(bus=bus, subscription=subscription, event=event)
|
{aidev_bkplugin-2.2.2rc46 → aidev_bkplugin-2.2.2rc49}/tests/database_events/test_resource_manager.py
RENAMED
|
@@ -1,14 +1,14 @@
|
|
|
1
|
-
import runpy
|
|
2
1
|
from types import SimpleNamespace
|
|
3
2
|
from unittest.mock import Mock
|
|
4
3
|
|
|
5
4
|
import pytest
|
|
5
|
+
from aidev_agent.config import settings as agent_settings
|
|
6
6
|
from aidev_bkplugin.services.event_resource_manager import EventResourceManager, with_database_events
|
|
7
7
|
|
|
8
8
|
|
|
9
9
|
@pytest.mark.parametrize("enabled", [False, True])
|
|
10
|
-
def test_injection_respects_setting_and_preserves_original_resource_manager(
|
|
11
|
-
|
|
10
|
+
def test_injection_respects_setting_and_preserves_original_resource_manager(monkeypatch, enabled):
|
|
11
|
+
monkeypatch.setattr(agent_settings, "BKAI_EVENT_DATABASE_ENABLED", enabled, raising=False)
|
|
12
12
|
original = SimpleNamespace(username="author", get_agent_config=lambda: "custom-config")
|
|
13
13
|
wrapped = with_database_events(original, "app")
|
|
14
14
|
assert wrapped.get_agent_config() == "custom-config" and wrapped.username == "author"
|
|
@@ -16,23 +16,6 @@ def test_injection_respects_setting_and_preserves_original_resource_manager(sett
|
|
|
16
16
|
assert with_database_events(wrapped, "app") is wrapped
|
|
17
17
|
|
|
18
18
|
|
|
19
|
-
@pytest.mark.parametrize("value, enabled", [(None, True), ("1", True), ("0", False), ("", False), ("true", False)])
|
|
20
|
-
def test_database_events_environment_default_and_override(settings, monkeypatch, value, enabled):
|
|
21
|
-
monkeypatch.setenv("BKPAAS_ENGINE_REGION", "ieod")
|
|
22
|
-
if value is None:
|
|
23
|
-
monkeypatch.delenv("BKAPP_AIDEV_DATABASE_EVENTS_ENABLED", raising=False)
|
|
24
|
-
else:
|
|
25
|
-
monkeypatch.setenv("BKAPP_AIDEV_DATABASE_EVENTS_ENABLED", value)
|
|
26
|
-
config = runpy.run_module("aidev_bkplugin.settings")
|
|
27
|
-
settings.AIDEV_DATABASE_EVENTS_ENABLED = config["AIDEV_DATABASE_EVENTS_ENABLED"]
|
|
28
|
-
assert settings.AIDEV_DATABASE_EVENTS_ENABLED is enabled
|
|
29
|
-
original = object()
|
|
30
|
-
wrapped = with_database_events(original, "app")
|
|
31
|
-
assert isinstance(wrapped, EventResourceManager) is enabled
|
|
32
|
-
if not enabled:
|
|
33
|
-
assert wrapped is original
|
|
34
|
-
|
|
35
|
-
|
|
36
19
|
@pytest.mark.parametrize("failing", [False, True])
|
|
37
20
|
def test_publishing_cleans_producer_connections_even_on_error(monkeypatch, failing):
|
|
38
21
|
calls = []
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import pytest
|
|
2
|
+
from aidev_agent.utils import tracing
|
|
3
|
+
from opentelemetry.sdk.trace import TracerProvider
|
|
4
|
+
from opentelemetry.sdk.trace.export import SimpleSpanProcessor
|
|
5
|
+
from opentelemetry.sdk.trace.export.in_memory_span_exporter import InMemorySpanExporter
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
@pytest.fixture
|
|
9
|
+
def spans(monkeypatch):
|
|
10
|
+
exporter = InMemorySpanExporter()
|
|
11
|
+
provider = TracerProvider()
|
|
12
|
+
provider.add_span_processor(SimpleSpanProcessor(exporter))
|
|
13
|
+
monkeypatch.setattr(tracing, "_agent_tracer", provider.get_tracer("test"))
|
|
14
|
+
yield exporter
|
|
15
|
+
provider.shutdown()
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
def test_empty_poll_does_not_create_unbounded_spans(event_case, spans):
|
|
19
|
+
assert event_case.bus.claim("wxbot:test") is None
|
|
20
|
+
assert not spans.get_finished_spans()
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
def test_publish_failure_records_type_not_payload(event_case, spans):
|
|
24
|
+
case = event_case
|
|
25
|
+
case.bus.publish(case.event)
|
|
26
|
+
case.event.value["secret"] = "private-value"
|
|
27
|
+
with pytest.raises(ValueError):
|
|
28
|
+
case.bus.publish(case.event)
|
|
29
|
+
failed = spans.get_finished_spans()[-1]
|
|
30
|
+
assert failed.attributes["error.type"] == "ValueError"
|
|
31
|
+
assert failed.events == ()
|
|
32
|
+
assert "private-value" not in str(dict(failed.attributes))
|
|
@@ -94,9 +94,10 @@ def test_by_thread_id_skips_save_when_save_content_false():
|
|
|
94
94
|
sm.save_content.assert_not_called()
|
|
95
95
|
|
|
96
96
|
|
|
97
|
-
def
|
|
97
|
+
def test_by_session_code_wraps_user_resource_manager_for_events():
|
|
98
98
|
from aidev_agent.packages.resource_manager.agent import AgentResourceManager
|
|
99
99
|
from aidev_bkplugin.services.agent_builder import AgentBuilder
|
|
100
|
+
from aidev_bkplugin.services.event_resource_manager import EventResourceManager
|
|
100
101
|
|
|
101
102
|
build_p, factory_p, client_p, checkpointer_p = _patch_factories()
|
|
102
103
|
with build_p as mock_build, factory_p as mock_factory, client_p, checkpointer_p:
|
|
@@ -105,7 +106,8 @@ def test_by_session_code_passes_user_resource_manager():
|
|
|
105
106
|
AgentBuilder(username="alice").by_session_code("sc-1")
|
|
106
107
|
|
|
107
108
|
rm = mock_build.call_args.kwargs["resource_manager"]
|
|
108
|
-
assert isinstance(rm,
|
|
109
|
+
assert isinstance(rm, EventResourceManager)
|
|
110
|
+
assert isinstance(rm._resource_manager, AgentResourceManager)
|
|
109
111
|
assert rm.username == "alice"
|
|
110
112
|
|
|
111
113
|
|
{aidev_bkplugin-2.2.2rc46 → aidev_bkplugin-2.2.2rc49}/tests/services/agent/test_session_manager.py
RENAMED
|
@@ -68,6 +68,14 @@ def test_update_title_only_writes_name_with_user_identity(session_manager, mock_
|
|
|
68
68
|
mock_plugin_rm_client.resource_manager_mock.get_or_create_session.assert_not_called()
|
|
69
69
|
|
|
70
70
|
|
|
71
|
+
def test_ai_rename_calls_platform_with_user_identity(session_manager, mock_plugin_rm_client):
|
|
72
|
+
session_manager.ai_rename("sc-1")
|
|
73
|
+
mock_plugin_rm_client.api.rename_chat_session.assert_called_once_with(
|
|
74
|
+
path_params={"session_code": "sc-1"}, headers={"X-BKAIDEV-USER": "alice"}
|
|
75
|
+
)
|
|
76
|
+
mock_plugin_rm_client.api.retrieve_chat_session.assert_not_called()
|
|
77
|
+
|
|
78
|
+
|
|
71
79
|
@pytest.mark.parametrize("result", [{}, {"data": None}, {"data": {"session_name": "old"}}])
|
|
72
80
|
def test_update_title_requires_platform_confirmation(session_manager, mock_plugin_rm_client, result):
|
|
73
81
|
mock_plugin_rm_client.api.update_chat_session.return_value = result
|
|
@@ -41,6 +41,22 @@ from opentelemetry.sdk.resources import Resource
|
|
|
41
41
|
from opentelemetry.sdk.util.instrumentation import InstrumentationScope
|
|
42
42
|
|
|
43
43
|
|
|
44
|
+
@pytest.fixture(autouse=True)
|
|
45
|
+
def isolate_agent_metric_settings(monkeypatch):
|
|
46
|
+
from aidev_bkplugin.services import otel_metrics
|
|
47
|
+
|
|
48
|
+
defaults = {
|
|
49
|
+
"BKAI_AGENT_ENABLE_METRICS": None,
|
|
50
|
+
"BKAI_AGENT_METRICS_DATA_ID": "",
|
|
51
|
+
"BKAI_AGENT_METRICS_TOKEN": "",
|
|
52
|
+
"BKAI_AGENT_METRICS_HOST": "",
|
|
53
|
+
"BKAI_AGENT_METRICS_TARGET": "",
|
|
54
|
+
"BKAI_AGENT_METRICS_TASK_TTL_SECONDS": 3600,
|
|
55
|
+
}
|
|
56
|
+
for name, value in defaults.items():
|
|
57
|
+
monkeypatch.setattr(otel_metrics.agent_settings, name, value)
|
|
58
|
+
|
|
59
|
+
|
|
44
60
|
def _import_tasks_with_celery_stub(mocker):
|
|
45
61
|
"""Load task functions without requiring the template-only Celery dependency."""
|
|
46
62
|
celery = ModuleType("celery")
|
|
@@ -48,6 +64,7 @@ def _import_tasks_with_celery_stub(mocker):
|
|
|
48
64
|
def shared_task(**options):
|
|
49
65
|
def decorate(function):
|
|
50
66
|
function.shared_task_options = options
|
|
67
|
+
function.apply_async = mocker.Mock()
|
|
51
68
|
return function
|
|
52
69
|
|
|
53
70
|
return decorate
|
|
@@ -124,6 +141,7 @@ def test_metric_settings_parse_nested_otel_info():
|
|
|
124
141
|
"enabled": True,
|
|
125
142
|
"export_interval_millis": 1500,
|
|
126
143
|
"export_timeout_millis": 7000,
|
|
144
|
+
"task_ttl_seconds": 120,
|
|
127
145
|
"agent_data_id": "1001",
|
|
128
146
|
"agent_access_token": "metric-secret",
|
|
129
147
|
"agent_push_url": "http://proxy:10205/v2/push/",
|
|
@@ -137,6 +155,7 @@ def test_metric_settings_parse_nested_otel_info():
|
|
|
137
155
|
assert settings.enabled is True
|
|
138
156
|
assert settings.export_interval_millis == 1500
|
|
139
157
|
assert settings.export_timeout_millis == 7000
|
|
158
|
+
assert settings.task_ttl_seconds == 120
|
|
140
159
|
assert settings.export_via_celery is True
|
|
141
160
|
assert settings.bkm_data_id == 1001
|
|
142
161
|
assert settings.bkm_access_token == "metric-secret"
|
|
@@ -145,10 +164,13 @@ def test_metric_settings_parse_nested_otel_info():
|
|
|
145
164
|
|
|
146
165
|
|
|
147
166
|
def test_metric_settings_use_local_environment_fallback(monkeypatch):
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
monkeypatch.
|
|
151
|
-
monkeypatch.
|
|
167
|
+
from aidev_bkplugin.services import otel_metrics
|
|
168
|
+
|
|
169
|
+
monkeypatch.setattr(otel_metrics.agent_settings, "BKAI_AGENT_METRICS_DATA_ID", "1002")
|
|
170
|
+
monkeypatch.setattr(otel_metrics.agent_settings, "BKAI_AGENT_METRICS_TOKEN", "local-secret")
|
|
171
|
+
monkeypatch.setattr(otel_metrics.agent_settings, "BKAI_AGENT_METRICS_HOST", "local-proxy")
|
|
172
|
+
monkeypatch.setattr(otel_metrics.agent_settings, "BKAI_AGENT_METRICS_TARGET", "local-target")
|
|
173
|
+
monkeypatch.setattr(otel_metrics.agent_settings, "BKAI_AGENT_METRICS_TASK_TTL_SECONDS", 1800)
|
|
152
174
|
|
|
153
175
|
settings = MetricExportSettings.from_agent_info({}, default_enabled=False)
|
|
154
176
|
|
|
@@ -159,12 +181,10 @@ def test_metric_settings_use_local_environment_fallback(monkeypatch):
|
|
|
159
181
|
assert settings.bkm_target == "local-target"
|
|
160
182
|
assert settings.export_via_celery is True
|
|
161
183
|
assert settings.export_interval_millis == 10_000
|
|
184
|
+
assert settings.task_ttl_seconds == 1800
|
|
162
185
|
|
|
163
186
|
|
|
164
|
-
def test_metric_settings_keep_direct_otlp_transport_without_bkm_config(
|
|
165
|
-
for name in ("BKAI_AGENT_METRICS_DATA_ID", "BKAI_AGENT_METRICS_TOKEN", "BKAI_AGENT_METRICS_HOST", "PROXY_IP"):
|
|
166
|
-
monkeypatch.delenv(name, raising=False)
|
|
167
|
-
|
|
187
|
+
def test_metric_settings_keep_direct_otlp_transport_without_bkm_config():
|
|
168
188
|
settings = MetricExportSettings.from_agent_info({}, default_enabled=True)
|
|
169
189
|
|
|
170
190
|
assert settings.enabled is True
|
|
@@ -226,7 +246,9 @@ def test_metric_settings_parse_false_string_safely():
|
|
|
226
246
|
|
|
227
247
|
|
|
228
248
|
def test_metric_settings_explicit_environment_disable_overrides_agent_info(monkeypatch):
|
|
229
|
-
|
|
249
|
+
from aidev_bkplugin.services import otel_metrics
|
|
250
|
+
|
|
251
|
+
monkeypatch.setattr(otel_metrics.agent_settings, "BKAI_AGENT_ENABLE_METRICS", False)
|
|
230
252
|
|
|
231
253
|
settings = MetricExportSettings.from_agent_info(
|
|
232
254
|
{
|
|
@@ -322,14 +344,17 @@ def test_celery_exporter_enqueues_bkm_records_without_credentials(mocker):
|
|
|
322
344
|
result = CeleryMetricExporter(
|
|
323
345
|
endpoint_key="endpoint-fingerprint",
|
|
324
346
|
target="127.0.0.1",
|
|
347
|
+
task_ttl_seconds=3600,
|
|
325
348
|
enqueue=delay,
|
|
326
349
|
).export(_sample_metrics_data())
|
|
327
350
|
|
|
328
351
|
assert result is MetricExportResult.SUCCESS
|
|
329
|
-
endpoint_key, payload = delay.call_args.args
|
|
352
|
+
endpoint_key, payload, created_at_millis, ttl_seconds = delay.call_args.args
|
|
330
353
|
assert endpoint_key == "endpoint-fingerprint"
|
|
331
354
|
assert "secret" not in payload
|
|
332
355
|
assert json.loads(payload)[0]["target"] == "127.0.0.1"
|
|
356
|
+
assert created_at_millis > 0
|
|
357
|
+
assert ttl_seconds == 3600
|
|
333
358
|
|
|
334
359
|
|
|
335
360
|
def test_metric_service_uses_credential_free_bkm_endpoint_key():
|
|
@@ -346,6 +371,7 @@ def test_metric_service_uses_credential_free_bkm_endpoint_key():
|
|
|
346
371
|
|
|
347
372
|
assert isinstance(exporter, CeleryMetricExporter)
|
|
348
373
|
assert exporter.endpoint_key == _bkm_endpoint_key(settings)
|
|
374
|
+
assert exporter.task_ttl_seconds == 3600
|
|
349
375
|
assert settings.bkm_access_token not in exporter.endpoint_key
|
|
350
376
|
|
|
351
377
|
|
|
@@ -437,9 +463,36 @@ def test_celery_task_exports_through_process_local_metric_service(mocker):
|
|
|
437
463
|
tasks = _import_tasks_with_celery_stub(mocker)
|
|
438
464
|
service = mocker.Mock()
|
|
439
465
|
mocker.patch.object(tasks, "get_metric_service", return_value=service)
|
|
466
|
+
mocker.patch.object(tasks.time, "time_ns", return_value=2_000_000_000)
|
|
440
467
|
|
|
441
|
-
tasks.push_bkm_metrics_task("endpoint-fingerprint", "payload")
|
|
468
|
+
tasks.push_bkm_metrics_task("endpoint-fingerprint", "payload", 1500, 1)
|
|
442
469
|
|
|
443
470
|
service.push_bkm.assert_called_once_with("endpoint-fingerprint", "payload")
|
|
444
471
|
assert tasks.push_bkm_metrics_task.shared_task_options["autoretry_for"] == (RetryableMetricPushError,)
|
|
445
472
|
assert tasks.push_bkm_metrics_task.shared_task_options["max_retries"] == 3
|
|
473
|
+
assert tasks.agent_settings.BKAI_AGENT_TASK == "bkai_agent_task"
|
|
474
|
+
assert tasks.push_bkm_metrics_task.shared_task_options["queue"] == "bkai_agent_task"
|
|
475
|
+
assert tasks.run_bkplugin_background_agent_task.shared_task_options["queue"] == "bkai_agent_task"
|
|
476
|
+
|
|
477
|
+
|
|
478
|
+
def test_celery_task_discards_expired_metric_snapshot(mocker, caplog):
|
|
479
|
+
tasks = _import_tasks_with_celery_stub(mocker)
|
|
480
|
+
service = mocker.Mock()
|
|
481
|
+
mocker.patch.object(tasks, "get_metric_service", return_value=service)
|
|
482
|
+
mocker.patch.object(tasks.time, "time_ns", return_value=4_000_000_000)
|
|
483
|
+
|
|
484
|
+
tasks.push_bkm_metrics_task("endpoint-fingerprint", "payload", 0, 1)
|
|
485
|
+
|
|
486
|
+
service.push_bkm.assert_not_called()
|
|
487
|
+
assert "stale metric snapshot discarded" in caplog.text
|
|
488
|
+
|
|
489
|
+
|
|
490
|
+
def test_enqueue_metric_task_sets_celery_expiry(mocker):
|
|
491
|
+
tasks = _import_tasks_with_celery_stub(mocker)
|
|
492
|
+
|
|
493
|
+
tasks.enqueue_bkm_metrics_task("endpoint-fingerprint", "payload", 1000, 3600)
|
|
494
|
+
|
|
495
|
+
tasks.push_bkm_metrics_task.apply_async.assert_called_once_with(
|
|
496
|
+
args=("endpoint-fingerprint", "payload", 1000, 3600),
|
|
497
|
+
expires=3600,
|
|
498
|
+
)
|