-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
feat(server-utils): Rewrite SentryLangChainInstrumentation to orchestrion
#22266
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
logaretm
wants to merge
4
commits into
develop
Choose a base branch
from
feat/langchain-orchestrion
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+229
−30
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
fcebfb9
feat(server-utils): Rewrite LangChain integration to orchestrion
logaretm 0fa0cc1
feat(langchain): cover embeddings for all OTel provider packages
logaretm e90583e
fix(langchain): hook only embedDocuments on google-common to avoid ne…
logaretm 0e63ac1
chore: bump diagnostics channel injection size limit to 144 KB
logaretm File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
121 changes: 121 additions & 0 deletions
121
packages/server-utils/src/integrations/tracing-channel/langchain.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,121 @@ | ||
| import * as diagnosticsChannel from 'node:diagnostics_channel'; | ||
| import type { IntegrationFn, LangChainOptions, Span } from '@sentry/core'; | ||
| import { | ||
| _INTERNAL_getLangChainEmbeddingsSpanOptions, | ||
| _INTERNAL_mergeLangChainCallbackHandler, | ||
| _INTERNAL_skipAiProviderWrapping, | ||
| ANTHROPIC_AI_INTEGRATION_NAME, | ||
| createLangChainCallbackHandler, | ||
| debug, | ||
| defineIntegration, | ||
| GOOGLE_GENAI_INTEGRATION_NAME, | ||
| LANGCHAIN_INTEGRATION_NAME, | ||
| OPENAI_INTEGRATION_NAME, | ||
| startInactiveSpan, | ||
| waitForTracingChannelBinding, | ||
| } from '@sentry/core'; | ||
| import { DEBUG_BUILD } from '../../debug-build'; | ||
| import { CHANNELS } from '../../orchestrion/channels'; | ||
| import { langchainEmbeddingsChannels } from '../../orchestrion/config/langchain'; | ||
| import { bindTracingChannelToSpan } from '../../tracing-channel'; | ||
|
|
||
| // Same name as the OTel integration by design: when enabled, the OTel 'LangChain' integration is | ||
| // dropped from the default set (see the Node opt-in loader). | ||
| const INTEGRATION_NAME = LANGCHAIN_INTEGRATION_NAME; | ||
|
|
||
| // LangChain drives the underlying AI provider SDKs itself, so while it's active those providers must | ||
| // not also instrument, or every call would produce two spans (mirrors the OTel path's skip list). | ||
| const SKIPPED_PROVIDERS = [OPENAI_INTEGRATION_NAME, ANTHROPIC_AI_INTEGRATION_NAME, GOOGLE_GENAI_INTEGRATION_NAME]; | ||
|
|
||
| // The chat-model channels carry the live args array of `invoke(input, options)` / `_streamIterator(input, options)`. | ||
| interface RunnableChannelContext { | ||
| arguments: unknown[]; | ||
| } | ||
|
|
||
| // The embeddings channels carry the instance (`self`) and the `embedQuery(text)` / `embedDocuments(texts)` args. | ||
| interface EmbeddingsChannelContext { | ||
| self?: unknown; | ||
| arguments: unknown[]; | ||
| } | ||
|
|
||
| let subscribed = false; | ||
|
|
||
| // Registered lazily on the first LangChain call (not at `setupOnce`) so a direct provider call made | ||
| // before any LangChain call still gets its own span — matches the OTel patch-on-import timing. It | ||
| // also stops the underlying SDK from double-instrumenting embeddings, whose `embedQuery`/ | ||
| // `embedDocuments` call the provider SDK (e.g. `openai`) internally. | ||
| function markProvidersSkipped(): void { | ||
| _INTERNAL_skipAiProviderWrapping(SKIPPED_PROVIDERS); | ||
| } | ||
|
|
||
| const _langChainChannelIntegration = ((options: LangChainOptions = {}) => { | ||
| return { | ||
| name: INTEGRATION_NAME, | ||
| setupOnce() { | ||
| // `tracingChannel` is unavailable before Node 18.19, and a second `init()` would double-subscribe. | ||
| if (!diagnosticsChannel.tracingChannel || subscribed) { | ||
| return; | ||
| } | ||
| subscribed = true; | ||
|
|
||
| // One stateful handler tracks spans across the whole run tree, just like the OTel path. | ||
| const sentryHandler = createLangChainCallbackHandler(options); | ||
|
|
||
| // Chat models: inject the Sentry callback handler into the call options (arg 1). LangChain's own | ||
| // callback dispatch then creates the spans, exactly as in the OTel path, so no span is opened | ||
| // here — a `start` subscriber (which also makes orchestrion wrap the function) is enough. | ||
| const injectHandler = (message: unknown): void => { | ||
| markProvidersSkipped(); | ||
|
|
||
| const args = (message as RunnableChannelContext).arguments; | ||
| if (!Array.isArray(args)) { | ||
| return; | ||
| } | ||
|
|
||
| let callOptions = args[1] as Record<string, unknown> | undefined; | ||
| if (!callOptions || typeof callOptions !== 'object' || Array.isArray(callOptions)) { | ||
| callOptions = {}; | ||
| args[1] = callOptions; | ||
| } | ||
|
|
||
| callOptions.callbacks = _INTERNAL_mergeLangChainCallbackHandler(callOptions.callbacks, sentryHandler); | ||
| }; | ||
|
|
||
| for (const channelName of [CHANNELS.LANGCHAIN_CHAT_MODEL_INVOKE, CHANNELS.LANGCHAIN_CHAT_MODEL_STREAM]) { | ||
| DEBUG_BUILD && debug.log(`[orchestrion:langchain] subscribing to channel "${channelName}"`); | ||
| diagnosticsChannel.tracingChannel<RunnableChannelContext>(channelName).start.subscribe(injectHandler); | ||
| } | ||
|
|
||
| // Embeddings don't use the callback system — the OTel path wraps the method in its own span, so | ||
| // do the same here. `bindTracingChannelToSpan` needs the async-context binding that | ||
| // `initOpenTelemetry()` registers after `setupOnce`, so wait for it before subscribing. | ||
| waitForTracingChannelBinding(() => { | ||
| for (const channelName of langchainEmbeddingsChannels) { | ||
| DEBUG_BUILD && debug.log(`[orchestrion:langchain] subscribing to channel "${channelName}"`); | ||
| bindTracingChannelToSpan( | ||
| diagnosticsChannel.tracingChannel<EmbeddingsChannelContext>(channelName), | ||
| data => createEmbeddingsSpan(data, options), | ||
| { captureError: () => ({ mechanism: { handled: false, type: 'auto.ai.langchain' } }) }, | ||
| ); | ||
| } | ||
| }); | ||
| }, | ||
| }; | ||
| }) satisfies IntegrationFn; | ||
|
|
||
| function createEmbeddingsSpan(data: EmbeddingsChannelContext, options: LangChainOptions): Span { | ||
| // `embedQuery`/`embedDocuments` call the provider SDK internally, so skip that SDK's own | ||
| // instrumentation before its channel fires (the producer runs at the embeddings channel's `start`). | ||
| markProvidersSkipped(); | ||
|
|
||
| const input = (data.arguments ?? [])[0]; | ||
|
|
||
| return startInactiveSpan(_INTERNAL_getLangChainEmbeddingsSpanOptions(data.self, input, options)); | ||
| } | ||
|
|
||
| /** | ||
| * EXPERIMENTAL — orchestrion-driven LangChain integration. Subscribes to the diagnostics_channels | ||
| * injected into `@langchain/core`'s `BaseChatModel` (to inject the Sentry callback handler) and into | ||
| * `@langchain/openai`'s embedding methods, so it requires the orchestrion runtime hook or bundler plugin. | ||
| */ | ||
| export const langChainChannelIntegration = defineIntegration(_langChainChannelIntegration); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,73 @@ | ||
| import type { InstrumentationConfig } from '@apm-js-collab/code-transformer'; | ||
|
|
||
| // TODO: Stub for the `langchain` orchestrion integration (ports `SentryLangChainInstrumentation`). | ||
| export const langchainConfig: InstrumentationConfig[] = []; | ||
| // `@langchain/*` packages ship dual CJS/ESM builds (`.cjs` for `require`, `.js` for `import`) and the | ||
| // matcher compares `filePath` exactly, so each hook is declared once per built file. | ||
|
|
||
| export const langchainChannels = {} as const; | ||
| // LangChain's chat model methods live on `BaseChatModel` in `@langchain/core` and are inherited by | ||
| // every provider class (`ChatAnthropic`, `ChatOpenAI`, …), so a single hook there covers all | ||
| // providers. `invoke` also backs `.batch()` (which calls `invoke` per item); `_streamIterator` | ||
| // backs `.stream()`. The vendored OTel instrumentation instead patched each provider package to | ||
| // dodge `@langchain/core` being bundled, but orchestrion transforms its source directly regardless | ||
| // of bundling. | ||
| const chatModelConfig = ['dist/language_models/chat_models.cjs', 'dist/language_models/chat_models.js'].flatMap( | ||
| filePath => { | ||
| const module = { name: '@langchain/core', versionRange: '>=0.1.0 <2.0.0', filePath }; | ||
|
|
||
| return [ | ||
| { | ||
| channelName: 'chatModelInvoke', | ||
| module, | ||
| functionQuery: { className: 'BaseChatModel', methodName: 'invoke', kind: 'Async' as const }, | ||
| }, | ||
| { | ||
| channelName: 'chatModelStream', | ||
| module, | ||
| functionQuery: { className: 'BaseChatModel', methodName: '_streamIterator', kind: 'Async' as const }, | ||
| }, | ||
| ]; | ||
| }, | ||
| ); | ||
|
|
||
| // Embeddings have no shared concrete method on the base class (each provider implements | ||
| // `embedQuery`/`embedDocuments`), so they're hooked per package. These are the provider packages the | ||
| // vendored OTel instrumentation covered that actually ship an `Embeddings` subclass: `@langchain/openai`, | ||
| // `@langchain/google-genai` and `@langchain/mistralai` define the two methods on their own class, while | ||
| // `@langchain/google-vertexai` inherits them from the shared `@langchain/google-common` base, so that base | ||
| // is the module hooked for it. (anthropic and groq are chat-only — they ship no embeddings class.) The | ||
| // `embedQuery`/`embedDocuments` channel names are per-method; orchestrion prefixes them with the module | ||
| // name, so the full channel strings stay distinct across packages. | ||
| const EMBED_QUERY = 'embedQuery'; | ||
| const EMBED_DOCUMENTS = 'embedDocuments'; | ||
|
|
||
| const EMBEDDINGS_PROVIDERS = [ | ||
| { name: '@langchain/openai', versionRange: '>=0.1.0 <2.0.0', methods: [EMBED_QUERY, EMBED_DOCUMENTS] }, | ||
| { name: '@langchain/google-genai', versionRange: '>=0.1.0 <3.0.0', methods: [EMBED_QUERY, EMBED_DOCUMENTS] }, | ||
| { name: '@langchain/mistralai', versionRange: '>=0.1.0 <2.0.0', methods: [EMBED_QUERY, EMBED_DOCUMENTS] }, | ||
| // `@langchain/google-vertexai` inherits its embed methods from this shared base. The base's | ||
| // `embedQuery` delegates to `embedDocuments`, so hooking only `embedDocuments` still traces both | ||
| // entry points as a single span each, instead of emitting a nested duplicate for `embedQuery`. | ||
| { name: '@langchain/google-common', versionRange: '>=0.1.0 <3.0.0', methods: [EMBED_DOCUMENTS] }, | ||
| ]; | ||
|
|
||
| const embeddingsConfig = EMBEDDINGS_PROVIDERS.flatMap(({ name, versionRange, methods }) => | ||
| ['dist/embeddings.cjs', 'dist/embeddings.js'].flatMap(filePath => | ||
| methods.map(method => ({ | ||
| channelName: method, | ||
| module: { name, versionRange, filePath }, | ||
| functionQuery: { methodName: method, kind: 'Async' as const }, | ||
| })), | ||
| ), | ||
| ); | ||
|
|
||
| export const langchainConfig = [...chatModelConfig, ...embeddingsConfig] satisfies InstrumentationConfig[]; | ||
|
|
||
| // The embeddings channel strings the subscriber binds to, derived from the provider list above so that | ||
| // adding a provider is a single edit that both instruments it and subscribes the listener to it. | ||
| export const langchainEmbeddingsChannels = EMBEDDINGS_PROVIDERS.flatMap(({ name, methods }) => | ||
| methods.map(method => `orchestrion:${name}:${method}`), | ||
| ); | ||
|
|
||
| export const langchainChannels = { | ||
| LANGCHAIN_CHAT_MODEL_INVOKE: 'orchestrion:@langchain/core:chatModelInvoke', | ||
| LANGCHAIN_CHAT_MODEL_STREAM: 'orchestrion:@langchain/core:chatModelStream', | ||
| } as const; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
captureErroronbindTracingChannelToSpan, so rejected embedding calls callcaptureExceptionwhile 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 omitcaptureError.Triggered by project rule: PR Review Guidelines for Cursor Bot
Reviewed by Cursor Bugbot for commit 6abf5e8. Configure here.