Skip to content

Commit 5d2d542

Browse files
chargomeclaude
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 11b4573 commit 5d2d542

4 files changed

Lines changed: 52 additions & 40 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: 22 additions & 14 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';
@@ -132,24 +132,31 @@ export function waitForTraceMetaTag(
132132
return { giveUp: () => settle(onGiveUp) };
133133
}
134134

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

155162
/** Reads the marker the server writes into a shell while it prerenders it, see `nextSentryPropagator.ts`. */
@@ -159,11 +166,12 @@ export function readShellMarker(): string | undefined {
159166
}
160167

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

‎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';
@@ -341,7 +341,7 @@ describe('appRouterInstrumentPageLoad', () => {
341341
expect.objectContaining({
342342
trace_id: '11111111111111111111111111111111',
343343
span_id: '1111111111111111',
344-
attributes: expect.objectContaining({ 'sentry.link.type': 'cache_origin' }),
344+
attributes: expect.objectContaining({ [SENTRY_LINK_TYPE]: 'cache_origin' }),
345345
}),
346346
]);
347347
meta.remove();

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

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
// @vitest-environment jsdom
2+
import { SENTRY_LINK_TYPE } from '@sentry/conventions/attributes';
23
import type { Span } from '@sentry/core';
34
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
45
import {
56
addTraceMetaTagLink,
6-
markCachedShell,
7+
cachedShellSpanOptions,
78
readShellMarker,
89
canWaitForTraceMetaTag,
910
readTraceMetaTags,
@@ -232,25 +233,20 @@ describe('readShellMarker', () => {
232233
});
233234
});
234235

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

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

0 commit comments

Comments
 (0)