|
| 1 | +import { beforeEach, describe, expect, it, vi } from 'vitest'; |
| 2 | +import { dropMiddlewareTunnelRequests } from '../../src/common/utils/dropMiddlewareTunnelRequests'; |
| 3 | +import { TRANSACTION_ATTR_SHOULD_DROP_TRANSACTION } from '../../src/common/span-attributes-with-logic-attached'; |
| 4 | + |
| 5 | +const globalWithInjectedValues = global as typeof global & { |
| 6 | + _sentryRewritesTunnelPath?: string; |
| 7 | +}; |
| 8 | + |
| 9 | +vi.mock('@sentry/core', async requireActual => { |
| 10 | + return { |
| 11 | + ...(await requireActual<any>()), |
| 12 | + getClient: () => ({ |
| 13 | + getOptions: () => ({}), |
| 14 | + }), |
| 15 | + }; |
| 16 | +}); |
| 17 | + |
| 18 | +vi.mock('@sentry/opentelemetry', () => ({ |
| 19 | + isSentryRequestSpan: () => false, |
| 20 | +})); |
| 21 | + |
| 22 | +function createMockSpan(): { setAttribute: ReturnType<typeof vi.fn>; attributes: Record<string, unknown> } { |
| 23 | + const attributes: Record<string, unknown> = {}; |
| 24 | + return { |
| 25 | + attributes, |
| 26 | + setAttribute: vi.fn((key: string, value: unknown) => { |
| 27 | + attributes[key] = value; |
| 28 | + }), |
| 29 | + }; |
| 30 | +} |
| 31 | + |
| 32 | +beforeEach(() => { |
| 33 | + globalWithInjectedValues._sentryRewritesTunnelPath = undefined; |
| 34 | +}); |
| 35 | + |
| 36 | +describe('dropMiddlewareTunnelRequests', () => { |
| 37 | + describe('BaseServer.handleRequest spans', () => { |
| 38 | + it('marks BaseServer.handleRequest span for dropping when http.target matches tunnel path', () => { |
| 39 | + globalWithInjectedValues._sentryRewritesTunnelPath = '/monitoring'; |
| 40 | + const span = createMockSpan(); |
| 41 | + |
| 42 | + dropMiddlewareTunnelRequests(span as any, { |
| 43 | + 'next.span_type': 'BaseServer.handleRequest', |
| 44 | + 'http.target': '/monitoring?o=123&p=456', |
| 45 | + }); |
| 46 | + |
| 47 | + expect(span.setAttribute).toHaveBeenCalledWith(TRANSACTION_ATTR_SHOULD_DROP_TRANSACTION, true); |
| 48 | + }); |
| 49 | + |
| 50 | + it('marks BaseServer.handleRequest span for dropping when http.target exactly matches tunnel path', () => { |
| 51 | + globalWithInjectedValues._sentryRewritesTunnelPath = '/monitoring'; |
| 52 | + const span = createMockSpan(); |
| 53 | + |
| 54 | + dropMiddlewareTunnelRequests(span as any, { |
| 55 | + 'next.span_type': 'BaseServer.handleRequest', |
| 56 | + 'http.target': '/monitoring', |
| 57 | + }); |
| 58 | + |
| 59 | + expect(span.setAttribute).toHaveBeenCalledWith(TRANSACTION_ATTR_SHOULD_DROP_TRANSACTION, true); |
| 60 | + }); |
| 61 | + |
| 62 | + it('does not mark BaseServer.handleRequest span for dropping when http.target does not match tunnel path', () => { |
| 63 | + globalWithInjectedValues._sentryRewritesTunnelPath = '/monitoring'; |
| 64 | + const span = createMockSpan(); |
| 65 | + |
| 66 | + dropMiddlewareTunnelRequests(span as any, { |
| 67 | + 'next.span_type': 'BaseServer.handleRequest', |
| 68 | + 'http.target': '/api/users', |
| 69 | + }); |
| 70 | + |
| 71 | + expect(span.setAttribute).not.toHaveBeenCalled(); |
| 72 | + }); |
| 73 | + |
| 74 | + it('does not mark BaseServer.handleRequest span for dropping when http.target shares tunnel path prefix', () => { |
| 75 | + globalWithInjectedValues._sentryRewritesTunnelPath = '/monitoring'; |
| 76 | + const span = createMockSpan(); |
| 77 | + |
| 78 | + dropMiddlewareTunnelRequests(span as any, { |
| 79 | + 'next.span_type': 'BaseServer.handleRequest', |
| 80 | + 'http.target': '/monitoring-dashboard', |
| 81 | + }); |
| 82 | + |
| 83 | + expect(span.setAttribute).not.toHaveBeenCalled(); |
| 84 | + }); |
| 85 | + |
| 86 | + it('does not mark BaseServer.handleRequest span when no tunnel path is configured', () => { |
| 87 | + const span = createMockSpan(); |
| 88 | + |
| 89 | + dropMiddlewareTunnelRequests(span as any, { |
| 90 | + 'next.span_type': 'BaseServer.handleRequest', |
| 91 | + 'http.target': '/monitoring', |
| 92 | + }); |
| 93 | + |
| 94 | + expect(span.setAttribute).not.toHaveBeenCalled(); |
| 95 | + }); |
| 96 | + |
| 97 | + it('handles BaseServer.handleRequest span with basePath prefix in http.target', () => { |
| 98 | + globalWithInjectedValues._sentryRewritesTunnelPath = '/basepath/monitoring'; |
| 99 | + const span = createMockSpan(); |
| 100 | + |
| 101 | + dropMiddlewareTunnelRequests(span as any, { |
| 102 | + 'next.span_type': 'BaseServer.handleRequest', |
| 103 | + 'http.target': '/basepath/monitoring?o=123&p=456', |
| 104 | + }); |
| 105 | + |
| 106 | + expect(span.setAttribute).toHaveBeenCalledWith(TRANSACTION_ATTR_SHOULD_DROP_TRANSACTION, true); |
| 107 | + }); |
| 108 | + }); |
| 109 | + |
| 110 | + describe('Middleware.execute spans', () => { |
| 111 | + it('marks middleware span for dropping when http.target matches tunnel path', () => { |
| 112 | + globalWithInjectedValues._sentryRewritesTunnelPath = '/monitoring'; |
| 113 | + const span = createMockSpan(); |
| 114 | + |
| 115 | + dropMiddlewareTunnelRequests(span as any, { |
| 116 | + 'next.span_type': 'Middleware.execute', |
| 117 | + 'http.target': '/monitoring?o=123&p=456', |
| 118 | + }); |
| 119 | + |
| 120 | + expect(span.setAttribute).toHaveBeenCalledWith(TRANSACTION_ATTR_SHOULD_DROP_TRANSACTION, true); |
| 121 | + }); |
| 122 | + }); |
| 123 | + |
| 124 | + describe('unrelated spans', () => { |
| 125 | + it('does not process spans without matching span type or origin', () => { |
| 126 | + globalWithInjectedValues._sentryRewritesTunnelPath = '/monitoring'; |
| 127 | + const span = createMockSpan(); |
| 128 | + |
| 129 | + dropMiddlewareTunnelRequests(span as any, { |
| 130 | + 'next.span_type': 'SomeOtherSpanType', |
| 131 | + 'http.target': '/monitoring', |
| 132 | + }); |
| 133 | + |
| 134 | + expect(span.setAttribute).not.toHaveBeenCalled(); |
| 135 | + }); |
| 136 | + }); |
| 137 | + |
| 138 | + describe('skipOpenTelemetrySetup', () => { |
| 139 | + it('does not process spans when skipOpenTelemetrySetup is true', async () => { |
| 140 | + const core = await import('@sentry/core'); |
| 141 | + const originalGetClient = core.getClient; |
| 142 | + vi.spyOn(core, 'getClient').mockReturnValueOnce({ |
| 143 | + getOptions: () => ({ skipOpenTelemetrySetup: true }), |
| 144 | + } as any); |
| 145 | + |
| 146 | + globalWithInjectedValues._sentryRewritesTunnelPath = '/monitoring'; |
| 147 | + const span = createMockSpan(); |
| 148 | + |
| 149 | + dropMiddlewareTunnelRequests(span as any, { |
| 150 | + 'next.span_type': 'BaseServer.handleRequest', |
| 151 | + 'http.target': '/monitoring', |
| 152 | + }); |
| 153 | + |
| 154 | + expect(span.setAttribute).not.toHaveBeenCalled(); |
| 155 | + |
| 156 | + vi.mocked(core.getClient).mockRestore(); |
| 157 | + }); |
| 158 | + }); |
| 159 | +}); |
0 commit comments