UNPKG

@sentry/core

Version:
96 lines (95 loc) • 4.88 kB
import { getActiveSpan, getRootSpan, spanToJSON } from "./spanUtils.js"; import { browserPerformanceTimeOrigin } from "./time.js"; import { getTraceData } from "./traceData.js"; import { extractTraceparentData } from "./tracing.js"; import { GLOBAL_OBJ } from "./worldwide.js"; import { PAGE_SERVER_RENDERING_AGE, PAGE_SERVER_RENDERING_TYPE, SENTRY_LINK_TYPE } from "@sentry/conventions/attributes"; // src/utils/cacheOrigin.ts /** * Replaces `sentry-trace` and `baggage` in a cached or prerendered page, so that no visitor continues the * trace of the render. * * - No tag: the server rendered the page for this request, or rendered no content (a client-side shell) * - Empty `content`: there is no trace of the render to link, for example for a page prerendered in the build * - A `sentry-trace` value: the client links the render */ 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`. */ const _INTERNAL_RENDER_START_META_TAG_NAME = "sentry-render-start"; /** * Returns the `sentry-cache-origin` meta tag and the `sentry-render-start` meta tag for the request that rendered the * page. Without `renderTrace`, the `sentry-cache-origin` tag is empty. `requestStart` is in seconds. */ function _INTERNAL_getCacheOriginMetaTags(renderTrace, requestStart) { const renderStart = _INTERNAL_getRenderStart(getActiveRootSpan(), requestStart); return [`<meta name="${_INTERNAL_CACHE_ORIGIN_META_TAG_NAME}" content="${renderTrace ?? ""}"/>`, `<meta name="${_INTERNAL_RENDER_START_META_TAG_NAME}" content="${renderStart}"/>`].join("\n"); } /** * Returns the `sentry-trace` of the request's root span, which a cached page links as its render. Unlike * `getTraceData()`, it ignores an active child span, such as SvelteKit's `sveltekit.resolve`. */ function _INTERNAL_getRootSpanSentryTrace() { return getTraceData({ span: getActiveRootSpan() })["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. */ function _INTERNAL_getRenderStart(span, startWithoutSpan) { return Math.floor(span?.isRecording() ? spanToJSON(span).start_timestamp : startWithoutSpan); } function getActiveRootSpan() { const activeSpan = getActiveSpan(); return activeSpan && getRootSpan(activeSpan); } /** * 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, 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. */ function _INTERNAL_withCacheOrigin(spanOptions, serverRenderingType) { 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 !== void 0 && { [PAGE_SERVER_RENDERING_AGE]: renderAge } }, ...link && { links: [...spanOptions.links ?? [], link] } }; } function getMetaTagContent(name) { return GLOBAL_OBJ.document?.querySelector(`meta[name="${name}"]`)?.getAttribute("content"); } const MAX_RENDER_AGE = 31536e3; function getRenderAge(renderStart) { const timeOrigin = browserPerformanceTimeOrigin(); if (!renderStart || !/^\d+$/.test(renderStart) || !timeOrigin) return; const age = Math.max(0, Math.floor(timeOrigin / 1e3 - Number(renderStart))); return age <= MAX_RENDER_AGE ? age : void 0; } function getSampledRenderLink(renderTrace) { const traceparent = extractTraceparentData(renderTrace); if (!traceparent?.traceId || !traceparent.parentSpanId || !traceparent.parentSampled) return; return { context: { traceId: traceparent.traceId, spanId: traceparent.parentSpanId, traceFlags: 1 }, attributes: { [SENTRY_LINK_TYPE]: "cache_origin" } }; } export { _INTERNAL_CACHE_ORIGIN_META_TAG_NAME, _INTERNAL_RENDER_START_META_TAG_NAME, _INTERNAL_getCacheOriginMetaTags, _INTERNAL_getRenderStart, _INTERNAL_getRootSpanSentryTrace, _INTERNAL_withCacheOrigin }; //# sourceMappingURL=cacheOrigin.js.map