Skip to content

Commit b418d36

Browse files
Samuronpichlermarc
andauthored
perf(instrumentation-http): remove obvious temp allocations (#4576)
* perf(instrumentation-http): remove obvious temp allocations * fix: changelog --------- Co-authored-by: Marc Pichler <[email protected]>
1 parent 9a5688e commit b418d36

File tree

2 files changed

+19
-21
lines changed

2 files changed

+19
-21
lines changed

experimental/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ All notable changes to experimental packages in this project will be documented
2626

2727
* feat(opentelemetry-instrumentation-xhr): optionally ignore network events [#4571](https://github.com/open-telemetry/opentelemetry-js/pull/4571/) @mustafahaddara
2828
* refactor(instr-http): use exported strings for semconv. [#4573](https://github.com/open-telemetry/opentelemetry-js/pull/4573/) @JamieDanielson
29+
* perf(instrumentation-http): remove obvious temp allocations [#4576](https://github.com/open-telemetry/opentelemetry-js/pull/4576) @Samuron
2930
* feat(sdk-node): add `HostDetector` as default resource detector
3031

3132
### :bug: (Bug Fix)

experimental/packages/opentelemetry-instrumentation-http/src/utils.ts

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -167,11 +167,8 @@ export const isIgnored = (
167167
export const setSpanWithError = (span: Span, error: Err): void => {
168168
const message = error.message;
169169

170-
span.setAttributes({
171-
[AttributeNames.HTTP_ERROR_NAME]: error.name,
172-
[AttributeNames.HTTP_ERROR_MESSAGE]: message,
173-
});
174-
170+
span.setAttribute(AttributeNames.HTTP_ERROR_NAME, error.name);
171+
span.setAttribute(AttributeNames.HTTP_ERROR_MESSAGE, message);
175172
span.setStatus({ code: SpanStatusCode.ERROR, message });
176173
span.recordException(error);
177174
};
@@ -371,7 +368,7 @@ export const getOutgoingRequestAttributes = (
371368
[SEMATTRS_HTTP_METHOD]: method,
372369
[SEMATTRS_HTTP_TARGET]: requestOptions.path || '/',
373370
[SEMATTRS_NET_PEER_NAME]: hostname,
374-
[SEMATTRS_HTTP_HOST]: requestOptions.headers?.host ?? `${hostname}:${port}`,
371+
[SEMATTRS_HTTP_HOST]: headers.host ?? `${hostname}:${port}`,
375372
};
376373

377374
if (userAgent !== undefined) {
@@ -399,8 +396,10 @@ export const getOutgoingRequestMetricAttributes = (
399396
* Returns attributes related to the kind of HTTP protocol used
400397
* @param {string} [kind] Kind of HTTP protocol used: "1.0", "1.1", "2", "SPDY" or "QUIC".
401398
*/
402-
export const getAttributesFromHttpKind = (kind?: string): SpanAttributes => {
403-
const attributes: SpanAttributes = {};
399+
export const setAttributesFromHttpKind = (
400+
kind: string | undefined,
401+
attributes: SpanAttributes
402+
): void => {
404403
if (kind) {
405404
attributes[SEMATTRS_HTTP_FLAVOR] = kind;
406405
if (kind.toUpperCase() !== 'QUIC') {
@@ -409,7 +408,6 @@ export const getAttributesFromHttpKind = (kind?: string): SpanAttributes => {
409408
attributes[SEMATTRS_NET_TRANSPORT] = NETTRANSPORTVALUES_IP_UDP;
410409
}
411410
}
412-
return attributes;
413411
};
414412

415413
/**
@@ -436,8 +434,8 @@ export const getOutgoingRequestAttributesOnResponse = (
436434
).toUpperCase();
437435
}
438436

439-
const httpKindAttributes = getAttributesFromHttpKind(httpVersion);
440-
return Object.assign(attributes, httpKindAttributes);
437+
setAttributesFromHttpKind(httpVersion, attributes);
438+
return attributes;
441439
};
442440

443441
/**
@@ -509,9 +507,8 @@ export const getIncomingRequestAttributes = (
509507
attributes[SEMATTRS_HTTP_USER_AGENT] = userAgent;
510508
}
511509
setRequestContentLengthAttribute(request, attributes);
512-
513-
const httpKindAttributes = getAttributesFromHttpKind(httpVersion);
514-
return Object.assign(attributes, httpKindAttributes, options.hookAttributes);
510+
setAttributesFromHttpKind(httpVersion, attributes);
511+
return Object.assign(attributes, options.hookAttributes);
515512
};
516513

517514
/**
@@ -584,24 +581,24 @@ export const getIncomingRequestMetricAttributesOnResponse = (
584581
};
585582

586583
export function headerCapture(type: 'request' | 'response', headers: string[]) {
587-
const normalizedHeaders = new Map(
588-
headers.map(header => [
589-
header.toLowerCase(),
590-
header.toLowerCase().replace(/-/g, '_'),
591-
])
592-
);
584+
const normalizedHeaders = new Map<string, string>();
585+
for (let i = 0, len = headers.length; i < len; i++) {
586+
const capturedHeader = headers[i].toLowerCase();
587+
normalizedHeaders.set(capturedHeader, capturedHeader.replace(/-/g, '_'));
588+
}
593589

594590
return (
595591
span: Span,
596592
getHeader: (key: string) => undefined | string | string[] | number
597593
) => {
598-
for (const [capturedHeader, normalizedHeader] of normalizedHeaders) {
594+
for (const capturedHeader of normalizedHeaders.keys()) {
599595
const value = getHeader(capturedHeader);
600596

601597
if (value === undefined) {
602598
continue;
603599
}
604600

601+
const normalizedHeader = normalizedHeaders.get(capturedHeader);
605602
const key = `http.${type}.header.${normalizedHeader}`;
606603

607604
if (typeof value === 'string') {

0 commit comments

Comments
 (0)