[Log] Add trace log and add loggingInstrumentor tool #4692
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
FastDeploy 日志与追踪改进方案
一、需求背景
在 RL Profiler 能力建设 过程中,需要支持用户在分析单条数据时能够下钻到具体服务,进一步分析各模块的耗时情况。
目前推理阶段缺乏细粒度的时间打点数据,无法支撑对推理内部阶段的耗时分布查询。因此,需要对 FastDeploy 推理阶段 进行细化划分,并增加日志打点。
此外,现有日志系统存在以下问题:
日志仅包含时间、进程、消息等预定义信息,缺乏上下文链路信息,无法快速定位所属的 Trace 与 Span。
系统可观测性不足,故障排查效率低。
当前日志格式固定,仅支持
msg参数,缺乏扩展能力。为了解决无法快速定位的问题,引入 OpenTelemetry LoggingInstrumentor 工具,将 日志(Logs) 与 追踪(Traces) 关联起来,从而提升系统的可观测性与调试能力。
二、改动点
1. 新增 Trace Logger
新建
trace_logger,并提供获取函数get_trace_logger。后续所有 FastDeploy 各阶段耗时打点 均通过此 logger 进行。
日志输出至独立文件
trace_logger.log。2. 新增自定义 Formatter
创建新的格式化器
CustomColoredFormatter,与 RL Profiler 格式兼容。支持日志扩展字段
extra={"attributes": {...}}。目前在打印时会添加以下关键信息:
request_iduser_ideventstage有助于后续日志聚合与性能分析。
打点示例
打印示例(未开启 Trace)
3. 引入 LoggingInstrumentor
接入 OpenTelemetry LoggingInstrumentor。
当系统开启 Trace 时,会自动在日志中注入:
otel_trace_idotel_span_id修改 Formatter 逻辑,在日志中体现 Trace 相关字段。
打印示例(开启 Trace)
trace_logger:
api_server_logger:
4. FastDeploy 阶段划分与打点
在 FastDeploy 各主要阶段插入日志打点,以支持耗时分析与追踪。
阶段划分图
打点事件与阶段对应表
5. 打点工具类实现
为了规范化和自动化日志追踪信息的记录,定义了以下核心组件:核心枚举类 (Enums)
这些枚举定义了 FastDeploy 请求处理流程中的标准打点事件和阶段,是实现细粒度追踪的基础。
LoggingEventName:PREPROCESSING_START,INFERENCE_END)。StageName:PREPROCESSING,SCHEDULE,DECODE)。映射关系
EVENT_TO_STAGE_MAP:LoggingEventName映射到其所属的StageName。追踪日志函数 (
trace_logging)这是实际执行日志打点的工具函数。它封装了复杂的日志
extra字段构造逻辑,确保日志记录的简洁性和上下文完整性。request_id,user_id,event和stage等关键字段。msg、任务对象(dict或object形式)和event即可完成完整的打点。