Skip to content

Commit 3379f93

Browse files
author
Luca Forstner
authored
fix(nextjs): Trace with performance disabled (#9389)
1 parent 43ddbbe commit 3379f93

10 files changed

+47
-79
lines changed

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

Lines changed: 29 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { captureException, flush, getCurrentHub, hasTracingEnabled, startTransaction } from '@sentry/core';
1+
import { captureException, flush, getCurrentHub, startTransaction } from '@sentry/core';
22
import type { Span } from '@sentry/types';
33
import { addExceptionMechanism, logger, objectify, tracingContextFromHeaders } from '@sentry/utils';
44

@@ -18,40 +18,38 @@ export function withEdgeWrapping<H extends EdgeRouteHandler>(
1818

1919
let span: Span | undefined;
2020

21-
if (hasTracingEnabled()) {
22-
if (prevSpan) {
23-
span = prevSpan.startChild({
24-
description: options.spanDescription,
25-
op: options.spanOp,
26-
origin: 'auto.function.nextjs',
27-
});
28-
} else if (req instanceof Request) {
29-
const sentryTrace = req.headers.get('sentry-trace') || '';
30-
const baggage = req.headers.get('baggage');
31-
const { traceparentData, dynamicSamplingContext, propagationContext } = tracingContextFromHeaders(
32-
sentryTrace,
33-
baggage,
34-
);
35-
currentScope.setPropagationContext(propagationContext);
36-
if (traceparentData) {
37-
__DEBUG_BUILD__ && logger.log(`[Tracing] Continuing trace ${traceparentData.traceId}.`);
38-
}
39-
40-
span = startTransaction({
41-
name: options.spanDescription,
42-
op: options.spanOp,
43-
origin: 'auto.ui.nextjs.withEdgeWrapping',
44-
...traceparentData,
45-
metadata: {
46-
dynamicSamplingContext: traceparentData && !dynamicSamplingContext ? {} : dynamicSamplingContext,
47-
source: 'route',
48-
},
49-
});
21+
if (prevSpan) {
22+
span = prevSpan.startChild({
23+
description: options.spanDescription,
24+
op: options.spanOp,
25+
origin: 'auto.function.nextjs',
26+
});
27+
} else if (req instanceof Request) {
28+
const sentryTrace = req.headers.get('sentry-trace') || '';
29+
const baggage = req.headers.get('baggage');
30+
const { traceparentData, dynamicSamplingContext, propagationContext } = tracingContextFromHeaders(
31+
sentryTrace,
32+
baggage,
33+
);
34+
currentScope.setPropagationContext(propagationContext);
35+
if (traceparentData) {
36+
__DEBUG_BUILD__ && logger.log(`[Tracing] Continuing trace ${traceparentData.traceId}.`);
5037
}
5138

52-
currentScope?.setSpan(span);
39+
span = startTransaction({
40+
name: options.spanDescription,
41+
op: options.spanOp,
42+
origin: 'auto.ui.nextjs.withEdgeWrapping',
43+
...traceparentData,
44+
metadata: {
45+
dynamicSamplingContext: traceparentData && !dynamicSamplingContext ? {} : dynamicSamplingContext,
46+
source: 'route',
47+
},
48+
});
5349
}
5450

51+
currentScope?.setSpan(span);
52+
5553
try {
5654
const handlerResult: ReturnType<H> = await handler.apply(this, args);
5755

packages/nextjs/src/common/wrapApiHandlerWithSentry.ts

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,4 @@
1-
import {
2-
captureException,
3-
getCurrentHub,
4-
hasTracingEnabled,
5-
runWithAsyncContext,
6-
startTransaction,
7-
} from '@sentry/core';
1+
import { captureException, getCurrentHub, runWithAsyncContext, startTransaction } from '@sentry/core';
82
import type { Transaction } from '@sentry/types';
93
import {
104
addExceptionMechanism,
@@ -91,7 +85,7 @@ export function withSentry(apiHandler: NextApiHandler, parameterizedRoute?: stri
9185

9286
currentScope.setSDKProcessingMetadata({ request: req });
9387

94-
if (hasTracingEnabled(options) && options?.instrumenter === 'sentry') {
88+
if (options?.instrumenter === 'sentry') {
9589
const sentryTrace =
9690
req.headers && isString(req.headers['sentry-trace']) ? req.headers['sentry-trace'] : undefined;
9791
const baggage = req.headers?.baggage;

packages/nextjs/src/common/wrapAppGetInitialPropsWithSentry.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { getCurrentHub, hasTracingEnabled } from '@sentry/core';
1+
import { getCurrentHub } from '@sentry/core';
22
import { dynamicSamplingContextToSentryBaggageHeader } from '@sentry/utils';
33
import type App from 'next/app';
44

@@ -37,7 +37,7 @@ export function wrapAppGetInitialPropsWithSentry(origAppGetInitialProps: AppGetI
3737
// https://nextjs.org/docs/api-reference/data-fetching/get-initial-props#context-object
3838
// This does not seem to be the case in dev mode. Because we have no clean way of associating the the data fetcher
3939
// span with each other when there are no req or res objects, we simply do not trace them at all here.
40-
if (hasTracingEnabled() && req && res && options?.instrumenter === 'sentry') {
40+
if (req && res && options?.instrumenter === 'sentry') {
4141
const tracedGetInitialProps = withTracedServerSideDataFetcher(errorWrappedAppGetInitialProps, req, res, {
4242
dataFetcherRouteName: '/_app',
4343
requestedRouteName: context.ctx.pathname,

packages/nextjs/src/common/wrapDocumentGetInitialPropsWithSentry.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { getCurrentHub, hasTracingEnabled } from '@sentry/core';
1+
import { getCurrentHub } from '@sentry/core';
22
import type Document from 'next/document';
33

44
import { isBuild } from './utils/isBuild';
@@ -33,7 +33,7 @@ export function wrapDocumentGetInitialPropsWithSentry(
3333
// https://nextjs.org/docs/api-reference/data-fetching/get-initial-props#context-object
3434
// This does not seem to be the case in dev mode. Because we have no clean way of associating the the data fetcher
3535
// span with each other when there are no req or res objects, we simply do not trace them at all here.
36-
if (hasTracingEnabled() && req && res && options?.instrumenter === 'sentry') {
36+
if (req && res && options?.instrumenter === 'sentry') {
3737
const tracedGetInitialProps = withTracedServerSideDataFetcher(errorWrappedGetInitialProps, req, res, {
3838
dataFetcherRouteName: '/_document',
3939
requestedRouteName: context.pathname,

packages/nextjs/src/common/wrapErrorGetInitialPropsWithSentry.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { getCurrentHub, hasTracingEnabled } from '@sentry/core';
1+
import { getCurrentHub } from '@sentry/core';
22
import { dynamicSamplingContextToSentryBaggageHeader } from '@sentry/utils';
33
import type { NextPageContext } from 'next';
44
import type { ErrorProps } from 'next/error';
@@ -40,7 +40,7 @@ export function wrapErrorGetInitialPropsWithSentry(
4040
// https://nextjs.org/docs/api-reference/data-fetching/get-initial-props#context-object
4141
// This does not seem to be the case in dev mode. Because we have no clean way of associating the the data fetcher
4242
// span with each other when there are no req or res objects, we simply do not trace them at all here.
43-
if (hasTracingEnabled() && req && res && options?.instrumenter === 'sentry') {
43+
if (req && res && options?.instrumenter === 'sentry') {
4444
const tracedGetInitialProps = withTracedServerSideDataFetcher(errorWrappedGetInitialProps, req, res, {
4545
dataFetcherRouteName: '/_error',
4646
requestedRouteName: context.pathname,

packages/nextjs/src/common/wrapGetInitialPropsWithSentry.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { getCurrentHub, hasTracingEnabled } from '@sentry/core';
1+
import { getCurrentHub } from '@sentry/core';
22
import { dynamicSamplingContextToSentryBaggageHeader } from '@sentry/utils';
33
import type { NextPage } from 'next';
44

@@ -36,7 +36,7 @@ export function wrapGetInitialPropsWithSentry(origGetInitialProps: GetInitialPro
3636
// https://nextjs.org/docs/api-reference/data-fetching/get-initial-props#context-object
3737
// This does not seem to be the case in dev mode. Because we have no clean way of associating the the data fetcher
3838
// span with each other when there are no req or res objects, we simply do not trace them at all here.
39-
if (hasTracingEnabled() && req && res && options?.instrumenter === 'sentry') {
39+
if (req && res && options?.instrumenter === 'sentry') {
4040
const tracedGetInitialProps = withTracedServerSideDataFetcher(errorWrappedGetInitialProps, req, res, {
4141
dataFetcherRouteName: context.pathname,
4242
requestedRouteName: context.pathname,

packages/nextjs/src/common/wrapGetServerSidePropsWithSentry.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { getCurrentHub, hasTracingEnabled } from '@sentry/core';
1+
import { getCurrentHub } from '@sentry/core';
22
import { dynamicSamplingContextToSentryBaggageHeader } from '@sentry/utils';
33
import type { GetServerSideProps } from 'next';
44

@@ -33,7 +33,7 @@ export function wrapGetServerSidePropsWithSentry(
3333
const hub = getCurrentHub();
3434
const options = hub.getClient()?.getOptions();
3535

36-
if (hasTracingEnabled() && options?.instrumenter === 'sentry') {
36+
if (options?.instrumenter === 'sentry') {
3737
const tracedGetServerSideProps = withTracedServerSideDataFetcher(errorWrappedGetServerSideProps, req, res, {
3838
dataFetcherRouteName: parameterizedRoute,
3939
requestedRouteName: parameterizedRoute,

packages/nextjs/src/common/wrapGetStaticPropsWithSentry.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { getCurrentHub, hasTracingEnabled } from '@sentry/core';
1+
import { getCurrentHub } from '@sentry/core';
22
import type { GetStaticProps } from 'next';
33

44
import { isBuild } from './utils/isBuild';
@@ -26,7 +26,7 @@ export function wrapGetStaticPropsWithSentry(
2626
const errorWrappedGetStaticProps = withErrorInstrumentation(wrappingTarget);
2727
const options = getCurrentHub().getClient()?.getOptions();
2828

29-
if (hasTracingEnabled() && options?.instrumenter === 'sentry') {
29+
if (options?.instrumenter === 'sentry') {
3030
return callDataFetcherTraced(errorWrappedGetStaticProps, args, {
3131
parameterizedRoute,
3232
dataFetchingMethodName: 'getStaticProps',

packages/nextjs/src/server/index.ts

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { hasTracingEnabled } from '@sentry/core';
21
import { RewriteFrames } from '@sentry/integrations';
32
import type { NodeOptions } from '@sentry/node';
43
import { configureScope, getCurrentHub, init as nodeInit, Integrations } from '@sentry/node';
@@ -142,12 +141,10 @@ function addServerIntegrations(options: NodeOptions): void {
142141
_options: { exitEvenIfOtherHandlersAreRegistered: false },
143142
});
144143

145-
if (hasTracingEnabled(options)) {
146-
const defaultHttpTracingIntegration = new Integrations.Http({ tracing: true });
147-
integrations = addOrUpdateIntegration(defaultHttpTracingIntegration, integrations, {
148-
_tracing: {},
149-
});
150-
}
144+
const defaultHttpTracingIntegration = new Integrations.Http({ tracing: true });
145+
integrations = addOrUpdateIntegration(defaultHttpTracingIntegration, integrations, {
146+
_tracing: {},
147+
});
151148

152149
options.integrations = integrations;
153150
}

packages/nextjs/test/serverSdk.test.ts

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -186,15 +186,6 @@ describe('Server init()', () => {
186186
expect(httpIntegration).toEqual(expect.objectContaining({ _tracing: {} }));
187187
});
188188

189-
it('does not add `Http` integration if tracing not enabled in SDK', () => {
190-
init({});
191-
192-
const nodeInitOptions = nodeInit.mock.calls[0][0] as ModifiedInitOptions;
193-
const httpIntegration = findIntegrationByName(nodeInitOptions.integrations, 'Http');
194-
195-
expect(httpIntegration).toBeUndefined();
196-
});
197-
198189
it('forces `_tracing = true` if `tracesSampleRate` is set', () => {
199190
init({
200191
tracesSampleRate: 1.0,
@@ -220,18 +211,6 @@ describe('Server init()', () => {
220211
expect(httpIntegration).toBeDefined();
221212
expect(httpIntegration).toEqual(expect.objectContaining({ _tracing: {} }));
222213
});
223-
224-
it('does not force `_tracing = true` if tracing not enabled in SDK', () => {
225-
init({
226-
integrations: [new Integrations.Http({ tracing: false })],
227-
});
228-
229-
const nodeInitOptions = nodeInit.mock.calls[0][0] as ModifiedInitOptions;
230-
const httpIntegration = findIntegrationByName(nodeInitOptions.integrations, 'Http');
231-
232-
expect(httpIntegration).toBeDefined();
233-
expect(httpIntegration).toEqual(expect.objectContaining({ _tracing: undefined }));
234-
});
235214
});
236215
});
237216
});

0 commit comments

Comments
 (0)