UNPKG

@sentry/node

Version:

Sentry Node SDK using OpenTelemetry for performance instrumentation

217 lines (216 loc) • 10.4 kB
import { addFetchRequestBreadcrumb, addTracePropagationHeadersToFetchRequest } from "../../utils/outgoingFetchRequest.js"; import { DEBUG_BUILD } from "../../debug-build.js"; import { HTTP_REQUEST_METHOD, HTTP_RESPONSE_STATUS_CODE, NETWORK_PEER_ADDRESS, NETWORK_PEER_PORT, SENTRY_KIND, SENTRY_OP, SENTRY_ORIGIN, SERVER_ADDRESS, SERVER_PORT, URL_DOMAIN, URL_FRAGMENT, URL_FULL, URL_PATH, URL_QUERY, URL_SCHEME, USER_AGENT_ORIGINAL } from "@sentry/conventions/attributes"; import { HTTP_SPAN_NAME_FALLBACK, LRUMap, SEMANTIC_ATTRIBUTE_SENTRY_CUSTOM_SPAN_NAME, SPAN_STATUS_ERROR, debug, filterCollectedUrl, filterCollectedUrlFragment, filterCollectedUrlQuery, getClient, getSanitizedUrlString, getSpanStatusFromHttpCode, getUrlFragment, getUrlQuery, hasSpanStreamingEnabled, httpHeadersToSpanAttributes, isSentryRequestUrl, isTracingSuppressed, parseUrl, safeCallback, startInactiveSpan, stripDataUrlContent } from "@sentry/core"; import { subscribeDiagnosticsChannel } from "@sentry/server-utils"; import { HTTP_CLIENT } from "@sentry/conventions/op"; import { URL } from "url"; // src/integrations/node-fetch/undici-instrumentation.ts const ATTR_HTTP_REQUEST_METHOD_ORIGINAL = "http.request.method_original"; let _isInstrumented = false; const spanFromReq = /* @__PURE__ */ new WeakMap(); const ignoreRequestMap = /* @__PURE__ */ new WeakMap(); const propagationDecisionMap = new LRUMap(100); /** * Instrument outgoing HTTP requests made through `undici` or the global `fetch` API: emit `http.client` * spans, record breadcrumbs, and propagate traces into the outgoing request headers. * * Span creation is gated by the `spans` option (defaults to `true`). When spans are disabled, breadcrumbs * are still recorded and trace propagation headers are still injected (gated by `tracePropagation`). * * undici reports its request lifecycle via `diagnostics_channel`, so rather than patching any module we * subscribe to those channels directly. This is idempotent — subsequent calls are no-ops once the * channels have been subscribed to, and the config of the first call wins. * * A combination of https://github.com/elastic/apm-agent-nodejs and * https://github.com/gadget-inc/opentelemetry-instrumentations/blob/main/packages/opentelemetry-instrumentation-undici/src/index.ts */ function instrumentUndici(config = {}) { if (_isInstrumented) return; _isInstrumented = true; subscribeToChannel("undici:request:create", (message) => onRequestCreated(config, message)); subscribeToChannel("undici:client:sendHeaders", (message) => onRequestHeaders(config, message)); subscribeToChannel("undici:request:headers", (message) => onResponseHeaders(config, message)); subscribeToChannel("undici:request:trailers", (message) => onDone(message)); subscribeToChannel("undici:request:error", (message) => onError(message)); } function subscribeToChannel(diagnosticChannel, onMessage) { subscribeDiagnosticsChannel(diagnosticChannel, onMessage); } function parseRequestHeaders(request) { const result = /* @__PURE__ */ new Map(); if (Array.isArray(request.headers)) for (let i = 0; i < request.headers.length; i += 2) { const key = request.headers[i]; const value = request.headers[i + 1]; if (typeof key === "string" && value !== void 0) result.set(key.toLowerCase(), value); } else if (typeof request.headers === "string") { const headers = request.headers.split("\r\n"); for (const line of headers) { if (!line) continue; const colonIndex = line.indexOf(":"); if (colonIndex === -1) continue; const key = line.substring(0, colonIndex).toLowerCase(); const value = line.substring(colonIndex + 1).trim(); const allValues = result.get(key); if (allValues && Array.isArray(allValues)) allValues.push(value); else if (allValues) result.set(key, [allValues, value]); else result.set(key, value); } } return result; } function onRequestCreated(config, { request }) { const url = getAbsoluteUrl(request.origin, request.path); const isIgnored = isSentryRequestUrl(url, getClient()) || safeCallback(DEBUG_BUILD ? "The `ignoreOutgoingRequests` callback threw an error, not ignoring the request:" : "", () => !!config.ignoreOutgoingRequests?.(url), () => false); const ignoreForBreadcrumbs = isTracingSuppressed() || isIgnored; ignoreRequestMap.set(request, ignoreForBreadcrumbs); if (!config.spans) { if (config.tracePropagation !== false && !ignoreForBreadcrumbs) addTracePropagationHeadersToFetchRequest(request, propagationDecisionMap); return; } if (request.method === "CONNECT" || isIgnored) return; let requestUrl; try { requestUrl = new URL(request.path, request.origin); } catch (err) { DEBUG_BUILD && debug.warn("could not determine url.full:", err); return; } const urlScheme = requestUrl.protocol.replace(":", ""); const requestMethod = getRequestMethod(request.method); const attributes = { [SENTRY_KIND]: "client", [SENTRY_OP]: HTTP_CLIENT, [HTTP_REQUEST_METHOD]: requestMethod, [ATTR_HTTP_REQUEST_METHOD_ORIGINAL]: request.method, [URL_FULL]: filterCollectedUrl(requestUrl.toString()), [URL_DOMAIN]: requestUrl.hostname || void 0, [URL_PATH]: requestUrl.pathname, [URL_QUERY]: filterCollectedUrlQuery(getUrlQuery(requestUrl.search)), [URL_FRAGMENT]: filterCollectedUrlFragment(getUrlFragment(requestUrl.hash)), [URL_SCHEME]: urlScheme, [SENTRY_ORIGIN]: "auto.http.node_fetch" }; if (url.startsWith("data:")) { const sanitizedUrl = stripDataUrlContent(url); attributes[URL_FULL] = sanitizedUrl; attributes[SEMANTIC_ATTRIBUTE_SENTRY_CUSTOM_SPAN_NAME] = `${request.method || "GET"} ${sanitizedUrl}`; } const schemePorts = { https: "443", http: "80" }; const serverAddress = requestUrl.hostname; const serverPort = requestUrl.port || schemePorts[urlScheme]; attributes[SERVER_ADDRESS] = serverAddress; if (serverPort && !isNaN(Number(serverPort))) attributes[SERVER_PORT] = Number(serverPort); const userAgentValues = parseRequestHeaders(request).get("user-agent"); if (userAgentValues) { const userAgent = Array.isArray(userAgentValues) ? userAgentValues[userAgentValues.length - 1] : userAgentValues; attributes[USER_AGENT_ORIGINAL] = userAgent; } const isDataUrl = url.startsWith("data:"); const client = getClient(); let spanName; if (requestMethod === "_OTHER") spanName = HTTP_SPAN_NAME_FALLBACK; else if (!!client && hasSpanStreamingEnabled(client)) spanName = requestUrl.hostname ? `${requestMethod} ${requestUrl.hostname}` : requestMethod; else if (isDataUrl) spanName = `${request.method || "GET"} ${stripDataUrlContent(url)}`; else spanName = `${requestMethod} ${getSanitizedUrlString(parseUrl(requestUrl.toString()))}`; const span = startInactiveSpan({ name: spanName, attributes, onlyIfParent: !client || !hasSpanStreamingEnabled(client) }); safeCallback(DEBUG_BUILD ? "The `requestHook` callback threw an error:" : "", () => config.requestHook?.(span, request), () => void 0); addTracePropagationHeadersToFetchRequest(request, propagationDecisionMap, span); spanFromReq.set(request, span); } function onRequestHeaders(config, { request, socket }) { const span = spanFromReq.get(request); if (!span) return; const { remoteAddress, remotePort } = socket; const spanAttributes = { [NETWORK_PEER_ADDRESS]: remoteAddress, [NETWORK_PEER_PORT]: remotePort }; const client = getClient(); if (config.headersToSpanAttributes?.requestHeaders && client) { const headersToAttribs = new Set(config.headersToSpanAttributes.requestHeaders.map((n) => n.toLowerCase())); const headersMap = parseRequestHeaders(request); const allowlisted = {}; for (const [name, value] of headersMap.entries()) if (headersToAttribs.has(name)) allowlisted[name] = value; Object.assign(spanAttributes, httpHeadersToSpanAttributes(allowlisted, client.getDataCollectionOptions())); } span.setAttributes(spanAttributes); } function onResponseHeaders(config, { request, response }) { if (config.breadcrumbs !== false && !ignoreRequestMap.get(request)) addFetchRequestBreadcrumb(request, response); const span = spanFromReq.get(request); if (!span) return; const spanAttributes = { [HTTP_RESPONSE_STATUS_CODE]: response.statusCode }; safeCallback(DEBUG_BUILD ? "The `responseHook` callback threw an error:" : "", () => config.responseHook?.(span, { request, response }), () => void 0); const client = getClient(); if (config.headersToSpanAttributes?.responseHeaders && client) { const headersToAttribs = /* @__PURE__ */ new Set(); config.headersToSpanAttributes?.responseHeaders.forEach((name) => headersToAttribs.add(name.toLowerCase())); const allowlisted = {}; for (let idx = 0; idx < response.headers.length; idx = idx + 2) { const nameBuf = response.headers[idx]; const valueBuf = response.headers[idx + 1]; if (nameBuf === void 0 || valueBuf === void 0) continue; const name = nameBuf.toString().toLowerCase(); if (headersToAttribs.has(name)) (allowlisted[name] ?? (allowlisted[name] = [])).push(valueBuf.toString()); } Object.assign(spanAttributes, httpHeadersToSpanAttributes(allowlisted, client.getDataCollectionOptions(), "response")); } span.setAttributes(spanAttributes); if (response.statusCode >= 400) span.setStatus(getSpanStatusFromHttpCode(response.statusCode)); } function onDone({ request }) { const span = spanFromReq.get(request); if (!span) return; span.end(); spanFromReq.delete(request); } function onError({ request, error }) { const span = spanFromReq.get(request); if (!span) return; if (error.name === "AbortError" || typeof DOMException !== "undefined" && error instanceof DOMException && error.code === DOMException.ABORT_ERR) { span.end(); spanFromReq.delete(request); return; } span.setStatus({ code: SPAN_STATUS_ERROR, message: error.message }); span.end(); spanFromReq.delete(request); } function getRequestMethod(original) { if (original.toUpperCase() in { CONNECT: true, OPTIONS: true, HEAD: true, GET: true, POST: true, PUT: true, PATCH: true, DELETE: true, TRACE: true, QUERY: true }) return original.toUpperCase(); return "_OTHER"; } function getAbsoluteUrl(origin, path = "/") { 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 { instrumentUndici }; //# sourceMappingURL=undici-instrumentation.js.map