{"version":3,"file":"SentryHttpInstrumentation.js","sources":["../../../../src/integrations/http/SentryHttpInstrumentation.ts"],"sourcesContent":["import type { ChannelListener } from 'node:diagnostics_channel';\nimport { subscribe, unsubscribe } from 'node:diagnostics_channel';\nimport type * as http from 'node:http';\nimport type * as https from 'node:https';\nimport { context } from '@opentelemetry/api';\nimport { isTracingSuppressed } from '@opentelemetry/core';\nimport type { InstrumentationConfig } from '@opentelemetry/instrumentation';\nimport { InstrumentationBase, InstrumentationNodeModuleDefinition } from '@opentelemetry/instrumentation';\nimport type { Span } from '@sentry/core';\nimport { debug, LRUMap, SDK_VERSION } from '@sentry/core';\nimport { DEBUG_BUILD } from '../../debug-build';\nimport { getRequestUrl } from '../../utils/getRequestUrl';\nimport { INSTRUMENTATION_NAME } from './constants';\nimport {\n addRequestBreadcrumb,\n addTracePropagationHeadersToOutgoingRequest,\n getRequestOptions,\n} from './outgoing-requests';\n\ntype Http = typeof http;\ntype Https = typeof https;\n\nexport type SentryHttpInstrumentationOptions = InstrumentationConfig & {\n /**\n * Whether breadcrumbs should be recorded for outgoing requests.\n *\n * @default `true`\n */\n breadcrumbs?: boolean;\n\n /**\n * Whether to propagate Sentry trace headers in outgoing requests.\n * By default this is done by the HttpInstrumentation, but if that is not added (e.g. because tracing is disabled)\n * then this instrumentation can take over.\n *\n * @default `false`\n */\n propagateTraceInOutgoingRequests?: boolean;\n\n /**\n * Do not capture breadcrumbs for outgoing HTTP requests to URLs where the given callback returns `true`.\n * For the scope of this instrumentation, this callback only controls breadcrumb creation.\n * The same option can be passed to the top-level httpIntegration where it controls both, breadcrumb and\n * span creation.\n *\n * @param url Contains the entire URL, including query string (if any), protocol, host, etc. of the outgoing request.\n * @param request Contains the {@type RequestOptions} object used to make the outgoing request.\n */\n ignoreOutgoingRequests?: (url: string, request: http.RequestOptions) => boolean;\n\n // All options below do not do anything anymore in this instrumentation, and will be removed in the future.\n // They are only kept here for backwards compatibility - the respective functionality is now handled by the httpServerIntegration/httpServerSpansIntegration.\n\n /**\n * @deprecated This no longer does anything.\n */\n spans?: boolean;\n\n /**\n * @depreacted This no longer does anything.\n */\n extractIncomingTraceFromHeader?: boolean;\n\n /**\n * @deprecated This no longer does anything.\n */\n ignoreStaticAssets?: boolean;\n\n /**\n * @deprecated This no longer does anything.\n */\n disableIncomingRequestSpans?: boolean;\n\n /**\n * @deprecated This no longer does anything.\n */\n ignoreSpansForIncomingRequests?: (urlPath: string, request: http.IncomingMessage) => boolean;\n\n /**\n * @deprecated This no longer does anything.\n */\n ignoreIncomingRequestBody?: (url: string, request: http.RequestOptions) => boolean;\n\n /**\n * @deprecated This no longer does anything.\n */\n maxIncomingRequestBodySize?: 'none' | 'small' | 'medium' | 'always';\n\n /**\n * @deprecated This no longer does anything.\n */\n trackIncomingRequestsAsSessions?: boolean;\n\n /**\n * @deprecated This no longer does anything.\n */\n instrumentation?: {\n requestHook?: (span: Span, req: http.ClientRequest | http.IncomingMessage) => void;\n responseHook?: (span: Span, response: http.IncomingMessage | http.ServerResponse) => void;\n applyCustomAttributesOnSpan?: (\n span: Span,\n request: http.ClientRequest | http.IncomingMessage,\n response: http.IncomingMessage | http.ServerResponse,\n ) => void;\n };\n\n /**\n * @deprecated This no longer does anything.\n */\n sessionFlushingDelayMS?: number;\n};\n\n/**\n * This custom HTTP instrumentation is used to isolate incoming requests and annotate them with additional information.\n * It does not emit any spans.\n *\n * The reason this is isolated from the OpenTelemetry instrumentation is that users may overwrite this,\n * which would lead to Sentry not working as expected.\n *\n * Important note: Contrary to other OTEL instrumentation, this one cannot be unwrapped.\n * It only does minimal things though and does not emit any spans.\n *\n * This is heavily inspired & adapted from:\n * https://github.com/open-telemetry/opentelemetry-js/blob/f8ab5592ddea5cba0a3b33bf8d74f27872c0367f/experimental/packages/opentelemetry-instrumentation-http/src/http.ts\n */\nexport class SentryHttpInstrumentation extends InstrumentationBase {\n private _propagationDecisionMap: LRUMap;\n private _ignoreOutgoingRequestsMap: WeakMap;\n\n public constructor(config: SentryHttpInstrumentationOptions = {}) {\n super(INSTRUMENTATION_NAME, SDK_VERSION, config);\n\n this._propagationDecisionMap = new LRUMap(100);\n this._ignoreOutgoingRequestsMap = new WeakMap();\n }\n\n /** @inheritdoc */\n public init(): [InstrumentationNodeModuleDefinition, InstrumentationNodeModuleDefinition] {\n // We register handlers when either http or https is instrumented\n // but we only want to register them once, whichever is loaded first\n let hasRegisteredHandlers = false;\n\n const onHttpClientResponseFinish = ((_data: unknown) => {\n const data = _data as { request: http.ClientRequest; response: http.IncomingMessage };\n this._onOutgoingRequestFinish(data.request, data.response);\n }) satisfies ChannelListener;\n\n const onHttpClientRequestError = ((_data: unknown) => {\n const data = _data as { request: http.ClientRequest };\n this._onOutgoingRequestFinish(data.request, undefined);\n }) satisfies ChannelListener;\n\n const onHttpClientRequestCreated = ((_data: unknown) => {\n const data = _data as { request: http.ClientRequest };\n this._onOutgoingRequestCreated(data.request);\n }) satisfies ChannelListener;\n\n const wrap = (moduleExports: T): T => {\n if (hasRegisteredHandlers) {\n return moduleExports;\n }\n\n hasRegisteredHandlers = true;\n\n subscribe('http.client.response.finish', onHttpClientResponseFinish);\n\n // When an error happens, we still want to have a breadcrumb\n // In this case, `http.client.response.finish` is not triggered\n subscribe('http.client.request.error', onHttpClientRequestError);\n\n // NOTE: This channel only exist since Node 22\n // Before that, outgoing requests are not patched\n // and trace headers are not propagated, sadly.\n if (this.getConfig().propagateTraceInOutgoingRequests) {\n subscribe('http.client.request.created', onHttpClientRequestCreated);\n }\n\n return moduleExports;\n };\n\n const unwrap = (): void => {\n unsubscribe('http.client.response.finish', onHttpClientResponseFinish);\n unsubscribe('http.client.request.error', onHttpClientRequestError);\n unsubscribe('http.client.request.created', onHttpClientRequestCreated);\n };\n\n /**\n * You may be wondering why we register these diagnostics-channel listeners\n * in such a convoluted way (as InstrumentationNodeModuleDefinition...)˝,\n * instead of simply subscribing to the events once in here.\n * The reason for this is timing semantics: These functions are called once the http or https module is loaded.\n * If we'd subscribe before that, there seem to be conflicts with the OTEL native instrumentation in some scenarios,\n * especially the \"import-on-top\" pattern of setting up ESM applications.\n */\n return [\n new InstrumentationNodeModuleDefinition('http', ['*'], wrap, unwrap),\n new InstrumentationNodeModuleDefinition('https', ['*'], wrap, unwrap),\n ];\n }\n\n /**\n * This is triggered when an outgoing request finishes.\n * It has access to the final request and response objects.\n */\n private _onOutgoingRequestFinish(request: http.ClientRequest, response?: http.IncomingMessage): void {\n DEBUG_BUILD && debug.log(INSTRUMENTATION_NAME, 'Handling finished outgoing request');\n\n const _breadcrumbs = this.getConfig().breadcrumbs;\n const breadCrumbsEnabled = typeof _breadcrumbs === 'undefined' ? true : _breadcrumbs;\n\n // Note: We cannot rely on the map being set by `_onOutgoingRequestCreated`, because that is not run in Node <22\n const shouldIgnore = this._ignoreOutgoingRequestsMap.get(request) ?? this._shouldIgnoreOutgoingRequest(request);\n this._ignoreOutgoingRequestsMap.set(request, shouldIgnore);\n\n if (breadCrumbsEnabled && !shouldIgnore) {\n addRequestBreadcrumb(request, response);\n }\n }\n\n /**\n * This is triggered when an outgoing request is created.\n * It has access to the request object, and can mutate it before the request is sent.\n */\n private _onOutgoingRequestCreated(request: http.ClientRequest): void {\n const shouldIgnore = this._ignoreOutgoingRequestsMap.get(request) ?? this._shouldIgnoreOutgoingRequest(request);\n this._ignoreOutgoingRequestsMap.set(request, shouldIgnore);\n\n if (shouldIgnore) {\n return;\n }\n\n addTracePropagationHeadersToOutgoingRequest(request, this._propagationDecisionMap);\n }\n\n /**\n * Check if the given outgoing request should be ignored.\n */\n private _shouldIgnoreOutgoingRequest(request: http.ClientRequest): boolean {\n if (isTracingSuppressed(context.active())) {\n return true;\n }\n\n const ignoreOutgoingRequests = this.getConfig().ignoreOutgoingRequests;\n\n if (!ignoreOutgoingRequests) {\n return false;\n }\n\n const options = getRequestOptions(request);\n const url = getRequestUrl(request);\n return ignoreOutgoingRequests(url, options);\n }\n}\n"],"names":["InstrumentationBase","INSTRUMENTATION_NAME","SDK_VERSION","LRUMap","subscribe","unsubscribe","InstrumentationNodeModuleDefinition","DEBUG_BUILD","debug","addRequestBreadcrumb","addTracePropagationHeadersToOutgoingRequest","isTracingSuppressed","context","getRequestOptions","getRequestUrl"],"mappings":";;;;;;;;;;;;AAgHA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACO,MAAM,yBAAA,SAAkCA,mCAAmB,CAAmC;;AAIrG,GAAS,WAAW,CAAC,MAAM,GAAqC,EAAE,EAAE;AACpE,IAAI,KAAK,CAACC,8BAAoB,EAAEC,gBAAW,EAAE,MAAM,CAAC;;AAEpD,IAAI,IAAI,CAAC,uBAAA,GAA0B,IAAIC,WAAM,CAAkB,GAAG,CAAC;AACnE,IAAI,IAAI,CAAC,0BAAA,GAA6B,IAAI,OAAO,EAA+B;AAChF,EAAE;;AAEF;AACA,GAAS,IAAI,GAA+E;AAC5F;AACA;AACA,IAAI,IAAI,qBAAA,GAAwB,KAAK;;AAErC,IAAI,MAAM,0BAAA,IAA8B,CAAC,KAAK,KAAc;AAC5D,MAAM,MAAM,IAAA,GAAO,KAAA;AACnB,MAAM,IAAI,CAAC,wBAAwB,CAAC,IAAI,CAAC,OAAO,EAAE,IAAI,CAAC,QAAQ,CAAC;AAChE,IAAI,CAAC,CAAA;;AAEL,IAAI,MAAM,wBAAA,IAA4B,CAAC,KAAK,KAAc;AAC1D,MAAM,MAAM,IAAA,GAAO,KAAA;AACnB,MAAM,IAAI,CAAC,wBAAwB,CAAC,IAAI,CAAC,OAAO,EAAE,SAAS,CAAC;AAC5D,IAAI,CAAC,CAAA;;AAEL,IAAI,MAAM,0BAAA,IAA8B,CAAC,KAAK,KAAc;AAC5D,MAAM,MAAM,IAAA,GAAO,KAAA;AACnB,MAAM,IAAI,CAAC,yBAAyB,CAAC,IAAI,CAAC,OAAO,CAAC;AAClD,IAAI,CAAC,CAAA;;AAEL,IAAI,MAAM,IAAA,GAAO,CAAyB,aAAa,KAAW;AAClE,MAAM,IAAI,q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