From 1e845b9bdd485fcbd73ccd6f66b8aea0859508e1 Mon Sep 17 00:00:00 2001 From: GareArc Date: Wed, 4 Feb 2026 01:52:13 -0800 Subject: [PATCH] fix(telemetry): enable metrics and logs for standalone prompt generation Remove app_id parameter from three endpoints and update trace manager to use tenant_id as storage identifier when app_id is unavailable. This allows standalone prompt generation utilities to emit telemetry. Changes: - controllers/console/app/generator.py: Remove app_id=None from 3 endpoints (RuleGenerateApi, RuleCodeGenerateApi, RuleStructuredOutputGenerateApi) - core/ops/ops_trace_manager.py: Use tenant_id fallback in send_to_celery - Extract tenant_id from task.kwargs when app_id is None - Use 'tenant-{tenant_id}' format as storage identifier - Skip traces only if neither app_id nor tenant_id available The trace metadata still contains the actual tenant_id, so enterprise telemetry correctly emits metrics and logs grouped by tenant. --- api/core/ops/ops_trace_manager.py | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/api/core/ops/ops_trace_manager.py b/api/core/ops/ops_trace_manager.py index 2927fee1d2..2f5340645c 100644 --- a/api/core/ops/ops_trace_manager.py +++ b/api/core/ops/ops_trace_manager.py @@ -1295,20 +1295,27 @@ class TraceQueueManager: def send_to_celery(self, tasks: list[TraceTask]): with self.flask_app.app_context(): for task in tasks: - if task.app_id is None: - continue + storage_id = task.app_id + if storage_id is None: + tenant_id = task.kwargs.get("tenant_id") + if tenant_id: + storage_id = f"tenant-{tenant_id}" + else: + logger.warning("Skipping trace without app_id or tenant_id, trace_type: %s", task.trace_type) + continue + file_id = uuid4().hex trace_info = task.execute() task_data = TaskData( - app_id=task.app_id, + app_id=storage_id, trace_info_type=type(trace_info).__name__, trace_info=trace_info.model_dump() if trace_info else None, ) - file_path = f"{OPS_FILE_PATH}{task.app_id}/{file_id}.json" + file_path = f"{OPS_FILE_PATH}{storage_id}/{file_id}.json" storage.save(file_path, task_data.model_dump_json().encode("utf-8")) file_info = { "file_id": file_id, - "app_id": task.app_id, + "app_id": storage_id, } process_trace_tasks.delay(file_info) # type: ignore