Skip to content

feat(server-utils): Rewrite SentryLangChainInstrumentation to orchestrion#22266

Open
logaretm wants to merge 1 commit into
developfrom
feat/langchain-orchestrion
Open

feat(server-utils): Rewrite SentryLangChainInstrumentation to orchestrion#22266
logaretm wants to merge 1 commit into
developfrom
feat/langchain-orchestrion

Conversation

@logaretm

@logaretm logaretm commented Jul 14, 2026

Copy link
Copy Markdown
Member

Rewrites the LangChain integration to a node:diagnostics_channel listener instead of InstrumentationBase, with orchestrion injecting the channels. Follows the OpenAI/Anthropic/dataloader pattern; the vendored OTel path stays as the fallback when orchestrion isn't injected.

Some decisions due to some differences with other integrations:

  • Origin is auto.ai.langchain (spans come from the shared core handler/builder, not the subscriber). The dual-mode test still catches broken selectors: under orchestrion the OTel integration is filtered out, so wrong selectors yield zero spans.
  • Provider-skip (avoid double-instrumenting the underlying SDK) fires lazily on first langchain call, matching OTel timing; embeddings need it too since embedQuery/embedDocuments call the SDK internally.
  • @langchain/core range >=0.1.0 <2.0.0 covers langchain v0.3 and v1.

closes #20915

Rewrites the LangChain integration to a diagnostics-channel listener
instead of `InstrumentationBase`, with orchestrion injecting the channels.

Chat models are hooked once on `@langchain/core`'s `BaseChatModel`
(`invoke` + `_streamIterator`), which every provider class inherits, so a
single module covers all providers. The listener injects the Sentry
callback handler into the call options, and LangChain's own callback
dispatch creates the spans exactly as before, so the span output is
identical. Embeddings have no shared base method, so they're hooked per
provider (`@langchain/openai` for now) and get their span via
`bindTracingChannelToSpan`, reusing the same core span builder as the
OTel path.

The OTel path stays as the fallback when orchestrion isn't injected. The
existing node-integration suite runs against both paths under
`INJECT_ORCHESTRION`, proving parity for langchain v0.3 and v1.
@github-actions

Copy link
Copy Markdown
Contributor

size-limit report 📦

Path Size % Change Change
@sentry/browser 27.65 kB - -
@sentry/browser - with treeshaking flags 26.1 kB - -
@sentry/browser (incl. Tracing) 46.42 kB - -
@sentry/browser (incl. Tracing + Span Streaming) 48.21 kB - -
@sentry/browser (incl. Tracing, Profiling) 51.2 kB - -
@sentry/browser (incl. Tracing, Replay) 85.67 kB - -
@sentry/browser (incl. Tracing, Replay) - with treeshaking flags 75.31 kB - -
@sentry/browser (incl. Tracing, Replay with Canvas) 90.38 kB - -
@sentry/browser (incl. Tracing, Replay, Feedback) 103.04 kB - -
@sentry/browser (incl. Feedback) 44.83 kB - -
@sentry/browser (incl. sendFeedback) 32.45 kB - -
@sentry/browser (incl. FeedbackAsync) 37.58 kB - -
@sentry/browser (incl. Metrics) 28.74 kB - -
@sentry/browser (incl. Logs) 28.98 kB - -
@sentry/browser (incl. Metrics & Logs) 29.67 kB - -
@sentry/react 29.45 kB - -
@sentry/react (incl. Tracing) 48.68 kB - -
@sentry/vue 33.08 kB - -
@sentry/vue (incl. Tracing) 48.4 kB - -
@sentry/svelte 27.67 kB - -
CDN Bundle 30.05 kB - -
CDN Bundle (incl. Tracing) 48.41 kB - -
CDN Bundle (incl. Logs, Metrics) 31.64 kB - -
CDN Bundle (incl. Tracing, Logs, Metrics) 49.71 kB - -
CDN Bundle (incl. Replay, Logs, Metrics) 70.87 kB - -
CDN Bundle (incl. Tracing, Replay) 85.9 kB - -
CDN Bundle (incl. Tracing, Replay, Logs, Metrics) 87.21 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback) 91.7 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback, Logs, Metrics) 92.97 kB - -
CDN Bundle - uncompressed 89.58 kB - -
CDN Bundle (incl. Tracing) - uncompressed 146.33 kB - -
CDN Bundle (incl. Logs, Metrics) - uncompressed 94.29 kB - -
CDN Bundle (incl. Tracing, Logs, Metrics) - uncompressed 150.31 kB - -
CDN Bundle (incl. Replay, Logs, Metrics) - uncompressed 219.02 kB - -
CDN Bundle (incl. Tracing, Replay) - uncompressed 265.54 kB - -
CDN Bundle (incl. Tracing, Replay, Logs, Metrics) - uncompressed 269.5 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback) - uncompressed 279.24 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback, Logs, Metrics) - uncompressed 283.19 kB - -
@sentry/nextjs (client) 51.22 kB - -
@sentry/sveltekit (client) 46.86 kB - -
@sentry/core/server 78.47 kB -0.05% -38 B 🔽
@sentry/core/browser 64.88 kB +0.03% +18 B 🔺
@sentry/node-core 62.79 kB - -
@sentry/node 125.21 kB +0.02% +14 B 🔺
@sentry/node (incl. diagnostics channel injection) 140.42 kB +0.32% +446 B 🔺
@sentry/node/import (ESM hook with diagnostics-channel injection) 69.96 kB - -
@sentry/node/light 50.8 kB - -
@sentry/node - without tracing 74.17 kB - -
@sentry/aws-serverless 83.39 kB -0.01% -3 B 🔽
@sentry/cloudflare (withSentry) - minified 181.79 kB - -
@sentry/cloudflare (withSentry) 449.98 kB - -

View base workflow run

@logaretm logaretm marked this pull request as ready for review July 14, 2026 19:40
@logaretm logaretm requested a review from a team as a code owner July 14, 2026 19:40
@logaretm logaretm requested review from JPeer264, andreiborza, chargome, isaacs, s1gr1d and stephanie-anderson and removed request for a team, chargome and stephanie-anderson July 14, 2026 19:40

@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.

Reviewed by Cursor Bugbot for commit 6abf5e8. Configure here.

diagnosticsChannel.tracingChannel<EmbeddingsChannelContext>(channelName),
data => createEmbeddingsSpan(data, options),
{ captureError: () => ({ mechanism: { handled: false, type: 'auto.ai.langchain' } }) },
);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Embedding errors double-reported

Medium Severity

The orchestrion LangChain embeddings binding enables captureError on bindTracingChannelToSpan, so rejected embedding calls call captureException while the underlying LangChain error still propagates to the caller. That can produce duplicate Sentry error events alongside the global unhandled-rejection path, unlike other orchestrion gen-AI channel integrations that omit captureError.

Fix in Cursor Fix in Web

Triggered by project rule: PR Review Guidelines for Cursor Bot

Reviewed by Cursor Bugbot for commit 6abf5e8. 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.

Rewrite SentryLangChainInstrumentation to orchestrion

1 participant