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
15
commits 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
Show all changes
15 commits
Select commit
Hold shift + click to select a range
5353fe6
fix(nestjs): trace WebSocket interceptor contexts
faizansaiyed123 e61512e
fix(nestjs): instrument existing WebSocket observable
faizansaiyed123 daea785
fix(nestjs): remove unnecessary rxjs peer dependency
faizansaiyed123 b784167
test(nestjs): assert scope during WebSocket subscription
faizansaiyed123 c8d133c
test(nestjs): add namespaced WebSocket tracing endpoint
faizansaiyed123 40e8cc4
test(nestjs): register namespaced WebSocket gateway
faizansaiyed123 2a76c76
test(nestjs): assert WebSocket transaction naming in e2e
faizansaiyed123 8838780
fix(nestjs): create spans for WebSocket handlers
faizansaiyed123 3708479
test(nestjs): assert WebSocket spans are created and ended
faizansaiyed123 8b83065
test(nestjs): assert WebSocket span operation in e2e
faizansaiyed123 61af198
style(nestjs): format WebSocket tracing imports
faizansaiyed123 55a5769
test(nestjs): assert isolation scope during handler execution
faizansaiyed123 d62c631
fix(nestjs): use conventional websocket span attributes
faizansaiyed123 b7f16f5
test(nestjs): cover websocket span cleanup
faizansaiyed123 faf9eaa
test(nestjs): assert conventional websocket span op
faizansaiyed123 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
Some comments aren't visible on the classic Files Changed page.
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
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
156 changes: 156 additions & 0 deletions
156
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,156 @@ | ||
| import type { CallHandler, ExecutionContext } from '@nestjs/common'; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. h: this PR only relies on unit tests to verify the introduced behavior. let's please move as much as possible of this to our |
||
| import { SENTRY_OP, SENTRY_ORIGIN } from '@sentry/conventions/attributes'; | ||
| import { WEBSOCKET } from '@sentry/conventions/op'; | ||
| import { getDefaultIsolationScope, getIsolationScope, startSpanManual } from '@sentry/core'; | ||
| import { of, tap } 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, | ||
| span: { | ||
| end: vi.fn(), | ||
| }, | ||
| })); | ||
|
|
||
| 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; | ||
| } | ||
| }), | ||
| withActiveSpan: vi.fn((_span: unknown, callback: () => unknown) => callback()), | ||
| startSpanManual: vi.fn((_options: unknown, callback: (span: unknown) => unknown) => callback(mocks.span)), | ||
| 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'); | ||
| expect(startSpanManual).toHaveBeenCalledWith( | ||
| { | ||
| name: 'WS /events message', | ||
| attributes: { | ||
| [SENTRY_OP]: WEBSOCKET, | ||
| [SENTRY_ORIGIN]: 'auto.middleware.nestjs.interceptor', | ||
| }, | ||
| }, | ||
| expect.any(Function), | ||
| ); | ||
| expect(mocks.span.end).toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('ends the WebSocket span if the handler throws', () => { | ||
| const interceptor = createInterceptor(); | ||
| const context = createContext({ nsp: { name: '/events' } }, 'message'); | ||
| const next = { handle: vi.fn(() => { throw new Error('handler failed'); }) } as unknown as CallHandler; | ||
|
|
||
| expect(() => interceptor.intercept(context, next)).toThrow('handler failed'); | ||
| expect(mocks.span.end).toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| 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).pipe( | ||
| tap(() => { | ||
| expect(mocks.inIsolationScope).toBe(true); | ||
| }), | ||
| ); | ||
| }), | ||
| } 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.