Skip to content

Commit 24c0287

Browse files
committed
improve span code & test??
1 parent 1425072 commit 24c0287

File tree

2 files changed

+30
-31
lines changed

2 files changed

+30
-31
lines changed

packages/nextjs/src/common/utils/wrapperUtils.ts

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ export function getSpanFromRequest(req: IncomingMessage): Span | undefined {
3535
return req._sentrySpan;
3636
}
3737

38-
function setSpanOnRequest(transaction: Span, req: IncomingMessage): void {
39-
req._sentrySpan = transaction;
38+
function setSpanOnRequest(span: Span, req: IncomingMessage): void {
39+
req._sentrySpan = span;
4040
}
4141

4242
/**
@@ -99,25 +99,9 @@ export function withTracedServerSideDataFetcher<F extends (...args: any[]) => Pr
9999
const baggage = req.headers?.baggage;
100100

101101
return continueTrace({ sentryTrace, baggage }, () => {
102-
let requestSpan: Span | undefined = getSpanFromRequest(req);
103-
if (!requestSpan) {
104-
// TODO(v8): Simplify these checks when startInactiveSpan always returns a span
105-
requestSpan = startInactiveSpan({
106-
name: options.requestedRouteName,
107-
op: 'http.server',
108-
attributes: {
109-
[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.function.nextjs',
110-
[SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]: 'route',
111-
},
112-
});
113-
if (requestSpan) {
114-
requestSpan.setStatus({ code: SPAN_STATUS_OK });
115-
setSpanOnRequest(requestSpan, req);
116-
autoEndSpanOnResponseEnd(requestSpan, res);
117-
}
118-
}
102+
const requestSpan = getOrStartRequestSpan(req, res, options.requestedRouteName);
119103

120-
const withActiveSpanCallback = (): Promise<ReturnType<F>> => {
104+
return withActiveSpan(requestSpan, () => {
121105
return startSpanManual(
122106
{
123107
op: 'function.nextjs',
@@ -143,18 +127,34 @@ export function withTracedServerSideDataFetcher<F extends (...args: any[]) => Pr
143127
}
144128
},
145129
);
146-
};
147-
148-
if (requestSpan) {
149-
return withActiveSpan(requestSpan, withActiveSpanCallback);
150-
} else {
151-
return withActiveSpanCallback();
152-
}
130+
});
153131
});
154132
});
155133
};
156134
}
157135

136+
function getOrStartRequestSpan(req: IncomingMessage, res: ServerResponse, name: string): Span {
137+
const existingSpan = getSpanFromRequest(req);
138+
if (existingSpan) {
139+
return existingSpan;
140+
}
141+
142+
const requestSpan = startInactiveSpan({
143+
name,
144+
op: 'http.server',
145+
attributes: {
146+
[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.function.nextjs',
147+
[SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]: 'route',
148+
},
149+
});
150+
151+
requestSpan.setStatus({ code: SPAN_STATUS_OK });
152+
setSpanOnRequest(requestSpan, req);
153+
autoEndSpanOnResponseEnd(requestSpan, res);
154+
155+
return requestSpan;
156+
}
157+
158158
/**
159159
* Call a data fetcher and trace it. Only traces the function if there is an active transaction on the scope.
160160
*

packages/nextjs/test/integration/test/client/tracingClientGetInitialProps.test.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { expect, test } from '@playwright/test';
2-
import { Transaction } from '@sentry/types';
2+
import { TransactionEvent } from '@sentry/types';
33
import { countEnvelopes, getMultipleSentryEnvelopeRequests } from './utils/helpers';
44

55
test('should instrument `getInitialProps` for performance tracing', async ({ page }) => {
6-
const transaction = await getMultipleSentryEnvelopeRequests<Transaction>(page, 1, {
6+
const transaction = await getMultipleSentryEnvelopeRequests<TransactionEvent>(page, 1, {
77
url: '/42/withInitialProps',
88
envelopeType: 'transaction',
99
});
@@ -19,8 +19,7 @@ test('should instrument `getInitialProps` for performance tracing', async ({ pag
1919
const nextDataTag = await page.waitForSelector('#__NEXT_DATA__', { state: 'attached' });
2020
const nextDataTagValue = JSON.parse(await nextDataTag.evaluate(tag => (tag as HTMLElement).innerText));
2121

22-
// @ts-expect-error - We know `contexts` is defined in the Transaction envelope
23-
const traceId = transaction[0].contexts.trace.trace_id;
22+
const traceId = transaction[0].contexts?.trace?.trace_id;
2423

2524
expect(traceId).toBeDefined();
2625

0 commit comments

Comments
 (0)