-
Notifications
You must be signed in to change notification settings - Fork 826
/
http.ts
754 lines (686 loc) · 25.2 KB
/
http.ts
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
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
/*
* Copyright The OpenTelemetry Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import {
context,
HrTime,
INVALID_SPAN_CONTEXT,
propagation,
ROOT_CONTEXT,
Span,
SpanKind,
SpanOptions,
SpanStatus,
SpanStatusCode,
trace,
} from '@opentelemetry/api';
import { Histogram, MeterProvider, MetricAttributes, ValueType } from '@opentelemetry/api-metrics';
import { hrTime, hrTimeDuration, hrTimeToMilliseconds, suppressTracing } from '@opentelemetry/core';
import type * as http from 'http';
import type * as https from 'https';
import { Socket } from 'net';
import * as semver from 'semver';
import * as url from 'url';
import {
Err,
Func,
Http,
HttpInstrumentationConfig,
HttpRequestArgs,
Https,
ResponseEndArgs,
} from './types';
import * as utils from './utils';
import { VERSION } from './version';
import {
InstrumentationBase,
InstrumentationNodeModuleDefinition,
isWrapped,
safeExecuteInTheMiddle,
} from '@opentelemetry/instrumentation';
import { RPCMetadata, RPCType, setRPCMetadata } from '@opentelemetry/core';
/**
* Http instrumentation instrumentation for Opentelemetry
*/
export class HttpInstrumentation extends InstrumentationBase<Http> {
/** keep track on spans not ended */
private readonly _spanNotEnded: WeakSet<Span> = new WeakSet<Span>();
private readonly _version = process.versions.node;
private _headerCapture;
private _httpServerDurationHistogram!: Histogram;
private _httpClientDurationHistogram!: Histogram;
constructor(config?: HttpInstrumentationConfig) {
super(
'@opentelemetry/instrumentation-http',
VERSION,
config
);
this._headerCapture = this._createHeaderCapture();
this._updateMetricInstruments();
}
override setMeterProvider(meterProvider: MeterProvider) {
super.setMeterProvider(meterProvider);
this._updateMetricInstruments();
}
private _updateMetricInstruments() {
this._httpServerDurationHistogram = this.meter.createHistogram('http.server.duration', {
description: 'measures the duration of the inbound HTTP requests',
unit: 'ms',
valueType: ValueType.DOUBLE
});
this._httpClientDurationHistogram = this.meter.createHistogram('http.client.duration', {
description: 'measures the duration of the outbound HTTP requests',
unit: 'ms',
valueType: ValueType.DOUBLE
});
}
private _getConfig(): HttpInstrumentationConfig {
return this._config;
}
override setConfig(config?: HttpInstrumentationConfig): void {
super.setConfig(config);
this._headerCapture = this._createHeaderCapture();
}
init(): [InstrumentationNodeModuleDefinition<Https>, InstrumentationNodeModuleDefinition<Http>] {
return [this._getHttpsInstrumentation(), this._getHttpInstrumentation()];
}
private _getHttpInstrumentation() {
return new InstrumentationNodeModuleDefinition<Http>(
'http',
['*'],
moduleExports => {
this._diag.debug(`Applying patch for http@${this._version}`);
if (isWrapped(moduleExports.request)) {
this._unwrap(moduleExports, 'request');
}
this._wrap(
moduleExports,
'request',
this._getPatchOutgoingRequestFunction('http')
);
if (isWrapped(moduleExports.get)) {
this._unwrap(moduleExports, 'get');
}
this._wrap(
moduleExports,
'get',
this._getPatchOutgoingGetFunction(moduleExports.request)
);
if (isWrapped(moduleExports.Server.prototype.emit)) {
this._unwrap(moduleExports.Server.prototype, 'emit');
}
this._wrap(
moduleExports.Server.prototype,
'emit',
this._getPatchIncomingRequestFunction('http')
);
return moduleExports;
},
moduleExports => {
if (moduleExports === undefined) return;
this._diag.debug(`Removing patch for http@${this._version}`);
this._unwrap(moduleExports, 'request');
this._unwrap(moduleExports, 'get');
this._unwrap(moduleExports.Server.prototype, 'emit');
}
);
}
private _getHttpsInstrumentation() {
return new InstrumentationNodeModuleDefinition<Https>(
'https',
['*'],
moduleExports => {
this._diag.debug(`Applying patch for https@${this._version}`);
if (isWrapped(moduleExports.request)) {
this._unwrap(moduleExports, 'request');
}
this._wrap(
moduleExports,
'request',
this._getPatchHttpsOutgoingRequestFunction('https')
);
if (isWrapped(moduleExports.get)) {
this._unwrap(moduleExports, 'get');
}
this._wrap(
moduleExports,
'get',
this._getPatchHttpsOutgoingGetFunction(moduleExports.request)
);
if (isWrapped(moduleExports.Server.prototype.emit)) {
this._unwrap(moduleExports.Server.prototype, 'emit');
}
this._wrap(
moduleExports.Server.prototype,
'emit',
this._getPatchIncomingRequestFunction('https')
);
return moduleExports;
},
moduleExports => {
if (moduleExports === undefined) return;
this._diag.debug(`Removing patch for https@${this._version}`);
this._unwrap(moduleExports, 'request');
this._unwrap(moduleExports, 'get');
this._unwrap(moduleExports.Server.prototype, 'emit');
}
);
}
/**
* Creates spans for incoming requests, restoring spans' context if applied.
*/
protected _getPatchIncomingRequestFunction(component: 'http' | 'https') {
return (original: (event: string, ...args: unknown[]) => boolean): (this: unknown, event: string, ...args: unknown[]) => boolean => {
return this._incomingRequestFunction(component, original);
};
}
/**
* Creates spans for outgoing requests, sending spans' context for distributed
* tracing.
*/
protected _getPatchOutgoingRequestFunction(component: 'http' | 'https') {
return (original: Func<http.ClientRequest>): Func<http.ClientRequest> => {
return this._outgoingRequestFunction(component, original);
};
}
protected _getPatchOutgoingGetFunction(
clientRequest: (
options: http.RequestOptions | string | url.URL,
...args: HttpRequestArgs
) => http.ClientRequest
) {
return (_original: Func<http.ClientRequest>): Func<http.ClientRequest> => {
// Re-implement http.get. This needs to be done (instead of using
// getPatchOutgoingRequestFunction to patch it) because we need to
// set the trace context header before the returned http.ClientRequest is
// ended. The Node.js docs state that the only differences between
// request and get are that (1) get defaults to the HTTP GET method and
// (2) the returned request object is ended immediately. The former is
// already true (at least in supported Node versions up to v10), so we
// simply follow the latter. Ref:
// https://nodejs.org/dist/latest/docs/api/http.html#http_http_get_options_callback
// https://github.com/googleapis/cloud-trace-nodejs/blob/master/src/instrumentations/instrumentation-http.ts#L198
return function outgoingGetRequest<
T extends http.RequestOptions | string | url.URL
>(options: T, ...args: HttpRequestArgs): http.ClientRequest {
const req = clientRequest(options, ...args);
req.end();
return req;
};
};
}
/** Patches HTTPS outgoing requests */
private _getPatchHttpsOutgoingRequestFunction(component: 'http' | 'https') {
return (original: Func<http.ClientRequest>): Func<http.ClientRequest> => {
const instrumentation = this;
return function httpsOutgoingRequest(
// eslint-disable-next-line node/no-unsupported-features/node-builtins
options: https.RequestOptions | string | URL,
...args: HttpRequestArgs
): http.ClientRequest {
// Makes sure options will have default HTTPS parameters
if (
component === 'https' &&
typeof options === 'object' &&
options?.constructor?.name !== 'URL'
) {
options = Object.assign({}, options);
instrumentation._setDefaultOptions(options);
}
return instrumentation._getPatchOutgoingRequestFunction(component)(
original
)(options, ...args);
};
};
}
private _setDefaultOptions(options: https.RequestOptions) {
options.protocol = options.protocol || 'https:';
options.port = options.port || 443;
}
/** Patches HTTPS outgoing get requests */
private _getPatchHttpsOutgoingGetFunction(
clientRequest: (
// eslint-disable-next-line node/no-unsupported-features/node-builtins
options: http.RequestOptions | string | URL,
...args: HttpRequestArgs
) => http.ClientRequest
) {
return (original: Func<http.ClientRequest>): Func<http.ClientRequest> => {
const instrumentation = this;
return function httpsOutgoingRequest(
// eslint-disable-next-line node/no-unsupported-features/node-builtins
options: https.RequestOptions | string | URL,
...args: HttpRequestArgs
): http.ClientRequest {
return instrumentation._getPatchOutgoingGetFunction(clientRequest)(
original
)(options, ...args);
};
};
}
/**
* Attach event listeners to a client request to end span and add span attributes.
*
* @param request The original request object.
* @param options The arguments to the original function.
* @param span representing the current operation
* @param startTime representing the start time of the request to calculate duration in Metric
* @param metricAttributes metric attributes
*/
private _traceClientRequest(
request: http.ClientRequest,
hostname: string,
span: Span,
startTime: HrTime,
metricAttributes: MetricAttributes
): http.ClientRequest {
if (this._getConfig().requestHook) {
this._callRequestHook(span, request);
}
/*
* User 'response' event listeners can be added before our listener,
* force our listener to be the first, so response emitter is bound
* before any user listeners are added to it.
*/
request.prependListener(
'response',
(response: http.IncomingMessage & { aborted?: boolean }) => {
const responseAttributes = utils.getOutgoingRequestAttributesOnResponse(
response,
);
span.setAttributes(responseAttributes);
metricAttributes = Object.assign(metricAttributes, utils.getOutgoingRequestMetricAttributesOnResponse(responseAttributes));
if (this._getConfig().responseHook) {
this._callResponseHook(span, response);
}
this._headerCapture.client.captureRequestHeaders(span, header => request.getHeader(header));
this._headerCapture.client.captureResponseHeaders(span, header => response.headers[header]);
context.bind(context.active(), response);
this._diag.debug('outgoingRequest on response()');
response.on('end', () => {
this._diag.debug('outgoingRequest on end()');
let status: SpanStatus;
if (response.aborted && !response.complete) {
status = { code: SpanStatusCode.ERROR };
} else {
status = { code: utils.parseResponseStatus(SpanKind.CLIENT, response.statusCode) };
}
span.setStatus(status);
if (this._getConfig().applyCustomAttributesOnSpan) {
safeExecuteInTheMiddle(
() =>
this._getConfig().applyCustomAttributesOnSpan!(
span,
request,
response
),
() => { },
true
);
}
this._closeHttpSpan(span, SpanKind.CLIENT, startTime, metricAttributes);
});
response.on('error', (error: Err) => {
this._diag.debug('outgoingRequest on error()', error);
utils.setSpanWithError(span, error);
const code = utils.parseResponseStatus(SpanKind.CLIENT, response.statusCode);
span.setStatus({ code, message: error.message });
this._closeHttpSpan(span, SpanKind.CLIENT, startTime, metricAttributes);
});
}
);
request.on('close', () => {
this._diag.debug('outgoingRequest on request close()');
if (!request.aborted) {
this._closeHttpSpan(span, SpanKind.CLIENT, startTime, metricAttributes);
}
});
request.on('error', (error: Err) => {
this._diag.debug('outgoingRequest on request error()', error);
utils.setSpanWithError(span, error);
this._closeHttpSpan(span, SpanKind.CLIENT, startTime, metricAttributes);
});
this._diag.debug('http.ClientRequest return request');
return request;
}
private _incomingRequestFunction(
component: 'http' | 'https',
original: (event: string, ...args: unknown[]) => boolean
) {
const instrumentation = this;
return function incomingRequest(
this: unknown,
event: string,
...args: unknown[]
): boolean {
// Only traces request events
if (event !== 'request') {
return original.apply(this, [event, ...args]);
}
const request = args[0] as http.IncomingMessage;
const response = args[1] as http.ServerResponse & { socket: Socket };
const pathname = request.url
? url.parse(request.url).pathname || '/'
: '/';
const method = request.method || 'GET';
instrumentation._diag.debug(`${component} instrumentation incomingRequest`);
if (
utils.isIgnored(
pathname,
instrumentation._getConfig().ignoreIncomingPaths,
(e: unknown) => instrumentation._diag.error('caught ignoreIncomingPaths error: ', e)
) ||
safeExecuteInTheMiddle(
() => instrumentation._getConfig().ignoreIncomingRequestHook?.(request),
(e: unknown) => {
if (e != null) {
instrumentation._diag.error('caught ignoreIncomingRequestHook error: ', e);
}
},
true
)
) {
return context.with(suppressTracing(context.active()), () => {
context.bind(context.active(), request);
context.bind(context.active(), response);
return original.apply(this, [event, ...args]);
});
}
const headers = request.headers;
const spanAttributes = utils.getIncomingRequestAttributes(request, {
component: component,
serverName: instrumentation._getConfig().serverName,
hookAttributes: instrumentation._callStartSpanHook(
request,
instrumentation._getConfig().startIncomingSpanHook
),
});
const spanOptions: SpanOptions = {
kind: SpanKind.SERVER,
attributes: spanAttributes,
};
const startTime = hrTime();
let metricAttributes: MetricAttributes = utils.getIncomingRequestMetricAttributes(spanAttributes);
const ctx = propagation.extract(ROOT_CONTEXT, headers);
const span = instrumentation._startHttpSpan(
`${component.toLocaleUpperCase()} ${method}`,
spanOptions,
ctx
);
const rpcMetadata: RPCMetadata = {
type: RPCType.HTTP,
span,
};
return context.with(
setRPCMetadata(trace.setSpan(ctx, span), rpcMetadata),
() => {
context.bind(context.active(), request);
context.bind(context.active(), response);
if (instrumentation._getConfig().requestHook) {
instrumentation._callRequestHook(span, request);
}
if (instrumentation._getConfig().responseHook) {
instrumentation._callResponseHook(span, response);
}
instrumentation._headerCapture.server.captureRequestHeaders(span, header => request.headers[header]);
// Wraps end (inspired by:
// https://github.com/GoogleCloudPlatform/cloud-trace-nodejs/blob/master/src/instrumentations/instrumentation-connect.ts#L75)
const originalEnd = response.end;
response.end = function (
this: http.ServerResponse,
..._args: ResponseEndArgs
) {
response.end = originalEnd;
// Cannot pass args of type ResponseEndArgs,
const returned = safeExecuteInTheMiddle(
() => response.end.apply(this, arguments as never),
error => {
if (error) {
utils.setSpanWithError(span, error);
instrumentation._closeHttpSpan(span, SpanKind.SERVER, startTime, metricAttributes);
throw error;
}
}
);
const attributes = utils.getIncomingRequestAttributesOnResponse(
request,
response
);
metricAttributes = Object.assign(metricAttributes, utils.getIncomingRequestMetricAttributesOnResponse(attributes));
instrumentation._headerCapture.server.captureResponseHeaders(span, header => response.getHeader(header));
span
.setAttributes(attributes)
.setStatus({ code: utils.parseResponseStatus(SpanKind.SERVER, response.statusCode) });
if (instrumentation._getConfig().applyCustomAttributesOnSpan) {
safeExecuteInTheMiddle(
() =>
instrumentation._getConfig().applyCustomAttributesOnSpan!(
span,
request,
response
),
() => { },
true
);
}
instrumentation._closeHttpSpan(span, SpanKind.SERVER, startTime, metricAttributes);
return returned;
};
return safeExecuteInTheMiddle(
() => original.apply(this, [event, ...args]),
error => {
if (error) {
utils.setSpanWithError(span, error);
instrumentation._closeHttpSpan(span, SpanKind.SERVER, startTime, metricAttributes);
throw error;
}
}
);
}
);
};
}
private _outgoingRequestFunction(
component: 'http' | 'https',
original: Func<http.ClientRequest>
): Func<http.ClientRequest> {
const instrumentation = this;
return function outgoingRequest(
this: unknown,
options: url.URL | http.RequestOptions | string,
...args: unknown[]
): http.ClientRequest {
if (!utils.isValidOptionsType(options)) {
return original.apply(this, [options, ...args]);
}
const extraOptions =
typeof args[0] === 'object' &&
(typeof options === 'string' || options instanceof url.URL)
? (args.shift() as http.RequestOptions)
: undefined;
const { origin, pathname, method, optionsParsed } = utils.getRequestInfo(
options,
extraOptions
);
/**
* Node 8's https module directly call the http one so to avoid creating
* 2 span for the same request we need to check that the protocol is correct
* See: https://github.com/nodejs/node/blob/v8.17.0/lib/https.js#L245
*/
if (
component === 'http' &&
semver.lt(process.version, '9.0.0') &&
optionsParsed.protocol === 'https:'
) {
return original.apply(this, [optionsParsed, ...args]);
}
if (
utils.isIgnored(
origin + pathname,
instrumentation._getConfig().ignoreOutgoingUrls,
(e: unknown) => instrumentation._diag.error('caught ignoreOutgoingUrls error: ', e)
) ||
safeExecuteInTheMiddle(
() => instrumentation._getConfig().ignoreOutgoingRequestHook?.(optionsParsed),
(e: unknown) => {
if (e != null) {
instrumentation._diag.error('caught ignoreOutgoingRequestHook error: ', e);
}
},
true
)
) {
return original.apply(this, [optionsParsed, ...args]);
}
const operationName = `${component.toUpperCase()} ${method}`;
const { hostname, port } = utils.extractHostnameAndPort(optionsParsed);
const attributes = utils.getOutgoingRequestAttributes(optionsParsed, {
component,
port,
hostname,
hookAttributes: instrumentation._callStartSpanHook(
optionsParsed,
instrumentation._getConfig().startOutgoingSpanHook
),
});
const startTime = hrTime();
const metricAttributes: MetricAttributes = utils.getOutgoingRequestMetricAttributes(attributes);
const spanOptions: SpanOptions = {
kind: SpanKind.CLIENT,
attributes,
};
const span = instrumentation._startHttpSpan(operationName, spanOptions);
const parentContext = context.active();
const requestContext = trace.setSpan(parentContext, span);
if (!optionsParsed.headers) {
optionsParsed.headers = {};
}
propagation.inject(requestContext, optionsParsed.headers);
return context.with(requestContext, () => {
/*
* The response callback is registered before ClientRequest is bound,
* thus it is needed to bind it before the function call.
*/
const cb = args[args.length - 1];
if (typeof cb === 'function') {
args[args.length - 1] = context.bind(parentContext, cb);
}
const request: http.ClientRequest = safeExecuteInTheMiddle(
() => original.apply(this, [optionsParsed, ...args]),
error => {
if (error) {
utils.setSpanWithError(span, error);
instrumentation._closeHttpSpan(span, SpanKind.CLIENT, startTime, metricAttributes);
throw error;
}
}
);
instrumentation._diag.debug(`${component} instrumentation outgoingRequest`);
context.bind(parentContext, request);
return instrumentation._traceClientRequest(
request,
hostname,
span,
startTime,
metricAttributes
);
});
};
}
private _startHttpSpan(
name: string,
options: SpanOptions,
ctx = context.active()
) {
/*
* If a parent is required but not present, we use a `NoopSpan` to still
* propagate context without recording it.
*/
const requireParent =
options.kind === SpanKind.CLIENT
? this._getConfig().requireParentforOutgoingSpans
: this._getConfig().requireParentforIncomingSpans;
let span: Span;
const currentSpan = trace.getSpan(ctx);
if (requireParent === true && currentSpan === undefined) {
span = trace.wrapSpanContext(INVALID_SPAN_CONTEXT);
} else if (requireParent === true && currentSpan?.spanContext().isRemote) {
span = currentSpan;
} else {
span = this.tracer.startSpan(name, options, ctx);
}
this._spanNotEnded.add(span);
return span;
}
private _closeHttpSpan(span: Span, spanKind: SpanKind, startTime: HrTime, metricAttributes: MetricAttributes) {
if (!this._spanNotEnded.has(span)) {
return;
}
span.end();
this._spanNotEnded.delete(span);
// Record metrics
const duration = hrTimeToMilliseconds(hrTimeDuration(startTime, hrTime()));
if (spanKind === SpanKind.SERVER) {
this._httpServerDurationHistogram.record(duration, metricAttributes);
} else if (spanKind === SpanKind.CLIENT) {
this._httpClientDurationHistogram.record(duration, metricAttributes);
}
}
private _callResponseHook(
span: Span,
response: http.IncomingMessage | http.ServerResponse
) {
safeExecuteInTheMiddle(
() => this._getConfig().responseHook!(span, response),
() => { },
true
);
}
private _callRequestHook(
span: Span,
request: http.ClientRequest | http.IncomingMessage
) {
safeExecuteInTheMiddle(
() => this._getConfig().requestHook!(span, request),
() => { },
true
);
}
private _callStartSpanHook(
request: http.IncomingMessage | http.RequestOptions,
hookFunc: Function | undefined,
) {
if (typeof hookFunc === 'function') {
return safeExecuteInTheMiddle(
() => hookFunc(request),
() => { },
true
);
}
}
private _createHeaderCapture() {
const config = this._getConfig();
return {
client: {
captureRequestHeaders: utils.headerCapture('request', config.headersToSpanAttributes?.client?.requestHeaders ?? []),
captureResponseHeaders: utils.headerCapture('response', config.headersToSpanAttributes?.client?.responseHeaders ?? [])
},
server: {
captureRequestHeaders: utils.headerCapture('request', config.headersToSpanAttributes?.server?.requestHeaders ?? []),
captureResponseHeaders: utils.headerCapture('response', config.headersToSpanAttributes?.server?.responseHeaders ?? []),
}
};
}
}