Skip to content

Commit 0a6c1e5

Browse files
authored
fix(node): Fix virtual parent span ID handling & update create-next-app E2E test (#12368)
This started out as updating the create-next-app test to not send to sentry anymore, but instead check payloads. However, while doing this, I noticed some inconsistencies, mostly that there was a weird `parent_span_id` in api route transactions/errors where there should be none. **EDIT** OK, another iteration on this! Turns out setting an invalid spanID on a span will make OTEL ignore all of this, including the trace ID, and instead will create a new trace ID for a new span, which is not what we want. So we can't use this... So instead, I now adjusted the already existing code to keep the incoming parentSpanId on the trace state. The major change I did is ensure we set this even if it is empty (it is set to an empty string then). This way, we can identify if this has been set, and if it has, use this as source of truth. And we can fall back to use the regular parentSpanId if this is not set (for whatever reason). **ORIGINAL** So I set out to figure out what was happening there, and the problem was that when continuing a virtual trace, we would construct a parent spanContext like this: ```js const spanContext: SpanContext = { traceId: propagationContext.traceId, spanId: propagationContext.parentSpanId || propagationContext.spanId, isRemote: true, traceFlags: propagationContext.sampled ? TraceFlags.SAMPLED : TraceFlags.NONE, traceState, }; ``` The problematic line is this: `spanId: propagationContext.parentSpanId || propagationContext.spanId,`. Since `spanId` is required on the SpanContext, we had to set it to something, but `propagationContext.parentSpanId` is by design often undefined. With this behavior, we always set this to the random span ID we have on the propagationContext, and picked this up downstream. this now became: ```js const spanContext: SpanContext = { traceId: propagationContext.traceId, spanId: propagationContext.parentSpanId || INVALID_SPANID, isRemote: true, traceFlags: propagationContext.sampled ? TraceFlags.SAMPLED : TraceFlags.NONE, traceState, }; ``` Plus a check further down: ```js const traceState = makeTraceState({ dsc, parentSpanId: spanId !== INVALID_SPANID ? spanId : undefined, sampled, }); ``` (Note, `INVALID_SPANID` is a constant exported from OTEL, which is basically `0000....`). I'll investigate in a follow up if it would make sense to always use this for the propagation context, instead of a random one today, plus ensuring that we always filter this out before we send, or something like this 🤔 Part of #11910
1 parent a062912 commit 0a6c1e5

31 files changed

+320
-386
lines changed

dev-packages/e2e-tests/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
"test:prepare": "ts-node prepare.ts",
1515
"test:validate": "run-s test:validate-configuration test:validate-test-app-setups",
1616
"clean": "rimraf tmp node_modules pnpm-lock.yaml && yarn clean:test-applications",
17-
"clean:test-applications": "rimraf test-applications/**/{node_modules,dist,build,.next,.sveltekit,pnpm-lock.yaml} .last-run.json"
17+
"clean:test-applications": "rimraf test-applications/**/{node_modules,dist,build,.next,.sveltekit,pnpm-lock.yaml} .last-run.json && pnpm store prune"
1818
},
1919
"devDependencies": {
2020
"@types/glob": "8.0.0",
Original file line numberDiff line numberDiff line change
@@ -1,4 +0,0 @@
1-
interface Window {
2-
recordedTransactions?: string[];
3-
capturedExceptionId?: string;
4-
}
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,13 @@
11
import * as Sentry from '@sentry/nextjs';
22

3-
declare global {
4-
namespace globalThis {
5-
var transactionIds: string[];
6-
}
7-
}
8-
93
export function register() {
104
if (process.env.NEXT_RUNTIME === 'nodejs') {
115
Sentry.init({
126
environment: 'qa', // dynamic sampling bias to keep transactions
137
dsn: process.env.NEXT_PUBLIC_E2E_TEST_DSN,
148
// Adjust this value in production, or use tracesSampler for greater control
159
tracesSampleRate: 1.0,
16-
integrations: [Sentry.localVariablesIntegration()],
17-
});
18-
19-
Sentry.addEventProcessor(event => {
20-
global.transactionIds = global.transactionIds || [];
21-
22-
if (event.type === 'transaction') {
23-
const eventId = event.event_id;
24-
25-
if (eventId) {
26-
global.transactionIds.push(eventId);
27-
}
28-
}
29-
30-
return event;
10+
tunnel: 'http://localhost:3031',
3111
});
3212
}
3313
}

dev-packages/e2e-tests/test-applications/create-next-app/package.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"private": true,
55
"scripts": {
66
"build": "next build",
7-
"clean": "npx rimraf node_modules pnpm-lock.yaml",
7+
"clean": "npx rimraf node_modules pnpm-lock.yaml .next",
88
"test:prod": "TEST_ENV=prod playwright test",
99
"test:dev": "TEST_ENV=dev playwright test",
1010
"test:build": "pnpm install && npx playwright install && pnpm build",
@@ -23,7 +23,8 @@
2323
"typescript": "4.9.5"
2424
},
2525
"devDependencies": {
26-
"@playwright/test": "^1.44.1"
26+
"@playwright/test": "^1.44.1",
27+
"@sentry-internal/test-utils": "link:../../../test-utils"
2728
},
2829
"volta": {
2930
"extends": "../../package.json"
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,6 @@
1-
import * as Sentry from '@sentry/nextjs';
21
// Next.js API route support: https://nextjs.org/docs/api-routes/introduction
32
import type { NextApiRequest, NextApiResponse } from 'next';
43

54
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
6-
const exceptionId = Sentry.captureException(new Error('This is an error'));
7-
8-
await Sentry.flush(2000);
9-
10-
res.status(200).json({ exceptionId });
5+
throw new Error('I am a server error!');
116
}

dev-packages/e2e-tests/test-applications/create-next-app/pages/api/success.ts

+2-4
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,9 @@ import * as Sentry from '@sentry/nextjs';
33
import type { NextApiRequest, NextApiResponse } from 'next';
44

55
export default function handler(req: NextApiRequest, res: NextApiResponse) {
6-
Sentry.startSpan({ name: 'test-span' }, span => undefined);
6+
Sentry.startSpan({ name: 'test-span' }, () => undefined);
77

88
Sentry.flush().then(() => {
9-
res.status(200).json({
10-
transactionIds: global.transactionIds,
11-
});
9+
res.status(200).json({});
1210
});
1311
}

dev-packages/e2e-tests/test-applications/create-next-app/pages/index.tsx

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import * as Sentry from '@sentry/nextjs';
21
import Head from 'next/head';
32
import Link from 'next/link';
43

@@ -17,8 +16,7 @@ export default function Home() {
1716
value="Capture Exception"
1817
id="exception-button"
1918
onClick={() => {
20-
const eventId = Sentry.captureException(new Error('I am an error!'));
21-
window.capturedExceptionId = eventId;
19+
throw new Error('I am an error!');
2220
}}
2321
/>
2422
<Link href="/user/5" id="navigation">
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { getPlaywrightConfig } from '@sentry-internal/test-utils';
2+
3+
const testEnv = process.env.TEST_ENV;
4+
5+
if (!testEnv) {
6+
throw new Error('No test env defined');
7+
}
8+
9+
const config = getPlaywrightConfig({
10+
startCommand: testEnv === 'development' ? `pnpm next dev -p 3030` : `pnpm next start -p 3030`,
11+
});
12+
13+
export default config;

dev-packages/e2e-tests/test-applications/create-next-app/playwright.config.ts

-75
This file was deleted.

dev-packages/e2e-tests/test-applications/create-next-app/sentry.client.config.ts

+1-21
Original file line numberDiff line numberDiff line change
@@ -7,26 +7,6 @@ import * as Sentry from '@sentry/nextjs';
77
Sentry.init({
88
environment: 'qa', // dynamic sampling bias to keep transactions
99
dsn: process.env.NEXT_PUBLIC_E2E_TEST_DSN,
10-
// Adjust this value in production, or use tracesSampler for greater control
1110
tracesSampleRate: 1.0,
12-
13-
// ...
14-
// Note: if you want to override the automatic release value, do not set a
15-
// `release` value here - use the environment variable `SENTRY_RELEASE`, so
16-
// that it will also get attached to your source maps
17-
});
18-
19-
Sentry.addEventProcessor(event => {
20-
if (
21-
event.type === 'transaction' &&
22-
(event.contexts?.trace?.op === 'pageload' || event.contexts?.trace?.op === 'navigation')
23-
) {
24-
const eventId = event.event_id;
25-
if (eventId) {
26-
window.recordedTransactions = window.recordedTransactions || [];
27-
window.recordedTransactions.push(eventId);
28-
}
29-
}
30-
31-
return event;
11+
tunnel: 'http://localhost:3031',
3212
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { startEventProxyServer } from '@sentry-internal/test-utils';
2+
3+
startEventProxyServer({
4+
port: 3031,
5+
proxyServerName: 'create-next-app',
6+
});

dev-packages/e2e-tests/test-applications/create-next-app/tests/behaviour-client.test.ts

-140
This file was deleted.

0 commit comments

Comments
 (0)