Repository navigation
fix(nestjs): trace WebSocket interceptor contexts #24862
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
faizansaiyed123
wants to merge
1
commit into
getsentry:develop
Choose a base branch
from
faizansaiyed123:fix/nestjs-websocket-tracing-interceptor
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 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
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
125 changes: 125 additions & 0 deletions
125
packages/nestjs/test/sentry-tracing-interceptor.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,125 @@ | ||
| import type { CallHandler, ExecutionContext } from '@nestjs/common'; | ||
| import { getDefaultIsolationScope, getIsolationScope } from '@sentry/core'; | ||
| import { of } from 'rxjs'; | ||
| import { beforeEach, describe, expect, it, vi } from 'vitest'; | ||
| import { SentryModule } from '../src/setup'; | ||
|
|
||
| const mocks = vi.hoisted(() => ({ | ||
| isolationScope: { | ||
| setTransactionName: vi.fn(), | ||
| }, | ||
| defaultIsolationScope: {}, | ||
| debugWarn: vi.fn(), | ||
| inIsolationScope: false, | ||
| })); | ||
|
|
||
| vi.mock('@sentry/core', () => ({ | ||
| getDefaultIsolationScope: vi.fn(() => mocks.defaultIsolationScope), | ||
| getIsolationScope: vi.fn(() => mocks.isolationScope), | ||
| withIsolationScope: vi.fn((callback: (scope: unknown) => unknown) => { | ||
| mocks.inIsolationScope = true; | ||
| try { | ||
| return callback(mocks.isolationScope); | ||
| } finally { | ||
| mocks.inIsolationScope = false; | ||
| } | ||
| }), | ||
| debug: { | ||
| warn: mocks.debugWarn, | ||
| }, | ||
| captureException: vi.fn(), | ||
| })); | ||
|
|
||
| type Interceptor = { | ||
| intercept: (context: ExecutionContext, next: CallHandler) => ReturnType<CallHandler['handle']>; | ||
| }; | ||
|
|
||
| function createInterceptor(): Interceptor { | ||
| const provider = SentryModule.forRoot().providers?.[0] as { | ||
| useClass: new () => Interceptor; | ||
| }; | ||
|
|
||
| return new provider.useClass(); | ||
| } | ||
|
|
||
| function createContext(client: unknown, pattern: unknown): ExecutionContext { | ||
| return { | ||
| getType: vi.fn().mockReturnValue('ws'), | ||
| switchToWs: vi.fn().mockReturnValue({ | ||
| getClient: vi.fn().mockReturnValue(client), | ||
| getPattern: vi.fn().mockReturnValue(pattern), | ||
| }), | ||
| } as unknown as ExecutionContext; | ||
| } | ||
|
|
||
| describe('SentryTracingInterceptor', () => { | ||
| beforeEach(() => { | ||
| vi.clearAllMocks(); | ||
| vi.mocked(getIsolationScope).mockReturnValue(mocks.isolationScope as never); | ||
| vi.mocked(getDefaultIsolationScope).mockReturnValue(mocks.defaultIsolationScope as never); | ||
| }); | ||
|
|
||
| it('sets a transaction name for Socket.IO WebSocket contexts', () => { | ||
| const interceptor = createInterceptor(); | ||
| const context = createContext({ nsp: { name: '/events' } }, 'message'); | ||
| const next = { handle: vi.fn(() => of(undefined)) } as unknown as CallHandler; | ||
|
|
||
| interceptor.intercept(context, next).subscribe(); | ||
|
|
||
| expect(mocks.isolationScope.setTransactionName).toHaveBeenCalledWith('WS /events message'); | ||
| }); | ||
|
|
||
| it('sets a transaction name without a namespace for other WebSocket adapters', () => { | ||
| const interceptor = createInterceptor(); | ||
| const context = createContext({}, 'message'); | ||
| const next = { handle: vi.fn(() => of(undefined)) } as unknown as CallHandler; | ||
|
|
||
| interceptor.intercept(context, next).subscribe(); | ||
|
|
||
| expect(mocks.isolationScope.setTransactionName).toHaveBeenCalledWith('WS message'); | ||
| }); | ||
|
|
||
| it('passes through WebSocket contexts without a pattern', () => { | ||
| const interceptor = createInterceptor(); | ||
| const context = createContext({ nsp: { name: '/events' } }, undefined); | ||
| const next = { handle: vi.fn(() => of(undefined)) } as unknown as CallHandler; | ||
|
|
||
| interceptor.intercept(context, next).subscribe(); | ||
|
|
||
| expect(mocks.isolationScope.setTransactionName).not.toHaveBeenCalled(); | ||
| expect(next.handle).toHaveBeenCalledTimes(1); | ||
| }); | ||
|
|
||
| it('supports WebSocket hosts without getPattern', () => { | ||
| const interceptor = createInterceptor(); | ||
| const context = { | ||
| getType: vi.fn().mockReturnValue('ws'), | ||
| switchToWs: vi.fn().mockReturnValue({ | ||
| getClient: vi.fn().mockReturnValue({ nsp: { name: '/events' } }), | ||
| }), | ||
| } as unknown as ExecutionContext; | ||
| const next = { handle: vi.fn(() => of(undefined)) } as unknown as CallHandler; | ||
|
|
||
| interceptor.intercept(context, next).subscribe(); | ||
|
|
||
| expect(mocks.isolationScope.setTransactionName).not.toHaveBeenCalled(); | ||
| expect(next.handle).toHaveBeenCalledTimes(1); | ||
| }); | ||
|
|
||
| it('names WebSocket transactions when the current isolation scope is the default scope', () => { | ||
| const interceptor = createInterceptor(); | ||
| const context = createContext({ nsp: { name: '/events' } }, 'message'); | ||
| const next = { | ||
| handle: vi.fn(() => { | ||
| expect(mocks.inIsolationScope).toBe(true); | ||
| return of(undefined); | ||
| }), | ||
| } as unknown as CallHandler; | ||
| vi.mocked(getIsolationScope).mockReturnValue(mocks.defaultIsolationScope as never); | ||
|
|
||
| interceptor.intercept(context, next).subscribe(); | ||
|
|
||
| expect(mocks.isolationScope.setTransactionName).toHaveBeenCalledWith('WS /events message'); | ||
| expect(next.handle).toHaveBeenCalledTimes(1); | ||
| }); | ||
| }); |
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.