Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ref(nextjs): Make individual features opt into OTEL tracing #13910

Merged
Merged
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
10 changes: 4 additions & 6 deletions dev-packages/e2e-tests/publish-packages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,19 @@ const packageTarballPaths = glob.sync('packages/*/sentry-*.tgz', {

// Publish built packages to the fake registry
packageTarballPaths.forEach(tarballPath => {
// eslint-disable-next-line no-console
console.log(`Publishing tarball ${tarballPath} ...`);
// `--userconfig` flag needs to be before `publish`
childProcess.exec(
`npm --userconfig ${__dirname}/test-registry.npmrc publish ${tarballPath}`,
{
cwd: repositoryRoot, // Can't use __dirname here because npm would try to publish `@sentry-internal/e2e-tests`
encoding: 'utf8',
},
(err, stdout, stderr) => {
// eslint-disable-next-line no-console
console.log(stdout);
// eslint-disable-next-line no-console
console.log(stderr);
err => {
if (err) {
// eslint-disable-next-line no-console
console.error(err);
console.error(`Error publishing tarball ${tarballPath}`, err);
process.exit(1);
}
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { expect, test } from '@playwright/test';
import { waitForTransaction } from '@sentry-internal/test-utils';

test('should not automatically create transactions for routes that were excluded from auto wrapping (string)', async ({
// TODO(lforst): This cannot make it into production - Make sure to fix this test
// The problem is that if we are not applying build time instrumentation Next.js will still emit spans (which is fine, but we need to find a different way of testing that build time instrumentation is successfully disabled - maybe with an attribute or something if build-time instrumentation is applied)
test.skip('should not automatically create transactions for routes that were excluded from auto wrapping (string)', async ({
request,
}) => {
const transactionPromise = waitForTransaction('nextjs-13', async transactionEvent => {
Expand All @@ -23,7 +25,9 @@ test('should not automatically create transactions for routes that were excluded
expect(transactionPromiseReceived).toBe(false);
});

test('should not automatically create transactions for routes that were excluded from auto wrapping (regex)', async ({
// TODO(lforst): This cannot make it into production - Make sure to fix this test
// The problem is that if we are not applying build time instrumentation Next.js will still emit spans (which is fine, but we need to find a different way of testing that build time instrumentation is successfully disabled - maybe with an attribute or something if build-time instrumentation is applied)
test.skip('should not automatically create transactions for routes that were excluded from auto wrapping (regex)', async ({
request,
}) => {
const transactionPromise = waitForTransaction('nextjs-13', async transactionEvent => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ export default function Layout({ children }: { children: React.ReactNode }) {
<Link href="/server-component/parameter/42">/server-component/parameter/42</Link>
</li>
<li>
<Link href="/server-component/parameter/foo/bar/baz">/server-component/parameter/foo/bar/baz</Link>
<Link href="/server-component/parameter/foo/bar/baz" prefetch={false}>
/server-component/parameter/foo/bar/baz
</Link>
</li>
<li>
<Link href="/not-found">/not-found</Link>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,17 @@ import {
SEMANTIC_ATTRIBUTE_SENTRY_SOURCE,
captureException,
continueTrace,
getActiveSpan,
getRootSpan,
setHttpStatus,
startSpanManual,
withIsolationScope,
} from '@sentry/core';
import { isString, logger, objectify, vercelWaitUntil } from '@sentry/utils';
import { isString, logger, objectify } from '@sentry/utils';

import { vercelWaitUntil } from '@sentry/utils';
import type { NextApiRequest } from 'next';
import { TRANSACTION_ATTR_SHOULD_DROP_TRANSACTION } from '../span-attributes-with-logic-attached';
import type { AugmentedNextApiResponse, NextApiHandler } from '../types';
import { flushSafelyWithTimeout } from '../utils/responseEnd';
import { escapeNextjsTracing } from '../utils/tracingUtils';
import { dropNextjsRootContext, escapeNextjsTracing } from '../utils/tracingUtils';

export type AugmentedNextApiRequest = NextApiRequest & {
__withSentry_applied__?: boolean;
Expand All @@ -29,21 +28,13 @@ export type AugmentedNextApiRequest = NextApiRequest & {
* @returns The wrapped handler which will always return a Promise.
*/
export function wrapApiHandlerWithSentry(apiHandler: NextApiHandler, parameterizedRoute: string): NextApiHandler {
// Since the API route handler spans emitted by Next.js are super buggy with completely wrong timestamps
// (fix pending at the time of writing this: https://github.com/vercel/next.js/pull/70908) we want to intentionally
// drop them. In the future, when Next.js' OTEL instrumentation is in a high-quality place we can potentially think
// about keeping them.
const nextJsOwnedSpan = getActiveSpan();
if (nextJsOwnedSpan) {
getRootSpan(nextJsOwnedSpan)?.setAttribute(TRANSACTION_ATTR_SHOULD_DROP_TRANSACTION, true);
}

return new Proxy(apiHandler, {
apply: (
wrappingTarget,
thisArg,
args: [AugmentedNextApiRequest | undefined, AugmentedNextApiResponse | undefined],
) => {
dropNextjsRootContext();
return escapeNextjsTracing(() => {
const [req, res] = args;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ type EdgeRequest = {
};

/**
* Wraps a function with Sentry crons instrumentation by automaticaly sending check-ins for the given Vercel crons config.
* Wraps a function with Sentry crons instrumentation by automatically sending check-ins for the given Vercel crons config.
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function wrapApiHandlerWithSentryVercelCrons<F extends (...args: any[]) => any>(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { captureException, getCurrentScope, withIsolationScope } from '@sentry/core';
import { extractTraceparentData } from '@sentry/utils';
import { escapeNextjsTracing } from '../utils/tracingUtils';
import { dropNextjsRootContext, escapeNextjsTracing } from '../utils/tracingUtils';

interface FunctionComponent {
(...args: unknown[]): unknown;
Expand All @@ -25,6 +25,7 @@ export function wrapPageComponentWithSentry(pageComponent: FunctionComponent | C
if (isReactClassComponent(pageComponent)) {
return class SentryWrappedPageComponent extends pageComponent {
public render(...args: unknown[]): unknown {
dropNextjsRootContext();
return escapeNextjsTracing(() => {
return withIsolationScope(() => {
const scope = getCurrentScope();
Expand Down Expand Up @@ -62,6 +63,7 @@ export function wrapPageComponentWithSentry(pageComponent: FunctionComponent | C
} else if (typeof pageComponent === 'function') {
return new Proxy(pageComponent, {
apply(target, thisArg, argArray: [{ _sentryTraceData?: string } | undefined]) {
dropNextjsRootContext();
return escapeNextjsTracing(() => {
return withIsolationScope(() => {
const scope = getCurrentScope();
Expand Down
19 changes: 18 additions & 1 deletion packages/nextjs/src/common/utils/tracingUtils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { Scope, startNewTrace } from '@sentry/core';
import { Scope, getActiveSpan, getRootSpan, spanToJSON, startNewTrace } from '@sentry/core';
import type { PropagationContext } from '@sentry/types';
import { GLOBAL_OBJ, logger } from '@sentry/utils';
import { DEBUG_BUILD } from '../debug-build';
import { TRANSACTION_ATTR_SHOULD_DROP_TRANSACTION } from '../span-attributes-with-logic-attached';

const commonPropagationContextMap = new WeakMap<object, PropagationContext>();

Expand Down Expand Up @@ -92,3 +93,19 @@ export function escapeNextjsTracing<T>(cb: () => T): T {
});
}
}

/**
* Ideally this function never lands in the develop branch.
*
* Drops the entire span tree this function was called in, if it was a span tree created by Next.js.
*/
export function dropNextjsRootContext(): void {
const nextJsOwnedSpan = getActiveSpan();
if (nextJsOwnedSpan) {
const rootSpan = getRootSpan(nextJsOwnedSpan);
const rootSpanAttributes = spanToJSON(rootSpan).data;
if (rootSpanAttributes?.['next.span_type']) {
getRootSpan(nextJsOwnedSpan)?.setAttribute(TRANSACTION_ATTR_SHOULD_DROP_TRANSACTION, true);
}
}
}
3 changes: 2 additions & 1 deletion packages/nextjs/src/common/utils/wrapperUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import type { Span } from '@sentry/types';
import { isString, vercelWaitUntil } from '@sentry/utils';

import { autoEndSpanOnResponseEnd, flushSafelyWithTimeout } from './responseEnd';
import { commonObjectToIsolationScope, escapeNextjsTracing } from './tracingUtils';
import { commonObjectToIsolationScope, dropNextjsRootContext, escapeNextjsTracing } from './tracingUtils';

declare module 'http' {
interface IncomingMessage {
Expand Down Expand Up @@ -93,6 +93,7 @@ export function withTracedServerSideDataFetcher<F extends (...args: any[]) => Pr
this: unknown,
...args: Parameters<F>
): Promise<{ data: ReturnType<F>; sentryTrace?: string; baggage?: string }> {
dropNextjsRootContext();
return escapeNextjsTracing(() => {
const isolationScope = commonObjectToIsolationScope(req);
return withIsolationScope(isolationScope, () => {
Expand Down
9 changes: 7 additions & 2 deletions packages/nextjs/src/common/withServerActionInstrumentation.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
import {
SEMANTIC_ATTRIBUTE_SENTRY_SOURCE,
SPAN_STATUS_ERROR,
captureException,
continueTrace,
getClient,
getIsolationScope,
handleCallbackErrors,
startSpan,
withIsolationScope,
} from '@sentry/core';
import { captureException, continueTrace, getClient, handleCallbackErrors, startSpan } from '@sentry/core';
import { logger, vercelWaitUntil } from '@sentry/utils';

import { DEBUG_BUILD } from './debug-build';
import { isNotFoundNavigationError, isRedirectNavigationError } from './nextNavigationErrorUtils';
import { flushSafelyWithTimeout } from './utils/responseEnd';
import { escapeNextjsTracing } from './utils/tracingUtils';
import { dropNextjsRootContext, escapeNextjsTracing } from './utils/tracingUtils';

interface Options {
formData?: FormData;
Expand Down Expand Up @@ -64,6 +68,7 @@ async function withServerActionInstrumentationImplementation<A extends (...args:
options: Options,
callback: A,
): Promise<ReturnType<A>> {
dropNextjsRootContext();
return escapeNextjsTracing(() => {
return withIsolationScope(async isolationScope => {
const sendDefaultPii = getClient()?.getOptions().sendDefaultPii;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,6 @@ export function wrapGenerationFunctionWithSentry<F extends (...args: any[]) => a
const rootSpan = getRootSpan(activeSpan);
const { scope } = getCapturedScopesOnSpan(rootSpan);
setCapturedScopesOnSpan(rootSpan, scope ?? new Scope(), isolationScope);

// We mark the root span as an app router span so we can allow-list it in our span processor that would normally filter out all Next.js transactions/spans
rootSpan.setAttribute('sentry.rsc', true);
}

let data: Record<string, unknown> | undefined = undefined;
Expand Down
1 change: 0 additions & 1 deletion packages/nextjs/src/common/wrapRouteHandlerWithSentry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ export function wrapRouteHandlerWithSentry<F extends (...args: any[]) => any>(
const activeSpan = getActiveSpan();
if (activeSpan) {
const rootSpan = getRootSpan(activeSpan);
rootSpan.setAttribute('sentry.route_handler', true);
const { scope } = getCapturedScopesOnSpan(rootSpan);
setCapturedScopesOnSpan(rootSpan, scope ?? new Scope(), isolationScope);
}
Expand Down
3 changes: 0 additions & 3 deletions packages/nextjs/src/common/wrapServerComponentWithSentry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,6 @@ export function wrapServerComponentWithSentry<F extends (...args: any[]) => any>
const rootSpan = getRootSpan(activeSpan);
const { scope } = getCapturedScopesOnSpan(rootSpan);
setCapturedScopesOnSpan(rootSpan, scope ?? new Scope(), isolationScope);

// We mark the root span as an app router span so we can allow-list it in our span processor that would normally filter out all Next.js transactions/spans
rootSpan.setAttribute('sentry.rsc', true);
}

const headersDict = context.headers ? winterCGHeadersToDict(context.headers) : undefined;
Expand Down
7 changes: 5 additions & 2 deletions packages/nextjs/src/config/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -409,12 +409,15 @@ export type SentryBuildOptions = {
autoInstrumentAppDirectory?: boolean;

/**
* Exclude certain serverside API routes or pages from being instrumented with Sentry. This option takes an array of
* strings or regular expressions. This options also affects pages in the `app` directory.
* Exclude certain serverside API routes or pages from being instrumented with Sentry during build-time. This option
* takes an array of strings or regular expressions. This options also affects pages in the `app` directory.
*
* NOTE: Pages should be specified as routes (`/animals` or `/api/animals/[animalType]/habitat`), not filepaths
* (`pages/animals/index.js` or `.\src\pages\api\animals\[animalType]\habitat.tsx`), and strings must be be a full,
* exact match.
*
* Notice: If you build Next.js with turbopack, the Sentry SDK will no longer apply build-time instrumentation and
* purely rely on Next.js telemetry features, meaning that this option will effectively no-op.
*/
excludeServerRoutes?: Array<RegExp | string>;

Expand Down
Loading
Loading