Skip to content

Commit 0ef2163

Browse files
chargomeclaude
authored andcommitted
fix(nextjs): Mark a cached shell at pageload span start
The attribute and the link are part of the start options now, so `tracesSampler` and `ignoreSpans` see that a pageload came from a cached shell. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
1 parent 24d49e5 commit 0ef2163

4 files changed

Lines changed: 52 additions & 41 deletions

File tree

‎packages/nextjs/src/client/routing/appRouterPageloadInstrumentation.ts‎

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ import { stripTrailingSlash } from './parameterization';
1919
import type { TraceMetaTagValues, TraceMetaTagWait } from './traceMetaTags';
2020
import {
2121
addTraceMetaTagLink,
22+
cachedShellSpanOptions,
2223
canWaitForTraceMetaTag,
23-
markCachedShell,
2424
readShellMarker,
2525
readTraceMetaTags,
2626
waitForTraceMetaTag,
@@ -53,16 +53,24 @@ function startPageloadSpan(
5353
documentUrl: string,
5454
traceMetaTags?: TraceMetaTagValues,
5555
): Span | undefined {
56-
const span = startBrowserTracingPageLoadSpan(client, spanOptions, traceMetaTags);
56+
const shellMarker = readShellMarker();
57+
const shellOptions = shellMarker ? cachedShellSpanOptions(shellMarker) : undefined;
58+
const span = startBrowserTracingPageLoadSpan(
59+
client,
60+
shellOptions
61+
? {
62+
...spanOptions,
63+
attributes: { ...spanOptions.attributes, ...shellOptions.attributes },
64+
links: shellOptions.links,
65+
}
66+
: spanOptions,
67+
traceMetaTags,
68+
);
5769
const scope = getCurrentScope();
5870
const { normalizedRequest } = scope.getScopeData().sdkProcessingMetadata;
5971
if (normalizedRequest) {
6072
scope.setSDKProcessingMetadata({ normalizedRequest: { ...normalizedRequest, url: documentUrl } });
6173
}
62-
const shellMarker = readShellMarker();
63-
if (span?.isRecording() && shellMarker) {
64-
markCachedShell(span, shellMarker);
65-
}
6674
return span;
6775
}
6876

‎packages/nextjs/src/client/routing/traceMetaTags.ts‎

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { SENTRY_LINK_TYPE } from '@sentry/conventions/attributes';
2-
import type { Span } from '@sentry/core';
2+
import type { Span, SpanLink, StartSpanOptions } from '@sentry/core';
33
import { extractTraceparentData, GLOBAL_OBJ } from '@sentry/core';
44
import { SHELL_META_TAG_NAME } from '../../common/traceMetaTags';
55
import { WINDOW } from '@sentry/react';
@@ -134,24 +134,31 @@ export function waitForTraceMetaTag(
134134
return { giveUp: () => settle(onGiveUp) };
135135
}
136136

137-
/**
138-
* Adds a span link to the span described by a `sentry-trace` value. Used when the tag arrives after
139-
* the pageload span exists, which can only link the server request, not continue it.
140-
*/
141-
export function addTraceMetaTagLink(span: Span, { sentryTrace }: TraceMetaTagValues, linkType?: string): void {
137+
/** The span link to the span a `sentry-trace` value describes, if the value is one. */
138+
function traceMetaTagLink(sentryTrace: string, linkType: string): SpanLink | undefined {
142139
const traceparent = extractTraceparentData(sentryTrace);
143140
if (!traceparent?.traceId || !traceparent.parentSpanId) {
144-
return;
141+
return undefined;
145142
}
146-
147-
span.addLink({
143+
return {
148144
context: {
149145
traceId: traceparent.traceId,
150146
spanId: traceparent.parentSpanId,
151147
traceFlags: traceparent.parentSampled ? TRACE_FLAG_SAMPLED : TRACE_FLAG_NONE,
152148
},
153-
attributes: { [SENTRY_LINK_TYPE]: linkType ?? SERVER_REQUEST_LINK_TYPE },
154-
});
149+
attributes: { [SENTRY_LINK_TYPE]: linkType },
150+
};
151+
}
152+
153+
/**
154+
* Adds a span link to the span described by a `sentry-trace` value. Used when the tag arrives after
155+
* the pageload span exists, which can only link the server request, not continue it.
156+
*/
157+
export function addTraceMetaTagLink(span: Span, { sentryTrace }: TraceMetaTagValues): void {
158+
const link = traceMetaTagLink(sentryTrace, SERVER_REQUEST_LINK_TYPE);
159+
if (link) {
160+
span.addLink(link);
161+
}
155162
}
156163

157164
/** Reads the marker the server writes into a shell while it prerenders it, see `nextSentryPropagator.ts`. */
@@ -161,11 +168,12 @@ export function readShellMarker(): string | undefined {
161168
}
162169

163170
/**
164-
* Marks a pageload whose document is a cached shell and links the request that prerendered the shell.
171+
* The start options that mark a pageload whose document is a cached shell and link the request that
172+
* prerendered the shell. Applied at span start, so `tracesSampler` and `ignoreSpans` see the attribute.
165173
* The marker is never continued: the shell is served to every later visitor, and the request behind it
166174
* handled none of their visits.
167175
*/
168-
export function markCachedShell(span: Span, marker: string): void {
169-
span.setAttribute(CACHED_SHELL_ATTRIBUTE, true);
170-
addTraceMetaTagLink(span, { sentryTrace: marker, baggage: undefined }, 'cache_origin');
176+
export function cachedShellSpanOptions(marker: string): Pick<StartSpanOptions, 'attributes' | 'links'> {
177+
const link = traceMetaTagLink(marker, 'cache_origin');
178+
return { attributes: { [CACHED_SHELL_ATTRIBUTE]: true }, ...(link && { links: [link] }) };
171179
}

‎packages/nextjs/test/client/appRouterRoutingInstrumentation.test.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// @vitest-environment jsdom
2-
import { SENTRY_OP, URL_FULL, URL_PATH } from '@sentry/conventions/attributes';
2+
import { SENTRY_LINK_TYPE, SENTRY_OP, URL_FULL, URL_PATH } from '@sentry/conventions/attributes';
33
import { PAGELOAD } from '@sentry/conventions/op';
44
import type { Client } from '@sentry/core';
55
import type * as SentryCore from '@sentry/core';
@@ -361,7 +361,7 @@ describe('appRouterInstrumentPageLoad', () => {
361361
expect.objectContaining({
362362
trace_id: '11111111111111111111111111111111',
363363
span_id: '1111111111111111',
364-
attributes: expect.objectContaining({ 'sentry.link.type': 'cache_origin' }),
364+
attributes: expect.objectContaining({ [SENTRY_LINK_TYPE]: 'cache_origin' }),
365365
}),
366366
]);
367367
meta.remove();

‎packages/nextjs/test/client/traceMetaTags.test.ts‎

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import type { Span } from '@sentry/core';
44
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
55
import {
66
addTraceMetaTagLink,
7-
markCachedShell,
7+
cachedShellSpanOptions,
88
readShellMarker,
99
canWaitForTraceMetaTag,
1010
readTraceMetaTags,
@@ -235,25 +235,20 @@ describe('readShellMarker', () => {
235235
});
236236
});
237237

238-
describe('markCachedShell', () => {
239-
it('sets the attribute and links the prerender as cache origin', () => {
240-
const span = { setAttribute: vi.fn(), addLink: vi.fn() } as unknown as Span;
241-
242-
markCachedShell(span, HEAD_TRACE);
243-
244-
expect(span.setAttribute).toHaveBeenCalledWith('sentry.nextjs.cached_shell', true);
245-
expect(span.addLink).toHaveBeenCalledWith({
246-
context: { traceId: '11111111111111111111111111111111', spanId: '1111111111111111', traceFlags: 1 },
247-
attributes: { 'sentry.link.type': 'cache_origin' },
238+
describe('cachedShellSpanOptions', () => {
239+
it('marks the span and links the prerender as cache origin', () => {
240+
expect(cachedShellSpanOptions(HEAD_TRACE)).toEqual({
241+
attributes: { 'sentry.nextjs.cached_shell': true },
242+
links: [
243+
{
244+
context: { traceId: '11111111111111111111111111111111', spanId: '1111111111111111', traceFlags: 1 },
245+
attributes: { [SENTRY_LINK_TYPE]: 'cache_origin' },
246+
},
247+
],
248248
});
249249
});
250250

251-
it('sets the attribute even when the marker is not a sentry-trace value', () => {
252-
const span = { setAttribute: vi.fn(), addLink: vi.fn() } as unknown as Span;
253-
254-
markCachedShell(span, 'prerendered');
255-
256-
expect(span.setAttribute).toHaveBeenCalledWith('sentry.nextjs.cached_shell', true);
257-
expect(span.addLink).not.toHaveBeenCalled();
251+
it('marks the span even when the marker is not a sentry-trace value', () => {
252+
expect(cachedShellSpanOptions('prerendered')).toEqual({ attributes: { 'sentry.nextjs.cached_shell': true } });
258253
});
259254
});

0 commit comments

Comments
 (0)