-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathSentryHttpInstrumentation.ts
More file actions
529 lines (446 loc) · 18.9 KB
/
SentryHttpInstrumentation.ts
File metadata and controls
529 lines (446 loc) · 18.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
/* eslint-disable max-lines */
import type { ChannelListener } from 'node:diagnostics_channel';
import { subscribe, unsubscribe } from 'node:diagnostics_channel';
import { errorMonitor } from 'node:events';
import type * as http from 'node:http';
import type * as https from 'node:https';
import { context, SpanStatusCode, trace } from '@opentelemetry/api';
import { isTracingSuppressed } from '@opentelemetry/core';
import type { InstrumentationConfig } from '@opentelemetry/instrumentation';
import { InstrumentationBase, InstrumentationNodeModuleDefinition } from '@opentelemetry/instrumentation';
import {
ATTR_HTTP_RESPONSE_STATUS_CODE,
ATTR_NETWORK_PEER_ADDRESS,
ATTR_NETWORK_PEER_PORT,
ATTR_NETWORK_PROTOCOL_VERSION,
ATTR_NETWORK_TRANSPORT,
ATTR_URL_FULL,
ATTR_USER_AGENT_ORIGINAL,
SEMATTRS_HTTP_RESPONSE_CONTENT_LENGTH,
SEMATTRS_HTTP_RESPONSE_CONTENT_LENGTH_UNCOMPRESSED,
} from '@opentelemetry/semantic-conventions';
import type { Span, SpanAttributes, SpanStatus } from '@sentry/core';
import {
debug,
getHttpSpanDetailsFromUrlObject,
getSpanStatusFromHttpCode,
LRUMap,
parseStringToURLObject,
SDK_VERSION,
SEMANTIC_ATTRIBUTE_SENTRY_OP,
startInactiveSpan,
} from '@sentry/core';
import { DEBUG_BUILD } from '../../debug-build';
import { INSTRUMENTATION_NAME } from './constants';
import {
addRequestBreadcrumb,
addTracePropagationHeadersToOutgoingRequest,
getClientRequestUrl,
getRequestOptions,
} from './outgoing-requests';
type Http = typeof http;
type Https = typeof https;
type IncomingHttpHeaders = http.IncomingHttpHeaders;
type OutgoingHttpHeaders = http.OutgoingHttpHeaders;
export type SentryHttpInstrumentationOptions = InstrumentationConfig & {
/**
* Whether breadcrumbs should be recorded for outgoing requests.
*
* @default `true`
*/
breadcrumbs?: boolean;
/**
* Whether to propagate Sentry trace headers in outgoing requests.
* By default this is done by the HttpInstrumentation, but if that is not added (e.g. because tracing is disabled)
* then this instrumentation can take over.
*
* @default `false`
*/
propagateTraceInOutgoingRequests?: boolean;
/**
* Whether to enable the capability to create spans for outgoing requests via diagnostic channels.
* If enabled, spans will only be created if the `spans` option is also enabled (default: true).
*
* This is a feature flag that should be enabled by SDKs when the runtime supports it (Node 22.12+).
* Individual users should not need to configure this directly.
*
* @default `false`
*/
createSpansForOutgoingRequests?: boolean;
/**
* Whether to create spans for outgoing requests (user preference).
* This only takes effect if `createSpansForOutgoingRequests` is also enabled.
* If `createSpansForOutgoingRequests` is not enabled, this option is ignored.
*
* @default `true`
*/
spans?: boolean;
/**
* Do not capture breadcrumbs for outgoing HTTP requests to URLs where the given callback returns `true`.
* For the scope of this instrumentation, this callback only controls breadcrumb creation.
* The same option can be passed to the top-level httpIntegration where it controls both, breadcrumb and
* span creation.
*
* @param url Contains the entire URL, including query string (if any), protocol, host, etc. of the outgoing request.
* @param request Contains the {@type RequestOptions} object used to make the outgoing request.
*/
ignoreOutgoingRequests?: (url: string, request: http.RequestOptions) => boolean;
/**
* Hooks for outgoing request spans, called when `createSpansForOutgoingRequests` is enabled.
* These mirror the OTEL HttpInstrumentation hooks for backwards compatibility.
*/
outgoingRequestHook?: (span: Span, request: http.ClientRequest) => void;
outgoingResponseHook?: (span: Span, response: http.IncomingMessage) => void;
outgoingRequestApplyCustomAttributes?: (
span: Span,
request: http.ClientRequest,
response: http.IncomingMessage,
) => void;
// All options below do not do anything anymore in this instrumentation, and will be removed in the future.
// They are only kept here for backwards compatibility - the respective functionality is now handled by the httpServerIntegration/httpServerSpansIntegration.
/**
* @depreacted This no longer does anything.
*/
extractIncomingTraceFromHeader?: boolean;
/**
* @deprecated This no longer does anything.
*/
ignoreStaticAssets?: boolean;
/**
* @deprecated This no longer does anything.
*/
disableIncomingRequestSpans?: boolean;
/**
* @deprecated This no longer does anything.
*/
ignoreSpansForIncomingRequests?: (urlPath: string, request: http.IncomingMessage) => boolean;
/**
* @deprecated This no longer does anything.
*/
ignoreIncomingRequestBody?: (url: string, request: http.RequestOptions) => boolean;
/**
* @deprecated This no longer does anything.
*/
maxIncomingRequestBodySize?: 'none' | 'small' | 'medium' | 'always';
/**
* @deprecated This no longer does anything.
*/
trackIncomingRequestsAsSessions?: boolean;
/**
* @deprecated This no longer does anything.
*/
instrumentation?: {
requestHook?: (span: Span, req: http.ClientRequest | http.IncomingMessage) => void;
responseHook?: (span: Span, response: http.IncomingMessage | http.ServerResponse) => void;
applyCustomAttributesOnSpan?: (
span: Span,
request: http.ClientRequest | http.IncomingMessage,
response: http.IncomingMessage | http.ServerResponse,
) => void;
};
/**
* @deprecated This no longer does anything.
*/
sessionFlushingDelayMS?: number;
};
/**
* This custom HTTP instrumentation handles outgoing HTTP requests.
*
* It provides:
* - Breadcrumbs for all outgoing requests
* - Trace propagation headers (when enabled)
* - Span creation for outgoing requests (when createSpansForOutgoingRequests is enabled)
*
* Span creation requires Node 22+ and uses diagnostic channels to avoid monkey-patching.
* By default, this is only enabled in the node SDK, not in node-core or other runtime SDKs.
*
* Important note: Contrary to other OTEL instrumentation, this one cannot be unwrapped.
*
* This is heavily inspired & adapted from:
* https://github.com/open-telemetry/opentelemetry-js/blob/f8ab5592ddea5cba0a3b33bf8d74f27872c0367f/experimental/packages/opentelemetry-instrumentation-http/src/http.ts
*/
export class SentryHttpInstrumentation extends InstrumentationBase<SentryHttpInstrumentationOptions> {
private _propagationDecisionMap: LRUMap<string, boolean>;
private _ignoreOutgoingRequestsMap: WeakMap<http.ClientRequest, boolean>;
public constructor(config: SentryHttpInstrumentationOptions = {}) {
super(INSTRUMENTATION_NAME, SDK_VERSION, config);
this._propagationDecisionMap = new LRUMap<string, boolean>(100);
this._ignoreOutgoingRequestsMap = new WeakMap<http.ClientRequest, boolean>();
}
/** @inheritdoc */
public init(): [InstrumentationNodeModuleDefinition, InstrumentationNodeModuleDefinition] {
// We register handlers when either http or https is instrumented
// but we only want to register them once, whichever is loaded first
let hasRegisteredHandlers = false;
const onHttpClientResponseFinish = ((_data: unknown) => {
const data = _data as { request: http.ClientRequest; response: http.IncomingMessage };
this._onOutgoingRequestFinish(data.request, data.response);
}) satisfies ChannelListener;
const onHttpClientRequestError = ((_data: unknown) => {
const data = _data as { request: http.ClientRequest };
this._onOutgoingRequestFinish(data.request, undefined);
}) satisfies ChannelListener;
const onHttpClientRequestCreated = ((_data: unknown) => {
const data = _data as { request: http.ClientRequest };
this._onOutgoingRequestCreated(data.request);
}) satisfies ChannelListener;
const wrap = <T extends Http | Https>(moduleExports: T): T => {
if (hasRegisteredHandlers) {
return moduleExports;
}
hasRegisteredHandlers = true;
subscribe('http.client.response.finish', onHttpClientResponseFinish);
// When an error happens, we still want to have a breadcrumb
// In this case, `http.client.response.finish` is not triggered
subscribe('http.client.request.error', onHttpClientRequestError);
// NOTE: This channel only exists since Node 22.12+
// Before that, outgoing requests are not patched
// and trace headers are not propagated, sadly.
if (this.getConfig().propagateTraceInOutgoingRequests || this.getConfig().createSpansForOutgoingRequests) {
subscribe('http.client.request.created', onHttpClientRequestCreated);
}
return moduleExports;
};
const unwrap = (): void => {
unsubscribe('http.client.response.finish', onHttpClientResponseFinish);
unsubscribe('http.client.request.error', onHttpClientRequestError);
unsubscribe('http.client.request.created', onHttpClientRequestCreated);
};
/**
* You may be wondering why we register these diagnostics-channel listeners
* in such a convoluted way (as InstrumentationNodeModuleDefinition...)˝,
* instead of simply subscribing to the events once in here.
* The reason for this is timing semantics: These functions are called once the http or https module is loaded.
* If we'd subscribe before that, there seem to be conflicts with the OTEL native instrumentation in some scenarios,
* especially the "import-on-top" pattern of setting up ESM applications.
*/
return [
new InstrumentationNodeModuleDefinition('http', ['*'], wrap, unwrap),
new InstrumentationNodeModuleDefinition('https', ['*'], wrap, unwrap),
];
}
/**
* Start a span for an outgoing request.
* The span wraps the callback of the request, and ends when the response is finished.
*/
private _startSpanForOutgoingRequest(request: http.ClientRequest): Span {
// We monkey-patch `req.once('response'), which is used to trigger the callback of the request
// eslint-disable-next-line @typescript-eslint/unbound-method, deprecation/deprecation
const originalOnce = request.once;
const [name, attributes] = _getOutgoingRequestSpanData(request);
const span = startInactiveSpan({
name,
attributes,
onlyIfParent: true,
});
this.getConfig().outgoingRequestHook?.(span, request);
const newOnce = new Proxy(originalOnce, {
apply(target, thisArg, args: Parameters<typeof originalOnce>) {
const [event] = args;
if (event !== 'response') {
return target.apply(thisArg, args);
}
const parentContext = context.active();
const requestContext = trace.setSpan(parentContext, span);
return context.with(requestContext, () => {
return target.apply(thisArg, args);
});
},
});
// eslint-disable-next-line deprecation/deprecation
request.once = newOnce;
/**
* Determines if the request has errored or the response has ended/errored.
*/
let responseFinished = false;
const endSpan = (status: SpanStatus): void => {
if (responseFinished) {
return;
}
responseFinished = true;
span.setStatus(status);
span.end();
};
request.prependListener('response', response => {
if (request.listenerCount('response') <= 1) {
response.resume();
}
context.bind(context.active(), response);
const additionalAttributes = _getOutgoingRequestEndedSpanData(response);
span.setAttributes(additionalAttributes);
this.getConfig().outgoingResponseHook?.(span, response);
this.getConfig().outgoingRequestApplyCustomAttributes?.(span, request, response);
const endHandler = (forceError: boolean = false): void => {
this._diag.debug('outgoingRequest on end()');
const status =
// eslint-disable-next-line deprecation/deprecation
forceError || typeof response.statusCode !== 'number' || (response.aborted && !response.complete)
? { code: SpanStatusCode.ERROR }
: getSpanStatusFromHttpCode(response.statusCode);
endSpan(status);
};
response.on('end', () => {
endHandler();
});
response.on(errorMonitor, error => {
this._diag.debug('outgoingRequest on response error()', error);
endHandler(true);
});
});
// Fallback if proper response end handling above fails
request.on('close', () => {
endSpan({ code: SpanStatusCode.UNSET });
});
request.on(errorMonitor, error => {
this._diag.debug('outgoingRequest on request error()', error);
endSpan({ code: SpanStatusCode.ERROR });
});
return span;
}
/**
* This is triggered when an outgoing request finishes.
* It has access to the final request and response objects.
*/
private _onOutgoingRequestFinish(request: http.ClientRequest, response?: http.IncomingMessage): void {
DEBUG_BUILD && debug.log(INSTRUMENTATION_NAME, 'Handling finished outgoing request');
const _breadcrumbs = this.getConfig().breadcrumbs;
const breadCrumbsEnabled = typeof _breadcrumbs === 'undefined' ? true : _breadcrumbs;
// Note: We cannot rely on the map being set by `_onOutgoingRequestCreated`, because that is not run in Node <22
const shouldIgnore = this._ignoreOutgoingRequestsMap.get(request) ?? this._shouldIgnoreOutgoingRequest(request);
this._ignoreOutgoingRequestsMap.set(request, shouldIgnore);
if (breadCrumbsEnabled && !shouldIgnore) {
addRequestBreadcrumb(request, response);
}
}
/**
* This is triggered when an outgoing request is created.
* It creates a span (if enabled) and propagates trace headers within the span's context,
* so downstream services link to the outgoing HTTP span rather than its parent.
*/
private _onOutgoingRequestCreated(request: http.ClientRequest): void {
DEBUG_BUILD && debug.log(INSTRUMENTATION_NAME, 'Handling outgoing request created');
const shouldIgnore = this._ignoreOutgoingRequestsMap.get(request) ?? this._shouldIgnoreOutgoingRequest(request);
this._ignoreOutgoingRequestsMap.set(request, shouldIgnore);
if (shouldIgnore) {
return;
}
const shouldCreateSpan = this.getConfig().createSpansForOutgoingRequests && (this.getConfig().spans ?? true);
const shouldPropagate = this.getConfig().propagateTraceInOutgoingRequests;
if (shouldCreateSpan) {
const span = this._startSpanForOutgoingRequest(request);
// Propagate headers within the span's context so the sentry-trace header
// contains the outgoing span's ID, not the parent span's ID.
// Only do this if the span is recording (has a parent) - otherwise the non-recording
// span would produce all-zero trace IDs instead of using the scope's propagation context.
if (shouldPropagate && span.isRecording()) {
const requestContext = trace.setSpan(context.active(), span);
context.with(requestContext, () => {
addTracePropagationHeadersToOutgoingRequest(request, this._propagationDecisionMap);
});
} else if (shouldPropagate) {
addTracePropagationHeadersToOutgoingRequest(request, this._propagationDecisionMap);
}
} else if (shouldPropagate) {
addTracePropagationHeadersToOutgoingRequest(request, this._propagationDecisionMap);
}
}
/**
* Check if the given outgoing request should be ignored.
*/
private _shouldIgnoreOutgoingRequest(request: http.ClientRequest): boolean {
if (isTracingSuppressed(context.active())) {
return true;
}
const ignoreOutgoingRequests = this.getConfig().ignoreOutgoingRequests;
if (!ignoreOutgoingRequests) {
return false;
}
const options = getRequestOptions(request);
const url = getClientRequestUrl(request);
return ignoreOutgoingRequests(url, options);
}
}
function _getOutgoingRequestSpanData(request: http.ClientRequest): [string, SpanAttributes] {
const url = getClientRequestUrl(request);
const [name, attributes] = getHttpSpanDetailsFromUrlObject(
parseStringToURLObject(url),
'client',
'auto.http.otel.http',
request,
);
const userAgent = request.getHeader('user-agent');
return [
name,
{
[SEMANTIC_ATTRIBUTE_SENTRY_OP]: 'http.client',
'otel.kind': 'CLIENT',
[ATTR_USER_AGENT_ORIGINAL]: userAgent,
[ATTR_URL_FULL]: url,
'http.url': url,
'http.method': request.method,
'http.target': request.path || '/',
'net.peer.name': request.host,
'http.host': request.getHeader('host'),
...attributes,
},
];
}
/**
* Exported for testing purposes.
*/
export function _getOutgoingRequestEndedSpanData(response: http.IncomingMessage): SpanAttributes {
const { statusCode, statusMessage, httpVersion, socket } = response;
// httpVersion can be undefined in some cases and we seem to have encountered this before:
// https://github.com/getsentry/sentry-javascript/blob/ec8c8c64cde6001123db0199a8ca017b8863eac8/packages/node-core/src/integrations/http/httpServerSpansIntegration.ts#L158
// see: #20415
const transport = httpVersion?.toUpperCase() !== 'QUIC' ? 'ip_tcp' : 'ip_udp';
const additionalAttributes: SpanAttributes = {
[ATTR_HTTP_RESPONSE_STATUS_CODE]: statusCode,
[ATTR_NETWORK_PROTOCOL_VERSION]: httpVersion,
'http.flavor': httpVersion,
[ATTR_NETWORK_TRANSPORT]: transport,
'net.transport': transport,
['http.status_text']: statusMessage?.toUpperCase(),
'http.status_code': statusCode,
...getResponseContentLengthAttributes(response),
};
if (socket) {
const { remoteAddress, remotePort } = socket;
additionalAttributes[ATTR_NETWORK_PEER_ADDRESS] = remoteAddress;
additionalAttributes[ATTR_NETWORK_PEER_PORT] = remotePort;
additionalAttributes['net.peer.ip'] = remoteAddress;
additionalAttributes['net.peer.port'] = remotePort;
}
return additionalAttributes;
}
function getResponseContentLengthAttributes(response: http.IncomingMessage): SpanAttributes {
const length = getContentLength(response.headers);
if (length == null) {
return {};
}
if (isCompressed(response.headers)) {
// eslint-disable-next-line deprecation/deprecation
return { [SEMATTRS_HTTP_RESPONSE_CONTENT_LENGTH]: length };
} else {
// eslint-disable-next-line deprecation/deprecation
return { [SEMATTRS_HTTP_RESPONSE_CONTENT_LENGTH_UNCOMPRESSED]: length };
}
}
function getContentLength(headers: http.OutgoingHttpHeaders | http.IncomingHttpHeaders): number | undefined {
const contentLengthHeader = headers['content-length'];
if (typeof contentLengthHeader === 'number') {
return contentLengthHeader;
}
if (typeof contentLengthHeader !== 'string') {
return undefined;
}
const contentLength = parseInt(contentLengthHeader, 10);
if (isNaN(contentLength)) {
return undefined;
}
return contentLength;
}
function isCompressed(headers: OutgoingHttpHeaders | IncomingHttpHeaders): boolean {
const encoding = headers['content-encoding'];
return !!encoding && encoding !== 'identity';
}