Skip to content

Commit

Permalink
fix(http-intrumentation): prevent request socket null from throwing u…
Browse files Browse the repository at this point in the history
…ncaught error (#3858)

Co-authored-by: Marc Pichler <marc.pichler@dynatrace.com>
  • Loading branch information
aodysseos and pichlermarc committed Jun 29, 2023
1 parent b3d57bb commit bb8a4f7
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 15 deletions.
1 change: 1 addition & 0 deletions experimental/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit bb8a4f7

Please sign in to comment.