-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
fix(node): Preserve CallbackManager handlers in LangChain instrumentation #20849
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
mdnanocom
wants to merge
15
commits into
getsentry:develop
Choose a base branch
from
mdnanocom:fix/langchain-callbackmanager-preservation
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.
Open
Changes from 3 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
e2b872a
fix(node): Preserve CallbackManager handlers in LangChain instrumenta…
mdnanocom 4c74bfa
test(node): Add unit tests + changelog for LangChain callback fix
mdnanocom b33352b
fix(node): Defensively check inheritableHandlers in LangChain dedupe
mdnanocom 9e2939d
refactor(core): Consolidate LangChain callback merging into mergeSent…
mdnanocom 7dcdd93
review: Tighten CallbackManager detection + drop changelog entry
mdnanocom fb95ad5
Merge remote-tracking branch 'origin/develop' into fix/langchain-call…
mdnanocom 5657854
Merge branch 'develop' into fix/langchain-callbackmanager-preservation
mdnanocom 7333d66
Merge branch 'develop' into fix/langchain-callbackmanager-preservation
mdnanocom d88c503
Merge branch 'develop' into fix/langchain-callbackmanager-preservation
mdnanocom 5e5627a
review: Address andreiborza review on PR #20849
mdnanocom cde3b58
Merge branch 'develop' into fix/langchain-callbackmanager-preservation
mdnanocom 6f36bd1
review: Skip copy when manager already contains the sentry handler
mdnanocom 6bb5463
fix(langchain): Use renamed `_INTERNAL_mergeLangChainCallbackHandler`
mdnanocom 745b9e1
Merge branch 'develop' into fix/langchain-callbackmanager-preservation
mdnanocom 315373d
Merge branch 'develop' into fix/langchain-callbackmanager-preservation
mdnanocom 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
106 changes: 106 additions & 0 deletions
106
packages/node/test/integrations/tracing/langchain.test.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,106 @@ | ||
| import { describe, expect, test, vi } from 'vitest'; | ||
| import { _INTERNAL_augmentCallbackHandlers } from '../../../src/integrations/tracing/langchain/instrumentation'; | ||
|
|
||
| const sentryHandler = { name: 'SentryCallbackHandler' }; | ||
|
|
||
| /** | ||
| * Minimal `CallbackManager` stand-in. We only need the duck-typed shape | ||
| * (`addHandler` + `copy`) for the production code to recognize this as a | ||
| * `CallbackManager` rather than fall through to the "unknown" branch. | ||
| */ | ||
| function makeFakeCallbackManager(existingHandlers: unknown[] = [], existingInheritableHandlers?: unknown[]) { | ||
| const manager = { | ||
| handlers: [...existingHandlers], | ||
| inheritableHandlers: [...(existingInheritableHandlers ?? existingHandlers)], | ||
| addHandler: vi.fn(function (this: any, handler: unknown, inherit?: boolean) { | ||
| this.handlers.push(handler); | ||
| if (inherit !== false) { | ||
| this.inheritableHandlers.push(handler); | ||
| } | ||
| }), | ||
| copy: vi.fn(function (this: any) { | ||
| return makeFakeCallbackManager(this.handlers, this.inheritableHandlers); | ||
| }), | ||
| }; | ||
| return manager; | ||
| } | ||
|
|
||
| describe('augmentCallbackHandlers', () => { | ||
| test('wraps Sentry handler in an array when no callbacks are configured', () => { | ||
| const result = _INTERNAL_augmentCallbackHandlers(undefined, sentryHandler); | ||
| expect(result).toEqual([sentryHandler]); | ||
| }); | ||
|
|
||
| test('appends Sentry handler when callbacks is already an array', () => { | ||
| const other = { name: 'OtherHandler' }; | ||
| const result = _INTERNAL_augmentCallbackHandlers([other], sentryHandler); | ||
| expect(result).toEqual([other, sentryHandler]); | ||
| }); | ||
|
|
||
| test('is idempotent when Sentry handler is already in the array', () => { | ||
| const result = _INTERNAL_augmentCallbackHandlers([sentryHandler], sentryHandler); | ||
| expect(result).toEqual([sentryHandler]); | ||
| }); | ||
|
|
||
| test('preserves inheritable handlers when callbacks is a CallbackManager', () => { | ||
| // Reproduces the LangGraph `streamMode: ['messages']` setup: a | ||
| // CallbackManager carrying a StreamMessagesHandler is passed via | ||
| // options.callbacks. Without this fix, the manager would be wrapped as | ||
| // `[manager, sentryHandler]`, dropping all its inheritable children. | ||
| const streamMessagesHandler = { | ||
| name: 'StreamMessagesHandler', | ||
| lc_prefer_streaming: true, | ||
| }; | ||
| const manager = makeFakeCallbackManager([streamMessagesHandler]); | ||
|
|
||
| const result = _INTERNAL_augmentCallbackHandlers(manager, sentryHandler) as { | ||
| handlers: unknown[]; | ||
| }; | ||
|
|
||
| // The result is a manager (object), not a wrapping array. | ||
| expect(Array.isArray(result)).toBe(false); | ||
| // The original child handler is still there alongside Sentry's. | ||
| expect(result.handlers).toEqual([streamMessagesHandler, sentryHandler]); | ||
| }); | ||
|
|
||
| test('copies the manager rather than mutating the caller-supplied one', () => { | ||
| // If we mutated the original manager, repeated invocations would | ||
| // accumulate Sentry handlers (and tracers from prior runs would leak | ||
| // into subsequent unrelated runs). | ||
| const manager = makeFakeCallbackManager([]); | ||
| _INTERNAL_augmentCallbackHandlers(manager, sentryHandler); | ||
| expect(manager.copy).toHaveBeenCalledTimes(1); | ||
| expect(manager.handlers).toEqual([]); | ||
| }); | ||
|
|
||
| test('does not double-register Sentry handler when copy already contains it', () => { | ||
| const manager = makeFakeCallbackManager([sentryHandler]); | ||
| const result = _INTERNAL_augmentCallbackHandlers(manager, sentryHandler) as { | ||
| handlers: unknown[]; | ||
| addHandler: ReturnType<typeof vi.fn>; | ||
| }; | ||
| expect(result.handlers).toEqual([sentryHandler]); | ||
| expect(result.addHandler).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| test('does not double-register when the handler lives only on inheritableHandlers', () => { | ||
| // Defensive: a CallbackManager subclass or externally-constructed | ||
| // instance might keep the Sentry handler on `inheritableHandlers` | ||
| // without mirroring it onto `handlers`. We must still recognize it | ||
| // as already-registered to avoid duplicate spans on nested calls. | ||
| const manager = makeFakeCallbackManager([], [sentryHandler]); | ||
| const result = _INTERNAL_augmentCallbackHandlers(manager, sentryHandler) as { | ||
| handlers: unknown[]; | ||
| inheritableHandlers: unknown[]; | ||
| addHandler: ReturnType<typeof vi.fn>; | ||
| }; | ||
| expect(result.addHandler).not.toHaveBeenCalled(); | ||
| expect(result.inheritableHandlers).toEqual([sentryHandler]); | ||
| }); | ||
|
|
||
| test('returns the value unchanged when it is neither an array nor a CallbackManager', () => { | ||
| const opaque = { name: 'NotAManager' }; | ||
| const result = _INTERNAL_augmentCallbackHandlers(opaque, sentryHandler); | ||
| expect(result).toBe(opaque); | ||
| }); | ||
| }); |
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.
Uh oh!
There was an error while loading. Please reload this page.