Skip to content

feat(nexjs): Sample out low-quality spans on older Next.js versions #11722

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

Merged
merged 8 commits into from
Apr 24, 2024
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
28 changes: 28 additions & 0 deletions packages/core/src/baseclient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import type {
SessionAggregates,
SeverityLevel,
Span,
SpanAttributes,
SpanContextData,
StartSpanOptions,
TransactionEvent,
Transport,
Expand Down Expand Up @@ -416,6 +418,20 @@ export abstract class BaseClient<O extends ClientOptions> implements Client<O> {
callback: (feedback: FeedbackEvent, options?: { includeReplay: boolean }) => void,
): void;

/** @inheritdoc */
public on(
hook: 'beforeSampling',
callback: (
samplingData: {
spanAttributes: SpanAttributes;
spanName: string;
parentSampled?: boolean;
parentContext?: SpanContextData;
},
samplingDecision: { decision: boolean },
) => void,
): void;

/** @inheritdoc */
public on(
hook: 'startPageLoadSpan',
Expand All @@ -442,6 +458,18 @@ export abstract class BaseClient<O extends ClientOptions> implements Client<O> {
this._hooks[hook].push(callback);
}

/** @inheritdoc */
public emit(
hook: 'beforeSampling',
samplingData: {
spanAttributes: SpanAttributes;
spanName: string;
parentSampled?: boolean;
parentContext?: SpanContextData;
},
samplingDecision: { decision: boolean },
): void;

/** @inheritdoc */
public emit(hook: 'spanStart', span: Span): void;

Expand Down
74 changes: 38 additions & 36 deletions packages/nextjs/src/common/utils/wrapperUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { isString } from '@sentry/utils';

import { platformSupportsStreaming } from './platformSupportsStreaming';
import { autoEndSpanOnResponseEnd, flushQueue } from './responseEnd';
import { commonObjectToIsolationScope } from './tracingUtils';
import { commonObjectToIsolationScope, escapeNextjsTracing } from './tracingUtils';

declare module 'http' {
interface IncomingMessage {
Expand Down Expand Up @@ -90,44 +90,46 @@ export function withTracedServerSideDataFetcher<F extends (...args: any[]) => Pr
},
): (...params: Parameters<F>) => Promise<ReturnType<F>> {
return async function (this: unknown, ...args: Parameters<F>): Promise<ReturnType<F>> {
const isolationScope = commonObjectToIsolationScope(req);
return withIsolationScope(isolationScope, () => {
isolationScope.setSDKProcessingMetadata({
request: req,
});
return escapeNextjsTracing(() => {
const isolationScope = commonObjectToIsolationScope(req);
return withIsolationScope(isolationScope, () => {
isolationScope.setSDKProcessingMetadata({
request: req,
});

const sentryTrace =
req.headers && isString(req.headers['sentry-trace']) ? req.headers['sentry-trace'] : undefined;
const baggage = req.headers?.baggage;

return continueTrace({ sentryTrace, baggage }, () => {
const requestSpan = getOrStartRequestSpan(req, res, options.requestedRouteName);
return withActiveSpan(requestSpan, () => {
return startSpanManual(
{
op: 'function.nextjs',
name: `${options.dataFetchingMethodName} (${options.dataFetcherRouteName})`,
attributes: {
[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.function.nextjs',
[SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]: 'route',
const sentryTrace =
req.headers && isString(req.headers['sentry-trace']) ? req.headers['sentry-trace'] : undefined;
const baggage = req.headers?.baggage;

return continueTrace({ sentryTrace, baggage }, () => {
const requestSpan = getOrStartRequestSpan(req, res, options.requestedRouteName);
return withActiveSpan(requestSpan, () => {
return startSpanManual(
{
op: 'function.nextjs',
name: `${options.dataFetchingMethodName} (${options.dataFetcherRouteName})`,
attributes: {
[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.function.nextjs',
[SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]: 'route',
},
},
},
async dataFetcherSpan => {
dataFetcherSpan.setStatus({ code: SPAN_STATUS_OK });
try {
return await origDataFetcher.apply(this, args);
} catch (e) {
dataFetcherSpan.setStatus({ code: SPAN_STATUS_ERROR, message: 'internal_error' });
requestSpan?.setStatus({ code: SPAN_STATUS_ERROR, message: 'internal_error' });
throw e;
} finally {
dataFetcherSpan.end();
if (!platformSupportsStreaming()) {
await flushQueue();
async dataFetcherSpan => {
dataFetcherSpan.setStatus({ code: SPAN_STATUS_OK });
try {
return await origDataFetcher.apply(this, args);
} catch (e) {
dataFetcherSpan.setStatus({ code: SPAN_STATUS_ERROR, message: 'internal_error' });
requestSpan?.setStatus({ code: SPAN_STATUS_ERROR, message: 'internal_error' });
throw e;
} finally {
dataFetcherSpan.end();
if (!platformSupportsStreaming()) {
await flushQueue();
}
}
}
},
);
},
);
});
});
});
});
Expand Down
32 changes: 31 additions & 1 deletion packages/nextjs/src/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,22 @@ const globalWithInjectedValues = GLOBAL_OBJ as typeof GLOBAL_OBJ & {
__sentryRewritesTunnelPath__?: string;
};

// https://github.com/lforst/nextjs-fork/blob/9051bc44d969a6e0ab65a955a2fc0af522a83911/packages/next/src/server/lib/trace/constants.ts#L11
const NEXTJS_SPAN_NAME_PREFIXES = [
'BaseServer.',
'LoadComponents.',
'NextServer.',
'createServer.',
'startServer.',
'NextNodeServer.',
'Render.',
'AppRender.',
'Router.',
'Node.',
'AppRouteRouteHandlers.',
'ResolveMetadata.',
];

/**
* A passthrough error boundary for the server that doesn't depend on any react. Error boundaries don't catch SSR errors
* so they should simply be a passthrough.
Expand Down Expand Up @@ -90,7 +106,7 @@ export function init(options: NodeOptions): void {
customDefaultIntegrations.push(distDirRewriteFramesIntegration({ distDirName }));
}

const opts = {
const opts: NodeOptions = {
environment: process.env.SENTRY_ENVIRONMENT || getVercelEnv(false) || process.env.NODE_ENV,
defaultIntegrations: customDefaultIntegrations,
...options,
Expand All @@ -113,6 +129,20 @@ export function init(options: NodeOptions): void {

nodeInit(opts);

const client = getClient();
client?.on('beforeSampling', ({ spanAttributes, spanName, parentSampled, parentContext }, samplingDecision) => {
// If we encounter a span emitted by Next.js, we do not want to sample it
// The reason for this is that the data quality of the spans varies, it is different per version of Next,
// and we need to keep our manual instrumentation around for the edge runtime anyhow.
// BUT we only do this if we don't have a parent span with a sampling decision yet (or if the parent is remote)
if (
(spanAttributes['next.span_type'] || NEXTJS_SPAN_NAME_PREFIXES.some(prefix => spanName.startsWith(prefix))) &&
(parentSampled === undefined || parentContext?.isRemote)
) {
samplingDecision.decision = false;
}
});

addEventProcessor(
Object.assign(
(event => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ describe('Error API Endpoints', () => {
const envelopes = await env.getMultipleEnvelopeRequest({
url,
envelopeType: 'transaction',
count: 2, // We will receive 2 transactions - one from Next.js instrumentation and one from our SDK
count: 1,
});

const sentryTransactionEnvelope = envelopes.find(envelope => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ describe('Error Server-side Props', () => {
const envelopes = await env.getMultipleEnvelopeRequest({
url,
envelopeType: 'transaction',
count: 2, // We will receive 2 transactions - one from Next.js instrumentation and one from our SDK
count: 1,
});

const sentryTransactionEnvelope = envelopes.find(envelope => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ describe('Tracing 200', () => {
const envelopes = await env.getMultipleEnvelopeRequest({
url,
envelopeType: 'transaction',
count: 2, // We will receive 2 transactions - one from Next.js instrumentation and one from our SDK
count: 1,
});

const sentryTransactionEnvelope = envelopes.find(envelope => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ describe('Tracing 500', () => {
const envelopes = await env.getMultipleEnvelopeRequest({
url,
envelopeType: 'transaction',
count: 2, // We will receive 2 transactions - one from Next.js instrumentation and one from our SDK
count: 1,
});

const sentryTransactionEnvelope = envelopes.find(envelope => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ describe('Tracing HTTP', () => {
const envelopes = await env.getMultipleEnvelopeRequest({
url,
envelopeType: 'transaction',
count: 2, // We will receive 2 transactions - one from Next.js instrumentation and one from our SDK
count: 1,
});

const sentryTransactionEnvelope = envelopes.find(envelope => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ describe('getInitialProps', () => {
const envelopes = await env.getMultipleEnvelopeRequest({
url,
envelopeType: 'transaction',
count: 2, // We will receive 2 transactions - one from Next.js instrumentation and one from our SDK
count: 1,
});

const sentryTransactionEnvelope = envelopes.find(envelope => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ describe('getServerSideProps', () => {
const envelopes = await env.getMultipleEnvelopeRequest({
url,
envelopeType: 'transaction',
count: 2, // We will receive 2 transactions - one from Next.js instrumentation and one from our SDK
count: 1,
});

const sentryTransactionEnvelope = envelopes.find(envelope => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ describe('tracingServerGetServerSidePropsCustomPageExtension', () => {
const envelopes = await env.getMultipleEnvelopeRequest({
url,
envelopeType: 'transaction',
count: 2, // We will receive 2 transactions - one from Next.js instrumentation and one from our SDK
count: 1,
});

const sentryTransactionEnvelope = envelopes.find(envelope => {
Expand Down
17 changes: 12 additions & 5 deletions packages/opentelemetry/src/sampler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,18 @@ export class SentrySampler implements Sampler {

const parentSampled = parentSpan ? getParentSampled(parentSpan, traceId, spanName) : undefined;

// If we encounter a span emitted by Next.js, we do not want to sample it
// The reason for this is that the data quality of the spans varies, it is different per version of Next,
// and we need to keep our manual instrumentation around for the edge runtime anyhow.
// BUT we only do this if we don't have a parent span with a sampling decision yet (or if the parent is remote)
if (spanAttributes['next.span_type'] && (typeof parentSampled !== 'boolean' || parentContext?.isRemote)) {
const mutableSamplingDecision = { decision: true };
this._client.emit(
'beforeSampling',
{
spanAttributes: spanAttributes,
spanName: spanName,
parentSampled: parentSampled,
parentContext: parentContext,
},
mutableSamplingDecision,
);
if (!mutableSamplingDecision.decision) {
return { decision: SamplingDecision.NOT_RECORD, traceState: traceState };
}

Expand Down
31 changes: 30 additions & 1 deletion packages/types/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import type { Scope } from './scope';
import type { SdkMetadata } from './sdkmetadata';
import type { Session, SessionAggregates } from './session';
import type { SeverityLevel } from './severity';
import type { Span } from './span';
import type { Span, SpanAttributes, SpanContextData } from './span';
import type { StartSpanOptions } from './startSpanOptions';
import type { Transport, TransportMakeRequestResponse } from './transport';

Expand Down Expand Up @@ -178,6 +178,23 @@ export interface Client<O extends ClientOptions = ClientOptions> {
*/
on(hook: 'spanStart', callback: (span: Span) => void): void;

/**
* Register a callback before span sampling runs. Receives a `samplingDecision` object argument with a `decision`
* property that can be used to make a sampling decision that will be enforced, before any span sampling runs.
*/
on(
hook: 'beforeSampling',
callback: (
samplingData: {
spanAttributes: SpanAttributes;
spanName: string;
parentSampled?: boolean;
parentContext?: SpanContextData;
},
samplingDecision: { decision: boolean },
) => void,
): void;

/**
* Register a callback for whenever a span is ended.
* Receives the span as argument.
Expand Down Expand Up @@ -262,6 +279,18 @@ export interface Client<O extends ClientOptions = ClientOptions> {
/** Fire a hook whener a span starts. */
emit(hook: 'spanStart', span: Span): void;

/** A hook that is called every time before a span is sampled. */
emit(
hook: 'beforeSampling',
samplingData: {
spanAttributes: SpanAttributes;
spanName: string;
parentSampled?: boolean;
parentContext?: SpanContextData;
},
samplingDecision: { decision: boolean },
): void;

/** Fire a hook whener a span ends. */
emit(hook: 'spanEnd', span: Span): void;

Expand Down