feat: 新增宿主接入 trace 的工具并把 trace_id 随会话内容入库 --story=136129662 - #848
Open
Vellun7 wants to merge 1 commit into
Open
Conversation
Co-authored-by: Cursor <cursoragent@cursor.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
背景
TAPD #136129662:会话记录里没有任何指向实际执行链路的线索,排障只能靠时间戳和
session_code去 APM 上翻。根因 / 现状:
ChatSessionContent只有turn_id(标识一轮 user-ai 对话),缺一个标识这轮实际调用链的 id,从一条会话记录跳不到 APM。BkAidevAgentInstrumentor刻意不把自己的TracerProvider注册为全局(见其 docstring),宿主若自建一个,产出的 span 会落在另一棵树上且无处上报。改动
改动一:新增
aidev_agent/utils/tracing.pycurrent_trace_id():读当前 span 的 32 位 hex trace id,无有效 span 返回空串。不像turn_id那样逐层透传参数,因为 OTel context 本来就跟着执行走,再铺一条平行参数链只会多出漏传的可能。start_request_span():为一次请求开 root span 并 yield 其 trace id。显式传空Context()强制成为 root —— 常驻 worker 线程上可能残留上一次未 detach 的 OTel context(LangChain 回调在生成器被中途丢弃时不保证走到on_chain_end),继承过去会让不同请求共用同一个 trace id。setup_tracing():装上全局TracerProvider(优先复用 Agent SDK 的 provider,取不到则 standalone 兜底)并 instrumentrequests,让出站调用自动带traceparent。日志里的provider=agent_sdk|standalone|existing标明 span 去了哪。改动二:
trace_id随会话内容入库BaseSessionWriter._stamp_trace_id(),把当前 trace id 盖进payload.property,与turn_id并列;user 输入落库、assistant 内容创建 / 更新、ask-user-question 收尾四条写入路径统一走它。无有效 span 时不写空值。aidev_bkplugin的SessionManager.create_chat_session_content同样补上trace_id。改动三:新增
get_agent_tracer_provider()BkAidevAgentInstrumentor已启动的 provider,供宿主复用同一套 exporter 与 resource。直接读单例属性而不是构造BkAidevAgentInstrumentor():BaseInstrumentor.__new__返回单例,但__init__仍会重跑并把_otel_service抹回None。改动四:依赖
opentelemetry-instrumentation-requests==0.54b1加入opentelemetryextras;它与opentelemetry-sdk一并加入 dev 组供单测使用。测试
src/agent/tests/utils/test_tracing.py(新增):请求级 trace 隔离、线程 context 污染下仍为 root、provider 选择、出站traceparent注入、console exporter。src/agent/tests/services/event_handlers/test_session_content_trace_id.py(新增):创建 / 更新 / user 记录三条路径的trace_id落库,以及无 span 时不写 key。src/agent/tests/services/event_handlers17 passed;src/plugins/aidev_bkplugin/tests/services87 passed, 1 skipped。兼容性
trace_id是property下的新增 key,无有效 span 时整个 key 不写入,老数据与老调用方不受影响。setup_tracing()是宿主显式调用的可选入口,不调用则现有行为完全不变。property.trace_id即可。关联
tapd: #136129662
发起人:
Made with Cursor