Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Controller, Get, Param, ParseIntPipe, UseFilters, UseGuards, UseInterceptors } from '@nestjs/common';
import { flush } from '@sentry/nestjs';
import { flush, getActiveSpan, getIsolationScope, getRootSpan } from '@sentry/nestjs';
import { AppService } from './app.service';
import { AsyncInterceptor } from './async-example.interceptor';
import { ScheduleService } from './schedule.service';
Expand Down Expand Up @@ -103,6 +103,17 @@ export class AppController {

@Get('test-schedule-isolation')
testScheduleIsolation() {
// Streamed spans carry no breadcrumbs, so the test reads from this attribute whether a
// breadcrumb added by a scheduled task leaked into this request's isolation scope
const activeSpan = getActiveSpan();
if (activeSpan) {
const breadcrumbs = getIsolationScope().getScopeData().breadcrumbs;
getRootSpan(activeSpan).setAttribute(
'isolation_scope.has_schedule_breadcrumb',
breadcrumbs.some(breadcrumb => breadcrumb.message === 'leaked-breadcrumb-from-schedule'),
);
}

return { message: 'ok' };
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import * as Sentry from '@sentry/nestjs';

Sentry.init({
traceLifecycle: 'static',
environment: 'qa', // dynamic sampling bias to keep transactions
dsn: process.env.E2E_TEST_DSN,
tunnel: `http://localhost:3031/`, // proxy server
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
import { expect, test } from '@playwright/test';
import { waitForError, waitForTransaction } from '@sentry-internal/test-utils';
import { waitForError, waitForStreamedSpan } from '@sentry-internal/test-utils';

const APP_NAME = 'nestjs-basic';

/**
* Resolves once the request's segment span has been streamed, which is how these specs know the
* request finished and any error it would have produced had its chance to be sent.
*/
function waitForSegmentSpan(name: string): Promise<unknown> {
return waitForStreamedSpan(APP_NAME, span => span.is_segment && span.name === name);
}

test('Sends exception to Sentry', async ({ baseURL }) => {
const errorEventPromise = waitForError('nestjs-basic', event => {
const errorEventPromise = waitForError(APP_NAME, event => {
return !event.type && event.exception?.values?.[0]?.value === 'This is an exception with id 123';
});

Expand Down Expand Up @@ -35,7 +45,7 @@ test('Sends exception to Sentry', async ({ baseURL }) => {
});

test('Sends AxiosError to Sentry', async ({ baseURL }) => {
const errorEventPromise = waitForError('nestjs-basic', event => {
const errorEventPromise = waitForError(APP_NAME, event => {
return !event.type && event.exception?.values?.[0]?.value === 'This is an axios error with id 123';
});

Expand All @@ -55,38 +65,33 @@ test('Sends AxiosError to Sentry', async ({ baseURL }) => {
test('Does not send HttpExceptions to Sentry', async ({ baseURL }) => {
let errorEventOccurred = false;

waitForError('nestjs-basic', event => {
waitForError(APP_NAME, event => {
if (!event.type && event.exception?.values?.[0]?.value === 'This is an expected 400 exception with id 123') {
errorEventOccurred = true;
}

return event?.transaction === 'GET /test-expected-400-exception/:id';
});

waitForError('nestjs-basic', event => {
waitForError(APP_NAME, event => {
if (!event.type && event.exception?.values?.[0]?.value === 'This is an expected 500 exception with id 123') {
errorEventOccurred = true;
}

return event?.transaction === 'GET /test-expected-500-exception/:id';
});

const transactionEventPromise400 = waitForTransaction('nestjs-basic', transactionEvent => {
return transactionEvent?.transaction === 'GET /test-expected-400-exception/:id';
});

const transactionEventPromise500 = waitForTransaction('nestjs-basic', transactionEvent => {
return transactionEvent?.transaction === 'GET /test-expected-500-exception/:id';
});
const segmentSpanPromise400 = waitForSegmentSpan('GET /test-expected-400-exception/:id');
const segmentSpanPromise500 = waitForSegmentSpan('GET /test-expected-500-exception/:id');

const response400 = await fetch(`${baseURL}/test-expected-400-exception/123`);
expect(response400.status).toBe(400);

const response500 = await fetch(`${baseURL}/test-expected-500-exception/123`);
expect(response500.status).toBe(500);

await transactionEventPromise400;
await transactionEventPromise500;
await segmentSpanPromise400;
await segmentSpanPromise500;

(await fetch(`${baseURL}/flush`)).text();

Expand All @@ -96,22 +101,20 @@ test('Does not send HttpExceptions to Sentry', async ({ baseURL }) => {
test('Does not send RpcExceptions to Sentry', async ({ baseURL }) => {
let errorEventOccurred = false;

waitForError('nestjs-basic', event => {
waitForError(APP_NAME, event => {
if (!event.type && event.exception?.values?.[0]?.value === 'This is an expected RPC exception with id 123') {
errorEventOccurred = true;
}

return event?.transaction === 'GET /test-expected-rpc-exception/:id';
});

const transactionEventPromise = waitForTransaction('nestjs-basic', transactionEvent => {
return transactionEvent?.transaction === 'GET /test-expected-rpc-exception/:id';
});
const segmentSpanPromise = waitForSegmentSpan('GET /test-expected-rpc-exception/:id');

const response = await fetch(`${baseURL}/test-expected-rpc-exception/123`);
expect(response.status).toBe(500);

await transactionEventPromise;
await segmentSpanPromise;

(await fetch(`${baseURL}/flush`)).text();

Expand All @@ -123,17 +126,15 @@ test('Global exception filter registered in main module is applied and exception
}) => {
let errorEventOccurred = false;

waitForError('nestjs-basic', event => {
waitForError(APP_NAME, event => {
if (!event.type && event.exception?.values?.[0]?.value === 'Example exception was handled by global filter!') {
errorEventOccurred = true;
}

return event?.transaction === 'GET /example-exception-global-filter';
});

const transactionEventPromise = waitForTransaction('nestjs-basic', transactionEvent => {
return transactionEvent?.transaction === 'GET /example-exception-global-filter';
});
const segmentSpanPromise = waitForSegmentSpan('GET /example-exception-global-filter');

const response = await fetch(`${baseURL}/example-exception-global-filter`);
const responseBody = await response.json();
Expand All @@ -146,7 +147,7 @@ test('Global exception filter registered in main module is applied and exception
message: 'Example exception was handled by global filter!',
});

await transactionEventPromise;
await segmentSpanPromise;

(await fetch(`${baseURL}/flush`)).text();

Expand All @@ -158,17 +159,15 @@ test('Local exception filter registered in main module is applied and exception
}) => {
let errorEventOccurred = false;

waitForError('nestjs-basic', event => {
waitForError(APP_NAME, event => {
if (!event.type && event.exception?.values?.[0]?.value === 'Example exception was handled by local filter!') {
errorEventOccurred = true;
}

return event?.transaction === 'GET /example-exception-local-filter';
});

const transactionEventPromise = waitForTransaction('nestjs-basic', transactionEvent => {
return transactionEvent?.transaction === 'GET /example-exception-local-filter';
});
const segmentSpanPromise = waitForSegmentSpan('GET /example-exception-local-filter');

const response = await fetch(`${baseURL}/example-exception-local-filter`);
const responseBody = await response.json();
Expand All @@ -181,7 +180,7 @@ test('Local exception filter registered in main module is applied and exception
message: 'Example exception was handled by local filter!',
});

await transactionEventPromise;
await segmentSpanPromise;

(await fetch(`${baseURL}/flush`)).text();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { expect, test } from '@playwright/test';
import { waitForError, waitForTransaction } from '@sentry-internal/test-utils';
import { waitForError, waitForStreamedSpan } from '@sentry-internal/test-utils';

test('Sends exceptions to Sentry on error in @Cron decorated method', async ({ baseURL }) => {
const errorEventPromise = waitForError('nestjs-basic', event => {
Expand Down Expand Up @@ -75,18 +75,19 @@ test('Scheduled task breadcrumbs do not leak into subsequent HTTP requests', asy
// Wait for at least one interval tick to fire
await new Promise(resolve => setTimeout(resolve, 3000));

const transactionPromise = waitForTransaction('nestjs-basic', transactionEvent => {
return transactionEvent.transaction === 'GET /test-schedule-isolation';
const segmentSpanPromise = waitForStreamedSpan('nestjs-basic', span => {
return span.is_segment && span.name === 'GET /test-schedule-isolation';
});

await fetch(`${baseURL}/test-schedule-isolation`);

const transaction = await transactionPromise;
const segmentSpan = await segmentSpanPromise;

const leakedBreadcrumb = (transaction.breadcrumbs || []).find(
(b: any) => b.message === 'leaked-breadcrumb-from-schedule',
);
expect(leakedBreadcrumb).toBeUndefined();
// Streamed spans carry no breadcrumbs, so the route reports the leak as a span attribute
expect(segmentSpan.attributes['isolation_scope.has_schedule_breadcrumb']).toEqual({
value: false,
type: 'boolean',
});

// kill interval so tests don't get stuck
await fetch(`${baseURL}/kill-test-schedule-interval/test-schedule-isolation`);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,73 +1,51 @@
import { expect, test } from '@playwright/test';
import { waitForTransaction } from '@sentry-internal/test-utils';
import { collectStreamedSpansUntilSegment } from '@sentry-internal/test-utils';

test('Transaction includes span and correct value for decorated async function', async ({ baseURL }) => {
const transactionEventPromise = waitForTransaction('nestjs-basic', transactionEvent => {
return (
transactionEvent?.contexts?.trace?.op === 'http.server' &&
transactionEvent?.transaction === 'GET /test-span-decorator-async'
);
});
const APP_NAME = 'nestjs-basic';

test('Trace includes span and correct value for decorated async function', async ({ baseURL }) => {
const spansPromise = collectStreamedSpansUntilSegment(APP_NAME, 'GET /test-span-decorator-async');

const response = await fetch(`${baseURL}/test-span-decorator-async`);
const body = await response.json();

expect(body.result).toEqual('test');

const transactionEvent = await transactionEventPromise;
const spans = await spansPromise;

expect(transactionEvent.spans).toEqual(
expect.arrayContaining([
expect.objectContaining({
span_id: expect.stringMatching(/[a-f0-9]{16}/),
trace_id: expect.stringMatching(/[a-f0-9]{32}/),
data: {
'sentry.origin': 'auto.function.nestjs.sentry_traced',
'sentry.op': 'wait and return a string',
},
description: 'wait',
parent_span_id: expect.stringMatching(/[a-f0-9]{16}/),
start_timestamp: expect.any(Number),
status: 'ok',
op: 'wait and return a string',
origin: 'auto.function.nestjs.sentry_traced',
expect(spans).toContainEqual(
expect.objectContaining({
name: 'wait',
is_segment: false,
status: 'ok',
attributes: expect.objectContaining({
'sentry.origin': { type: 'string', value: 'auto.function.nestjs.sentry_traced' },
'sentry.op': { type: 'string', value: 'wait and return a string' },
}),
]),
}),
);
});

test('Transaction includes span and correct value for decorated sync function', async ({ baseURL }) => {
const transactionEventPromise = waitForTransaction('nestjs-basic', transactionEvent => {
return (
transactionEvent?.contexts?.trace?.op === 'http.server' &&
transactionEvent?.transaction === 'GET /test-span-decorator-sync'
);
});
test('Trace includes span and correct value for decorated sync function', async ({ baseURL }) => {
const spansPromise = collectStreamedSpansUntilSegment(APP_NAME, 'GET /test-span-decorator-sync');

const response = await fetch(`${baseURL}/test-span-decorator-sync`);
const body = await response.json();

expect(body.result).toEqual('test');

const transactionEvent = await transactionEventPromise;
const spans = await spansPromise;

expect(transactionEvent.spans).toEqual(
expect.arrayContaining([
expect.objectContaining({
span_id: expect.stringMatching(/[a-f0-9]{16}/),
trace_id: expect.stringMatching(/[a-f0-9]{32}/),
data: {
'sentry.origin': 'auto.function.nestjs.sentry_traced',
'sentry.op': 'return a string',
},
description: 'getString',
parent_span_id: expect.stringMatching(/[a-f0-9]{16}/),
start_timestamp: expect.any(Number),
status: 'ok',
op: 'return a string',
origin: 'auto.function.nestjs.sentry_traced',
expect(spans).toContainEqual(
expect.objectContaining({
name: 'getString',
is_segment: false,
status: 'ok',
attributes: expect.objectContaining({
'sentry.origin': { type: 'string', value: 'auto.function.nestjs.sentry_traced' },
'sentry.op': { type: 'string', value: 'return a string' },
}),
]),
}),
);
});

Expand Down
Loading
Loading