From b1adb07a39fd30dfe2fc865bd4a2a03d4ffd1875 Mon Sep 17 00:00:00 2001 From: Samuron Date: Sat, 23 Mar 2024 12:55:29 +0200 Subject: [PATCH 1/2] perf(instrumentation-http): remove obvious temp allocations --- CHANGELOG.md | 2 + .../src/utils.ts | 39 +++++++++---------- 2 files changed, 20 insertions(+), 21 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f2980539eb..80d1db9a43 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,8 @@ For experimental package changes, see the [experimental CHANGELOG](experimental/ ### :rocket: (Enhancement) +* perf(instrumentation-http): remove obvious temp allocations + ### :bug: (Bug Fix) * fix(sdk-metrics): increase the depth of the output to the console such that objects in the metric are printed fully to the console [#4522](https://github.com/open-telemetry/opentelemetry-js/pull/4522) @JacksonWeber diff --git a/experimental/packages/opentelemetry-instrumentation-http/src/utils.ts b/experimental/packages/opentelemetry-instrumentation-http/src/utils.ts index a36d1f44cd..8191af242f 100644 --- a/experimental/packages/opentelemetry-instrumentation-http/src/utils.ts +++ b/experimental/packages/opentelemetry-instrumentation-http/src/utils.ts @@ -167,11 +167,8 @@ export const isIgnored = ( export const setSpanWithError = (span: Span, error: Err): void => { const message = error.message; - span.setAttributes({ - [AttributeNames.HTTP_ERROR_NAME]: error.name, - [AttributeNames.HTTP_ERROR_MESSAGE]: message, - }); - + span.setAttribute(AttributeNames.HTTP_ERROR_NAME, error.name); + span.setAttribute(AttributeNames.HTTP_ERROR_MESSAGE, message); span.setStatus({ code: SpanStatusCode.ERROR, message }); span.recordException(error); }; @@ -371,7 +368,7 @@ export const getOutgoingRequestAttributes = ( [SEMATTRS_HTTP_METHOD]: method, [SEMATTRS_HTTP_TARGET]: requestOptions.path || '/', [SEMATTRS_NET_PEER_NAME]: hostname, - [SEMATTRS_HTTP_HOST]: requestOptions.headers?.host ?? `${hostname}:${port}`, + [SEMATTRS_HTTP_HOST]: headers.host ?? `${hostname}:${port}`, }; if (userAgent !== undefined) { @@ -399,8 +396,10 @@ export const getOutgoingRequestMetricAttributes = ( * Returns attributes related to the kind of HTTP protocol used * @param {string} [kind] Kind of HTTP protocol used: "1.0", "1.1", "2", "SPDY" or "QUIC". */ -export const getAttributesFromHttpKind = (kind?: string): SpanAttributes => { - const attributes: SpanAttributes = {}; +export const setAttributesFromHttpKind = ( + kind: string | undefined, + attributes: SpanAttributes +): void => { if (kind) { attributes[SEMATTRS_HTTP_FLAVOR] = kind; if (kind.toUpperCase() !== 'QUIC') { @@ -409,7 +408,6 @@ export const getAttributesFromHttpKind = (kind?: string): SpanAttributes => { attributes[SEMATTRS_NET_TRANSPORT] = NETTRANSPORTVALUES_IP_UDP; } } - return attributes; }; /** @@ -436,8 +434,8 @@ export const getOutgoingRequestAttributesOnResponse = ( ).toUpperCase(); } - const httpKindAttributes = getAttributesFromHttpKind(httpVersion); - return Object.assign(attributes, httpKindAttributes); + setAttributesFromHttpKind(httpVersion, attributes); + return attributes; }; /** @@ -509,9 +507,8 @@ export const getIncomingRequestAttributes = ( attributes[SEMATTRS_HTTP_USER_AGENT] = userAgent; } setRequestContentLengthAttribute(request, attributes); - - const httpKindAttributes = getAttributesFromHttpKind(httpVersion); - return Object.assign(attributes, httpKindAttributes, options.hookAttributes); + setAttributesFromHttpKind(httpVersion, attributes); + return Object.assign(attributes, options.hookAttributes); }; /** @@ -584,24 +581,24 @@ export const getIncomingRequestMetricAttributesOnResponse = ( }; export function headerCapture(type: 'request' | 'response', headers: string[]) { - const normalizedHeaders = new Map( - headers.map(header => [ - header.toLowerCase(), - header.toLowerCase().replace(/-/g, '_'), - ]) - ); + const normalizedHeaders = new Map(); + for (let i = 0, len = headers.length; i < len; i++) { + const capturedHeader = headers[i].toLowerCase(); + normalizedHeaders.set(capturedHeader, capturedHeader.replace(/-/g, '_')); + } return ( span: Span, getHeader: (key: string) => undefined | string | string[] | number ) => { - for (const [capturedHeader, normalizedHeader] of normalizedHeaders) { + for (const capturedHeader of normalizedHeaders.keys()) { const value = getHeader(capturedHeader); if (value === undefined) { continue; } + const normalizedHeader = normalizedHeaders.get(capturedHeader); const key = `http.${type}.header.${normalizedHeader}`; if (typeof value === 'string') { From bd85347bc867e5b2fa797cecf84ec7e9c713d98e Mon Sep 17 00:00:00 2001 From: Samuron Date: Thu, 28 Mar 2024 17:54:24 +0200 Subject: [PATCH 2/2] fix: changelog --- CHANGELOG.md | 2 -- experimental/CHANGELOG.md | 1 + 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 80d1db9a43..f2980539eb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,8 +11,6 @@ For experimental package changes, see the [experimental CHANGELOG](experimental/ ### :rocket: (Enhancement) -* perf(instrumentation-http): remove obvious temp allocations - ### :bug: (Bug Fix) * fix(sdk-metrics): increase the depth of the output to the console such that objects in the metric are printed fully to the console [#4522](https://github.com/open-telemetry/opentelemetry-js/pull/4522) @JacksonWeber diff --git a/experimental/CHANGELOG.md b/experimental/CHANGELOG.md index 0db63bb216..6c4b94afbd 100644 --- a/experimental/CHANGELOG.md +++ b/experimental/CHANGELOG.md @@ -23,6 +23,7 @@ All notable changes to experimental packages in this project will be documented ### :rocket: (Enhancement) * refactor(instr-http): use exported strings for semconv. [#4573](https://github.com/open-telemetry/opentelemetry-js/pull/4573/) @JamieDanielson +* perf(instrumentation-http): remove obvious temp allocations [#4576](https://github.com/open-telemetry/opentelemetry-js/pull/4576) @Samuron ### :bug: (Bug Fix)