From 733cce6dd1df386ae9e906b7ba1bcd98527282bb Mon Sep 17 00:00:00 2001 From: Lukas Stracke Date: Mon, 13 Mar 2023 10:52:48 +0100 Subject: [PATCH] fix(node): Revert to dynamic `require` call to fix monkey patching --- packages/node/src/integrations/http.ts | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/packages/node/src/integrations/http.ts b/packages/node/src/integrations/http.ts index 017929fbe0eb..ea7a9eae173d 100644 --- a/packages/node/src/integrations/http.ts +++ b/packages/node/src/integrations/http.ts @@ -8,8 +8,8 @@ import { parseSemver, stringMatchesSomePattern, } from '@sentry/utils'; -import * as http from 'http'; -import * as https from 'https'; +import type * as http from 'http'; +import type * as https from 'https'; import { LRUMap } from 'lru_map'; import type { NodeClient } from '../client'; @@ -101,17 +101,25 @@ export class Http implements Integration { // and we will no longer have to do this optional merge, we can just pass `this._tracing` directly. const tracingOptions = this._tracing ? { ...clientOptions, ...this._tracing } : undefined; - const wrappedHttpHandlerMaker = _createWrappedRequestMethodFactory(this._breadcrumbs, tracingOptions, http); - fill(http, 'get', wrappedHttpHandlerMaker); - fill(http, 'request', wrappedHttpHandlerMaker); + // eslint-disable-next-line @typescript-eslint/no-var-requires + const httpModule = require('http'); + const wrappedHttpHandlerMaker = _createWrappedRequestMethodFactory(this._breadcrumbs, tracingOptions, httpModule); + fill(httpModule, 'get', wrappedHttpHandlerMaker); + fill(httpModule, 'request', wrappedHttpHandlerMaker); // NOTE: Prior to Node 9, `https` used internals of `http` module, thus we don't patch it. // If we do, we'd get double breadcrumbs and double spans for `https` calls. // It has been changed in Node 9, so for all versions equal and above, we patch `https` separately. if (NODE_VERSION.major && NODE_VERSION.major > 8) { - const wrappedHttpsHandlerMaker = _createWrappedRequestMethodFactory(this._breadcrumbs, tracingOptions, https); - fill(https, 'get', wrappedHttpsHandlerMaker); - fill(https, 'request', wrappedHttpsHandlerMaker); + // eslint-disable-next-line @typescript-eslint/no-var-requires + const httpsModule = require('https'); + const wrappedHttpsHandlerMaker = _createWrappedRequestMethodFactory( + this._breadcrumbs, + tracingOptions, + httpsModule, + ); + fill(httpsModule, 'get', wrappedHttpsHandlerMaker); + fill(httpsModule, 'request', wrappedHttpsHandlerMaker); } } }