Commit bbec84d
authored
feat: propagate W3C trace context (TRACEPARENT) to CLI subprocess (#821)
## What
If the SDK caller has an active OpenTelemetry span, inject its W3C trace
context into the CLI subprocess env as `TRACEPARENT`/`TRACESTATE`. The
CLI reads these in headless mode and parents its
`claude_code.interaction` spans under the caller's distributed trace.
- Optional: `pip install claude-agent-sdk[otel]` pulls
`opentelemetry-api`. Gracefully no-ops if not installed.
- `setdefault` so `ClaudeAgentOptions(env={"TRACEPARENT": ...})` wins.
## Why
Agent SDK customers see CLI traces as disconnected roots in their
collector. This is the minimal change to enable distributed tracing
end-to-end without adding a `tracer` option (consistent with the
env-var-only config decision).
Relates to #452. This PR does not add in-process SDK spans (#530/#542) —
only context propagation to the CLI.
## Requires
`claude` CLI **≥ v2.1.110** for the propagation to take effect — earlier
versions ignore `TRACEPARENT` (harmless no-op, spans just stay as
roots).
## Proof
- `pytest tests/test_transport.py` — 62 pass (3 new)
- `ruff check` / `ruff format --check` / `mypy src/` — clean1 parent aaac538 commit bbec84d
3 files changed
Lines changed: 417 additions & 0 deletions
File tree
- src/claude_agent_sdk/_internal/transport
- tests
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
39 | 39 | | |
40 | 40 | | |
41 | 41 | | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
42 | 45 | | |
43 | 46 | | |
44 | 47 | | |
| |||
84 | 87 | | |
85 | 88 | | |
86 | 89 | | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
87 | 96 | | |
88 | 97 | | |
89 | 98 | | |
| |||
Lines changed: 26 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
361 | 361 | | |
362 | 362 | | |
363 | 363 | | |
| 364 | + | |
| 365 | + | |
| 366 | + | |
| 367 | + | |
| 368 | + | |
| 369 | + | |
| 370 | + | |
| 371 | + | |
| 372 | + | |
| 373 | + | |
| 374 | + | |
| 375 | + | |
| 376 | + | |
| 377 | + | |
| 378 | + | |
| 379 | + | |
| 380 | + | |
| 381 | + | |
| 382 | + | |
| 383 | + | |
| 384 | + | |
| 385 | + | |
| 386 | + | |
| 387 | + | |
| 388 | + | |
| 389 | + | |
364 | 390 | | |
365 | 391 | | |
366 | 392 | | |
| |||
0 commit comments