diff --git a/instrumentation-api-semconv/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/network/internal/InternalClientAttributesExtractor.java b/instrumentation-api-semconv/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/network/internal/InternalClientAttributesExtractor.java index a0d554f1b221..a388793acd9f 100644 --- a/instrumentation-api-semconv/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/network/internal/InternalClientAttributesExtractor.java +++ b/instrumentation-api-semconv/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/network/internal/InternalClientAttributesExtractor.java @@ -33,14 +33,16 @@ public InternalClientAttributesExtractor( public void onStart(AttributesBuilder attributes, REQUEST request) { AddressAndPort clientAddressAndPort = addressAndPortExtractor.extract(request); - if (emitStableUrlAttributes) { - internalSet(attributes, SemanticAttributes.CLIENT_ADDRESS, clientAddressAndPort.address); - if (clientAddressAndPort.port != null && clientAddressAndPort.port > 0) { - internalSet(attributes, SemanticAttributes.CLIENT_PORT, (long) clientAddressAndPort.port); + if (clientAddressAndPort.address != null) { + if (emitStableUrlAttributes) { + internalSet(attributes, SemanticAttributes.CLIENT_ADDRESS, clientAddressAndPort.address); + if (clientAddressAndPort.port != null && clientAddressAndPort.port > 0) { + internalSet(attributes, SemanticAttributes.CLIENT_PORT, (long) clientAddressAndPort.port); + } + } + if (emitOldHttpAttributes) { + internalSet(attributes, SemanticAttributes.HTTP_CLIENT_IP, clientAddressAndPort.address); } - } - if (emitOldHttpAttributes) { - internalSet(attributes, SemanticAttributes.HTTP_CLIENT_IP, clientAddressAndPort.address); } } } diff --git a/instrumentation-api-semconv/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/network/internal/InternalServerAttributesExtractor.java b/instrumentation-api-semconv/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/network/internal/InternalServerAttributesExtractor.java index e0611409c435..1111943fc0f9 100644 --- a/instrumentation-api-semconv/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/network/internal/InternalServerAttributesExtractor.java +++ b/instrumentation-api-semconv/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/network/internal/InternalServerAttributesExtractor.java @@ -40,21 +40,23 @@ public InternalServerAttributesExtractor( public void onStart(AttributesBuilder attributes, REQUEST request) { AddressAndPort serverAddressAndPort = addressAndPortExtractor.extract(request); - if (emitStableUrlAttributes) { - internalSet(attributes, SemanticAttributes.SERVER_ADDRESS, serverAddressAndPort.address); - } - if (emitOldHttpAttributes) { - internalSet(attributes, oldSemconvMode.address, serverAddressAndPort.address); - } - - if (serverAddressAndPort.port != null - && serverAddressAndPort.port > 0 - && captureServerPortCondition.test(serverAddressAndPort.port, request)) { + if (serverAddressAndPort.address != null) { if (emitStableUrlAttributes) { - internalSet(attributes, SemanticAttributes.SERVER_PORT, (long) serverAddressAndPort.port); + internalSet(attributes, SemanticAttributes.SERVER_ADDRESS, serverAddressAndPort.address); } if (emitOldHttpAttributes) { - internalSet(attributes, oldSemconvMode.port, (long) serverAddressAndPort.port); + internalSet(attributes, oldSemconvMode.address, serverAddressAndPort.address); + } + + if (serverAddressAndPort.port != null + && serverAddressAndPort.port > 0 + && captureServerPortCondition.test(serverAddressAndPort.port, request)) { + if (emitStableUrlAttributes) { + internalSet(attributes, SemanticAttributes.SERVER_PORT, (long) serverAddressAndPort.port); + } + if (emitOldHttpAttributes) { + internalSet(attributes, oldSemconvMode.port, (long) serverAddressAndPort.port); + } } } } diff --git a/instrumentation-api-semconv/src/testStableHttpSemconv/java/io/opentelemetry/instrumentation/api/instrumenter/network/ClientAttributesExtractorTest.java b/instrumentation-api-semconv/src/testStableHttpSemconv/java/io/opentelemetry/instrumentation/api/instrumenter/network/ClientAttributesExtractorTest.java index aa0ef6f785ba..4fa75ba03198 100644 --- a/instrumentation-api-semconv/src/testStableHttpSemconv/java/io/opentelemetry/instrumentation/api/instrumenter/network/ClientAttributesExtractorTest.java +++ b/instrumentation-api-semconv/src/testStableHttpSemconv/java/io/opentelemetry/instrumentation/api/instrumenter/network/ClientAttributesExtractorTest.java @@ -89,4 +89,21 @@ void doesNotSetNegativePortValue() { extractor.onEnd(endAttributes, Context.root(), request, null, null); assertThat(endAttributes.build()).isEmpty(); } + + @Test + void portWithoutAddress() { + Map request = new HashMap<>(); + request.put("port", "80"); + + AttributesExtractor, Void> extractor = + ClientAttributesExtractor.create(new TestClientAttributesGetter()); + + AttributesBuilder startAttributes = Attributes.builder(); + extractor.onStart(startAttributes, Context.root(), request); + assertThat(startAttributes.build()).isEmpty(); + + AttributesBuilder endAttributes = Attributes.builder(); + extractor.onEnd(endAttributes, Context.root(), request, null, null); + assertThat(endAttributes.build()).isEmpty(); + } } diff --git a/instrumentation-api-semconv/src/testStableHttpSemconv/java/io/opentelemetry/instrumentation/api/instrumenter/network/ServerAttributesExtractorTest.java b/instrumentation-api-semconv/src/testStableHttpSemconv/java/io/opentelemetry/instrumentation/api/instrumenter/network/ServerAttributesExtractorTest.java index a7202943e5c1..5a855fa7a59e 100644 --- a/instrumentation-api-semconv/src/testStableHttpSemconv/java/io/opentelemetry/instrumentation/api/instrumenter/network/ServerAttributesExtractorTest.java +++ b/instrumentation-api-semconv/src/testStableHttpSemconv/java/io/opentelemetry/instrumentation/api/instrumenter/network/ServerAttributesExtractorTest.java @@ -87,4 +87,21 @@ void doesNotSetNegativePortValue() { assertThat(startAttributes.build()) .containsOnly(entry(SemanticAttributes.SERVER_ADDRESS, "opentelemetry.io")); } + + @Test + void portWithoutAddress() { + Map request = new HashMap<>(); + request.put("port", "80"); + + AttributesExtractor, Void> extractor = + ServerAttributesExtractor.create(new TestServerAttributesGetter()); + + AttributesBuilder startAttributes = Attributes.builder(); + extractor.onStart(startAttributes, Context.root(), request); + assertThat(startAttributes.build()).isEmpty(); + + AttributesBuilder endAttributes = Attributes.builder(); + extractor.onEnd(endAttributes, Context.root(), request, null, null); + assertThat(endAttributes.build()).isEmpty(); + } }