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
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,16 @@ import { waitForTransaction } from '@sentry-internal/test-utils';

const APP = 'nextjs-16-cacheComponents';

const PAGE_SERVER_RENDERING_AGE = 'page.server_rendering.age';

async function getRenderStart(page: Page): Promise<number> {
return Number(await page.locator('meta[name="sentry-render-start"]').getAttribute('content'));
}

function expectedRenderAge(pageloadStart: number, renderStart: number): number {
return Math.max(0, Math.floor(pageloadStart - renderStart));
}

// A self-hosted server sends the trace of the request in a `Server-Timing` header of a document
// that renders for the request, which reaches the browser before any HTML, so the pageload continues
// it right away. A document that a shared cache may store gets no header, as a CDN would replay it.
Expand Down Expand Up @@ -76,7 +86,13 @@ test('does not share a trace between visitors of a shell generated at request ti
// cached document is cacheable by a CDN as well, so the response carries no trace header either.
const id = `shell-${Date.now()}`;

const visit = async (): Promise<{ traceId: string | undefined; links: unknown; serverRenderingType: unknown }> => {
const visit = async (): Promise<{
traceId: string | undefined;
links: unknown;
serverRenderingType: unknown;
renderAge: unknown;
expectedRenderAge: number;
}> => {
const context = await browser.newContext();
const page: Page = await context.newPage();

Expand All @@ -91,6 +107,8 @@ test('does not share a trace between visitors of a shell generated at request ti
await expect(page.locator('#result')).toHaveText(`Hello, ${id}!`);
expect(await page.locator('meta[name="sentry-trace"]').count()).toBe(0);
expect(await page.locator('meta[name="sentry-cache-origin"]').count()).toBe(1);
expect(await page.locator('meta[name="sentry-render-start"]').count()).toBe(1);
const renderStart = await getRenderStart(page);
expect(response?.headers()['server-timing'] ?? '').not.toContain('sentry-trace');

const pageloadTx = await pageloadTxPromise;
Expand All @@ -100,6 +118,8 @@ test('does not share a trace between visitors of a shell generated at request ti
traceId: pageloadTx.contexts?.trace?.trace_id,
links: pageloadTx.contexts?.trace?.links,
serverRenderingType: pageloadTx.contexts?.trace?.data?.['page.server_rendering.type'],
renderAge: pageloadTx.contexts?.trace?.data?.[PAGE_SERVER_RENDERING_AGE],
expectedRenderAge: expectedRenderAge(pageloadTx.start_timestamp!, renderStart),
};
};

Expand All @@ -111,6 +131,8 @@ test('does not share a trace between visitors of a shell generated at request ti
expect(second.traceId).not.toBe(first.traceId);
expect(first.serverRenderingType).toBe('static');
expect(second.serverRenderingType).toBe('static');
expect(first.renderAge).toBe(first.expectedRenderAge);
expect(second.renderAge).toBe(second.expectedRenderAge);

// Both link the request that prerendered the shell, which is the first visitor's own request. The
// client cannot tell the first visit from later ones, so that pageload starts a fresh trace too.
Expand Down Expand Up @@ -160,13 +182,17 @@ test('continues the request trace on a shell prerendered at runtime and links th
const [prerenderTraceId, prerenderSpanId] = (await page
.locator('meta[name="sentry-cache-origin"]')
.getAttribute('content'))!.split('-');
const renderStart = await getRenderStart(page);

const pageloadTx = await pageloadTxPromise;

// Only the shell came from the cache. The rest of the page rendered for this request, and the
// pageload continues that trace.
expect(pageloadTx.contexts?.trace?.trace_id).toBe(requestTraceId);
expect(pageloadTx.contexts?.trace?.data?.['page.server_rendering.type']).toBe('partially_static');
expect(pageloadTx.contexts?.trace?.data?.[PAGE_SERVER_RENDERING_AGE]).toBe(
expectedRenderAge(pageloadTx.start_timestamp!, renderStart),
);
expect(pageloadTx.contexts?.trace?.links).toEqual([
{
trace_id: prerenderTraceId,
Expand Down Expand Up @@ -195,10 +221,12 @@ test('starts a fresh trace on a fully static page', async ({ page }) => {
// The response is cacheable, so the request that serves it sends no trace header either.
expect(await page.locator('meta[name="sentry-trace"]').count()).toBe(0);
expect(await page.locator('meta[name="sentry-cache-origin"]').count()).toBe(0);
expect(await page.locator('meta[name="sentry-render-start"]').count()).toBe(0);
expect(response?.headers()['server-timing'] ?? '').not.toContain('sentry-trace');
expect(pageloadTx.contexts?.trace?.trace_id).toBeTruthy();
expect(pageloadTx.contexts?.trace?.links).toBeUndefined();
expect(pageloadTx.contexts?.trace?.data?.['page.server_rendering.type']).toBeUndefined();
expect(pageloadTx.contexts?.trace?.data?.[PAGE_SERVER_RENDERING_AGE]).toBeUndefined();
});

test('keeps a navigation that starts while the document still streams', async ({ page }) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,16 @@ import { getSpanOp, waitForStreamedSpan } from '@sentry-internal/test-utils';

const APP = 'nextjs-16-streaming-cacheComponents';

const PAGE_SERVER_RENDERING_AGE = 'page.server_rendering.age';

async function getRenderStart(page: Page): Promise<number> {
return Number(await page.locator('meta[name="sentry-render-start"]').getAttribute('content'));
}

function expectedRenderAge(pageloadStart: number, renderStart: number): number {
return Math.max(0, Math.floor(pageloadStart - renderStart));
}

// A self-hosted server sends the trace of the request in a `Server-Timing` header of a document
// that renders for the request, which reaches the browser before any HTML, so the pageload continues
// it right away. A document that a shared cache may store gets no header, as a CDN would replay it.
Expand Down Expand Up @@ -66,7 +76,13 @@ test('does not share a trace between visitors of a shell generated at request ti
// cached document is cacheable by a CDN as well, so the response carries no trace header either.
const id = `shell-${Date.now()}`;

const visit = async (): Promise<{ traceId: string | undefined; links: unknown; serverRenderingType: unknown }> => {
const visit = async (): Promise<{
traceId: string | undefined;
links: unknown;
serverRenderingType: unknown;
renderAge: unknown;
expectedRenderAge: number;
}> => {
const context = await browser.newContext();
const page: Page = await context.newPage();

Expand All @@ -78,6 +94,8 @@ test('does not share a trace between visitors of a shell generated at request ti
await expect(page.locator('#result')).toHaveText(`Hello, ${id}!`);
expect(await page.locator('meta[name="sentry-trace"]').count()).toBe(0);
expect(await page.locator('meta[name="sentry-cache-origin"]').count()).toBe(1);
expect(await page.locator('meta[name="sentry-render-start"]').count()).toBe(1);
const renderStart = await getRenderStart(page);
expect(response?.headers()['server-timing'] ?? '').not.toContain('sentry-trace');

const pageloadSpan = await pageloadSpanPromise;
Expand All @@ -87,6 +105,8 @@ test('does not share a trace between visitors of a shell generated at request ti
traceId: pageloadSpan.trace_id,
links: pageloadSpan.links,
serverRenderingType: pageloadSpan.attributes['page.server_rendering.type']?.value,
renderAge: pageloadSpan.attributes[PAGE_SERVER_RENDERING_AGE]?.value,
expectedRenderAge: expectedRenderAge(pageloadSpan.start_timestamp, renderStart),
};
};

Expand All @@ -98,6 +118,8 @@ test('does not share a trace between visitors of a shell generated at request ti
expect(second.traceId).not.toBe(first.traceId);
expect(first.serverRenderingType).toBe('static');
expect(second.serverRenderingType).toBe('static');
expect(first.renderAge).toBe(first.expectedRenderAge);
expect(second.renderAge).toBe(second.expectedRenderAge);

// Both link the request that prerendered the shell, which is the first visitor's own request. The
// client cannot tell the first visit from later ones, so that pageload starts a fresh trace too.
Expand Down Expand Up @@ -148,13 +170,17 @@ test('continues the request trace on a shell prerendered at runtime and links th
const [prerenderTraceId, prerenderSpanId] = (await page
.locator('meta[name="sentry-cache-origin"]')
.getAttribute('content'))!.split('-');
const renderStart = await getRenderStart(page);

const pageloadSpan = await pageloadSpanPromise;

// Only the shell came from the cache. The rest of the page rendered for this request, and the
// pageload continues that trace.
expect(pageloadSpan.trace_id).toBe(requestTraceId);
expect(pageloadSpan.attributes['page.server_rendering.type']?.value).toBe('partially_static');
expect(pageloadSpan.attributes[PAGE_SERVER_RENDERING_AGE]?.value).toBe(
expectedRenderAge(pageloadSpan.start_timestamp, renderStart),
);
expect(pageloadSpan.links).toEqual([
{
trace_id: prerenderTraceId,
Expand Down Expand Up @@ -183,10 +209,12 @@ test('starts a fresh trace on a fully static page', async ({ page }) => {
// The response is cacheable, so the request that serves it sends no trace header either.
expect(await page.locator('meta[name="sentry-trace"]').count()).toBe(0);
expect(await page.locator('meta[name="sentry-cache-origin"]').count()).toBe(0);
expect(await page.locator('meta[name="sentry-render-start"]').count()).toBe(0);
expect(response?.headers()['server-timing'] ?? '').not.toContain('sentry-trace');
expect(pageloadSpan.trace_id).toBeTruthy();
expect(pageloadSpan.links).toBeUndefined();
expect(pageloadSpan.attributes['page.server_rendering.type']).toBeUndefined();
expect(pageloadSpan.attributes[PAGE_SERVER_RENDERING_AGE]).toBeUndefined();
});

test('keeps a navigation that starts while the document still streams', async ({ page }) => {
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ export { applySdkMetadata } from './utils/sdkMetadata';
export { getTraceData } from './utils/traceData';
export { matchesTracePropagationTargets, shouldPropagateTraceForUrl } from './utils/tracePropagationTargets';
export { getTraceMetaTags } from './utils/meta';
export { _INTERNAL_CACHE_ORIGIN_META_TAG_NAME } from './utils/cacheOrigin';
export { _INTERNAL_CACHE_ORIGIN_META_TAG_NAME, _INTERNAL_RENDER_START_META_TAG_NAME } from './utils/cacheOrigin';
export { getTraceServerTimingHeader, isSharedCacheable } from './utils/serverTiming';
export { debounce } from './utils/debounce';
export { uniq } from './utils/array';
Expand Down
6 changes: 5 additions & 1 deletion packages/core/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,11 @@ export { wrapMcpServerWithSentry } from './integrations/mcp-server';
export { isNodeEnv, loadModule } from './utils/node';
export { filenameIsInApp, node, nodeStackLineParser } from './utils/node-stack-trace';
export { vercelWaitUntil } from './utils/vercelWaitUntil';
export { _INTERNAL_getCacheOriginMetaTag, _INTERNAL_getRootSpanSentryTrace } from './utils/cacheOrigin';
export {
_INTERNAL_getCacheOriginMetaTag,
_INTERNAL_getRenderStart,
_INTERNAL_getRootSpanSentryTrace,
} from './utils/cacheOrigin';
export {
getReleaseFromCommitEnv as _INTERNAL_getReleaseFromCommitEnv,
readCommitEnvVar as _INTERNAL_readCommitEnvVar,
Expand Down
51 changes: 46 additions & 5 deletions packages/core/src/utils/cacheOrigin.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { PAGE_SERVER_RENDERING_TYPE, SENTRY_LINK_TYPE } from '@sentry/conventions/attributes';
import type { SpanLink } from '../types/link';
import type { Span } from '../types/span';
import type { StartSpanOptions } from '../types/startSpanOptions';
import { getActiveSpan, getRootSpan, TRACE_FLAG_SAMPLED } from './spanUtils';
import { getActiveSpan, getRootSpan, spanToJSON, TRACE_FLAG_SAMPLED } from './spanUtils';
import { browserPerformanceTimeOrigin } from './time';
import { getTraceData } from './traceData';
import { extractTraceparentData } from './tracing';
import { GLOBAL_OBJ } from './worldwide';
Expand All @@ -16,6 +18,15 @@ import { GLOBAL_OBJ } from './worldwide';
*/
export const _INTERNAL_CACHE_ORIGIN_META_TAG_NAME = 'sentry-cache-origin';

/**
* Holds the start of the render of a cached or prerendered page, as UNIX time in whole seconds. The server writes it
* next to `sentry-cache-origin`, also when that tag is empty. The client turns it into `page.server_rendering.age`.
*/
export const _INTERNAL_RENDER_START_META_TAG_NAME = 'sentry-render-start';

// TODO: Use the constant from `@sentry/conventions` when https://github.com/getsentry/sentry-conventions/pull/799 is released.
const PAGE_SERVER_RENDERING_AGE = 'page.server_rendering.age';

/** Returns the `sentry-cache-origin` meta tag. Without `renderTrace`, the tag is empty. */
export function _INTERNAL_getCacheOriginMetaTag(renderTrace: string | undefined): string {
return `<meta name="${_INTERNAL_CACHE_ORIGIN_META_TAG_NAME}" content="${renderTrace ?? ''}"/>`;
Expand All @@ -30,9 +41,19 @@ export function _INTERNAL_getRootSpanSentryTrace(): string | undefined {
return getTraceData({ span: activeSpan && getRootSpan(activeSpan) })['sentry-trace'];
}

/**
* Returns the start of a render as UNIX time in whole seconds: the start of `span` if it records, because the
* `cache_origin` link points to it, else `startWithoutSpan` (in seconds), for example in the build or in an unsampled
* request. The age needs no more precision, and whole seconds make the server clock harder to fingerprint.
*/
export function _INTERNAL_getRenderStart(span: Span | undefined, startWithoutSpan: number): number {
return Math.floor(span?.isRecording() ? spanToJSON(span).start_timestamp : startWithoutSpan);
}

/**
* Sets `page.server_rendering.type` on a pageload whose document has a `sentry-cache-origin` tag, even an
* empty one. Links the render if the tag holds the `sentry-trace` of a sampled render. Pass `undefined` as
* empty one. Links the render if the tag holds the `sentry-trace` of a sampled render, and sets
* `page.server_rendering.age` if the document has a `sentry-render-start` tag. Pass `undefined` as
* `serverRenderingType` while the type is not known.
*
* Call it before the span starts, so `tracesSampler` and `ignoreSpans` see the attribute.
Expand All @@ -41,24 +62,44 @@ export function _INTERNAL_withCacheOrigin(
spanOptions: StartSpanOptions,
serverRenderingType: 'static' | 'partially_static' | undefined,
): StartSpanOptions {
const renderTrace = (GLOBAL_OBJ as { document?: Document }).document
?.querySelector(`meta[name="${_INTERNAL_CACHE_ORIGIN_META_TAG_NAME}"]`)
?.getAttribute('content');
const renderTrace = getMetaTagContent(_INTERNAL_CACHE_ORIGIN_META_TAG_NAME);
if (renderTrace == null) {
return spanOptions;
}

const link = getSampledRenderLink(renderTrace);
const renderAge = getRenderAge(getMetaTagContent(_INTERNAL_RENDER_START_META_TAG_NAME));
return {
...spanOptions,
attributes: {
...spanOptions.attributes,
...(serverRenderingType && { [PAGE_SERVER_RENDERING_TYPE]: serverRenderingType }),
...(renderAge !== undefined && { [PAGE_SERVER_RENDERING_AGE]: renderAge }),
},
...(link && { links: [...(spanOptions.links ?? []), link] }),
};
}

function getMetaTagContent(name: string): string | null | undefined {
return (GLOBAL_OBJ as { document?: Document }).document
?.querySelector(`meta[name="${name}"]`)
?.getAttribute('content');
}

// Cap at 1 year. It's unlikely that an older server time is still correct
const MAX_RENDER_AGE = 365 * 24 * 60 * 60;

// A pageload starts at the time origin. The server and the browser clocks differ, so a render shortly before the
// pageload can seem to start after it.
function getRenderAge(renderStart: string | null | undefined): number | undefined {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This only clamps the lower bound, a sentry-render-start of 0 gives page.server_rendering.age: 1794834076. Should we generally drop attributes that seem implausible? Maybe cap it at a year or whatever?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's very unlikely that this happens (I hope the server clocks are quite correct :D). But it's fair to cap it. 1 year seems fine.

const timeOrigin = browserPerformanceTimeOrigin();
if (!renderStart || !/^\d+$/.test(renderStart) || !timeOrigin) {
return undefined;
}
const age = Math.max(0, Math.floor(timeOrigin / 1000 - Number(renderStart)));
return age <= MAX_RENDER_AGE ? age : undefined;
}

// Only a sampled render sent spans, so a link to any other render would point at nothing.
function getSampledRenderLink(renderTrace: string): SpanLink | undefined {
const traceparent = extractTraceparentData(renderTrace);
Expand Down
Loading
Loading