UNPKG

@sentry/node

Version:

Sentry Node SDK using OpenTelemetry for performance instrumentation

66 lines (65 loc) • 3.25 kB
import { DEBUG_BUILD } from "../debug-build.js"; import { debug, getTraceServerTimingHeader, isObjectLike, isSharedCacheable } from "@sentry/core"; // src/utils/serverTimingHeader.ts const SERVER_TIMING_HEADER = "server-timing"; function isHttpServerResponse(response) { return isObjectLike(response) && typeof response.writeHead === "function" && typeof response.getHeader === "function" && typeof response.setHeader === "function"; } /** * Sends the trace of the request in a `Server-Timing` header of a document response of the Node.js * server, so the browser SDK continues it in the pageload span before any HTML arrived. Next.js * flushes the headers with the first chunk of the document, inside the request span, so the value is * computed then rather than when the request comes in. The hook fires for every request, also when * incoming request spans are disabled, which the Next.js SDK does because Next.js starts the request * span. */ function addServerTimingTraceHeader(client) { client.on("httpServerRequest", (_request, response) => { if (!isHttpServerResponse(response)) return; const writeHead = response.writeHead.bind(response); response.writeHead = (...args) => { setServerTimingTraceHeader(response, getHeaderObjectArgument(args)); return writeHead(...args); }; }); } function getHeaderObjectArgument(args) { const last = args[args.length - 1]; return args.length > 1 && isObjectLike(last) && !Array.isArray(last) ? last : void 0; } function findHeaderKey(headers, name) { return headers && Object.keys(headers).find((key) => key.toLowerCase() === name); } function readHeader(response, headers, name) { const key = findHeaderKey(headers, name); const value = key ? headers?.[key] : response.getHeader(name); return Array.isArray(value) ? value.join(", ") : value?.toString(); } function isDocument(response, headers) { return !!readHeader(response, headers, "content-type")?.toLowerCase().startsWith("text/html"); } /** * Adds the trace of the current context to the `Server-Timing` header of a document response that no * shared cache may store. Next.js sends static and ISR documents with `s-maxage`, and a CDN in front of * the server that stores them would replay the trace, and the sampling decision, of one visitor to * everyone who gets the cached copy. An existing `sentry-trace` entry of the app is left alone. * * @internal */ function setServerTimingTraceHeader(response, headers) { try { if (response.headersSent || isSharedCacheable((name) => readHeader(response, headers, name)) || !isDocument(response, headers)) return; const existingValue = readHeader(response, headers, SERVER_TIMING_HEADER); if (existingValue?.includes("sentry-trace")) return; const traceHeader = getTraceServerTimingHeader(); if (!traceHeader) return; const value = existingValue ? `${existingValue}, ${traceHeader}` : traceHeader; const key = findHeaderKey(headers, SERVER_TIMING_HEADER); if (headers && key) headers[key] = value; else response.setHeader("Server-Timing", value); } catch (e) { DEBUG_BUILD && debug.warn("Failed to add the trace to the Server-Timing header", e); } } export { addServerTimingTraceHeader, setServerTimingTraceHeader }; //# sourceMappingURL=serverTimingHeader.js.map