-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
fix(nextjs): Skip tracing for tunnel requests #19861
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
chargome
wants to merge
2
commits into
develop
Choose a base branch
from
cg/next-skip-tunnel-route
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.
+168
−19
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
159 changes: 159 additions & 0 deletions
159
packages/nextjs/test/utils/dropMiddlewareTunnelRequests.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,159 @@ | ||
| import { beforeEach, describe, expect, it, vi } from 'vitest'; | ||
| import { dropMiddlewareTunnelRequests } from '../../src/common/utils/dropMiddlewareTunnelRequests'; | ||
| import { TRANSACTION_ATTR_SHOULD_DROP_TRANSACTION } from '../../src/common/span-attributes-with-logic-attached'; | ||
|
|
||
| const globalWithInjectedValues = global as typeof global & { | ||
| _sentryRewritesTunnelPath?: string; | ||
| }; | ||
|
|
||
| vi.mock('@sentry/core', async requireActual => { | ||
| return { | ||
| ...(await requireActual<any>()), | ||
| getClient: () => ({ | ||
| getOptions: () => ({}), | ||
| }), | ||
| }; | ||
| }); | ||
|
|
||
| vi.mock('@sentry/opentelemetry', () => ({ | ||
| isSentryRequestSpan: () => false, | ||
| })); | ||
|
|
||
| function createMockSpan(): { setAttribute: ReturnType<typeof vi.fn>; attributes: Record<string, unknown> } { | ||
| const attributes: Record<string, unknown> = {}; | ||
| return { | ||
| attributes, | ||
| setAttribute: vi.fn((key: string, value: unknown) => { | ||
| attributes[key] = value; | ||
| }), | ||
| }; | ||
| } | ||
|
|
||
| beforeEach(() => { | ||
| globalWithInjectedValues._sentryRewritesTunnelPath = undefined; | ||
| }); | ||
|
|
||
| describe('dropMiddlewareTunnelRequests', () => { | ||
| describe('BaseServer.handleRequest spans', () => { | ||
| it('marks BaseServer.handleRequest span for dropping when http.target matches tunnel path', () => { | ||
| globalWithInjectedValues._sentryRewritesTunnelPath = '/monitoring'; | ||
| const span = createMockSpan(); | ||
|
|
||
| dropMiddlewareTunnelRequests(span as any, { | ||
| 'next.span_type': 'BaseServer.handleRequest', | ||
| 'http.target': '/monitoring?o=123&p=456', | ||
| }); | ||
|
|
||
| expect(span.setAttribute).toHaveBeenCalledWith(TRANSACTION_ATTR_SHOULD_DROP_TRANSACTION, true); | ||
| }); | ||
|
|
||
| it('marks BaseServer.handleRequest span for dropping when http.target exactly matches tunnel path', () => { | ||
| globalWithInjectedValues._sentryRewritesTunnelPath = '/monitoring'; | ||
| const span = createMockSpan(); | ||
|
|
||
| dropMiddlewareTunnelRequests(span as any, { | ||
| 'next.span_type': 'BaseServer.handleRequest', | ||
| 'http.target': '/monitoring', | ||
| }); | ||
|
|
||
| expect(span.setAttribute).toHaveBeenCalledWith(TRANSACTION_ATTR_SHOULD_DROP_TRANSACTION, true); | ||
| }); | ||
|
|
||
| it('does not mark BaseServer.handleRequest span for dropping when http.target does not match tunnel path', () => { | ||
| globalWithInjectedValues._sentryRewritesTunnelPath = '/monitoring'; | ||
| const span = createMockSpan(); | ||
|
|
||
| dropMiddlewareTunnelRequests(span as any, { | ||
| 'next.span_type': 'BaseServer.handleRequest', | ||
| 'http.target': '/api/users', | ||
| }); | ||
|
|
||
| expect(span.setAttribute).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('does not mark BaseServer.handleRequest span for dropping when http.target shares tunnel path prefix', () => { | ||
| globalWithInjectedValues._sentryRewritesTunnelPath = '/monitoring'; | ||
| const span = createMockSpan(); | ||
|
|
||
| dropMiddlewareTunnelRequests(span as any, { | ||
| 'next.span_type': 'BaseServer.handleRequest', | ||
| 'http.target': '/monitoring-dashboard', | ||
| }); | ||
|
|
||
| expect(span.setAttribute).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('does not mark BaseServer.handleRequest span when no tunnel path is configured', () => { | ||
| const span = createMockSpan(); | ||
|
|
||
| dropMiddlewareTunnelRequests(span as any, { | ||
| 'next.span_type': 'BaseServer.handleRequest', | ||
| 'http.target': '/monitoring', | ||
| }); | ||
|
|
||
| expect(span.setAttribute).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('handles BaseServer.handleRequest span with basePath prefix in http.target', () => { | ||
| globalWithInjectedValues._sentryRewritesTunnelPath = '/basepath/monitoring'; | ||
| const span = createMockSpan(); | ||
|
|
||
| dropMiddlewareTunnelRequests(span as any, { | ||
| 'next.span_type': 'BaseServer.handleRequest', | ||
| 'http.target': '/basepath/monitoring?o=123&p=456', | ||
| }); | ||
|
|
||
| expect(span.setAttribute).toHaveBeenCalledWith(TRANSACTION_ATTR_SHOULD_DROP_TRANSACTION, true); | ||
| }); | ||
| }); | ||
|
|
||
| describe('Middleware.execute spans', () => { | ||
| it('marks middleware span for dropping when http.target matches tunnel path', () => { | ||
| globalWithInjectedValues._sentryRewritesTunnelPath = '/monitoring'; | ||
| const span = createMockSpan(); | ||
|
|
||
| dropMiddlewareTunnelRequests(span as any, { | ||
| 'next.span_type': 'Middleware.execute', | ||
| 'http.target': '/monitoring?o=123&p=456', | ||
| }); | ||
|
|
||
| expect(span.setAttribute).toHaveBeenCalledWith(TRANSACTION_ATTR_SHOULD_DROP_TRANSACTION, true); | ||
| }); | ||
| }); | ||
|
|
||
| describe('unrelated spans', () => { | ||
| it('does not process spans without matching span type or origin', () => { | ||
| globalWithInjectedValues._sentryRewritesTunnelPath = '/monitoring'; | ||
| const span = createMockSpan(); | ||
|
|
||
| dropMiddlewareTunnelRequests(span as any, { | ||
| 'next.span_type': 'SomeOtherSpanType', | ||
| 'http.target': '/monitoring', | ||
| }); | ||
|
|
||
| expect(span.setAttribute).not.toHaveBeenCalled(); | ||
| }); | ||
| }); | ||
|
|
||
| describe('skipOpenTelemetrySetup', () => { | ||
| it('does not process spans when skipOpenTelemetrySetup is true', async () => { | ||
| const core = await import('@sentry/core'); | ||
| const originalGetClient = core.getClient; | ||
| vi.spyOn(core, 'getClient').mockReturnValueOnce({ | ||
| getOptions: () => ({ skipOpenTelemetrySetup: true }), | ||
| } as any); | ||
|
|
||
| globalWithInjectedValues._sentryRewritesTunnelPath = '/monitoring'; | ||
| const span = createMockSpan(); | ||
|
|
||
| dropMiddlewareTunnelRequests(span as any, { | ||
| 'next.span_type': 'BaseServer.handleRequest', | ||
| 'http.target': '/monitoring', | ||
| }); | ||
|
|
||
| expect(span.setAttribute).not.toHaveBeenCalled(); | ||
|
|
||
| vi.mocked(core.getClient).mockRestore(); | ||
| }); | ||
| }); | ||
| }); | ||
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.