@opentelemetry/instrumentation-http 0.220.0 → 0.222.0-development.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,17 +1,30 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.headerCapture = exports.getIncomingStableRequestMetricAttributesOnResponse = exports.getIncomingRequestMetricAttributesOnResponse = exports.getIncomingRequestAttributesOnResponse = exports.getIncomingRequestMetricAttributes = exports.getIncomingRequestAttributes = exports.getRemoteClientAddress = exports.getOutgoingStableRequestMetricAttributesOnResponse = exports.getOutgoingRequestMetricAttributesOnResponse = exports.getOutgoingRequestAttributesOnResponse = exports.setAttributesFromHttpKind = exports.getOutgoingRequestMetricAttributes = exports.getOutgoingRequestAttributes = exports.extractHostnameAndPort = exports.isValidOptionsType = exports.getRequestInfo = exports.isCompressed = exports.setResponseContentLengthAttribute = exports.setRequestContentLengthAttribute = exports.setSpanWithError = exports.satisfiesPattern = exports.parseResponseStatus = exports.getAbsoluteUrl = void 0;
3
+ exports.headerCapture = exports.getIncomingStableRequestMetricAttributesOnResponse = exports.getIncomingRequestAttributesOnResponse = exports.getIncomingRequestAttributes = exports.getRemoteClientAddress = exports.getOutgoingStableRequestMetricAttributesOnResponse = exports.getOutgoingRequestAttributesOnResponse = exports.getOutgoingRequestAttributes = exports.extractHostnameAndPort = exports.isValidOptionsType = exports.getRequestInfo = exports.isCompressed = exports.setSpanWithError = exports.satisfiesPattern = exports.parseResponseStatus = exports.getAbsoluteUrl = exports.redactQueryString = void 0;
4
4
  const api_1 = require("@opentelemetry/api");
5
5
  const semantic_conventions_1 = require("@opentelemetry/semantic-conventions");
6
6
  const semconv_1 = require("./semconv");
7
7
  const core_1 = require("@opentelemetry/core");
8
- const instrumentation_1 = require("@opentelemetry/instrumentation");
9
8
  const url = require("url");
10
- const AttributeNames_1 = require("./enums/AttributeNames");
11
9
  const internal_types_1 = require("./internal-types");
12
10
  const internal_types_2 = require("./internal-types");
13
11
  // eslint-disable-next-line @typescript-eslint/no-require-imports
14
12
  const forwardedParse = require("forwarded-parse");
13
+ const defaultQueryStringsToRedact = Array.from(internal_types_2.DEFAULT_QUERY_STRINGS_TO_REDACT);
14
+ /**
15
+ * Redacts sensitive query parameters from a query string (without leading '?').
16
+ * Returns the input unchanged if it cannot be parsed.
17
+ */
18
+ const redactQueryString = (searchParams, paramsToRedact) => {
19
+ const params = new URLSearchParams(searchParams);
20
+ for (const param of paramsToRedact) {
21
+ if (params.has(param)) {
22
+ params.set(param, internal_types_2.STR_REDACTED);
23
+ }
24
+ }
25
+ return params.toString();
26
+ };
27
+ exports.redactQueryString = redactQueryString;
15
28
  /**
16
29
  * Get an absolute url
17
30
  */
@@ -29,17 +42,11 @@ const getAbsoluteUrl = (requestUrl, headers, fallbackProtocol = 'http:', redacte
29
42
  port !== '443') {
30
43
  host += `:${port}`;
31
44
  }
32
- // Redact sensitive query parameters
33
45
  if (path.includes('?')) {
34
46
  try {
35
47
  const parsedUrl = new URL(path, 'http://localhost');
36
- const sensitiveParamsToRedact = redactedQueryParams || [];
37
- for (const sensitiveParam of sensitiveParamsToRedact) {
38
- if (parsedUrl.searchParams.get(sensitiveParam)) {
39
- parsedUrl.searchParams.set(sensitiveParam, internal_types_2.STR_REDACTED);
40
- }
41
- }
42
- path = `${parsedUrl.pathname}${parsedUrl.search}`;
48
+ const redacted = (0, exports.redactQueryString)(parsedUrl.searchParams, redactedQueryParams);
49
+ path = `${parsedUrl.pathname}?${redacted}`;
43
50
  }
44
51
  catch {
45
52
  // Ignore error, as the path was not a valid URL.
@@ -87,66 +94,14 @@ exports.satisfiesPattern = satisfiesPattern;
87
94
  * Sets the span with the error passed in params
88
95
  * @param {Span} span the span that need to be set
89
96
  * @param {Error} error error that will be set to span
90
- * @param {SemconvStability} semconvStability determines which semconv version to use
91
97
  */
92
- const setSpanWithError = (span, error, semconvStability) => {
98
+ const setSpanWithError = (span, error) => {
93
99
  const message = error.message;
94
- if (semconvStability & instrumentation_1.SemconvStability.OLD) {
95
- span.setAttribute(AttributeNames_1.AttributeNames.HTTP_ERROR_NAME, error.name);
96
- span.setAttribute(AttributeNames_1.AttributeNames.HTTP_ERROR_MESSAGE, message);
97
- }
98
- if (semconvStability & instrumentation_1.SemconvStability.STABLE) {
99
- span.setAttribute(semantic_conventions_1.ATTR_ERROR_TYPE, error.name);
100
- }
100
+ span.setAttribute(semantic_conventions_1.ATTR_ERROR_TYPE, error.name);
101
101
  span.setStatus({ code: api_1.SpanStatusCode.ERROR, message });
102
102
  span.recordException(error);
103
103
  };
104
104
  exports.setSpanWithError = setSpanWithError;
105
- /**
106
- * Adds attributes for request content-length and content-encoding HTTP headers
107
- * @param { IncomingMessage } Request object whose headers will be analyzed
108
- * @param { Attributes } Attributes object to be modified
109
- */
110
- const setRequestContentLengthAttribute = (request, attributes) => {
111
- const length = getContentLength(request.headers);
112
- if (length === null)
113
- return;
114
- if ((0, exports.isCompressed)(request.headers)) {
115
- attributes[semconv_1.ATTR_HTTP_REQUEST_CONTENT_LENGTH] = length;
116
- }
117
- else {
118
- attributes[semconv_1.ATTR_HTTP_REQUEST_CONTENT_LENGTH_UNCOMPRESSED] = length;
119
- }
120
- };
121
- exports.setRequestContentLengthAttribute = setRequestContentLengthAttribute;
122
- /**
123
- * Adds attributes for response content-length and content-encoding HTTP headers
124
- * @param { IncomingMessage } Response object whose headers will be analyzed
125
- * @param { Attributes } Attributes object to be modified
126
- *
127
- * @deprecated this is for an older version of semconv. It is retained for compatibility using OTEL_SEMCONV_STABILITY_OPT_IN
128
- */
129
- const setResponseContentLengthAttribute = (response, attributes) => {
130
- const length = getContentLength(response.headers);
131
- if (length === null)
132
- return;
133
- if ((0, exports.isCompressed)(response.headers)) {
134
- attributes[semconv_1.ATTR_HTTP_RESPONSE_CONTENT_LENGTH] = length;
135
- }
136
- else {
137
- attributes[semconv_1.ATTR_HTTP_RESPONSE_CONTENT_LENGTH_UNCOMPRESSED] = length;
138
- }
139
- };
140
- exports.setResponseContentLengthAttribute = setResponseContentLengthAttribute;
141
- function getContentLength(headers) {
142
- const contentLengthHeader = headers['content-length'];
143
- if (contentLengthHeader === undefined)
144
- return null;
145
- const contentLength = parseInt(contentLengthHeader, 10);
146
- if (isNaN(contentLength))
147
- return null;
148
- return contentLength;
149
- }
150
105
  const isCompressed = (headers) => {
151
106
  const encoding = headers['content-encoding'];
152
107
  return !!encoding && encoding !== 'identity';
@@ -298,9 +253,8 @@ exports.extractHostnameAndPort = extractHostnameAndPort;
298
253
  * Returns outgoing request attributes scoped to the options passed to the request
299
254
  * @param {ParsedRequestOptions} requestOptions the same options used to make the request
300
255
  * @param {{ component: string, hostname: string, hookAttributes?: Attributes }} options used to pass data needed to create attributes
301
- * @param {SemconvStability} semconvStability determines which semconv version to use
302
256
  */
303
- const getOutgoingRequestAttributes = (requestOptions, options, semconvStability, enableSyntheticSourceDetection) => {
257
+ const getOutgoingRequestAttributes = (requestOptions, options, enableSyntheticSourceDetection) => {
304
258
  const hostname = options.hostname;
305
259
  const port = options.port;
306
260
  const method = requestOptions.method ?? 'GET';
@@ -308,14 +262,7 @@ const getOutgoingRequestAttributes = (requestOptions, options, semconvStability,
308
262
  const headers = (requestOptions.headers || {});
309
263
  const userAgent = headers['user-agent'];
310
264
  const urlFull = (0, exports.getAbsoluteUrl)(requestOptions, headers, `${options.component}:`, options.redactedQueryParams);
311
- const oldAttributes = {
312
- [semconv_1.ATTR_HTTP_URL]: urlFull,
313
- [semconv_1.ATTR_HTTP_METHOD]: method,
314
- [semconv_1.ATTR_HTTP_TARGET]: requestOptions.path || '/',
315
- [semconv_1.ATTR_NET_PEER_NAME]: hostname,
316
- [semconv_1.ATTR_HTTP_HOST]: headers.host ?? `${hostname}:${port}`,
317
- };
318
- const newAttributes = {
265
+ const attributes = {
319
266
  // Required attributes
320
267
  [semantic_conventions_1.ATTR_HTTP_REQUEST_METHOD]: normalizedMethod,
321
268
  [semantic_conventions_1.ATTR_SERVER_ADDRESS]: hostname,
@@ -329,51 +276,14 @@ const getOutgoingRequestAttributes = (requestOptions, options, semconvStability,
329
276
  };
330
277
  // conditionally required if request method required case normalization
331
278
  if (method !== normalizedMethod) {
332
- newAttributes[semantic_conventions_1.ATTR_HTTP_REQUEST_METHOD_ORIGINAL] = method;
279
+ attributes[semantic_conventions_1.ATTR_HTTP_REQUEST_METHOD_ORIGINAL] = method;
333
280
  }
334
281
  if (enableSyntheticSourceDetection && userAgent) {
335
- newAttributes[semconv_1.ATTR_USER_AGENT_SYNTHETIC_TYPE] = getSyntheticType(userAgent);
282
+ attributes[semconv_1.ATTR_USER_AGENT_SYNTHETIC_TYPE] = getSyntheticType(userAgent);
336
283
  }
337
- if (userAgent !== undefined) {
338
- oldAttributes[semconv_1.ATTR_HTTP_USER_AGENT] = userAgent;
339
- }
340
- switch (semconvStability) {
341
- case instrumentation_1.SemconvStability.STABLE:
342
- return Object.assign(newAttributes, options.hookAttributes);
343
- case instrumentation_1.SemconvStability.OLD:
344
- return Object.assign(oldAttributes, options.hookAttributes);
345
- }
346
- return Object.assign(oldAttributes, newAttributes, options.hookAttributes);
284
+ return Object.assign(attributes, options.hookAttributes);
347
285
  };
348
286
  exports.getOutgoingRequestAttributes = getOutgoingRequestAttributes;
349
- /**
350
- * Returns outgoing request Metric attributes scoped to the request data
351
- * @param {Attributes} spanAttributes the span attributes
352
- */
353
- const getOutgoingRequestMetricAttributes = (spanAttributes) => {
354
- const metricAttributes = {};
355
- metricAttributes[semconv_1.ATTR_HTTP_METHOD] = spanAttributes[semconv_1.ATTR_HTTP_METHOD];
356
- metricAttributes[semconv_1.ATTR_NET_PEER_NAME] = spanAttributes[semconv_1.ATTR_NET_PEER_NAME];
357
- //TODO: http.url attribute, it should substitute any parameters to avoid high cardinality.
358
- return metricAttributes;
359
- };
360
- exports.getOutgoingRequestMetricAttributes = getOutgoingRequestMetricAttributes;
361
- /**
362
- * Returns attributes related to the kind of HTTP protocol used
363
- * @param {string} [kind] Kind of HTTP protocol used: "1.0", "1.1", "2", "SPDY" or "QUIC".
364
- */
365
- const setAttributesFromHttpKind = (kind, attributes) => {
366
- if (kind) {
367
- attributes[semconv_1.ATTR_HTTP_FLAVOR] = kind;
368
- if (kind.toUpperCase() !== 'QUIC') {
369
- attributes[semconv_1.ATTR_NET_TRANSPORT] = semconv_1.NET_TRANSPORT_VALUE_IP_TCP;
370
- }
371
- else {
372
- attributes[semconv_1.ATTR_NET_TRANSPORT] = semconv_1.NET_TRANSPORT_VALUE_IP_UDP;
373
- }
374
- }
375
- };
376
- exports.setAttributesFromHttpKind = setAttributesFromHttpKind;
377
287
  /**
378
288
  * Returns the type of synthetic source based on the user agent
379
289
  * @param {OutgoingHttpHeader} userAgent the user agent string
@@ -395,61 +305,41 @@ const getSyntheticType = (userAgent) => {
395
305
  /**
396
306
  * Returns outgoing request attributes scoped to the response data
397
307
  * @param {IncomingMessage} response the response object
398
- * @param {SemconvStability} semconvStability determines which semconv version to use
399
308
  */
400
- const getOutgoingRequestAttributesOnResponse = (response, semconvStability) => {
401
- const { statusCode, statusMessage, httpVersion, socket } = response;
402
- const oldAttributes = {};
403
- const stableAttributes = {};
309
+ const getOutgoingRequestAttributesOnResponse = (response) => {
310
+ const { statusCode, socket } = response;
311
+ const attributes = {};
404
312
  if (statusCode != null) {
405
- stableAttributes[semantic_conventions_1.ATTR_HTTP_RESPONSE_STATUS_CODE] = statusCode;
313
+ attributes[semantic_conventions_1.ATTR_HTTP_RESPONSE_STATUS_CODE] = statusCode;
406
314
  }
407
315
  if (socket) {
408
316
  const { remoteAddress, remotePort } = socket;
409
- oldAttributes[semconv_1.ATTR_NET_PEER_IP] = remoteAddress;
410
- oldAttributes[semconv_1.ATTR_NET_PEER_PORT] = remotePort;
411
317
  // Recommended
412
- stableAttributes[semantic_conventions_1.ATTR_NETWORK_PEER_ADDRESS] = remoteAddress;
413
- stableAttributes[semantic_conventions_1.ATTR_NETWORK_PEER_PORT] = remotePort;
414
- stableAttributes[semantic_conventions_1.ATTR_NETWORK_PROTOCOL_VERSION] = response.httpVersion;
415
- }
416
- (0, exports.setResponseContentLengthAttribute)(response, oldAttributes);
417
- if (statusCode) {
418
- oldAttributes[semconv_1.ATTR_HTTP_STATUS_CODE] = statusCode;
419
- oldAttributes[AttributeNames_1.AttributeNames.HTTP_STATUS_TEXT] = (statusMessage || '').toUpperCase();
318
+ attributes[semantic_conventions_1.ATTR_NETWORK_PEER_ADDRESS] = remoteAddress;
319
+ attributes[semantic_conventions_1.ATTR_NETWORK_PEER_PORT] = remotePort;
320
+ attributes[semantic_conventions_1.ATTR_NETWORK_PROTOCOL_VERSION] = response.httpVersion;
420
321
  }
421
- (0, exports.setAttributesFromHttpKind)(httpVersion, oldAttributes);
422
- switch (semconvStability) {
423
- case instrumentation_1.SemconvStability.STABLE:
424
- return stableAttributes;
425
- case instrumentation_1.SemconvStability.OLD:
426
- return oldAttributes;
427
- }
428
- return Object.assign(oldAttributes, stableAttributes);
322
+ return attributes;
429
323
  };
430
324
  exports.getOutgoingRequestAttributesOnResponse = getOutgoingRequestAttributesOnResponse;
431
325
  /**
432
326
  * Returns outgoing request Metric attributes scoped to the response data
433
327
  * @param {Attributes} spanAttributes the span attributes
434
328
  */
435
- const getOutgoingRequestMetricAttributesOnResponse = (spanAttributes) => {
436
- const metricAttributes = {};
437
- metricAttributes[semconv_1.ATTR_NET_PEER_PORT] = spanAttributes[semconv_1.ATTR_NET_PEER_PORT];
438
- metricAttributes[semconv_1.ATTR_HTTP_STATUS_CODE] =
439
- spanAttributes[semconv_1.ATTR_HTTP_STATUS_CODE];
440
- metricAttributes[semconv_1.ATTR_HTTP_FLAVOR] = spanAttributes[semconv_1.ATTR_HTTP_FLAVOR];
441
- return metricAttributes;
442
- };
443
- exports.getOutgoingRequestMetricAttributesOnResponse = getOutgoingRequestMetricAttributesOnResponse;
444
329
  const getOutgoingStableRequestMetricAttributesOnResponse = (spanAttributes) => {
445
330
  const metricAttributes = {};
446
331
  if (spanAttributes[semantic_conventions_1.ATTR_NETWORK_PROTOCOL_VERSION]) {
447
332
  metricAttributes[semantic_conventions_1.ATTR_NETWORK_PROTOCOL_VERSION] =
448
333
  spanAttributes[semantic_conventions_1.ATTR_NETWORK_PROTOCOL_VERSION];
449
334
  }
450
- if (spanAttributes[semantic_conventions_1.ATTR_HTTP_RESPONSE_STATUS_CODE]) {
451
- metricAttributes[semantic_conventions_1.ATTR_HTTP_RESPONSE_STATUS_CODE] =
452
- spanAttributes[semantic_conventions_1.ATTR_HTTP_RESPONSE_STATUS_CODE];
335
+ const statusCode = spanAttributes[semantic_conventions_1.ATTR_HTTP_RESPONSE_STATUS_CODE];
336
+ if (statusCode) {
337
+ metricAttributes[semantic_conventions_1.ATTR_HTTP_RESPONSE_STATUS_CODE] = statusCode;
338
+ if (typeof statusCode === 'number' &&
339
+ statusCode >= 400 &&
340
+ statusCode < 600) {
341
+ metricAttributes[semantic_conventions_1.ATTR_ERROR_TYPE] ??= String(statusCode);
342
+ }
453
343
  }
454
344
  return metricAttributes;
455
345
  };
@@ -618,154 +508,66 @@ function getInfoFromIncomingMessage(component, request, logger) {
618
508
  /**
619
509
  * Returns incoming request attributes scoped to the request data
620
510
  * @param {IncomingMessage} request the request object
621
- * @param {{ component: string, serverName?: string, hookAttributes?: Attributes }} options used to pass data needed to create attributes
622
- * @param {SemconvStability} semconvStability determines which semconv version to use
511
+ * @param {{ component: string, hookAttributes?: Attributes, enableSyntheticSourceDetection: boolean }} options used to pass data needed to create attributes
623
512
  */
624
513
  const getIncomingRequestAttributes = (request, options, logger) => {
625
- const { component, enableSyntheticSourceDetection, hookAttributes, semconvStability, serverName, } = options;
626
- const { headers, httpVersion, method } = request;
627
- const { host, 'user-agent': userAgent, 'x-forwarded-for': ips } = headers;
514
+ const { component, enableSyntheticSourceDetection, hookAttributes, redactedQueryParams, } = options;
515
+ const { headers, method } = request;
516
+ const { 'user-agent': userAgent } = headers;
628
517
  const parsedUrl = getInfoFromIncomingMessage(component, request, logger);
629
- let newAttributes;
630
- let oldAttributes;
631
- if (semconvStability !== instrumentation_1.SemconvStability.OLD) {
632
- // Stable attributes are used.
633
- const normalizedMethod = normalizeMethod(method);
634
- const serverAddress = getServerAddress(request, component);
635
- const remoteClientAddress = getRemoteClientAddress(request);
636
- newAttributes = {
637
- [semantic_conventions_1.ATTR_HTTP_REQUEST_METHOD]: normalizedMethod,
638
- [semantic_conventions_1.ATTR_URL_SCHEME]: component,
639
- [semantic_conventions_1.ATTR_SERVER_ADDRESS]: serverAddress?.host,
640
- [semantic_conventions_1.ATTR_NETWORK_PEER_ADDRESS]: request.socket.remoteAddress,
641
- [semantic_conventions_1.ATTR_NETWORK_PEER_PORT]: request.socket.remotePort,
642
- [semantic_conventions_1.ATTR_NETWORK_PROTOCOL_VERSION]: request.httpVersion,
643
- [semantic_conventions_1.ATTR_USER_AGENT_ORIGINAL]: userAgent,
644
- };
645
- if (parsedUrl.pathname != null) {
646
- newAttributes[semantic_conventions_1.ATTR_URL_PATH] = parsedUrl.pathname;
647
- }
648
- if (parsedUrl.search) {
649
- // Remove leading '?' from URL search (https://developer.mozilla.org/en-US/docs/Web/API/URL/search).
650
- newAttributes[semantic_conventions_1.ATTR_URL_QUERY] = parsedUrl.search.slice(1);
651
- }
652
- if (remoteClientAddress != null) {
653
- newAttributes[semantic_conventions_1.ATTR_CLIENT_ADDRESS] = remoteClientAddress;
654
- }
655
- if (serverAddress?.port != null) {
656
- newAttributes[semantic_conventions_1.ATTR_SERVER_PORT] = Number(serverAddress.port);
657
- }
658
- // Conditionally required if request method required case normalization.
659
- if (method !== normalizedMethod) {
660
- newAttributes[semantic_conventions_1.ATTR_HTTP_REQUEST_METHOD_ORIGINAL] = method;
661
- }
662
- if (enableSyntheticSourceDetection && userAgent) {
663
- newAttributes[semconv_1.ATTR_USER_AGENT_SYNTHETIC_TYPE] =
664
- getSyntheticType(userAgent);
665
- }
518
+ // Stable attributes are used.
519
+ const normalizedMethod = normalizeMethod(method);
520
+ const serverAddress = getServerAddress(request, component);
521
+ const remoteClientAddress = getRemoteClientAddress(request);
522
+ const attributes = {
523
+ [semantic_conventions_1.ATTR_HTTP_REQUEST_METHOD]: normalizedMethod,
524
+ [semantic_conventions_1.ATTR_URL_SCHEME]: component,
525
+ [semantic_conventions_1.ATTR_SERVER_ADDRESS]: serverAddress?.host,
526
+ [semantic_conventions_1.ATTR_NETWORK_PEER_ADDRESS]: request.socket.remoteAddress,
527
+ [semantic_conventions_1.ATTR_NETWORK_PEER_PORT]: request.socket.remotePort,
528
+ [semantic_conventions_1.ATTR_NETWORK_PROTOCOL_VERSION]: request.httpVersion,
529
+ [semantic_conventions_1.ATTR_USER_AGENT_ORIGINAL]: userAgent,
530
+ };
531
+ if (parsedUrl.pathname != null) {
532
+ attributes[semantic_conventions_1.ATTR_URL_PATH] = parsedUrl.pathname;
666
533
  }
667
- if (semconvStability !== instrumentation_1.SemconvStability.STABLE) {
668
- // Old attributes are used.
669
- const hostname = host?.replace(/^(.*)(:[0-9]{1,5})/, '$1') || 'localhost';
670
- oldAttributes = {
671
- [semconv_1.ATTR_HTTP_URL]: parsedUrl.toString(),
672
- [semconv_1.ATTR_HTTP_HOST]: host,
673
- [semconv_1.ATTR_NET_HOST_NAME]: hostname,
674
- [semconv_1.ATTR_HTTP_METHOD]: method,
675
- [semconv_1.ATTR_HTTP_SCHEME]: component,
676
- };
677
- if (typeof ips === 'string') {
678
- oldAttributes[semconv_1.ATTR_HTTP_CLIENT_IP] = ips.split(',')[0];
679
- }
680
- if (typeof serverName === 'string') {
681
- oldAttributes[semconv_1.ATTR_HTTP_SERVER_NAME] = serverName;
682
- }
683
- if (parsedUrl.pathname) {
684
- oldAttributes[semconv_1.ATTR_HTTP_TARGET] =
685
- parsedUrl.pathname + parsedUrl.search || '/';
686
- }
687
- if (userAgent !== undefined) {
688
- oldAttributes[semconv_1.ATTR_HTTP_USER_AGENT] = userAgent;
689
- }
690
- (0, exports.setRequestContentLengthAttribute)(request, oldAttributes);
691
- (0, exports.setAttributesFromHttpKind)(httpVersion, oldAttributes);
534
+ if (parsedUrl.search) {
535
+ // Remove leading '?' from URL search (https://developer.mozilla.org/en-US/docs/Web/API/URL/search).
536
+ const paramsToRedact = redactedQueryParams ?? defaultQueryStringsToRedact;
537
+ attributes[semantic_conventions_1.ATTR_URL_QUERY] = (0, exports.redactQueryString)(new URLSearchParams(parsedUrl.search.slice(1)), paramsToRedact);
692
538
  }
693
- switch (semconvStability) {
694
- case instrumentation_1.SemconvStability.STABLE:
695
- return Object.assign(newAttributes, hookAttributes);
696
- case instrumentation_1.SemconvStability.OLD:
697
- return Object.assign(oldAttributes, hookAttributes);
698
- default:
699
- return Object.assign(oldAttributes, newAttributes, hookAttributes);
539
+ if (remoteClientAddress != null) {
540
+ attributes[semantic_conventions_1.ATTR_CLIENT_ADDRESS] = remoteClientAddress;
541
+ }
542
+ if (serverAddress?.port != null) {
543
+ attributes[semantic_conventions_1.ATTR_SERVER_PORT] = Number(serverAddress.port);
544
+ }
545
+ // Conditionally required if request method required case normalization.
546
+ if (method !== normalizedMethod) {
547
+ attributes[semantic_conventions_1.ATTR_HTTP_REQUEST_METHOD_ORIGINAL] = method;
548
+ }
549
+ if (enableSyntheticSourceDetection && userAgent) {
550
+ attributes[semconv_1.ATTR_USER_AGENT_SYNTHETIC_TYPE] = getSyntheticType(userAgent);
700
551
  }
552
+ return Object.assign(attributes, hookAttributes);
701
553
  };
702
554
  exports.getIncomingRequestAttributes = getIncomingRequestAttributes;
703
- /**
704
- * Returns incoming request Metric attributes scoped to the request data
705
- * @param {Attributes} spanAttributes the span attributes
706
- * @param {{ component: string }} options used to pass data needed to create attributes
707
- */
708
- const getIncomingRequestMetricAttributes = (spanAttributes) => {
709
- const metricAttributes = {};
710
- metricAttributes[semconv_1.ATTR_HTTP_SCHEME] = spanAttributes[semconv_1.ATTR_HTTP_SCHEME];
711
- metricAttributes[semconv_1.ATTR_HTTP_METHOD] = spanAttributes[semconv_1.ATTR_HTTP_METHOD];
712
- metricAttributes[semconv_1.ATTR_NET_HOST_NAME] = spanAttributes[semconv_1.ATTR_NET_HOST_NAME];
713
- metricAttributes[semconv_1.ATTR_HTTP_FLAVOR] = spanAttributes[semconv_1.ATTR_HTTP_FLAVOR];
714
- //TODO: http.target attribute, it should substitute any parameters to avoid high cardinality.
715
- return metricAttributes;
716
- };
717
- exports.getIncomingRequestMetricAttributes = getIncomingRequestMetricAttributes;
718
555
  /**
719
556
  * Returns incoming request attributes scoped to the response data
720
557
  * @param {(ServerResponse & { socket: Socket; })} response the response object
721
558
  */
722
- const getIncomingRequestAttributesOnResponse = (request, response, semconvStability) => {
723
- // take socket from the request,
724
- // since it may be detached from the response object in keep-alive mode
725
- const { socket } = request;
726
- const { statusCode, statusMessage } = response;
727
- const newAttributes = {
559
+ const getIncomingRequestAttributesOnResponse = (response) => {
560
+ const { statusCode } = response;
561
+ const attributes = {
728
562
  [semantic_conventions_1.ATTR_HTTP_RESPONSE_STATUS_CODE]: statusCode,
729
563
  };
730
564
  const rpcMetadata = (0, core_1.getRPCMetadata)(api_1.context.active());
731
- const oldAttributes = {};
732
- if (socket) {
733
- const { localAddress, localPort, remoteAddress, remotePort } = socket;
734
- oldAttributes[semconv_1.ATTR_NET_HOST_IP] = localAddress;
735
- oldAttributes[semconv_1.ATTR_NET_HOST_PORT] = localPort;
736
- oldAttributes[semconv_1.ATTR_NET_PEER_IP] = remoteAddress;
737
- oldAttributes[semconv_1.ATTR_NET_PEER_PORT] = remotePort;
738
- }
739
- oldAttributes[semconv_1.ATTR_HTTP_STATUS_CODE] = statusCode;
740
- oldAttributes[AttributeNames_1.AttributeNames.HTTP_STATUS_TEXT] = (statusMessage || '').toUpperCase();
741
565
  if (rpcMetadata?.type === core_1.RPCType.HTTP && rpcMetadata.route !== undefined) {
742
- oldAttributes[semantic_conventions_1.ATTR_HTTP_ROUTE] = rpcMetadata.route;
743
- newAttributes[semantic_conventions_1.ATTR_HTTP_ROUTE] = rpcMetadata.route;
744
- }
745
- switch (semconvStability) {
746
- case instrumentation_1.SemconvStability.STABLE:
747
- return newAttributes;
748
- case instrumentation_1.SemconvStability.OLD:
749
- return oldAttributes;
566
+ attributes[semantic_conventions_1.ATTR_HTTP_ROUTE] = rpcMetadata.route;
750
567
  }
751
- return Object.assign(oldAttributes, newAttributes);
568
+ return attributes;
752
569
  };
753
570
  exports.getIncomingRequestAttributesOnResponse = getIncomingRequestAttributesOnResponse;
754
- /**
755
- * Returns incoming request Metric attributes scoped to the request data
756
- * @param {Attributes} spanAttributes the span attributes
757
- */
758
- const getIncomingRequestMetricAttributesOnResponse = (spanAttributes) => {
759
- const metricAttributes = {};
760
- metricAttributes[semconv_1.ATTR_HTTP_STATUS_CODE] =
761
- spanAttributes[semconv_1.ATTR_HTTP_STATUS_CODE];
762
- metricAttributes[semconv_1.ATTR_NET_HOST_PORT] = spanAttributes[semconv_1.ATTR_NET_HOST_PORT];
763
- if (spanAttributes[semantic_conventions_1.ATTR_HTTP_ROUTE] !== undefined) {
764
- metricAttributes[semantic_conventions_1.ATTR_HTTP_ROUTE] = spanAttributes[semantic_conventions_1.ATTR_HTTP_ROUTE];
765
- }
766
- return metricAttributes;
767
- };
768
- exports.getIncomingRequestMetricAttributesOnResponse = getIncomingRequestMetricAttributesOnResponse;
769
571
  /**
770
572
  * Returns incoming stable request Metric attributes scoped to the request data
771
573
  * @param {Attributes} spanAttributes the span attributes
@@ -776,25 +578,23 @@ const getIncomingStableRequestMetricAttributesOnResponse = (spanAttributes) => {
776
578
  metricAttributes[semantic_conventions_1.ATTR_HTTP_ROUTE] = spanAttributes[semantic_conventions_1.ATTR_HTTP_ROUTE];
777
579
  }
778
580
  // required if and only if one was sent, same as span requirement
779
- if (spanAttributes[semantic_conventions_1.ATTR_HTTP_RESPONSE_STATUS_CODE]) {
780
- metricAttributes[semantic_conventions_1.ATTR_HTTP_RESPONSE_STATUS_CODE] =
781
- spanAttributes[semantic_conventions_1.ATTR_HTTP_RESPONSE_STATUS_CODE];
581
+ const statusCode = spanAttributes[semantic_conventions_1.ATTR_HTTP_RESPONSE_STATUS_CODE];
582
+ if (statusCode) {
583
+ metricAttributes[semantic_conventions_1.ATTR_HTTP_RESPONSE_STATUS_CODE] = statusCode;
584
+ if (typeof statusCode === 'number' &&
585
+ statusCode >= 500 &&
586
+ statusCode < 600) {
587
+ metricAttributes[semantic_conventions_1.ATTR_ERROR_TYPE] ??= String(statusCode);
588
+ }
782
589
  }
783
590
  return metricAttributes;
784
591
  };
785
592
  exports.getIncomingStableRequestMetricAttributesOnResponse = getIncomingStableRequestMetricAttributesOnResponse;
786
- function headerCapture(type, headers, semconvStability) {
593
+ function headerCapture(type, headers) {
787
594
  const normalizedHeaders = new Map();
788
595
  for (let i = 0, len = headers.length; i < len; i++) {
789
596
  const capturedHeader = headers[i].toLowerCase();
790
- if (semconvStability & instrumentation_1.SemconvStability.STABLE) {
791
- normalizedHeaders.set(capturedHeader, capturedHeader);
792
- }
793
- else {
794
- // In old semconv, the header name converted hypen to underscore, e.g.:
795
- // `http.request.header.content_length`.
796
- normalizedHeaders.set(capturedHeader, capturedHeader.replaceAll('-', '_'));
797
- }
597
+ normalizedHeaders.set(capturedHeader, capturedHeader);
798
598
  }
799
599
  return (getHeader) => {
800
600
  const attributes = {};