UNPKG

@sentry/node

Version:

Sentry Node SDK using OpenTelemetry for performance instrumentation

62 lines (61 loc) • 2.95 kB
import { NODE_VERSION } from "../../nodeVersion.js"; import { isTracingSuppressed } from "@sentry/core"; import { HTTP_ON_CLIENT_REQUEST, getHttpClientSubscriptions, getRequestOptions, patchHttpModuleClient } from "@sentry/core/server"; import { context, trace } from "@opentelemetry/api"; import { subscribeDiagnosticsChannel } from "@sentry/server-utils"; import { errorMonitor } from "node:events"; import * as http from "node:http"; // src/integrations/http/SentryHttpInstrumentation.ts const FULLY_SUPPORTS_HTTP_DIAGNOSTICS_CHANNEL = NODE_VERSION.major === 22 && NODE_VERSION.minor >= 12 || NODE_VERSION.major === 23 && NODE_VERSION.minor >= 2 || NODE_VERSION.major >= 24; /** * This instruments the http modules for outgoing requests. * It uses the diagnostics channel if available, otherwise it falls back to monkey-patching. * * The instrumentation will start spans, create breadcrumbs, and propagate trace headers in outgoing requests (depending on the settings). */ function instrumentHttpOutgoingRequests(instrumentationOptions = {}) { const { outgoingRequestApplyCustomAttributes: applyCustomAttributesOnSpan, ...options } = instrumentationOptions; const patchOptions = { applyCustomAttributesOnSpan, ...options, tracePropagation: options.tracePropagation ?? true, spans: options.spans ?? true, ignoreOutgoingRequests(url, request) { return isTracingSuppressed() || !!options.ignoreOutgoingRequests?.(url, getRequestOptions(request)); }, outgoingRequestHook(span, request) { options.outgoingRequestHook?.(span, request); const originalOnce = request.once; request.once = new Proxy(originalOnce, { apply(target, thisArg, args) { const [event] = args; if (event !== "response") return target.apply(thisArg, args); const parentContext = context.active(); const requestContext = trace.setSpan(parentContext, span); return context.with(requestContext, () => { return target.apply(thisArg, args); }); } }); }, outgoingResponseHook(span, response) { options.outgoingResponseHook?.(span, response); }, errorMonitor, http }; if (FULLY_SUPPORTS_HTTP_DIAGNOSTICS_CHANNEL) instrumentHttpOutgoingRequestsViaChannel(patchOptions); else instrumentHttpOutgoingRequestsViaMonkeyPatching(patchOptions); } function instrumentHttpOutgoingRequestsViaChannel(options) { const { [HTTP_ON_CLIENT_REQUEST]: onHttpClientRequestCreated } = getHttpClientSubscriptions(options); subscribeDiagnosticsChannel(HTTP_ON_CLIENT_REQUEST, onHttpClientRequestCreated); } /** * Instrument outgoing HTTP(S) requests by old-school monkey-patching the `http` module. * This is the fallback for runtimes that do not fully support the `node:http` client diagnostics channel * (Node < 22.12). */ function instrumentHttpOutgoingRequestsViaMonkeyPatching(options) { patchHttpModuleClient(http, options); } export { instrumentHttpOutgoingRequests }; //# sourceMappingURL=SentryHttpInstrumentation.js.map