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,CAAC;iBACf;YACH,CAAC,CAAC,CAAC;QACP,CAAC,CAAC;IACJ,CAAC,CAAC;AACJ,CAAC;AAED;;GAEG;AACH,SAAS,oBAAoB,CAAC,KAAa,EAAE,GAAW,EAAE,GAAuB,EAAE,GAA0B;IAC3G,IAAI,CAAC,oBAAa,EAAE,CAAC,cAAc,CAAC,IAAI,CAAC,EAAE;QACzC,OAAO;KACR;IAED,oBAAa,EAAE,CAAC,aAAa,CAC3B;QACE,QAAQ,EAAE,MAAM;QAChB,IAAI,EAAE;YACJ,MAAM,EAAE,GAAG,CAAC,MAAM;YAClB,WAAW,EAAE,GAAG,IAAI,GAAG,CAAC,UAAU;YAClC,GAAG,KAAA;SACJ;QACD,IAAI,EAAE,MAAM;KACb,EACD;QACE,KAAK,OAAA;QACL,OAAO,EAAE,GAAG;QACZ,QAAQ,EAAE,GAAG;KACd,CACF,CAAC;AACJ,CAAC","sourcesContent":["import { getCurrentHub } from '@sentry/core';\nimport { Integration, Span } from '@sentry/types';\nimport { fill, logger, parseSemver } from '@sentry/utils';\nimport * as http from 'http';\nimport * as https from 'https';\n\nimport {\n cleanSpanDescription,\n extractUrl,\n isSentryRequest,\n normalizeRequestArgs,\n RequestMethod,\n RequestMethodArgs,\n} from './utils/http';\n\nconst NODE_VERSION = parseSemver(process.versions.node);\n\n/** http module integration */\nexport class Http implements Integration {\n /**\n * @inheritDoc\n */\n public static id: string = 'Http';\n\n /**\n * @inheritDoc\n */\n public name: string = Http.id;\n\n /**\n * @inheritDoc\n */\n private readonly _breadcrumbs: boolean;\n\n /**\n * @inheritDoc\n */\n private readonly _tracing: boolean;\n\n /**\n * @inheritDoc\n */\n public constructor(options: { breadcrumbs?: boolean; tracing?: boolean } = {}) {\n this._breadcrumbs = typeof options.breadcrumbs === 'undefined' ? true : options.breadcrumbs;\n this._tracing = typeof options.tracing === 'undefined' ? false : options.tracing;\n }\n\n /**\n * @inheritDoc\n */\n public setupOnce(): void {\n // No need to instrument if we don't want to track anything\n if (!this._breadcrumbs && !this._tracing) {\n return;\n }\n\n const wrappedHandlerMaker = _createWrappedRequestMethodFactory(this._breadcrumbs, this._tracing);\n\n // eslint-disable-next-line @typescript-eslint/no-var-requires\n const httpModule = require('http');\n fill(httpModule, 'get', wrappedHandlerMaker);\n fill(httpModule, 'request', wrappedHandlerMaker);\n\n // NOTE: Prior to Node 9, `https` used internals of `http` module, thus we don't patch it.\n // If we do, we'd get double breadcrumbs and double spans for `https` calls.\n // It has been changed in Node 9, so for all versions equal and above, we patch `https` separately.\n if (NODE_VERSION.major && NODE_VERSION.major > 8) {\n // eslint-disable-next-line @typescript-eslint/no-var-requires\n const httpsModule = require('https');\n fill(httpsModule, 'get', wrappedHandlerMaker);\n fill(httpsModule, 'request', wrappedHandlerMaker);\n }\n }\n}\n\n// for ease of reading below\ntype OriginalRequestMethod = RequestMethod;\ntype WrappedRequestMethod = RequestMethod;\ntype WrappedRequestMethodFactory = (original: OriginalRequestMethod) => WrappedRequestMethod;\n\n/**\n * Function which creates a function which creates wrapped versions of internal `request` and `get` calls within `http`\n * and `https` modules. (NB: Not a typo - this is a creator^2!)\n *\n * @param breadcrumbsEnabled Whether or not to record outgoing requests as breadcrumbs\n * @param tracingEnabled Whether or not to record outgoing requests as tracing spans\n *\n * @returns A function which accepts the exiting handler and returns a wrapped handler\n */\nfunction _createWrappedRequestMethodFactory(\n breadcrumbsEnabled: boolean,\n tracingEnabled: boolean,\n): WrappedRequestMethodFactory {\n return function wrappedRequestMethodFactory(originalRequestMethod: OriginalRequestMethod): WrappedRequestMethod {\n return function wrappedMethod(this: typeof http | typeof https, ...args: RequestMethodArgs): http.ClientRequest {\n // eslint-disable-next-line @typescript-eslint/no-this-alias\n const httpModule = this;\n\n const requestArgs = normalizeRequestArgs(this, args);\n const requestOptions = requestArgs[0];\n const requestUrl = extractUrl(requestOptions);\n\n // we don't want to record requests to Sentry as either breadcrumbs or spans, so just use the original method\n if (isSentryRequest(requestUrl)) {\n return originalRequestMethod.apply(httpModule, requestArgs);\n }\n\n let span: Span | undefined;\n let parentSpan: Span | undefined;\n\n const scope = getCurrentHub().getScope();\n if (scope && tracingEnabled) {\n parentSpan = scope.getSpan();\n if (parentSpan) {\n span = parentSpan.startChild({\n description: `${requestOptions.method || 'GET'} ${requestUrl}`,\n op: 'http.client',\n });\n\n const sentryTraceHeader = span.toTraceparent();\n logger.log(\n `[Tracing] Adding sentry-trace header ${sentryTraceHeader} to outgoing request to ${requestUrl}: `,\n );\n requestOptions.headers = { ...requestOptions.headers, 'sentry-trace': sentryTraceHeader };\n }\n }\n\n // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access\n return originalRequestMethod\n .apply(httpModule, requestArgs)\n .once('response', function(this: http.ClientRequest, res: http.IncomingMessage): void {\n // eslint-disable-next-line @typescript-eslint/no-this-alias\n const req = this;\n if (breadcrumbsEnabled) {\n addRequestBreadcrumb('response', requestUrl, req, res);\n }\n if (tracingEnabled && span) {\n if (res.statusCode) {\n span.setHttpStatus(res.statusCode);\n }\n span.description = cleanSpanDescription(span.description, requestOptions, req);\n span.finish();\n }\n })\n .once('error', function(this: http.ClientRequest): void {\n // eslint-disable-next-line @typescript-eslint/no-this-alias\n const req = this;\n\n if (breadcrumbsEnabled) {\n addRequestBreadcrumb('error', requestUrl, req);\n }\n if (tracingEnabled && span) {\n span.setHttpStatus(500);\n span.description = cleanSpanDescription(span.description, requestOptions, req);\n span.finish();\n }\n });\n };\n };\n}\n\n/**\n * Captures Breadcrumb based on provided request/response pair\n */\nfunction addRequestBreadcrumb(event: string, url: string, req: http.ClientRequest, res?: http.IncomingMessage): void {\n if (!getCurrentHub().getIntegration(Http)) {\n return;\n }\n\n getCurrentHub().addBreadcrumb(\n {\n category: 'http',\n data: {\n method: req.method,\n status_code: res && res.statusCode,\n url,\n },\n type: 'http',\n },\n {\n event,\n request: req,\n response: res,\n },\n );\n}\n"]}
\No newline at end of file