Skip to content

fix(core): Instrument Anthropic client in place instead of via a deep proxy#22305

Open
logaretm wants to merge 2 commits into
developfrom
fix/anthropic-instrumentation-transparency
Open

fix(core): Instrument Anthropic client in place instead of via a deep proxy#22305
logaretm wants to merge 2 commits into
developfrom
fix/anthropic-instrumentation-transparency

Conversation

@logaretm

@logaretm logaretm commented Jul 15, 2026

Copy link
Copy Markdown
Member

Instruments the Anthropic client's methods in place instead of wrapping the client in a deep Proxy, so our instrumentation stops changing the client's observable behavior.

closes #20291

… proxy

The Anthropic integration wrapped the client in a deep Proxy that ran every
method with the raw object as `this`. That changed the client's observable
semantics: internal delegation (`messages.stream()` calling `this.create()`)
resolved against the raw object, so it escaped any outer wrapper and behaved
differently than an uninstrumented client.

Instrument the registered methods in place instead (own properties shadowing
the prototype), invoking them with the caller's `this`. Instrumentation is now
observationally transparent: private-field access and internal delegation work
as they do on a plain client. A re-entrancy guard keyed on the active streaming
helper span keeps `stream()`'s internal `create()` call from producing a
duplicate span.
@logaretm
logaretm marked this pull request as ready for review July 15, 2026 18:43
Comment thread packages/core/src/tracing/anthropic-ai/index.ts
@github-actions

github-actions Bot commented Jul 15, 2026

Copy link
Copy Markdown
Contributor

size-limit report 📦

Path Size % Change Change
@sentry/browser 27.75 kB - -
@sentry/browser - with treeshaking flags 26.18 kB - -
@sentry/browser (incl. Tracing) 46.56 kB - -
@sentry/browser (incl. Tracing + Span Streaming) 48.37 kB +0.01% +1 B 🔺
@sentry/browser (incl. Tracing, Profiling) 51.36 kB - -
@sentry/browser (incl. Tracing, Replay) 85.83 kB - -
@sentry/browser (incl. Tracing, Replay) - with treeshaking flags 75.46 kB - -
@sentry/browser (incl. Tracing, Replay with Canvas) 90.55 kB +0.01% +1 B 🔺
@sentry/browser (incl. Tracing, Replay, Feedback) 103.2 kB - -
@sentry/browser (incl. Feedback) 44.93 kB +0.01% +1 B 🔺
@sentry/browser (incl. sendFeedback) 32.55 kB - -
@sentry/browser (incl. FeedbackAsync) 37.69 kB - -
@sentry/browser (incl. Metrics) 28.83 kB - -
@sentry/browser (incl. Logs) 29.05 kB - -
@sentry/browser (incl. Metrics & Logs) 29.76 kB - -
@sentry/react 29.54 kB - -
@sentry/react (incl. Tracing) 48.84 kB -0.01% -1 B 🔽
@sentry/vue 33.17 kB - -
@sentry/vue (incl. Tracing) 48.52 kB - -
@sentry/svelte 27.78 kB - -
CDN Bundle 30.14 kB - -
CDN Bundle (incl. Tracing) 48.53 kB - -
CDN Bundle (incl. Logs, Metrics) 31.73 kB - -
CDN Bundle (incl. Tracing, Logs, Metrics) 49.83 kB +0.01% +1 B 🔺
CDN Bundle (incl. Replay, Logs, Metrics) 70.98 kB +0.01% +1 B 🔺
CDN Bundle (incl. Tracing, Replay) 86.04 kB - -
CDN Bundle (incl. Tracing, Replay, Logs, Metrics) 87.34 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback) 91.84 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback, Logs, Metrics) 93.11 kB - -
CDN Bundle - uncompressed 89.88 kB - -
CDN Bundle (incl. Tracing) - uncompressed 146.73 kB - -
CDN Bundle (incl. Logs, Metrics) - uncompressed 94.59 kB - -
CDN Bundle (incl. Tracing, Logs, Metrics) - uncompressed 150.71 kB - -
CDN Bundle (incl. Replay, Logs, Metrics) - uncompressed 219.32 kB - -
CDN Bundle (incl. Tracing, Replay) - uncompressed 265.94 kB - -
CDN Bundle (incl. Tracing, Replay, Logs, Metrics) - uncompressed 269.9 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback) - uncompressed 279.64 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback, Logs, Metrics) - uncompressed 283.6 kB - -
@sentry/nextjs (client) 51.36 kB - -
@sentry/sveltekit (client) 46.99 kB - -
@sentry/core/server 80.25 kB +0.16% +122 B 🔺
@sentry/core/browser 66.69 kB +0.2% +128 B 🔺
@sentry/node-core 63.21 kB +0.01% +1 B 🔺
@sentry/node 125.73 kB +0.06% +68 B 🔺
@sentry/node (incl. diagnostics channel injection) 149.52 kB +0.07% +103 B 🔺
@sentry/node/import (ESM hook with diagnostics-channel injection) 70.03 kB - -
@sentry/node/light 51.32 kB +0.01% +1 B 🔺
@sentry/node - without tracing 74.92 kB +0.01% +2 B 🔺
@sentry/aws-serverless 84.15 kB +0.01% +1 B 🔺
@sentry/cloudflare (withSentry) - minified 197.51 kB - -
@sentry/cloudflare (withSentry) 485.87 kB - -

View base workflow run

@nicohrubec nicohrubec left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

were you able to reproduce the broken behavior? we use the same deep proxy pattern in openai and google genai, if this approach is really inherently broken we should of course change it but we should first ensure that we are actually fixing anything imo. do we know what specifically causes the issues?

@logaretm

Copy link
Copy Markdown
Member Author

@nicohrubec Yep, reproduced it with a simulation of what braintrust does. The cause is our deep proxy binds every method to the raw underlying object, so when stream() runs it delegates to create on that raw object. The outer proxy gets escaped because our (inner) proxy locks this to raw before braintrust's layer can redirect it.

This kinda allows us to get out of the way while keeping the same capabilities we had.

The other two instrumentations don't use this internally so it doesn't change behavior like what happened here. The perils of monkey patching 😢

@nicohrubec nicohrubec left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, okay nice that it is reproducible thanks for the explanation. Should we add an integration test to cover this?

@chargome chargome left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1 for adding integration tests, otherwise LGTM

Adds an integration test asserting that instrumenting the Anthropic client does
not hide its own methods from an outer wrapper: `messages.stream()` delegates to
`create` through `this`, so a co-instrumenting wrapper must still observe that
internal call. Fails against the previous deep-proxy implementation and passes
with in-place instrumentation.
@logaretm
logaretm requested a review from a team as a code owner July 20, 2026 14:14
@logaretm
logaretm requested review from isaacs and removed request for a team July 20, 2026 14:14

Copy link
Copy Markdown
Member Author

Added a few contrived tests since this is a bit unconventional case.

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Want reviews to match your repository better? Bugbot Learning can learn team-specific rules from PR activity. A team admin can enable Learning in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit 76874ee. Configure here.

if (activeSpan && STREAMING_HELPER_SPANS.has(activeSpan)) {
return target.apply(invocationThis, args);
}
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Stream dedup over-suppresses later calls

Medium Severity

The duplicate-span gate skips instrumentation whenever the active span is a streaming-helper span. On Node, that helper span stays active across MessageStream's async continuations via async context, so later non-streaming calls such as messages.create from stream event handlers can be skipped for the whole stream lifetime, not only the internal sync create delegation.

Additional Locations (1)
Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit 76874ee. Configure here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Sentry tracing breaks third-party diagnostics_channel TracingChannel stream instrumentation

3 participants