@sentry/node
Version:
Sentry Node SDK using OpenTelemetry for performance instrumentation
163 lines (162 loc) • 7.25 kB
JavaScript
import { HTTP_REQUEST_METHOD, URL_FRAGMENT, URL_QUERY } from "@sentry/conventions/attributes";
import { addBreadcrumb, debug, filterCollectedUrlFragment, filterCollectedUrlQuery, getActiveSpan, getBreadcrumbLogLevelFromHttpStatusCode, getClient, getSanitizedUrlString, getTraceData, getUrlFragment, getUrlQuery, mergeBaggageHeaders, parseUrl, shouldPropagateTraceForUrl, spanIsIgnored, withActiveSpan } from "@sentry/core";
// src/utils/outgoingFetchRequest.ts
const SENTRY_TRACE_HEADER = "sentry-trace";
const SENTRY_BAGGAGE_HEADER = "baggage";
const W3C_TRACEPARENT_HEADER = "traceparent";
/**
* Add trace propagation headers to an outgoing fetch/undici request.
*
* Checks if the request URL matches trace propagation targets,
* then injects sentry-trace, traceparent, and baggage headers.
*
* When a `span` is passed (the outgoing `http.client` span), its trace data is propagated so downstream
* services are parented to that span. Without a span, the active scope's trace data is used.
*
* Existing trace headers (e.g. set manually by the user via `getTraceData()`) always take precedence and
* are de-duplicated rather than overwritten, so we never emit two `sentry-trace`/`baggage` entries.
*/
function addTracePropagationHeadersToFetchRequest(request, propagationDecisionMap, span) {
const url = getAbsoluteUrl(request.origin, request.path);
const { tracePropagationTargets, propagateTraceparent } = getClient()?.getOptions() || {};
if (!shouldPropagateTraceForUrl(url, tracePropagationTargets, propagationDecisionMap)) return;
const spanForTraceHeaders = span && spanIsIgnored(span) && getActiveSpan() ? void 0 : span;
const addedHeaders = spanForTraceHeaders ? withActiveSpan(spanForTraceHeaders, () => getTraceData({ propagateTraceparent })) : getTraceData({ propagateTraceparent });
if (!addedHeaders) return;
const { "sentry-trace": sentryTrace, baggage, traceparent } = addedHeaders;
const requestHeaders = Array.isArray(request.headers) ? normalizeUndiciHeaderPairs(request.headers) : stringToArrayHeaders(request.headers);
_deduplicateArrayHeader(requestHeaders, SENTRY_TRACE_HEADER);
_deduplicateArrayHeader(requestHeaders, SENTRY_BAGGAGE_HEADER);
if (propagateTraceparent) _deduplicateArrayHeader(requestHeaders, W3C_TRACEPARENT_HEADER);
if (!(_findExistingHeaderIndex(requestHeaders, SENTRY_TRACE_HEADER) !== -1)) {
if (sentryTrace) requestHeaders.push(SENTRY_TRACE_HEADER, sentryTrace);
if (traceparent && _findExistingHeaderIndex(requestHeaders, "traceparent") === -1) requestHeaders.push("traceparent", traceparent);
const existingBaggageIndex = _findExistingHeaderIndex(requestHeaders, SENTRY_BAGGAGE_HEADER);
if (baggage && existingBaggageIndex === -1) requestHeaders.push(SENTRY_BAGGAGE_HEADER, baggage);
else if (baggage) {
const existingBaggageValue = requestHeaders[existingBaggageIndex + 1];
const merged = mergeBaggageHeaders(existingBaggageValue, baggage);
if (merged) requestHeaders[existingBaggageIndex + 1] = merged;
}
}
if (Array.isArray(request.headers)) request.headers.splice(0, request.headers.length, ...requestHeaders);
else request.headers = arrayToStringHeaders(requestHeaders);
}
/**
* Convert undici’s header array into `[name, value, name, value, ...]` where every value is a string.
*
* Undici v6 uses this shape: `[k1, v1, k2, v2, ...]`. Types allow each `v` to be `string | string[]` when
* that header has multiple values. Sentry’s dedupe/merge helpers expect one string per value slot, so
* multi-value arrays are joined with `', '`. Missing value slots become `''`.
*/
function normalizeUndiciHeaderPairs(headers) {
const out = [];
for (let i = 0; i < headers.length; i++) {
const entry = headers[i];
if (i % 2 === 0) out.push(typeof entry === "string" ? entry : String(entry));
else out.push(Array.isArray(entry) ? entry.join(", ") : entry ?? "");
}
return out;
}
function stringToArrayHeaders(requestHeaders) {
const headersArray = requestHeaders.split("\r\n");
const headers = [];
for (const header of headersArray) try {
const colonIndex = header.indexOf(":");
if (colonIndex === -1) continue;
const key = header.slice(0, colonIndex).trim();
const value = header.slice(colonIndex + 1).trim();
if (key) headers.push(key, value);
} catch {
debug.warn(`Failed to convert string request header to array header: ${header}`);
}
return headers;
}
function arrayToStringHeaders(headers) {
const headerPairs = [];
for (let i = 0; i < headers.length; i += 2) {
const key = headers[i];
const value = headers[i + 1];
if (!key || value == null) continue;
headerPairs.push(`${key}: ${value}`);
}
if (!headerPairs.length) return "";
return headerPairs.join("\r\n").concat("\r\n");
}
/**
* For a given header name, if there are multiple entries in the [key, value, key, value, ...] array,
* keep the first entry and remove the rest.
* For baggage, values are merged to preserve all entries but to dedupe sentry- values, and always
* keep the first occurrence of them
*/
function _deduplicateArrayHeader(headers, headerName) {
let firstIndex = -1;
for (let i = 0; i < headers.length; i += 2) {
if (headers[i] !== headerName) continue;
if (firstIndex === -1) {
firstIndex = i;
continue;
}
const firstHeaderValue = headers[firstIndex + 1];
if (headerName === SENTRY_BAGGAGE_HEADER && firstHeaderValue) {
const merged = mergeBaggageHeaders(headers[i + 1], firstHeaderValue);
if (merged) headers[firstIndex + 1] = merged;
}
headers.splice(i, 2);
i -= 2;
}
}
/**
* Find the index of an existing header in an array of headers.
* Only take even indices, because headers are in format [key_0, value_0, key_1, value_1, ...]
* otherwise we could match a header _value_ with @param name
*/
function _findExistingHeaderIndex(headers, name) {
return headers.findIndex((header, i) => i % 2 === 0 && header === name);
}
/** Add a breadcrumb for an outgoing fetch/undici request. */
function addFetchRequestBreadcrumb(request, response) {
const data = getBreadcrumbData(request);
const statusCode = response.statusCode;
const level = getBreadcrumbLogLevelFromHttpStatusCode(statusCode);
addBreadcrumb({
category: "http",
data: {
status_code: statusCode,
...data
},
type: "http",
level
}, {
event: "response",
request,
response
});
}
function getBreadcrumbData(request) {
try {
const url = getAbsoluteUrl(request.origin, request.path);
const parsedUrl = parseUrl(url);
return {
url: getSanitizedUrlString(parsedUrl),
[HTTP_REQUEST_METHOD]: request.method || "GET",
[URL_QUERY]: filterCollectedUrlQuery(getUrlQuery(parsedUrl.search)),
[URL_FRAGMENT]: filterCollectedUrlFragment(getUrlFragment(parsedUrl.hash))
};
} catch {
return {};
}
}
/** Get the absolute URL from an origin and path. */
function getAbsoluteUrl(origin, path = "/") {
try {
return new URL(path, origin).toString();
} catch {
const url = `${origin}`;
if (url.endsWith("/") && path.startsWith("/")) return `${url}${path.slice(1)}`;
if (!url.endsWith("/") && !path.startsWith("/")) return `${url}/${path}`;
return `${url}${path}`;
}
}
export { addFetchRequestBreadcrumb, addTracePropagationHeadersToFetchRequest, getAbsoluteUrl };
//# sourceMappingURL=outgoingFetchRequest.js.map