Skip to content

Commit e040002

Browse files
authored
feat(nestjs): Stop creating spans for TracingInterceptor (#16501)
1 parent 4f9c89e commit e040002

File tree

2 files changed

+12
-48
lines changed

2 files changed

+12
-48
lines changed

dev-packages/e2e-tests/test-applications/nestjs-fastify/tests/transactions.test.ts

Lines changed: 6 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -112,36 +112,6 @@ test('Sends an API route transaction', async ({ baseURL }) => {
112112
op: 'request_context.nestjs',
113113
origin: 'auto.http.otel.nestjs',
114114
},
115-
{
116-
span_id: expect.stringMatching(/[a-f0-9]{16}/),
117-
trace_id: expect.stringMatching(/[a-f0-9]{32}/),
118-
data: {
119-
'sentry.origin': 'auto.middleware.nestjs',
120-
'sentry.op': 'middleware.nestjs',
121-
},
122-
description: 'SentryTracingInterceptor',
123-
parent_span_id: expect.stringMatching(/[a-f0-9]{16}/),
124-
start_timestamp: expect.any(Number),
125-
timestamp: expect.any(Number),
126-
status: 'ok',
127-
op: 'middleware.nestjs',
128-
origin: 'auto.middleware.nestjs',
129-
},
130-
{
131-
span_id: expect.stringMatching(/[a-f0-9]{16}/),
132-
trace_id: expect.stringMatching(/[a-f0-9]{32}/),
133-
data: {
134-
'sentry.origin': 'auto.middleware.nestjs',
135-
'sentry.op': 'middleware.nestjs',
136-
},
137-
description: 'SentryTracingInterceptor',
138-
parent_span_id: expect.stringMatching(/[a-f0-9]{16}/),
139-
start_timestamp: expect.any(Number),
140-
timestamp: expect.any(Number),
141-
status: 'ok',
142-
op: 'middleware.nestjs',
143-
origin: 'auto.middleware.nestjs',
144-
},
145115
{
146116
span_id: expect.stringMatching(/[a-f0-9]{16}/),
147117
trace_id: expect.stringMatching(/[a-f0-9]{32}/),
@@ -183,29 +153,19 @@ test('Sends an API route transaction', async ({ baseURL }) => {
183153
status: 'ok',
184154
origin: 'manual',
185155
},
186-
{
187-
span_id: expect.stringMatching(/[a-f0-9]{16}/),
188-
trace_id: expect.stringMatching(/[a-f0-9]{32}/),
189-
data: {
190-
'sentry.origin': 'auto.middleware.nestjs',
191-
'sentry.op': 'middleware.nestjs',
192-
},
193-
description: 'Interceptors - After Route',
194-
parent_span_id: expect.stringMatching(/[a-f0-9]{16}/),
195-
start_timestamp: expect.any(Number),
196-
timestamp: expect.any(Number),
197-
status: 'ok',
198-
op: 'middleware.nestjs',
199-
origin: 'auto.middleware.nestjs',
200-
},
201156
]),
157+
start_timestamp: expect.any(Number),
158+
timestamp: expect.any(Number),
202159
transaction: 'GET /test-transaction',
203-
type: 'transaction',
204160
transaction_info: {
205161
source: 'route',
206162
},
163+
type: 'transaction',
207164
}),
208165
);
166+
167+
const spanDescriptions = transactionEvent.spans.map(span => span.description);
168+
expect(spanDescriptions).not.toContain('SentryTracingInterceptor');
209169
});
210170

211171
test('API route transaction includes nest middleware span. Spans created in and after middleware are nested correctly', async ({

packages/nestjs/src/integrations/sentry-nest-instrumentation.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,8 +183,12 @@ export class SentryNestInstrumentation extends InstrumentationBase {
183183
const parentSpan = getActiveSpan();
184184
let afterSpan: Span | undefined;
185185

186-
// Check that we can reasonably assume that the target is an interceptor.
187-
if (!context || !next || typeof next.handle !== 'function') {
186+
if (
187+
!context ||
188+
!next ||
189+
typeof next.handle !== 'function' || // Check that we can reasonably assume that the target is an interceptor.
190+
target.name === 'SentryTracingInterceptor' // We don't want to trace this internal interceptor
191+
) {
188192
return originalIntercept.apply(thisArgIntercept, argsIntercept);
189193
}
190194

0 commit comments

Comments
 (0)