Skip to content
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
14 changes: 13 additions & 1 deletion packages/nextjs/src/server/nextSentryPropagator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { defaultTextMapSetter, propagation, trace } from '@opentelemetry/api';
import {
_INTERNAL_CACHE_ORIGIN_META_TAG_NAME,
_INTERNAL_RENDER_START_META_TAG_NAME,
extractTraceparentData,
timestampInSeconds,
} from '@sentry/core';
import { _INTERNAL_getRenderStart } from '@sentry/core/server';
Expand All @@ -16,6 +17,11 @@ import { isPrerenderContext, markPrerendersOnTracerProvider } from './prerenderC
* gets the prerender's trace in a `sentry-cache-origin` tag instead, which the client only links, and
* the start of the linked span in a `sentry-render-start` tag.
*
* Both values change with every prerender, and a platform such as Vercel bills an ISR regeneration only
* if its output changed. So the shell only gets them when the client can link the prerender, which is
* when the prerender is sampled. Otherwise it gets an empty `sentry-cache-origin` tag, which still
* marks the pageload as served from a prerender.
*
* The SDK registers it when it sets up OpenTelemetry. An app that sets up OpenTelemetry itself
* registers it in place of `SentryPropagator` and calls `markPrerendersOnTracerProvider` with its
* tracer provider, which this propagator relies on to recognize a prerender.
Expand All @@ -33,7 +39,13 @@ export class NextSentryPropagator extends SentryPropagator {
const values: Record<string, string> = {};
super.inject(ctx, values, defaultTextMapSetter);
const sentryTrace = values['sentry-trace'];
if (sentryTrace) {
if (!sentryTrace) {
return;
}

if (!extractTraceparentData(sentryTrace)?.parentSampled) {
setter.set(carrier, _INTERNAL_CACHE_ORIGIN_META_TAG_NAME, '');
} else {
setter.set(carrier, _INTERNAL_CACHE_ORIGIN_META_TAG_NAME, sentryTrace);
// Next.js computes the meta tags at the start of `prerenderToStream`, so the current time is the render start.
const renderStart = _INTERNAL_getRenderStart(trace.getSpan(ctx), timestampInSeconds());
Expand Down
7 changes: 2 additions & 5 deletions packages/nextjs/test/server/nextSentryPropagator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,8 @@ describe('NextSentryPropagator', () => {
});
});

it('hands out only the cache origin and the current time while a route is prerendered in an unsampled trace', () => {
it('hands out only an empty cache origin while a route is prerendered in an unsampled trace', () => {
setup(0);
const before = Math.floor(Date.now() / 1000);

trace.getTracer('next.js').startActiveSpan('GET /[id]', span => {
expect(span.isRecording()).toBe(false);
Expand All @@ -128,9 +127,7 @@ describe('NextSentryPropagator', () => {
.getTracer('next.js')
.startActiveSpan('prerender route (app) /[id]', PRERENDER_SPAN_OPTIONS, prerenderSpan => {
expect(prerenderSpan.isRecording()).toBe(false);
const { 'sentry-render-start': renderStart, ...tags } = inject();
expect(tags).toEqual({ 'sentry-cache-origin': expect.stringMatching(/-0$/) });
expect(Number(renderStart)).toBeGreaterThanOrEqual(before);
expect(inject()).toEqual({ 'sentry-cache-origin': '' });
prerenderSpan.end();
});
span.end();
Expand Down
Loading