为主消息链路接入trace_id追踪

- 为接收消息生成并透传trace_id到插件处理上下文
- 统一关键日志输出格式,支持按trace_id串联排障
- 将统计插件错误记录与执行日志补充trace_id关联信息
- 在工程优化文档中补充近期已完成治理项
This commit is contained in:
liuwei
2026-04-30 15:00:29 +08:00
parent a580bd12e5
commit ce38f66b7b
3 changed files with 71 additions and 13 deletions

View File

@@ -79,6 +79,7 @@ class StatsCollectorPlugin(PluginInterface):
is_group: bool,
process_result: bool,
process_time_ms: float,
trace_id: str = "",
) -> None:
"""由主链路直接调用,记录一次插件执行结果。"""
# 主链路可能会在高频场景下频繁回调这里,因此先做最便宜的开关判断,
@@ -96,7 +97,8 @@ class StatsCollectorPlugin(PluginInterface):
process_time_ms=process_time_ms,
)
self.LOG.debug(
f"记录插件调用结束: {plugin_name} - {command} - 成功: {process_result} - 处理时间: {process_time_ms}ms"
f"记录插件调用结束: trace_id={trace_id or '-'} "
f"{plugin_name} - {command} - 成功: {process_result} - 处理时间: {process_time_ms}ms"
)
except Exception as e:
self.LOG.error(f"记录插件调用统计数据出错: {e}")
@@ -110,6 +112,7 @@ class StatsCollectorPlugin(PluginInterface):
group_id: Optional[str],
is_group: bool,
error_message: str,
trace_id: str = "",
stack_trace: Optional[str] = None,
) -> None:
"""由主链路直接调用,记录一次插件执行异常。"""
@@ -122,10 +125,14 @@ class StatsCollectorPlugin(PluginInterface):
command=command,
user_id=user_id,
group_id=group_id if is_group else None,
error_message=error_message,
stack_trace=stack_trace,
# 错误表当前没有独立 trace_id 字段,因此先把 trace_id 前缀写入文案,
# 这样后台查看错误列表时,仍然可以把数据库错误记录与运行日志串起来。
error_message=f"[trace_id={trace_id}] {error_message}" if trace_id else error_message,
stack_trace=(f"[trace_id={trace_id}]\n{stack_trace}" if trace_id and stack_trace else stack_trace),
)
self.LOG.debug(
f"记录插件调用错误: trace_id={trace_id or '-'} {plugin_name} - {command} - {error_message}"
)
self.LOG.debug(f"记录插件调用错误: {plugin_name} - {command} - {error_message}")
except Exception as e:
self.LOG.error(f"记录插件错误信息出错: {e}")