完善消息链路trace并贯通AI与发送动作

This commit is contained in:
liuwei
2026-04-30 15:22:07 +08:00
parent 4ddab01b8d
commit 2d5a5547de
5 changed files with 67 additions and 2 deletions

View File

@@ -15,6 +15,7 @@ from requests import HTTPError
from loguru import logger
from utils.ai.llm_registry import LLMRegistry
from utils.trace_context import get_current_trace_id, format_trace_prefix
class UnifiedLLMClient:
@@ -58,6 +59,7 @@ class UnifiedLLMClient:
backend: str,
scene: str,
model: str,
trace_id: str,
success: bool,
latency_ms: float,
error: str = "",
@@ -70,6 +72,7 @@ class UnifiedLLMClient:
"backend": str(backend or "").strip(),
"scene": str(scene or "").strip(),
"model": str(model or "").strip(),
"trace_id": str(trace_id or "").strip(),
"success": bool(success),
"latency_ms": round(float(latency_ms or 0.0), 2),
"error": str(error or "").strip()[:300],
@@ -236,6 +239,7 @@ class UnifiedLLMClient:
started_at = time.monotonic()
self.last_error = ""
result: Optional[Dict[str, Any]] = None
current_trace_id = get_current_trace_id()
if not self.is_available():
self.last_error = "client_unavailable"
elif self.provider == "dify":
@@ -272,10 +276,19 @@ class UnifiedLLMClient:
backend=str(self.config.get("backend", "") or ""),
scene=str(self.config.get("scene", "") or ""),
model=self.model or str(self.mode or ""),
trace_id=current_trace_id,
success=bool(result and result.get("text")),
latency_ms=latency_ms,
error=self.last_error,
)
# 在统一出口补一条轻量 trace 日志,方便把“消息 -> AI 调用”快速串起来。
self.LOG.debug(
f"{format_trace_prefix(current_trace_id)}LLM调用结束 "
f"provider={self.provider} backend={self.config.get('backend', '') or '-'} "
f"scene={self.config.get('scene', '') or '-'} "
f"success={bool(result and result.get('text'))} latency_ms={round(latency_ms, 2)} "
f"error={self.last_error or '-'}"
)
return result
def _generate_openai(