From bb8a4f7ab71494a59b42daf6958a6f10d1ce6870 Mon Sep 17 00:00:00 2001 From: Andreas Odysseos Date: Thu, 29 Jun 2023 15:59:09 +0300 Subject: [PATCH] fix(http-intrumentation): prevent request socket null from throwing uncaught error (#3858) Co-authored-by: Marc Pichler --- experimental/CHANGELOG.md | 1 + .../src/utils.ts | 34 +++++++++++-------- 2 files changed, 20 insertions(+), 15 deletions(-) diff --git a/experimental/CHANGELOG.md b/experimental/CHANGELOG.md index dca209e1a64..af2ea3a26e0 100644 --- a/experimental/CHANGELOG.md +++ b/experimental/CHANGELOG.md @@ -18,6 +18,7 @@ All notable changes to experimental packages in this project will be documented * fix(exporter-logs-otlp-http): set useHex to true [#3875](https://github.com/open-telemetry/opentelemetry-js/pull/3875) @Nico385412 fix(otlp-proto-exporter-base): add missing type import [#3937](https://github.com/open-telemetry/opentelemetry-js/pull/3937) @pichlermarc * fix(example-opencensus-shim): avoid OpenCensus auto instrumentations [#3951](https://github.com/open-telemetry/opentelemetry-js/pull/3951) @llc1123 +* fix(http-intrumentation): prevent request socket null from throwing uncaught error [#3858](https://github.com/open-telemetry/opentelemetry-js/pull/3858) @aodysseos ### :books: (Refine Doc) diff --git a/experimental/packages/opentelemetry-instrumentation-http/src/utils.ts b/experimental/packages/opentelemetry-instrumentation-http/src/utils.ts index a563e294715..a70dd3de854 100644 --- a/experimental/packages/opentelemetry-instrumentation-http/src/utils.ts +++ b/experimental/packages/opentelemetry-instrumentation-http/src/utils.ts @@ -403,11 +403,12 @@ export const getOutgoingRequestAttributesOnResponse = ( response: IncomingMessage ): SpanAttributes => { const { statusCode, statusMessage, httpVersion, socket } = response; - const { remoteAddress, remotePort } = socket; - const attributes: SpanAttributes = { - [SemanticAttributes.NET_PEER_IP]: remoteAddress, - [SemanticAttributes.NET_PEER_PORT]: remotePort, - }; + const attributes: SpanAttributes = {}; + if (socket) { + const { remoteAddress, remotePort } = socket; + attributes[SemanticAttributes.NET_PEER_IP] = remoteAddress; + attributes[SemanticAttributes.NET_PEER_PORT] = remotePort; + } setResponseContentLengthAttribute(response, attributes); if (statusCode) { @@ -529,17 +530,20 @@ export const getIncomingRequestAttributesOnResponse = ( // since it may be detached from the response object in keep-alive mode const { socket } = request; const { statusCode, statusMessage } = response; - const { localAddress, localPort, remoteAddress, remotePort } = socket; - const rpcMetadata = getRPCMetadata(context.active()); - const attributes: SpanAttributes = { - [SemanticAttributes.NET_HOST_IP]: localAddress, - [SemanticAttributes.NET_HOST_PORT]: localPort, - [SemanticAttributes.NET_PEER_IP]: remoteAddress, - [SemanticAttributes.NET_PEER_PORT]: remotePort, - [SemanticAttributes.HTTP_STATUS_CODE]: statusCode, - [AttributeNames.HTTP_STATUS_TEXT]: (statusMessage || '').toUpperCase(), - }; + const rpcMetadata = getRPCMetadata(context.active()); + const attributes: SpanAttributes = {}; + if (socket) { + const { localAddress, localPort, remoteAddress, remotePort } = socket; + attributes[SemanticAttributes.NET_HOST_IP] = localAddress; + attributes[SemanticAttributes.NET_HOST_PORT] = localPort; + attributes[SemanticAttributes.NET_PEER_IP] = remoteAddress; + attributes[SemanticAttributes.NET_PEER_PORT] = remotePort; + } + attributes[SemanticAttributes.HTTP_STATUS_CODE] = statusCode; + attributes[AttributeNames.HTTP_STATUS_TEXT] = ( + statusMessage || '' + ).toUpperCase(); if (rpcMetadata?.type === RPCType.HTTP && rpcMetadata.route !== undefined) { attributes[SemanticAttributes.HTTP_ROUTE] = rpcMetadata.route;