Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf(instrumentation-http): remove obvious temp allocations #4576

Merged
merged 3 commits into from
Mar 29, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ For experimental package changes, see the [experimental CHANGELOG](experimental/

### :rocket: (Enhancement)

* perf(instrumentation-http): remove obvious temp allocations
pichlermarc marked this conversation as resolved.
Show resolved Hide resolved

### :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
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