fix(anthropic): disable the SDK native tracing to avoid duplicate spans #4703
All Vouch review confidence checks passed
Residual risk 0.24 = Change Risk (0.46) × (1 − Review Confidence (0.47)).
Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.46 · 477 lines diff across 5 files
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.02 · 1 substantive comment(s)
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size
diff_size
0.369
Shallow Review Depth
shallow_review_depth
0.344
Real PR Conversation Timeline
1 comments
"Review active on #4703"
general
weight: 0.35
simin-yuan
Fixes #4700.
Problem
anthropic >= 1.14emits its own span per API call when a client is constructed while aglobal tracer provider is registered (
anthropic.messages.create, instrumentation scopecom.anthropic.sdk.python). This instrumentation emitsanthropic.chatfor the same call,in a separate trace, and both spans carry the same
gen_ai.usage.*attributes, so backendsthat sum usage per span or per trace count the call twice.
Measured with
anthropic 2.8.0,opentelemetry-instrumentation-anthropic 0.63.0and anInMemorySpanExporter(mock transport, no network):Two trace IDs, identical usage → 1000 in / 200 out counted twice.
Change
AnthropicInstrumentor._instrument()now doesWhen the SDK reads it: not at import.
anthropic._tracing.resolve_open_telemetry()calls
env_open_telemetry()once per client, when the client is constructed, and theclient keeps the result; nothing reads the variable at import time. Verified by importing
anthropicfirst, then instrumenting, then building the client: one span. A client thatalready exists keeps what it resolved, so instrumenting after it was built does not reach
it (see the limitation below).
uninstrument()puts the environment back: the instrumentor records that it wrote thedefault and removes it again, so a client built after
uninstrument()gets the SDK's ownspans back. A value that was already there — including an explicit
false— is neverremoved.
Known limitation
A client that exists before
instrument()runs keeps the native tracing it resolved atconstruction: creating the client first and instrumenting afterwards still reports
span count: 2. SettingANTHROPIC_OPEN_TELEMETRY=falsein the application's environment,or building clients after
instrument(), avoids it.Tests
tests/test_native_tracing_coexistence.pyadds five tests:test_instrument_disables_native_sdk_tracing— afterinstrument()the variable is"false"; fails onmain.test_uninstrument_removes_the_default_it_wrote— afteruninstrument()the variable is gone; fails on the previous commit.test_instrument_keeps_explicit_native_tracing_setting— an explicit"true"survivesinstrument()anduninstrument().test_an_explicit_false_is_not_removed_by_uninstrument— a value that came from the environment stays.test_ownership_survives_a_second_construction—instrument()anduninstrument()on separately constructed instrumentor instances still clean up; fails without the class-level flag, becauseBaseInstrumentoris a singleton.The span count is not a test in the suite on purpose: reproducing the duplicate needs
trace.set_tracer_provider(...), which mutates process-wide state and would leak into theother tests in the same session. I checked it out of band instead, with the same mock
transport:
pytest tests/test_native_tracing_coexistence.py→ 5 passed;ruff check .clean.Merge order
Merge #4705 first. It ends the span of a stream that is left early, closed, dropped or failed, in
streaming.pyonly. Until it lands, turning the SDK's native span off would leave those calls with no span at all; with it landed, this PR only removes the duplicate.