-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
feat(node-core): Add integration disabling mechanism to prevent instrumentation conflicts #17972
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
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
e5374c0
update with disabling mechanisim
RulaKhaled 658e58b
reorder integrations
RulaKhaled b1ccb9b
deisable integration
RulaKhaled 23f66ec
refactor
RulaKhaled 25d060f
add missing import
RulaKhaled e01e2c4
update names
RulaKhaled eeae1b3
update with tests
RulaKhaled 72cb950
update to catch within patch
RulaKhaled baebb0d
Refactor to proposed wrapping helpers
andreiborza 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
95 changes: 95 additions & 0 deletions
95
...ages/node-integration-tests/suites/tracing/langchain/scenario-openai-before-langchain.mjs
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,95 @@ | ||
| import * as Sentry from '@sentry/node'; | ||
| import express from 'express'; | ||
|
|
||
| function startMockAnthropicServer() { | ||
| const app = express(); | ||
| app.use(express.json()); | ||
|
|
||
| app.post('/v1/messages', (req, res) => { | ||
| res.json({ | ||
| id: 'msg_test123', | ||
| type: 'message', | ||
| role: 'assistant', | ||
| content: [ | ||
| { | ||
| type: 'text', | ||
| text: 'Mock response from Anthropic!', | ||
| }, | ||
| ], | ||
| model: req.body.model, | ||
| stop_reason: 'end_turn', | ||
| stop_sequence: null, | ||
| usage: { | ||
| input_tokens: 10, | ||
| output_tokens: 15, | ||
| }, | ||
| }); | ||
| }); | ||
|
|
||
| return new Promise(resolve => { | ||
| const server = app.listen(0, () => { | ||
| resolve(server); | ||
| }); | ||
| }); | ||
| } | ||
|
|
||
| async function run() { | ||
| const server = await startMockAnthropicServer(); | ||
| const baseURL = `http://localhost:${server.address().port}`; | ||
|
|
||
| await Sentry.startSpan({ op: 'function', name: 'main' }, async () => { | ||
| // EDGE CASE: Import and instantiate Anthropic client BEFORE LangChain is imported | ||
| // This simulates the timing issue where a user creates an Anthropic client in one file | ||
| // before importing LangChain in another file | ||
| const { default: Anthropic } = await import('@anthropic-ai/sdk'); | ||
| const anthropicClient = new Anthropic({ | ||
| apiKey: 'mock-api-key', | ||
| baseURL, | ||
| }); | ||
|
|
||
| // Use the Anthropic client directly - this will be instrumented by the Anthropic integration | ||
| await anthropicClient.messages.create({ | ||
| model: 'claude-3-5-sonnet-20241022', | ||
| messages: [{ role: 'user', content: 'Direct Anthropic call' }], | ||
| temperature: 0.7, | ||
| max_tokens: 100, | ||
| }); | ||
|
|
||
| // NOW import LangChain - at this point it will mark Anthropic to be skipped | ||
| // But the client created above is already instrumented | ||
| const { ChatAnthropic } = await import('@langchain/anthropic'); | ||
|
|
||
| // Create a LangChain model - this uses Anthropic under the hood | ||
| const langchainModel = new ChatAnthropic({ | ||
| model: 'claude-3-5-sonnet-20241022', | ||
| temperature: 0.7, | ||
| maxTokens: 100, | ||
| apiKey: 'mock-api-key', | ||
| clientOptions: { | ||
| baseURL, | ||
| }, | ||
| }); | ||
|
|
||
| // Use LangChain - this will be instrumented by LangChain integration | ||
| await langchainModel.invoke('LangChain Anthropic call'); | ||
|
|
||
| // Create ANOTHER Anthropic client after LangChain was imported | ||
| // This one should NOT be instrumented (skip mechanism works correctly) | ||
| const anthropicClient2 = new Anthropic({ | ||
| apiKey: 'mock-api-key', | ||
| baseURL, | ||
| }); | ||
|
|
||
| await anthropicClient2.messages.create({ | ||
| model: 'claude-3-5-sonnet-20241022', | ||
| messages: [{ role: 'user', content: 'Second direct Anthropic call' }], | ||
| temperature: 0.7, | ||
| max_tokens: 100, | ||
| }); | ||
| }); | ||
|
|
||
| await Sentry.flush(2000); | ||
| server.close(); | ||
| } | ||
|
|
||
| run(); |
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
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,64 @@ | ||
| import { DEBUG_BUILD } from '../../debug-build'; | ||
| import { debug } from '../debug-logger'; | ||
|
|
||
| /** | ||
| * Registry tracking which AI provider modules should skip instrumentation wrapping. | ||
| * | ||
| * This prevents duplicate spans when a higher-level integration (like LangChain) | ||
| * already instruments AI providers at a higher abstraction level. | ||
| */ | ||
| const SKIPPED_AI_PROVIDERS = new Set<string>(); | ||
|
|
||
| /** | ||
| * Mark AI provider modules to skip instrumentation wrapping. | ||
| * | ||
| * This prevents duplicate spans when a higher-level integration (like LangChain) | ||
| * already instruments AI providers at a higher abstraction level. | ||
| * | ||
| * @internal | ||
| * @param modules - Array of npm module names to skip (e.g., '@anthropic-ai/sdk', 'openai') | ||
| * | ||
| * @example | ||
| * ```typescript | ||
| * // In LangChain integration | ||
| * _INTERNAL_skipAiProviderWrapping(['@anthropic-ai/sdk', 'openai', '@google/generative-ai']); | ||
| * ``` | ||
| */ | ||
| export function _INTERNAL_skipAiProviderWrapping(modules: string[]): void { | ||
| modules.forEach(module => { | ||
| SKIPPED_AI_PROVIDERS.add(module); | ||
| DEBUG_BUILD && debug.log(`AI provider "${module}" wrapping will be skipped`); | ||
| }); | ||
| } | ||
|
|
||
| /** | ||
| * Check if an AI provider module should skip instrumentation wrapping. | ||
| * | ||
| * @internal | ||
| * @param module - The npm module name (e.g., '@anthropic-ai/sdk', 'openai') | ||
| * @returns true if wrapping should be skipped | ||
| * | ||
| * @example | ||
| * ```typescript | ||
| * // In AI provider instrumentation | ||
| * if (_INTERNAL_shouldSkipAiProviderWrapping('@anthropic-ai/sdk')) { | ||
| * return Reflect.construct(Original, args); // Don't instrument | ||
| * } | ||
| * ``` | ||
| */ | ||
| export function _INTERNAL_shouldSkipAiProviderWrapping(module: string): boolean { | ||
| return SKIPPED_AI_PROVIDERS.has(module); | ||
| } | ||
|
|
||
| /** | ||
| * Clear all AI provider skip registrations. | ||
| * | ||
| * This is automatically called at the start of Sentry.init() to ensure a clean state | ||
| * between different client initializations. | ||
| * | ||
| * @internal | ||
| */ | ||
| export function _INTERNAL_clearAiProviderSkips(): void { | ||
| SKIPPED_AI_PROVIDERS.clear(); | ||
| DEBUG_BUILD && debug.log('Cleared AI provider skip registrations'); | ||
| } |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| import { beforeEach, describe, expect, it } from 'vitest'; | ||
| import { | ||
| _INTERNAL_clearAiProviderSkips, | ||
| _INTERNAL_shouldSkipAiProviderWrapping, | ||
| _INTERNAL_skipAiProviderWrapping, | ||
| ANTHROPIC_AI_INTEGRATION_NAME, | ||
| GOOGLE_GENAI_INTEGRATION_NAME, | ||
| OPENAI_INTEGRATION_NAME, | ||
| } from '../../../../src/index'; | ||
|
|
||
| describe('AI Provider Skip', () => { | ||
| beforeEach(() => { | ||
| _INTERNAL_clearAiProviderSkips(); | ||
| }); | ||
|
|
||
| describe('_INTERNAL_skipAiProviderWrapping', () => { | ||
| it('marks a single provider to be skipped', () => { | ||
| _INTERNAL_skipAiProviderWrapping([OPENAI_INTEGRATION_NAME]); | ||
| expect(_INTERNAL_shouldSkipAiProviderWrapping(OPENAI_INTEGRATION_NAME)).toBe(true); | ||
| expect(_INTERNAL_shouldSkipAiProviderWrapping(ANTHROPIC_AI_INTEGRATION_NAME)).toBe(false); | ||
| }); | ||
|
|
||
| it('marks multiple providers to be skipped', () => { | ||
| _INTERNAL_skipAiProviderWrapping([OPENAI_INTEGRATION_NAME, ANTHROPIC_AI_INTEGRATION_NAME]); | ||
| expect(_INTERNAL_shouldSkipAiProviderWrapping(OPENAI_INTEGRATION_NAME)).toBe(true); | ||
| expect(_INTERNAL_shouldSkipAiProviderWrapping(ANTHROPIC_AI_INTEGRATION_NAME)).toBe(true); | ||
| expect(_INTERNAL_shouldSkipAiProviderWrapping(GOOGLE_GENAI_INTEGRATION_NAME)).toBe(false); | ||
| }); | ||
|
|
||
| it('is idempotent - can mark same provider multiple times', () => { | ||
| _INTERNAL_skipAiProviderWrapping([OPENAI_INTEGRATION_NAME]); | ||
| _INTERNAL_skipAiProviderWrapping([OPENAI_INTEGRATION_NAME]); | ||
| _INTERNAL_skipAiProviderWrapping([OPENAI_INTEGRATION_NAME]); | ||
| expect(_INTERNAL_shouldSkipAiProviderWrapping(OPENAI_INTEGRATION_NAME)).toBe(true); | ||
| }); | ||
| }); | ||
|
|
||
| describe('_INTERNAL_shouldSkipAiProviderWrapping', () => { | ||
| it('returns false for unmarked providers', () => { | ||
| expect(_INTERNAL_shouldSkipAiProviderWrapping(OPENAI_INTEGRATION_NAME)).toBe(false); | ||
| expect(_INTERNAL_shouldSkipAiProviderWrapping(ANTHROPIC_AI_INTEGRATION_NAME)).toBe(false); | ||
| expect(_INTERNAL_shouldSkipAiProviderWrapping(GOOGLE_GENAI_INTEGRATION_NAME)).toBe(false); | ||
| }); | ||
|
|
||
| it('returns true after marking provider to be skipped', () => { | ||
| _INTERNAL_skipAiProviderWrapping([ANTHROPIC_AI_INTEGRATION_NAME]); | ||
| expect(_INTERNAL_shouldSkipAiProviderWrapping(ANTHROPIC_AI_INTEGRATION_NAME)).toBe(true); | ||
| }); | ||
| }); | ||
|
|
||
| describe('_INTERNAL_clearAiProviderSkips', () => { | ||
| it('clears all skip registrations', () => { | ||
| _INTERNAL_skipAiProviderWrapping([OPENAI_INTEGRATION_NAME, ANTHROPIC_AI_INTEGRATION_NAME]); | ||
| expect(_INTERNAL_shouldSkipAiProviderWrapping(OPENAI_INTEGRATION_NAME)).toBe(true); | ||
| expect(_INTERNAL_shouldSkipAiProviderWrapping(ANTHROPIC_AI_INTEGRATION_NAME)).toBe(true); | ||
|
|
||
| _INTERNAL_clearAiProviderSkips(); | ||
|
|
||
| expect(_INTERNAL_shouldSkipAiProviderWrapping(OPENAI_INTEGRATION_NAME)).toBe(false); | ||
| expect(_INTERNAL_shouldSkipAiProviderWrapping(ANTHROPIC_AI_INTEGRATION_NAME)).toBe(false); | ||
| }); | ||
|
|
||
| it('can be called multiple times safely', () => { | ||
| _INTERNAL_skipAiProviderWrapping([OPENAI_INTEGRATION_NAME]); | ||
| _INTERNAL_clearAiProviderSkips(); | ||
| _INTERNAL_clearAiProviderSkips(); | ||
| _INTERNAL_clearAiProviderSkips(); | ||
| expect(_INTERNAL_shouldSkipAiProviderWrapping(OPENAI_INTEGRATION_NAME)).toBe(false); | ||
| }); | ||
| }); | ||
| }); |
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.
Bug: Reinit Breaks Instrumentation Skip Logic
Calling
_INTERNAL_clearAiProviderSkips()in_setupIntegrations()can cause incorrect instrumentation whenSentry.init()is called multiple times. If LangChain was imported during the first initialization and marked AI providers to skip, a second initialization clears those markers. Since LangChain's_patchwon't run again (module already loaded), new AI provider client instances created after the second init will be incorrectly instrumented, even though LangChain is active. The skip markers should persist across client reinitializations or only be cleared when LangChain's patch logic runs.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.
This is a limitation of the approach taken, should probably be fine in most cases.