Skip to content

Commit b49d9f7

Browse files
authored
fix(node): Don't rely on this in http integration (#7135)
1 parent 7e57cb7 commit b49d9f7

File tree

1 file changed

+13
-11
lines changed
  • packages/node/src/integrations

1 file changed

+13
-11
lines changed

packages/node/src/integrations/http.ts

+13-11
Original file line numberDiff line numberDiff line change
@@ -101,21 +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 wrappedHandlerMaker = _createWrappedRequestMethodFactory(this._breadcrumbs, tracingOptions);
105-
106104
// eslint-disable-next-line @typescript-eslint/no-var-requires
107105
const httpModule = require('http');
108-
fill(httpModule, 'get', wrappedHandlerMaker);
109-
fill(httpModule, 'request', wrappedHandlerMaker);
106+
const wrappedHttpHandlerMaker = _createWrappedRequestMethodFactory(this._breadcrumbs, tracingOptions, httpModule);
107+
fill(httpModule, 'get', wrappedHttpHandlerMaker);
108+
fill(httpModule, 'request', wrappedHttpHandlerMaker);
110109

111110
// NOTE: Prior to Node 9, `https` used internals of `http` module, thus we don't patch it.
112111
// If we do, we'd get double breadcrumbs and double spans for `https` calls.
113112
// It has been changed in Node 9, so for all versions equal and above, we patch `https` separately.
114113
if (NODE_VERSION.major && NODE_VERSION.major > 8) {
115114
// eslint-disable-next-line @typescript-eslint/no-var-requires
116115
const httpsModule = require('https');
117-
fill(httpsModule, 'get', wrappedHandlerMaker);
118-
fill(httpsModule, 'request', wrappedHandlerMaker);
116+
const wrappedHttpsHandlerMaker = _createWrappedRequestMethodFactory(
117+
this._breadcrumbs,
118+
tracingOptions,
119+
httpsModule,
120+
);
121+
fill(httpsModule, 'get', wrappedHttpsHandlerMaker);
122+
fill(httpsModule, 'request', wrappedHttpsHandlerMaker);
119123
}
120124
}
121125
}
@@ -137,6 +141,7 @@ type WrappedRequestMethodFactory = (original: OriginalRequestMethod) => WrappedR
137141
function _createWrappedRequestMethodFactory(
138142
breadcrumbsEnabled: boolean,
139143
tracingOptions: TracingOptions | undefined,
144+
httpModule: typeof http | typeof https,
140145
): WrappedRequestMethodFactory {
141146
// We're caching results so we don't have to recompute regexp every time we create a request.
142147
const createSpanUrlMap = new LRUMap<string, boolean>(100);
@@ -172,11 +177,8 @@ function _createWrappedRequestMethodFactory(
172177
};
173178

174179
return function wrappedRequestMethodFactory(originalRequestMethod: OriginalRequestMethod): WrappedRequestMethod {
175-
return function wrappedMethod(this: typeof http | typeof https, ...args: RequestMethodArgs): http.ClientRequest {
176-
// eslint-disable-next-line @typescript-eslint/no-this-alias
177-
const httpModule = this;
178-
179-
const requestArgs = normalizeRequestArgs(this, args);
180+
return function wrappedMethod(this: unknown, ...args: RequestMethodArgs): http.ClientRequest {
181+
const requestArgs = normalizeRequestArgs(httpModule, args);
180182
const requestOptions = requestArgs[0];
181183
const requestUrl = extractUrl(requestOptions);
182184

0 commit comments

Comments
 (0)