anthropics / anthropics/claude-agent-sdk-python

OTel log events from bundled CLI have empty trace_id/span_id (upstream: claude-code#63074)

未关闭
#1,034 1 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看
bug
主要语言
Python
星标
8.1k
派生
1.3k
平均合并
2 天 31 分钟
30 天内合并 PR
1

描述

## Summary

When OTel telemetry is enabled on the SDK subprocess (`CLAUDE_CODE_ENABLE_TELEMETRY=1`, `OTEL_LOGS_EXPORTER=otlp`), the bundled CLI emits `claude_code.*` log events (`user_prompt`, `api_request_body`, `tool_decision`, `hook_execution_complete`) with **empty `trace_id`/`span_id`**, while sibling spans (`claude_code.interaction`, `claude_code.tool`, `claude_code.llm_request`) correlate correctly with the parent span via auto-injected `TRACEPARENT`.

This breaks log↔trace correlation for any SDK app that opens a parent OTel span around `query()` — the documented integration pattern. The only usable label on the events is the SDK's auto-generated `session.id`, forcing DB-side joins instead of backend-side trace filtering.

## Root cause (upstream)

Tracked in the CLI repo: anthropics/claude-code#63074. The CLI's OTel logger emits from callbacks that have lost AsyncLocalStorage context, so LogRecords ship with an empty `spanContext`. Spans are unaffected.

## Why file here too

The fix lands in the CLI, but SDK users only receive it through the bundled CLI (`claude_agent_sdk/_cli_version.py` — 0.2.87 bundles 2.1.150). Filing so that:
1. SDK users hitting broken log↔trace correlation can find this from the SDK repo.
2. Once the CLI fix ships, the bundled CLI bump can be tracked here.

## Environment

- `claude-agent-sdk` 0.2.87 (bundled CLI 2.1.150); also reproduced on CLI 2.1.153
- Python 3.14, parent process with active OTel span around `query()`
- Subprocess env: `CLAUDE_CODE_ENABLE_TELEMETRY=1`, `OTEL_LOGS_EXPORTER=otlp`, `OTEL_LOG_USER_PROMPTS=1`, `OTEL_LOG_TOOL_DETAILS=1`

贡献指南

这个仓库没有索引到贡献指南

评估

这个 Issue 还没有评估数据。

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。