-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
feat(node): move dc integration swap to _init #22173
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
+142
−23
Open
Changes from all commits
Commits
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
117 changes: 117 additions & 0 deletions
117
packages/node/test/sdk/diagnosticsChannelInjection.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,117 @@ | ||
| import type { Integration } from '@sentry/core'; | ||
| import { debug } from '@sentry/core'; | ||
| import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; | ||
| import { init, initWithoutDefaultIntegrations } from '../../src/sdk'; | ||
| import { setDiagnosticsChannelInjectionLoader } from '../../src/sdk/diagnosticsChannelInjection'; | ||
| import { cleanupOtel, resetGlobals } from '../helpers/mockSdkInit'; | ||
|
|
||
| // eslint-disable-next-line no-var | ||
| declare var global: any; | ||
|
|
||
| const PUBLIC_DSN = 'https://username@domain/123'; | ||
|
|
||
| function mockIntegration(name: string): Integration { | ||
| return { name, setupOnce: vi.fn() }; | ||
| } | ||
|
|
||
| // These tests run in definition order: the first runs before any loader is set | ||
| // (opt-out), the second sets it (opt-in). The module-level loader state is | ||
| // isolated per test file by vitest, so it doesn't leak elsewhere. | ||
| describe('diagnostics-channel injection integration swap', () => { | ||
| beforeEach(() => { | ||
| global.__SENTRY__ = {}; | ||
| vi.spyOn(debug, 'enable').mockImplementation(() => undefined); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| cleanupOtel(); | ||
| resetGlobals(); | ||
| vi.clearAllMocks(); | ||
| }); | ||
|
|
||
| it('does not swap integrations when not opted in', () => { | ||
| // Distinct names from the opt-in test below: `@sentry/core` only runs | ||
| // `setupOnce` once per integration name per process, so reusing names across | ||
| // tests would suppress later calls. | ||
| const otelNest = mockIntegration('OptOutNest'); | ||
| const http = mockIntegration('OptOutHttp'); | ||
|
|
||
| init({ | ||
| dsn: PUBLIC_DSN, | ||
| tracesSampleRate: 1, | ||
| skipOpenTelemetrySetup: true, | ||
| defaultIntegrations: [otelNest, http], | ||
| }); | ||
|
|
||
| // No opt-in -> the supplied defaults are set up untouched. | ||
| expect(otelNest.setupOnce).toHaveBeenCalledTimes(1); | ||
| expect(http.setupOnce).toHaveBeenCalledTimes(1); | ||
| }); | ||
|
|
||
| it('replaces the named OTel integrations with the channel integrations, even when defaultIntegrations are supplied by a framework SDK', () => { | ||
| const channelMysql = mockIntegration('Mysql'); | ||
| const channelNest = mockIntegration('Nest'); | ||
| const register = vi.fn(); | ||
| const detect = vi.fn(); | ||
| setDiagnosticsChannelInjectionLoader(() => ({ | ||
| integrations: [channelMysql, channelNest], | ||
| replacedOtelIntegrationNames: ['Mysql', 'Nest'], | ||
| register, | ||
| detect, | ||
| })); | ||
|
|
||
| // Mimics `@sentry/nestjs`, which prepends its OTel `Nest` integration to | ||
| // its own `defaultIntegrations` array (so node's `getDefaultIntegrations` | ||
| // swap never sees it; swap must happen in `init`). | ||
| const otelNest = mockIntegration('Nest'); | ||
| const http = mockIntegration('Http'); | ||
|
|
||
| init({ | ||
| dsn: PUBLIC_DSN, | ||
| tracesSampleRate: 1, | ||
| skipOpenTelemetrySetup: true, | ||
| defaultIntegrations: [otelNest, http], | ||
| }); | ||
|
|
||
| // OTel 'Nest' filtered out, never set up. | ||
| expect(otelNest.setupOnce).not.toHaveBeenCalled(); | ||
| // Channel replacements set up instead. | ||
| expect(channelNest.setupOnce).toHaveBeenCalledTimes(1); | ||
| expect(channelMysql.setupOnce).toHaveBeenCalledTimes(1); | ||
| // Unrelated default preserved. | ||
| expect(http.setupOnce).toHaveBeenCalledTimes(1); | ||
| // Hooks installed and detection ran once. | ||
| expect(register).toHaveBeenCalledTimes(1); | ||
| expect(detect).toHaveBeenCalledTimes(1); | ||
| }); | ||
|
|
||
| it('does not add channel integrations when defaults are explicitly empty', () => { | ||
| const channelEmptyMysql = mockIntegration('EmptyMysql'); | ||
| setDiagnosticsChannelInjectionLoader(() => ({ | ||
| integrations: [channelEmptyMysql], | ||
| replacedOtelIntegrationNames: ['EmptyMysql'], | ||
| register: vi.fn(), | ||
| detect: vi.fn(), | ||
| })); | ||
|
|
||
| // `defaultIntegrations: []` opts out of all defaults; the swap must not | ||
| // resurrect them by appending the channel integrations. | ||
| init({ dsn: PUBLIC_DSN, tracesSampleRate: 1, skipOpenTelemetrySetup: true, defaultIntegrations: [] }); | ||
|
|
||
| expect(channelEmptyMysql.setupOnce).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('does not add channel integrations to initWithoutDefaultIntegrations()', () => { | ||
| const channelNoDefaults = mockIntegration('NoDefaultsMysql'); | ||
| setDiagnosticsChannelInjectionLoader(() => ({ | ||
| integrations: [channelNoDefaults], | ||
| replacedOtelIntegrationNames: ['NoDefaultsMysql'], | ||
| register: vi.fn(), | ||
| detect: vi.fn(), | ||
| })); | ||
|
|
||
| initWithoutDefaultIntegrations({ dsn: PUBLIC_DSN, tracesSampleRate: 1, skipOpenTelemetrySetup: true }); | ||
|
|
||
| expect(channelNoDefaults.setupOnce).not.toHaveBeenCalled(); | ||
| }); | ||
| }); | ||
|
cursor[bot] marked this conversation as resolved.
|
||
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.
Uh oh!
There was an error while loading. Please reload this page.