Skip to content

Commit

Permalink
perf(instrumentation-http): remove obvious temp allocations (#4576)
Browse files Browse the repository at this point in the history
* perf(instrumentation-http): remove obvious temp allocations

* fix: changelog

---------

Co-authored-by: Marc Pichler <marc.pichler@dynatrace.com>
  • Loading branch information
Samuron and pichlermarc committed Mar 29, 2024
1 parent 9a5688e commit b418d36
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 21 deletions.
1 change: 1 addition & 0 deletions experimental/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ All notable changes to experimental packages in this project will be documented

* feat(opentelemetry-instrumentation-xhr): optionally ignore network events [#4571](https://github.com/open-telemetry/opentelemetry-js/pull/4571/) @mustafahaddara
* 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
* feat(sdk-node): add `HostDetector` as default resource detector

### :bug: (Bug Fix)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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') {
Expand All @@ -409,7 +408,6 @@ export const getAttributesFromHttpKind = (kind?: string): SpanAttributes => {
attributes[SEMATTRS_NET_TRANSPORT] = NETTRANSPORTVALUES_IP_UDP;
}
}
return attributes;
};

/**
Expand All @@ -436,8 +434,8 @@ export const getOutgoingRequestAttributesOnResponse = (
).toUpperCase();
}

const httpKindAttributes = getAttributesFromHttpKind(httpVersion);
return Object.assign(attributes, httpKindAttributes);
setAttributesFromHttpKind(httpVersion, attributes);
return attributes;
};

/**
Expand Down Expand Up @@ -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);
};

/**
Expand Down Expand Up @@ -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<string, string>();
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') {
Expand Down

0 comments on commit b418d36

Please sign in to comment.