Skip to content

Commit fbedd59

Browse files
authored
fix(node): Set the correct fallback URL fields for outgoing https requests if they are not defined (#15316)
1 parent 24409e3 commit fbedd59

File tree

1 file changed

+15
-4
lines changed

1 file changed

+15
-4
lines changed

packages/node/src/integrations/http/SentryHttpInstrumentation.ts

+15-4
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ export class SentryHttpInstrumentation extends InstrumentationBase<SentryHttpIns
113113
stealthWrap(moduleExports.Server.prototype, 'emit', this._getPatchIncomingRequestFunction());
114114

115115
// Patch outgoing requests for breadcrumbs
116-
const patchedRequest = stealthWrap(moduleExports, 'request', this._getPatchOutgoingRequestFunction());
116+
const patchedRequest = stealthWrap(moduleExports, 'request', this._getPatchOutgoingRequestFunction('http'));
117117
stealthWrap(moduleExports, 'get', this._getPatchOutgoingGetFunction(patchedRequest));
118118

119119
return moduleExports;
@@ -134,7 +134,7 @@ export class SentryHttpInstrumentation extends InstrumentationBase<SentryHttpIns
134134
stealthWrap(moduleExports.Server.prototype, 'emit', this._getPatchIncomingRequestFunction());
135135

136136
// Patch outgoing requests for breadcrumbs
137-
const patchedRequest = stealthWrap(moduleExports, 'request', this._getPatchOutgoingRequestFunction());
137+
const patchedRequest = stealthWrap(moduleExports, 'request', this._getPatchOutgoingRequestFunction('https'));
138138
stealthWrap(moduleExports, 'get', this._getPatchOutgoingGetFunction(patchedRequest));
139139

140140
return moduleExports;
@@ -211,7 +211,7 @@ export class SentryHttpInstrumentation extends InstrumentationBase<SentryHttpIns
211211
/**
212212
* Patch the outgoing request function for breadcrumbs.
213213
*/
214-
private _getPatchOutgoingRequestFunction(): (
214+
private _getPatchOutgoingRequestFunction(component: 'http' | 'https'): (
215215
// eslint-disable-next-line @typescript-eslint/no-explicit-any
216216
original: (...args: any[]) => http.ClientRequest,
217217
) => (options: URL | http.RequestOptions | string, ...args: unknown[]) => http.ClientRequest {
@@ -226,10 +226,21 @@ export class SentryHttpInstrumentation extends InstrumentationBase<SentryHttpIns
226226
// so that it matches what Otel instrumentation passes to `ignoreOutgoingRequestHook`.
227227
// @see https://github.com/open-telemetry/opentelemetry-js/blob/7293e69c1e55ca62e15d0724d22605e61bd58952/experimental/packages/opentelemetry-instrumentation-http/src/http.ts#L756-L789
228228
const requestArgs = [...args] as RequestArgs;
229-
const options = requestArgs[0];
229+
230+
let options = requestArgs[0];
231+
232+
// Make sure correct fallback attributes are set on the options object for https before we pass them to the vendored getRequestInfo function.
233+
// Ref: https://github.com/open-telemetry/opentelemetry-js/blob/887ff1cd6e3f795f703e40a9fbe89b3cba7e88c3/experimental/packages/opentelemetry-instrumentation-http/src/http.ts#L390
234+
if (component === 'https' && typeof options === 'object' && options?.constructor?.name !== 'URL') {
235+
options = Object.assign({}, options);
236+
options.protocol = options.protocol || 'https:';
237+
options.port = options.port || 443;
238+
}
239+
230240
const extraOptions = typeof requestArgs[1] === 'object' ? requestArgs[1] : undefined;
231241

232242
const { optionsParsed, origin, pathname } = getRequestInfo(instrumentation._diag, options, extraOptions);
243+
233244
const url = getAbsoluteUrl(origin, pathname);
234245

235246
addSentryHeadersToRequestOptions(url, optionsParsed, instrumentation._propagationDecisionMap);

0 commit comments

Comments
 (0)