Skip to content

Commit fe1231a

Browse files
authored
fix(node): Revert to dynamic require call to fix monkey patching (#7430)
Partially revert #7377 which caused monkey patching errors when patching the native `http` and `https` modules in the Node SDK (#7425). Similarly, also our Serverless SDK was subjected to the same problem (#7421). The problem is that `import` doesn't permit monkey patching of the imported (`http(s)`) module, producing this error: ```bash TypeError: Cannot assign to read only property 'get' of object '[object Module]' ``` I tried using a dynamic import instead but got the same result. So it seems like we can only use `require` here :(
1 parent 9c2971b commit fe1231a

File tree

1 file changed

+16
-8
lines changed
  • packages/node/src/integrations

1 file changed

+16
-8
lines changed

packages/node/src/integrations/http.ts

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ import {
88
parseSemver,
99
stringMatchesSomePattern,
1010
} from '@sentry/utils';
11-
import * as http from 'http';
12-
import * as https from 'https';
11+
import type * as http from 'http';
12+
import type * as https from 'https';
1313
import { LRUMap } from 'lru_map';
1414

1515
import type { NodeClient } from '../client';
@@ -101,17 +101,25 @@ export class Http implements Integration {
101101
// and we will no longer have to do this optional merge, we can just pass `this._tracing` directly.
102102
const tracingOptions = this._tracing ? { ...clientOptions, ...this._tracing } : undefined;
103103

104-
const wrappedHttpHandlerMaker = _createWrappedRequestMethodFactory(this._breadcrumbs, tracingOptions, http);
105-
fill(http, 'get', wrappedHttpHandlerMaker);
106-
fill(http, 'request', wrappedHttpHandlerMaker);
104+
// eslint-disable-next-line @typescript-eslint/no-var-requires
105+
const httpModule = require('http');
106+
const wrappedHttpHandlerMaker = _createWrappedRequestMethodFactory(this._breadcrumbs, tracingOptions, httpModule);
107+
fill(httpModule, 'get', wrappedHttpHandlerMaker);
108+
fill(httpModule, 'request', wrappedHttpHandlerMaker);
107109

108110
// NOTE: Prior to Node 9, `https` used internals of `http` module, thus we don't patch it.
109111
// If we do, we'd get double breadcrumbs and double spans for `https` calls.
110112
// It has been changed in Node 9, so for all versions equal and above, we patch `https` separately.
111113
if (NODE_VERSION.major && NODE_VERSION.major > 8) {
112-
const wrappedHttpsHandlerMaker = _createWrappedRequestMethodFactory(this._breadcrumbs, tracingOptions, https);
113-
fill(https, 'get', wrappedHttpsHandlerMaker);
114-
fill(https, 'request', wrappedHttpsHandlerMaker);
114+
// eslint-disable-next-line @typescript-eslint/no-var-requires
115+
const httpsModule = require('https');
116+
const wrappedHttpsHandlerMaker = _createWrappedRequestMethodFactory(
117+
this._breadcrumbs,
118+
tracingOptions,
119+
httpsModule,
120+
);
121+
fill(httpsModule, 'get', wrappedHttpsHandlerMaker);
122+
fill(httpsModule, 'request', wrappedHttpsHandlerMaker);
115123
}
116124
}
117125
}

0 commit comments

Comments
 (0)