From 801042d3d9dbe592f5a86e643b21e8e092bf064e Mon Sep 17 00:00:00 2001 From: Mateusz Rzeszutek Date: Fri, 13 Oct 2023 15:28:45 +0200 Subject: [PATCH] Replace (client|server).socket.(address|port) attributes with network.(peer|local).(address|port) --- ... => ForwardedAddressAndPortExtractor.java} | 7 +- ....java => HostAddressAndPortExtractor.java} | 6 +- .../http/HttpClientAttributesExtractor.java | 3 +- .../HttpClientAttributesExtractorBuilder.java | 22 ++-- .../http/HttpServerAttributesExtractor.java | 6 +- .../HttpServerAttributesExtractorBuilder.java | 33 ++++-- .../net/NetClientAttributesExtractor.java | 21 ++-- .../net/NetClientAttributesGetter.java | 19 ++++ .../net/NetServerAttributesExtractor.java | 30 +++-- .../net/NetServerAttributesGetter.java | 38 +++++++ .../InternalNetClientAttributesExtractor.java | 23 ++-- .../InternalNetServerAttributesExtractor.java | 10 +- .../network/ClientAttributesExtractor.java | 12 +- .../network/ClientAttributesGetter.java | 2 + .../network/NetworkAttributesExtractor.java | 5 + .../network/NetworkAttributesGetter.java | 94 ++++++++++++++- .../network/ServerAttributesExtractor.java | 15 +-- .../network/ServerAttributesGetter.java | 2 + .../network/internal/AddressAndPort.java | 4 +- ...ctor.java => AddressAndPortExtractor.java} | 12 +- .../ClientAddressAndPortExtractor.java | 38 +++++++ .../InternalClientAttributesExtractor.java | 52 +-------- .../InternalNetworkAttributesExtractor.java | 67 +++++++++++ .../InternalServerAttributesExtractor.java | 94 ++------------- .../network/internal/NetworkAttributes.java | 29 +++++ .../ServerAddressAndPortExtractor.java | 38 +++++++ ...ForwardedAddressAndPortExtractorTest.java} | 10 +- ...a => HostAddressAndPortExtractorTest.java} | 8 +- ...tAddressNetClientAttributesGetterTest.java | 7 +- ...tAddressNetServerAttributesGetterTest.java | 6 +- .../net/NetClientAttributesExtractorTest.java | 53 +-------- .../net/NetServerAttributesExtractorTest.java | 42 +------ ...ientAttributesExtractorOldSemconvTest.java | 20 +--- ...workAttributesExtractorOldSemconvTest.java | 36 +++++- ...rverAttributesExtractorOldSemconvTest.java | 36 +----- ...entAttributesExtractorBothSemconvTest.java | 7 +- ...verAttributesExtractorBothSemconvTest.java | 9 +- ...tAttributesExtractorStableSemconvTest.java | 85 ++++++++++++-- ...tExperimentalMetricsStableSemconvTest.java | 6 +- .../HttpClientMetricsStableSemconvTest.java | 6 +- ...rAttributesExtractorStableSemconvTest.java | 107 +++++++++++++++--- ...rExperimentalMetricsStableSemconvTest.java | 9 +- .../HttpServerMetricsStableSemconvTest.java | 9 +- ...tAttributesExtractorStableSemconvTest.java | 12 +- ...rAttributesExtractorStableSemconvTest.java | 9 +- ...ributesExtractorInetSocketAddressTest.java | 81 ------------- .../ClientAttributesExtractorTest.java | 46 +------- ...ributesExtractorInetSocketAddressTest.java | 77 +++++++++++++ .../NetworkAttributesExtractorTest.java | 60 +++++++++- ...ributesExtractorInetSocketAddressTest.java | 82 -------------- .../ServerAttributesExtractorTest.java | 61 +--------- .../test/groovy/Netty40ClientSslTest.groovy | 22 ++-- .../groovy/Netty40ConnectionSpanTest.groovy | 12 +- .../test/groovy/Netty41ClientSslTest.groovy | 22 ++-- .../groovy/Netty41ConnectionSpanTest.groovy | 12 +- .../src/test/groovy/UndertowServerTest.groovy | 10 +- .../junit/http/SemconvStabilityUtil.java | 12 +- 57 files changed, 888 insertions(+), 768 deletions(-) rename instrumentation-api-semconv/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/http/{ClientAddressAndPortExtractor.java => ForwardedAddressAndPortExtractor.java} (95%) rename instrumentation-api-semconv/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/http/{HttpAddressPortExtractor.java => HostAddressAndPortExtractor.java} (84%) rename instrumentation-api-semconv/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/network/internal/{FallbackAddressPortExtractor.java => AddressAndPortExtractor.java} (64%) create mode 100644 instrumentation-api-semconv/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/network/internal/ClientAddressAndPortExtractor.java create mode 100644 instrumentation-api-semconv/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/network/internal/NetworkAttributes.java create mode 100644 instrumentation-api-semconv/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/network/internal/ServerAddressAndPortExtractor.java rename instrumentation-api-semconv/src/test/java/io/opentelemetry/instrumentation/api/instrumenter/http/{ClientAddressAndPortExtractorTest.java => ForwardedAddressAndPortExtractorTest.java} (96%) rename instrumentation-api-semconv/src/test/java/io/opentelemetry/instrumentation/api/instrumenter/http/{HttpAddressPortExtractorTest.java => HostAddressAndPortExtractorTest.java} (90%) delete mode 100644 instrumentation-api-semconv/src/testStableHttpSemconv/java/io/opentelemetry/instrumentation/api/instrumenter/network/ClientAttributesExtractorInetSocketAddressTest.java create mode 100644 instrumentation-api-semconv/src/testStableHttpSemconv/java/io/opentelemetry/instrumentation/api/instrumenter/network/NetworkAttributesExtractorInetSocketAddressTest.java delete mode 100644 instrumentation-api-semconv/src/testStableHttpSemconv/java/io/opentelemetry/instrumentation/api/instrumenter/network/ServerAttributesExtractorInetSocketAddressTest.java diff --git a/instrumentation-api-semconv/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/http/ClientAddressAndPortExtractor.java b/instrumentation-api-semconv/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/http/ForwardedAddressAndPortExtractor.java similarity index 95% rename from instrumentation-api-semconv/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/http/ClientAddressAndPortExtractor.java rename to instrumentation-api-semconv/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/http/ForwardedAddressAndPortExtractor.java index 7ac0fb98399a..a9d625d61b42 100644 --- a/instrumentation-api-semconv/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/http/ClientAddressAndPortExtractor.java +++ b/instrumentation-api-semconv/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/http/ForwardedAddressAndPortExtractor.java @@ -5,15 +5,14 @@ package io.opentelemetry.instrumentation.api.instrumenter.http; -import io.opentelemetry.instrumentation.api.instrumenter.network.internal.FallbackAddressPortExtractor; +import io.opentelemetry.instrumentation.api.instrumenter.network.internal.AddressAndPortExtractor; import java.util.Locale; -final class ClientAddressAndPortExtractor - implements FallbackAddressPortExtractor { +final class ForwardedAddressAndPortExtractor implements AddressAndPortExtractor { private final HttpServerAttributesGetter getter; - ClientAddressAndPortExtractor(HttpServerAttributesGetter getter) { + ForwardedAddressAndPortExtractor(HttpServerAttributesGetter getter) { this.getter = getter; } diff --git a/instrumentation-api-semconv/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/http/HttpAddressPortExtractor.java b/instrumentation-api-semconv/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/http/HostAddressAndPortExtractor.java similarity index 84% rename from instrumentation-api-semconv/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/http/HttpAddressPortExtractor.java rename to instrumentation-api-semconv/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/http/HostAddressAndPortExtractor.java index bd56f8e87b8d..8da25acf58c5 100644 --- a/instrumentation-api-semconv/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/http/HttpAddressPortExtractor.java +++ b/instrumentation-api-semconv/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/http/HostAddressAndPortExtractor.java @@ -8,16 +8,16 @@ import static io.opentelemetry.instrumentation.api.instrumenter.http.HttpCommonAttributesExtractor.firstHeaderValue; import static java.util.logging.Level.FINE; -import io.opentelemetry.instrumentation.api.instrumenter.network.internal.FallbackAddressPortExtractor; +import io.opentelemetry.instrumentation.api.instrumenter.network.internal.AddressAndPortExtractor; import java.util.logging.Logger; -final class HttpAddressPortExtractor implements FallbackAddressPortExtractor { +final class HostAddressAndPortExtractor implements AddressAndPortExtractor { private static final Logger logger = Logger.getLogger(HttpCommonAttributesGetter.class.getName()); private final HttpCommonAttributesGetter getter; - HttpAddressPortExtractor(HttpCommonAttributesGetter getter) { + HostAddressAndPortExtractor(HttpCommonAttributesGetter getter) { this.getter = getter; } diff --git a/instrumentation-api-semconv/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/http/HttpClientAttributesExtractor.java b/instrumentation-api-semconv/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/http/HttpClientAttributesExtractor.java index 2571a337e843..6e663e88d5d2 100644 --- a/instrumentation-api-semconv/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/http/HttpClientAttributesExtractor.java +++ b/instrumentation-api-semconv/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/http/HttpClientAttributesExtractor.java @@ -84,7 +84,7 @@ public static HttpClientAttributesExtractorBuilder internalNetExtractor; private final InternalNetworkAttributesExtractor internalNetworkExtractor; - private final InternalServerAttributesExtractor internalServerExtractor; + private final InternalServerAttributesExtractor internalServerExtractor; private final ToIntFunction resendCountIncrementer; HttpClientAttributesExtractor(HttpClientAttributesExtractorBuilder builder) { @@ -132,7 +132,6 @@ public void onEnd( internalNetExtractor.onEnd(attributes, request, response); internalNetworkExtractor.onEnd(attributes, request, response); - internalServerExtractor.onEnd(attributes, request, response); } /** diff --git a/instrumentation-api-semconv/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/http/HttpClientAttributesExtractorBuilder.java b/instrumentation-api-semconv/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/http/HttpClientAttributesExtractorBuilder.java index 7e4758c7b35a..7ffe35d5eae9 100644 --- a/instrumentation-api-semconv/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/http/HttpClientAttributesExtractorBuilder.java +++ b/instrumentation-api-semconv/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/http/HttpClientAttributesExtractorBuilder.java @@ -11,8 +11,10 @@ import io.opentelemetry.context.Context; import io.opentelemetry.instrumentation.api.instrumenter.AttributesExtractor; import io.opentelemetry.instrumentation.api.instrumenter.net.internal.InternalNetClientAttributesExtractor; +import io.opentelemetry.instrumentation.api.instrumenter.network.internal.AddressAndPortExtractor; import io.opentelemetry.instrumentation.api.instrumenter.network.internal.InternalNetworkAttributesExtractor; import io.opentelemetry.instrumentation.api.instrumenter.network.internal.InternalServerAttributesExtractor; +import io.opentelemetry.instrumentation.api.instrumenter.network.internal.ServerAddressAndPortExtractor; import io.opentelemetry.instrumentation.api.internal.HttpConstants; import io.opentelemetry.instrumentation.api.internal.SemconvStability; import java.util.ArrayList; @@ -31,7 +33,7 @@ public final class HttpClientAttributesExtractorBuilder { REQUEST, RESPONSE> netAttributesGetter; - final HttpAddressPortExtractor addressPortExtractor; + final AddressAndPortExtractor serverAddressAndPortExtractor; List capturedRequestHeaders = emptyList(); List capturedResponseHeaders = emptyList(); Set knownMethods = HttpConstants.KNOWN_METHODS; @@ -45,7 +47,9 @@ public final class HttpClientAttributesExtractorBuilder { netAttributesGetter) { this.httpAttributesGetter = httpAttributesGetter; this.netAttributesGetter = netAttributesGetter; - addressPortExtractor = new HttpAddressPortExtractor<>(httpAttributesGetter); + serverAddressAndPortExtractor = + new ServerAddressAndPortExtractor<>( + netAttributesGetter, new HostAddressAndPortExtractor<>(httpAttributesGetter)); } /** @@ -125,25 +129,27 @@ public AttributesExtractor build() { InternalNetClientAttributesExtractor buildNetExtractor() { return new InternalNetClientAttributesExtractor<>( - netAttributesGetter, addressPortExtractor, SemconvStability.emitOldHttpSemconv()); + netAttributesGetter, serverAddressAndPortExtractor, SemconvStability.emitOldHttpSemconv()); } InternalNetworkAttributesExtractor buildNetworkExtractor() { return new InternalNetworkAttributesExtractor<>( netAttributesGetter, HttpNetworkTransportFilter.INSTANCE, + AddressAndPortExtractor.noop(), + serverAddressAndPortExtractor, + /* captureLocalSocketAttributes= */ false, + /* captureOldPeerDomainAttribute= */ true, SemconvStability.emitStableHttpSemconv(), SemconvStability.emitOldHttpSemconv()); } - InternalServerAttributesExtractor buildServerExtractor() { + InternalServerAttributesExtractor buildServerExtractor() { return new InternalServerAttributesExtractor<>( - netAttributesGetter, new ClientSideServerPortCondition<>(httpAttributesGetter), - addressPortExtractor, + serverAddressAndPortExtractor, SemconvStability.emitStableHttpSemconv(), SemconvStability.emitOldHttpSemconv(), - InternalServerAttributesExtractor.Mode.PEER, - /* captureServerSocketAttributes= */ true); + InternalServerAttributesExtractor.Mode.PEER); } } diff --git a/instrumentation-api-semconv/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/http/HttpServerAttributesExtractor.java b/instrumentation-api-semconv/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/http/HttpServerAttributesExtractor.java index eb851e56c04c..58411f67b904 100644 --- a/instrumentation-api-semconv/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/http/HttpServerAttributesExtractor.java +++ b/instrumentation-api-semconv/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/http/HttpServerAttributesExtractor.java @@ -86,8 +86,8 @@ public static HttpServerAttributesExtractorBuilder internalUrlExtractor; private final InternalNetServerAttributesExtractor internalNetExtractor; private final InternalNetworkAttributesExtractor internalNetworkExtractor; - private final InternalServerAttributesExtractor internalServerExtractor; - private final InternalClientAttributesExtractor internalClientExtractor; + private final InternalServerAttributesExtractor internalServerExtractor; + private final InternalClientAttributesExtractor internalClientExtractor; private final Function httpRouteGetter; HttpServerAttributesExtractor(HttpServerAttributesExtractorBuilder builder) { @@ -128,8 +128,6 @@ public void onEnd( super.onEnd(attributes, context, request, response, error); internalNetworkExtractor.onEnd(attributes, request, response); - internalServerExtractor.onEnd(attributes, request, response); - internalClientExtractor.onEnd(attributes, request, response); internalSet(attributes, SemanticAttributes.HTTP_ROUTE, httpRouteGetter.apply(context)); } diff --git a/instrumentation-api-semconv/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/http/HttpServerAttributesExtractorBuilder.java b/instrumentation-api-semconv/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/http/HttpServerAttributesExtractorBuilder.java index 9a672ddc0ba5..9194451c50c1 100644 --- a/instrumentation-api-semconv/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/http/HttpServerAttributesExtractorBuilder.java +++ b/instrumentation-api-semconv/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/http/HttpServerAttributesExtractorBuilder.java @@ -11,9 +11,12 @@ import io.opentelemetry.context.Context; import io.opentelemetry.instrumentation.api.instrumenter.AttributesExtractor; import io.opentelemetry.instrumentation.api.instrumenter.net.internal.InternalNetServerAttributesExtractor; +import io.opentelemetry.instrumentation.api.instrumenter.network.internal.AddressAndPortExtractor; +import io.opentelemetry.instrumentation.api.instrumenter.network.internal.ClientAddressAndPortExtractor; import io.opentelemetry.instrumentation.api.instrumenter.network.internal.InternalClientAttributesExtractor; import io.opentelemetry.instrumentation.api.instrumenter.network.internal.InternalNetworkAttributesExtractor; import io.opentelemetry.instrumentation.api.instrumenter.network.internal.InternalServerAttributesExtractor; +import io.opentelemetry.instrumentation.api.instrumenter.network.internal.ServerAddressAndPortExtractor; import io.opentelemetry.instrumentation.api.instrumenter.url.internal.InternalUrlAttributesExtractor; import io.opentelemetry.instrumentation.api.internal.HttpConstants; import io.opentelemetry.instrumentation.api.internal.SemconvStability; @@ -33,7 +36,8 @@ public final class HttpServerAttributesExtractorBuilder { REQUEST, RESPONSE> netAttributesGetter; - final HttpAddressPortExtractor addressPortExtractor; + final AddressAndPortExtractor clientAddressPortExtractor; + final AddressAndPortExtractor serverAddressPortExtractor; List capturedRequestHeaders = emptyList(); List capturedResponseHeaders = emptyList(); Set knownMethods = HttpConstants.KNOWN_METHODS; @@ -47,7 +51,13 @@ public final class HttpServerAttributesExtractorBuilder { netAttributesGetter) { this.httpAttributesGetter = httpAttributesGetter; this.netAttributesGetter = netAttributesGetter; - addressPortExtractor = new HttpAddressPortExtractor<>(httpAttributesGetter); + + clientAddressPortExtractor = + new ClientAddressAndPortExtractor<>( + netAttributesGetter, new ForwardedAddressAndPortExtractor<>(httpAttributesGetter)); + serverAddressPortExtractor = + new ServerAddressAndPortExtractor<>( + netAttributesGetter, new HostAddressAndPortExtractor<>(httpAttributesGetter)); } /** @@ -135,32 +145,33 @@ InternalUrlAttributesExtractor buildUrlExtractor() { InternalNetServerAttributesExtractor buildNetExtractor() { return new InternalNetServerAttributesExtractor<>( - netAttributesGetter, addressPortExtractor, SemconvStability.emitOldHttpSemconv()); + netAttributesGetter, serverAddressPortExtractor, SemconvStability.emitOldHttpSemconv()); } InternalNetworkAttributesExtractor buildNetworkExtractor() { return new InternalNetworkAttributesExtractor<>( netAttributesGetter, HttpNetworkTransportFilter.INSTANCE, + serverAddressPortExtractor, + clientAddressPortExtractor, + /* captureLocalSocketAttributes= */ false, + /* captureOldPeerDomainAttribute= */ false, SemconvStability.emitStableHttpSemconv(), SemconvStability.emitOldHttpSemconv()); } - InternalServerAttributesExtractor buildServerExtractor() { + InternalServerAttributesExtractor buildServerExtractor() { return new InternalServerAttributesExtractor<>( - netAttributesGetter, new ServerSideServerPortCondition<>(httpAttributesGetter), - addressPortExtractor, + serverAddressPortExtractor, SemconvStability.emitStableHttpSemconv(), SemconvStability.emitOldHttpSemconv(), - InternalServerAttributesExtractor.Mode.HOST, - /* captureServerSocketAttributes= */ false); + InternalServerAttributesExtractor.Mode.HOST); } - InternalClientAttributesExtractor buildClientExtractor() { + InternalClientAttributesExtractor buildClientExtractor() { return new InternalClientAttributesExtractor<>( - netAttributesGetter, - new ClientAddressAndPortExtractor<>(httpAttributesGetter), + clientAddressPortExtractor, SemconvStability.emitStableHttpSemconv(), SemconvStability.emitOldHttpSemconv()); } diff --git a/instrumentation-api-semconv/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/net/NetClientAttributesExtractor.java b/instrumentation-api-semconv/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/net/NetClientAttributesExtractor.java index 04665033183f..e74291670d65 100644 --- a/instrumentation-api-semconv/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/net/NetClientAttributesExtractor.java +++ b/instrumentation-api-semconv/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/net/NetClientAttributesExtractor.java @@ -9,10 +9,11 @@ import io.opentelemetry.context.Context; import io.opentelemetry.instrumentation.api.instrumenter.AttributesExtractor; import io.opentelemetry.instrumentation.api.instrumenter.net.internal.InternalNetClientAttributesExtractor; -import io.opentelemetry.instrumentation.api.instrumenter.network.internal.FallbackAddressPortExtractor; +import io.opentelemetry.instrumentation.api.instrumenter.network.internal.AddressAndPortExtractor; import io.opentelemetry.instrumentation.api.instrumenter.network.internal.InternalNetworkAttributesExtractor; import io.opentelemetry.instrumentation.api.instrumenter.network.internal.InternalServerAttributesExtractor; import io.opentelemetry.instrumentation.api.instrumenter.network.internal.NetworkTransportFilter; +import io.opentelemetry.instrumentation.api.instrumenter.network.internal.ServerAddressAndPortExtractor; import io.opentelemetry.instrumentation.api.internal.SemconvStability; import javax.annotation.Nullable; @@ -33,7 +34,7 @@ public final class NetClientAttributesExtractor private final InternalNetClientAttributesExtractor internalExtractor; private final InternalNetworkAttributesExtractor internalNetworkExtractor; - private final InternalServerAttributesExtractor internalServerExtractor; + private final InternalServerAttributesExtractor internalServerExtractor; public static AttributesExtractor create( NetClientAttributesGetter getter) { @@ -41,24 +42,29 @@ public static AttributesExtractor create( } private NetClientAttributesExtractor(NetClientAttributesGetter getter) { + ServerAddressAndPortExtractor serverAddressAndPortExtractor = + new ServerAddressAndPortExtractor<>(getter, AddressAndPortExtractor.noop()); + internalExtractor = new InternalNetClientAttributesExtractor<>( - getter, FallbackAddressPortExtractor.noop(), SemconvStability.emitOldHttpSemconv()); + getter, AddressAndPortExtractor.noop(), SemconvStability.emitOldHttpSemconv()); internalNetworkExtractor = new InternalNetworkAttributesExtractor<>( getter, NetworkTransportFilter.alwaysTrue(), + AddressAndPortExtractor.noop(), + serverAddressAndPortExtractor, + /* captureLocalSocketAttributes= */ false, + /* captureOldPeerDomainAttribute= */ true, SemconvStability.emitStableHttpSemconv(), SemconvStability.emitOldHttpSemconv()); internalServerExtractor = new InternalServerAttributesExtractor<>( - getter, (port, request) -> true, - FallbackAddressPortExtractor.noop(), + serverAddressAndPortExtractor, SemconvStability.emitStableHttpSemconv(), SemconvStability.emitOldHttpSemconv(), - InternalServerAttributesExtractor.Mode.PEER, - /* captureServerSocketAttributes= */ true); + InternalServerAttributesExtractor.Mode.PEER); } @Override @@ -75,6 +81,5 @@ public void onEnd( @Nullable Throwable error) { internalExtractor.onEnd(attributes, request, response); internalNetworkExtractor.onEnd(attributes, request, response); - internalServerExtractor.onEnd(attributes, request, response); } } diff --git a/instrumentation-api-semconv/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/net/NetClientAttributesGetter.java b/instrumentation-api-semconv/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/net/NetClientAttributesGetter.java index e6384b158ed6..05d79725d736 100644 --- a/instrumentation-api-semconv/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/net/NetClientAttributesGetter.java +++ b/instrumentation-api-semconv/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/net/NetClientAttributesGetter.java @@ -56,4 +56,23 @@ default String getNetworkType(REQUEST request, @Nullable RESPONSE response) { return InetSocketAddressUtil.getNetworkType( getServerInetSocketAddress(request, response), null); } + + @Nullable + @Override + default InetSocketAddress getNetworkPeerInetSocketAddress( + REQUEST request, @Nullable RESPONSE response) { + return getServerInetSocketAddress(request, response); + } + + @Nullable + @Override + default String getNetworkPeerAddress(REQUEST request, @Nullable RESPONSE response) { + return getServerSocketAddress(request, response); + } + + @Nullable + @Override + default Integer getNetworkPeerPort(REQUEST request, @Nullable RESPONSE response) { + return getServerSocketPort(request, response); + } } diff --git a/instrumentation-api-semconv/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/net/NetServerAttributesExtractor.java b/instrumentation-api-semconv/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/net/NetServerAttributesExtractor.java index 9f8d4dbbaba3..b819c4a2997a 100644 --- a/instrumentation-api-semconv/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/net/NetServerAttributesExtractor.java +++ b/instrumentation-api-semconv/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/net/NetServerAttributesExtractor.java @@ -9,11 +9,13 @@ import io.opentelemetry.context.Context; import io.opentelemetry.instrumentation.api.instrumenter.AttributesExtractor; import io.opentelemetry.instrumentation.api.instrumenter.net.internal.InternalNetServerAttributesExtractor; -import io.opentelemetry.instrumentation.api.instrumenter.network.internal.FallbackAddressPortExtractor; +import io.opentelemetry.instrumentation.api.instrumenter.network.internal.AddressAndPortExtractor; +import io.opentelemetry.instrumentation.api.instrumenter.network.internal.ClientAddressAndPortExtractor; import io.opentelemetry.instrumentation.api.instrumenter.network.internal.InternalClientAttributesExtractor; import io.opentelemetry.instrumentation.api.instrumenter.network.internal.InternalNetworkAttributesExtractor; import io.opentelemetry.instrumentation.api.instrumenter.network.internal.InternalServerAttributesExtractor; import io.opentelemetry.instrumentation.api.instrumenter.network.internal.NetworkTransportFilter; +import io.opentelemetry.instrumentation.api.instrumenter.network.internal.ServerAddressAndPortExtractor; import io.opentelemetry.instrumentation.api.internal.SemconvStability; import javax.annotation.Nullable; @@ -36,32 +38,38 @@ public static AttributesExtractor create( private final InternalNetServerAttributesExtractor internalExtractor; private final InternalNetworkAttributesExtractor internalNetworkExtractor; - private final InternalServerAttributesExtractor internalServerExtractor; - private final InternalClientAttributesExtractor internalClientExtractor; + private final InternalServerAttributesExtractor internalServerExtractor; + private final InternalClientAttributesExtractor internalClientExtractor; private NetServerAttributesExtractor(NetServerAttributesGetter getter) { + ServerAddressAndPortExtractor serverAddressAndPortExtractor = + new ServerAddressAndPortExtractor<>(getter, AddressAndPortExtractor.noop()); + ClientAddressAndPortExtractor clientAddressAndPortExtractor = + new ClientAddressAndPortExtractor<>(getter, AddressAndPortExtractor.noop()); + internalExtractor = new InternalNetServerAttributesExtractor<>( - getter, FallbackAddressPortExtractor.noop(), SemconvStability.emitOldHttpSemconv()); + getter, AddressAndPortExtractor.noop(), SemconvStability.emitOldHttpSemconv()); internalNetworkExtractor = new InternalNetworkAttributesExtractor<>( getter, NetworkTransportFilter.alwaysTrue(), + serverAddressAndPortExtractor, + clientAddressAndPortExtractor, + /* captureLocalSocketAttributes= */ true, + /* captureOldPeerDomainAttribute= */ false, SemconvStability.emitStableHttpSemconv(), SemconvStability.emitOldHttpSemconv()); internalServerExtractor = new InternalServerAttributesExtractor<>( - getter, (port, request) -> true, - FallbackAddressPortExtractor.noop(), + serverAddressAndPortExtractor, SemconvStability.emitStableHttpSemconv(), SemconvStability.emitOldHttpSemconv(), - InternalServerAttributesExtractor.Mode.HOST, - /* captureServerSocketAttributes= */ true); + InternalServerAttributesExtractor.Mode.HOST); internalClientExtractor = new InternalClientAttributesExtractor<>( - getter, - FallbackAddressPortExtractor.noop(), + clientAddressAndPortExtractor, SemconvStability.emitStableHttpSemconv(), SemconvStability.emitOldHttpSemconv()); } @@ -81,7 +89,5 @@ public void onEnd( @Nullable RESPONSE response, @Nullable Throwable error) { internalNetworkExtractor.onEnd(attributes, request, response); - internalServerExtractor.onEnd(attributes, request, response); - internalClientExtractor.onEnd(attributes, request, response); } } diff --git a/instrumentation-api-semconv/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/net/NetServerAttributesGetter.java b/instrumentation-api-semconv/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/net/NetServerAttributesGetter.java index 574e5ec1bcd1..0343c20aec23 100644 --- a/instrumentation-api-semconv/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/net/NetServerAttributesGetter.java +++ b/instrumentation-api-semconv/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/net/NetServerAttributesGetter.java @@ -62,4 +62,42 @@ default String getNetworkType(REQUEST request, @Nullable RESPONSE response) { getClientInetSocketAddress(request, response), getServerInetSocketAddress(request, response)); } + + @Nullable + @Override + default InetSocketAddress getNetworkLocalInetSocketAddress( + REQUEST request, @Nullable RESPONSE response) { + return getServerInetSocketAddress(request, response); + } + + @Nullable + @Override + default String getNetworkLocalAddress(REQUEST request, @Nullable RESPONSE response) { + return getServerSocketAddress(request, response); + } + + @Nullable + @Override + default Integer getNetworkLocalPort(REQUEST request, @Nullable RESPONSE response) { + return getServerSocketPort(request, response); + } + + @Nullable + @Override + default InetSocketAddress getNetworkPeerInetSocketAddress( + REQUEST request, @Nullable RESPONSE response) { + return getClientInetSocketAddress(request, response); + } + + @Nullable + @Override + default String getNetworkPeerAddress(REQUEST request, @Nullable RESPONSE response) { + return getClientSocketAddress(request, response); + } + + @Nullable + @Override + default Integer getNetworkPeerPort(REQUEST request, @Nullable RESPONSE response) { + return getClientSocketPort(request, response); + } } diff --git a/instrumentation-api-semconv/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/net/internal/InternalNetClientAttributesExtractor.java b/instrumentation-api-semconv/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/net/internal/InternalNetClientAttributesExtractor.java index 4d0a36d66ec1..f06fe03ebc44 100644 --- a/instrumentation-api-semconv/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/net/internal/InternalNetClientAttributesExtractor.java +++ b/instrumentation-api-semconv/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/net/internal/InternalNetClientAttributesExtractor.java @@ -8,8 +8,8 @@ import static io.opentelemetry.instrumentation.api.internal.AttributesExtractorUtil.internalSet; import io.opentelemetry.api.common.AttributesBuilder; -import io.opentelemetry.instrumentation.api.instrumenter.network.internal.AddressAndPort; -import io.opentelemetry.instrumentation.api.instrumenter.network.internal.FallbackAddressPortExtractor; +import io.opentelemetry.instrumentation.api.instrumenter.network.internal.AddressAndPortExtractor; +import io.opentelemetry.instrumentation.api.instrumenter.network.internal.ServerAddressAndPortExtractor; import io.opentelemetry.semconv.SemanticAttributes; import javax.annotation.Nullable; @@ -23,17 +23,18 @@ public final class InternalNetClientAttributesExtractor { private final io.opentelemetry.instrumentation.api.instrumenter.net.NetClientAttributesGetter< REQUEST, RESPONSE> getter; - private final FallbackAddressPortExtractor fallbackAddressPortExtractor; + private final AddressAndPortExtractor logicalAddressAndPortExtractor; private final boolean emitOldHttpAttributes; public InternalNetClientAttributesExtractor( io.opentelemetry.instrumentation.api.instrumenter.net.NetClientAttributesGetter< REQUEST, RESPONSE> getter, - FallbackAddressPortExtractor fallbackAddressPortExtractor, + AddressAndPortExtractor fallbackAddressAndPortExtractor, boolean emitOldHttpAttributes) { this.getter = getter; - this.fallbackAddressPortExtractor = fallbackAddressPortExtractor; + this.logicalAddressAndPortExtractor = + new ServerAddressAndPortExtractor<>(getter, fallbackAddressAndPortExtractor); this.emitOldHttpAttributes = emitOldHttpAttributes; } @@ -43,7 +44,7 @@ public void onEnd(AttributesBuilder attributes, REQUEST request, @Nullable RESPO internalSet( attributes, SemanticAttributes.NET_TRANSPORT, getter.getTransport(request, response)); - String peerName = extractPeerName(request); + String peerName = logicalAddressAndPortExtractor.extract(request).getAddress(); String sockPeerAddr = getter.getServerSocketAddress(request, response); if (sockPeerAddr != null && !sockPeerAddr.equals(peerName)) { String sockFamily = getter.getSockFamily(request, response); @@ -53,14 +54,4 @@ public void onEnd(AttributesBuilder attributes, REQUEST request, @Nullable RESPO } } } - - private String extractPeerName(REQUEST request) { - String serverAddress = getter.getServerAddress(request); - if (serverAddress != null) { - return serverAddress; - } - AddressAndPort addressAndPort = new AddressAndPort(); - fallbackAddressPortExtractor.extract(addressAndPort, request); - return addressAndPort.getAddress(); - } } diff --git a/instrumentation-api-semconv/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/net/internal/InternalNetServerAttributesExtractor.java b/instrumentation-api-semconv/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/net/internal/InternalNetServerAttributesExtractor.java index 591a5b08e646..cc1d14a7bd75 100644 --- a/instrumentation-api-semconv/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/net/internal/InternalNetServerAttributesExtractor.java +++ b/instrumentation-api-semconv/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/net/internal/InternalNetServerAttributesExtractor.java @@ -9,7 +9,7 @@ import io.opentelemetry.api.common.AttributesBuilder; import io.opentelemetry.instrumentation.api.instrumenter.network.internal.AddressAndPort; -import io.opentelemetry.instrumentation.api.instrumenter.network.internal.FallbackAddressPortExtractor; +import io.opentelemetry.instrumentation.api.instrumenter.network.internal.AddressAndPortExtractor; import io.opentelemetry.semconv.SemanticAttributes; /** @@ -22,17 +22,17 @@ public final class InternalNetServerAttributesExtractor { private final io.opentelemetry.instrumentation.api.instrumenter.net.NetServerAttributesGetter< REQUEST, RESPONSE> getter; - private final FallbackAddressPortExtractor fallbackAddressPortExtractor; + private final AddressAndPortExtractor fallbackAddressAndPortExtractor; private final boolean emitOldHttpAttributes; public InternalNetServerAttributesExtractor( io.opentelemetry.instrumentation.api.instrumenter.net.NetServerAttributesGetter< REQUEST, RESPONSE> getter, - FallbackAddressPortExtractor fallbackAddressPortExtractor, + AddressAndPortExtractor fallbackAddressAndPortExtractor, boolean emitOldHttpAttributes) { this.getter = getter; - this.fallbackAddressPortExtractor = fallbackAddressPortExtractor; + this.fallbackAddressAndPortExtractor = fallbackAddressAndPortExtractor; this.emitOldHttpAttributes = emitOldHttpAttributes; } @@ -69,7 +69,7 @@ private String extractServerAddress(REQUEST request) { return serverAddress; } AddressAndPort addressAndPort = new AddressAndPort(); - fallbackAddressPortExtractor.extract(addressAndPort, request); + fallbackAddressAndPortExtractor.extract(addressAndPort, request); return addressAndPort.getAddress(); } } diff --git a/instrumentation-api-semconv/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/network/ClientAttributesExtractor.java b/instrumentation-api-semconv/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/network/ClientAttributesExtractor.java index 72342496e278..81e025b684c5 100644 --- a/instrumentation-api-semconv/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/network/ClientAttributesExtractor.java +++ b/instrumentation-api-semconv/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/network/ClientAttributesExtractor.java @@ -8,7 +8,8 @@ import io.opentelemetry.api.common.AttributesBuilder; import io.opentelemetry.context.Context; import io.opentelemetry.instrumentation.api.instrumenter.AttributesExtractor; -import io.opentelemetry.instrumentation.api.instrumenter.network.internal.FallbackAddressPortExtractor; +import io.opentelemetry.instrumentation.api.instrumenter.network.internal.AddressAndPortExtractor; +import io.opentelemetry.instrumentation.api.instrumenter.network.internal.ClientAddressAndPortExtractor; import io.opentelemetry.instrumentation.api.instrumenter.network.internal.InternalClientAttributesExtractor; import io.opentelemetry.instrumentation.api.internal.SemconvStability; import javax.annotation.Nullable; @@ -30,13 +31,12 @@ public static ClientAttributesExtractor c return new ClientAttributesExtractor<>(getter); } - private final InternalClientAttributesExtractor internalExtractor; + private final InternalClientAttributesExtractor internalExtractor; ClientAttributesExtractor(ClientAttributesGetter getter) { internalExtractor = new InternalClientAttributesExtractor<>( - getter, - FallbackAddressPortExtractor.noop(), + new ClientAddressAndPortExtractor<>(getter, AddressAndPortExtractor.noop()), SemconvStability.emitStableHttpSemconv(), SemconvStability.emitOldHttpSemconv()); } @@ -52,7 +52,5 @@ public void onEnd( Context context, REQUEST request, @Nullable RESPONSE response, - @Nullable Throwable error) { - internalExtractor.onEnd(attributes, request, response); - } + @Nullable Throwable error) {} } diff --git a/instrumentation-api-semconv/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/network/ClientAttributesGetter.java b/instrumentation-api-semconv/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/network/ClientAttributesGetter.java index 788854d7ec2b..89b4dad2fc65 100644 --- a/instrumentation-api-semconv/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/network/ClientAttributesGetter.java +++ b/instrumentation-api-semconv/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/network/ClientAttributesGetter.java @@ -38,6 +38,8 @@ default Integer getClientPort(REQUEST request) { return null; } + // TODO deprecate + /** * Returns an {@link InetSocketAddress} object representing the immediate client socket address. * diff --git a/instrumentation-api-semconv/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/network/NetworkAttributesExtractor.java b/instrumentation-api-semconv/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/network/NetworkAttributesExtractor.java index ec9e0ac298da..a1d3ae026022 100644 --- a/instrumentation-api-semconv/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/network/NetworkAttributesExtractor.java +++ b/instrumentation-api-semconv/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/network/NetworkAttributesExtractor.java @@ -8,6 +8,7 @@ import io.opentelemetry.api.common.AttributesBuilder; import io.opentelemetry.context.Context; import io.opentelemetry.instrumentation.api.instrumenter.AttributesExtractor; +import io.opentelemetry.instrumentation.api.instrumenter.network.internal.AddressAndPortExtractor; import io.opentelemetry.instrumentation.api.instrumenter.network.internal.InternalNetworkAttributesExtractor; import io.opentelemetry.instrumentation.api.instrumenter.network.internal.NetworkTransportFilter; import io.opentelemetry.instrumentation.api.internal.SemconvStability; @@ -37,6 +38,10 @@ public static NetworkAttributesExtractor new InternalNetworkAttributesExtractor<>( getter, NetworkTransportFilter.alwaysTrue(), + AddressAndPortExtractor.noop(), + AddressAndPortExtractor.noop(), + /* captureLocalSocketAttributes= */ true, + /* captureOldPeerDomainAttribute= */ false, SemconvStability.emitStableHttpSemconv(), SemconvStability.emitOldHttpSemconv()); } diff --git a/instrumentation-api-semconv/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/network/NetworkAttributesGetter.java b/instrumentation-api-semconv/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/network/NetworkAttributesGetter.java index 0c172d253eec..e369b72f0b82 100644 --- a/instrumentation-api-semconv/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/network/NetworkAttributesGetter.java +++ b/instrumentation-api-semconv/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/network/NetworkAttributesGetter.java @@ -5,6 +5,8 @@ package io.opentelemetry.instrumentation.api.instrumenter.network; +import io.opentelemetry.instrumentation.api.instrumenter.net.internal.InetSocketAddressUtil; +import java.net.InetSocketAddress; import javax.annotation.Nullable; /** @@ -36,7 +38,9 @@ default String getNetworkTransport(REQUEST request, @Nullable RESPONSE response) */ @Nullable default String getNetworkType(REQUEST request, @Nullable RESPONSE response) { - return null; + return InetSocketAddressUtil.getNetworkType( + getNetworkLocalInetSocketAddress(request, response), + getNetworkPeerInetSocketAddress(request, response)); } /** @@ -59,4 +63,92 @@ default String getNetworkProtocolName(REQUEST request, @Nullable RESPONSE respon default String getNetworkProtocolVersion(REQUEST request, @Nullable RESPONSE response) { return null; } + + /** + * Returns an {@link InetSocketAddress} object representing the local socket address. + * + *

Implementing this method is equivalent to implementing both {@link + * #getNetworkLocalAddress(Object, Object)} and {@link #getNetworkLocalPort(Object, Object)}. + */ + @Nullable + default InetSocketAddress getNetworkLocalInetSocketAddress( + REQUEST request, @Nullable RESPONSE response) { + return null; + } + + /** + * Returns the local address of the network connection - IP address or Unix domain socket name. + * + *

Examples: {@code 10.1.2.80}, {@code /tmp/my.sock} + * + *

By default, this method attempts to retrieve the local address using the {@link + * #getNetworkLocalInetSocketAddress(Object, Object)} method. If this method is not implemented, + * it will simply return {@code null}. If the instrumented library does not expose {@link + * InetSocketAddress} in its API, you might want to implement this method instead of {@link + * #getNetworkLocalInetSocketAddress(Object, Object)}. + */ + @Nullable + default String getNetworkLocalAddress(REQUEST request, @Nullable RESPONSE response) { + return InetSocketAddressUtil.getIpAddress(getNetworkLocalInetSocketAddress(request, response)); + } + + /** + * Returns the local port number of the network connection. + * + *

Examples: {@code 65123} + * + *

By default, this method attempts to retrieve the local port using the {@link + * #getNetworkLocalInetSocketAddress(Object, Object)} method. If this method is not implemented, + * it will simply return {@code null}. If the instrumented library does not expose {@link + * InetSocketAddress} in its API, you might want to implement this method instead of {@link + * #getNetworkLocalInetSocketAddress(Object, Object)}. + */ + @Nullable + default Integer getNetworkLocalPort(REQUEST request, @Nullable RESPONSE response) { + return InetSocketAddressUtil.getPort(getNetworkLocalInetSocketAddress(request, response)); + } + + /** + * Returns an {@link InetSocketAddress} object representing the peer socket address. + * + *

Implementing this method is equivalent to implementing both {@link + * #getNetworkPeerAddress(Object, Object)} and {@link #getNetworkPeerPort(Object, Object)}. + */ + @Nullable + default InetSocketAddress getNetworkPeerInetSocketAddress( + REQUEST request, @Nullable RESPONSE response) { + return null; + } + + /** + * Returns the peer address of the network connection - IP address or Unix domain socket name. + * + *

Examples: {@code 10.1.2.80}, {@code /tmp/my.sock} + * + *

By default, this method attempts to retrieve the peer address using the {@link + * #getNetworkPeerInetSocketAddress(Object, Object)} method. If this method is not implemented, it + * will simply return {@code null}. If the instrumented library does not expose {@link + * InetSocketAddress} in its API, you might want to implement this method instead of {@link + * #getNetworkPeerInetSocketAddress(Object, Object)}. + */ + @Nullable + default String getNetworkPeerAddress(REQUEST request, @Nullable RESPONSE response) { + return InetSocketAddressUtil.getIpAddress(getNetworkPeerInetSocketAddress(request, response)); + } + + /** + * Returns the peer port number of the network connection. + * + *

Examples: {@code 65123} + * + *

By default, this method attempts to retrieve the peer port using the {@link + * #getNetworkPeerInetSocketAddress(Object, Object)} method. If this method is not implemented, it + * will simply return {@code null}. If the instrumented library does not expose {@link + * InetSocketAddress} in its API, you might want to implement this method instead of {@link + * #getNetworkPeerInetSocketAddress(Object, Object)}. + */ + @Nullable + default Integer getNetworkPeerPort(REQUEST request, @Nullable RESPONSE response) { + return InetSocketAddressUtil.getPort(getNetworkPeerInetSocketAddress(request, response)); + } } diff --git a/instrumentation-api-semconv/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/network/ServerAttributesExtractor.java b/instrumentation-api-semconv/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/network/ServerAttributesExtractor.java index f8788d26ac6f..d6005b67aee1 100644 --- a/instrumentation-api-semconv/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/network/ServerAttributesExtractor.java +++ b/instrumentation-api-semconv/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/network/ServerAttributesExtractor.java @@ -8,8 +8,9 @@ import io.opentelemetry.api.common.AttributesBuilder; import io.opentelemetry.context.Context; import io.opentelemetry.instrumentation.api.instrumenter.AttributesExtractor; -import io.opentelemetry.instrumentation.api.instrumenter.network.internal.FallbackAddressPortExtractor; +import io.opentelemetry.instrumentation.api.instrumenter.network.internal.AddressAndPortExtractor; import io.opentelemetry.instrumentation.api.instrumenter.network.internal.InternalServerAttributesExtractor; +import io.opentelemetry.instrumentation.api.instrumenter.network.internal.ServerAddressAndPortExtractor; import io.opentelemetry.instrumentation.api.internal.SemconvStability; import javax.annotation.Nullable; @@ -45,7 +46,7 @@ ServerAttributesExtractor createForServerSide( return new ServerAttributesExtractor<>(getter, InternalServerAttributesExtractor.Mode.HOST); } - private final InternalServerAttributesExtractor internalExtractor; + private final InternalServerAttributesExtractor internalExtractor; ServerAttributesExtractor( ServerAttributesGetter getter, @@ -53,13 +54,11 @@ ServerAttributesExtractor createForServerSide( // the ServerAttributesExtractor will always emit new semconv internalExtractor = new InternalServerAttributesExtractor<>( - getter, (port, request) -> true, - FallbackAddressPortExtractor.noop(), + new ServerAddressAndPortExtractor<>(getter, AddressAndPortExtractor.noop()), SemconvStability.emitStableHttpSemconv(), SemconvStability.emitOldHttpSemconv(), - mode, - /* captureServerSocketAttributes= */ true); + mode); } @Override @@ -73,7 +72,5 @@ public void onEnd( Context context, REQUEST request, @Nullable RESPONSE response, - @Nullable Throwable error) { - internalExtractor.onEnd(attributes, request, response); - } + @Nullable Throwable error) {} } diff --git a/instrumentation-api-semconv/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/network/ServerAttributesGetter.java b/instrumentation-api-semconv/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/network/ServerAttributesGetter.java index 672f79a1b897..df24124d315a 100644 --- a/instrumentation-api-semconv/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/network/ServerAttributesGetter.java +++ b/instrumentation-api-semconv/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/network/ServerAttributesGetter.java @@ -39,6 +39,8 @@ default Integer getServerPort(REQUEST request) { return null; } + // TODO deprecate + /** * Returns an {@link InetSocketAddress} object representing the server socket address. * diff --git a/instrumentation-api-semconv/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/network/internal/AddressAndPort.java b/instrumentation-api-semconv/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/network/internal/AddressAndPort.java index 803f9bc7d205..021de9c42121 100644 --- a/instrumentation-api-semconv/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/network/internal/AddressAndPort.java +++ b/instrumentation-api-semconv/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/network/internal/AddressAndPort.java @@ -11,7 +11,7 @@ * This class is internal and is hence not for public use. Its APIs are unstable and can change at * any time. */ -public final class AddressAndPort implements FallbackAddressPortExtractor.AddressPortSink { +public final class AddressAndPort implements AddressAndPortExtractor.AddressPortSink { @Nullable String address; @Nullable Integer port; @@ -22,7 +22,7 @@ public void setAddress(String address) { } @Override - public void setPort(int port) { + public void setPort(Integer port) { this.port = port; } diff --git a/instrumentation-api-semconv/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/network/internal/FallbackAddressPortExtractor.java b/instrumentation-api-semconv/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/network/internal/AddressAndPortExtractor.java similarity index 64% rename from instrumentation-api-semconv/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/network/internal/FallbackAddressPortExtractor.java rename to instrumentation-api-semconv/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/network/internal/AddressAndPortExtractor.java index cc83917d53bf..8f1c96ae4d0f 100644 --- a/instrumentation-api-semconv/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/network/internal/FallbackAddressPortExtractor.java +++ b/instrumentation-api-semconv/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/network/internal/AddressAndPortExtractor.java @@ -9,11 +9,17 @@ * This class is internal and is hence not for public use. Its APIs are unstable and can change at * any time. */ -public interface FallbackAddressPortExtractor { +public interface AddressAndPortExtractor { + + default AddressAndPort extract(REQUEST request) { + AddressAndPort addressAndPort = new AddressAndPort(); + extract(addressAndPort, request); + return addressAndPort; + } void extract(AddressPortSink sink, REQUEST request); - static FallbackAddressPortExtractor noop() { + static AddressAndPortExtractor noop() { return (sink, request) -> {}; } @@ -25,6 +31,6 @@ interface AddressPortSink { void setAddress(String address); - void setPort(int port); + void setPort(Integer port); } } diff --git a/instrumentation-api-semconv/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/network/internal/ClientAddressAndPortExtractor.java b/instrumentation-api-semconv/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/network/internal/ClientAddressAndPortExtractor.java new file mode 100644 index 000000000000..a3dedd9518b6 --- /dev/null +++ b/instrumentation-api-semconv/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/network/internal/ClientAddressAndPortExtractor.java @@ -0,0 +1,38 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +package io.opentelemetry.instrumentation.api.instrumenter.network.internal; + +import io.opentelemetry.instrumentation.api.instrumenter.network.ClientAttributesGetter; + +/** + * This class is internal and is hence not for public use. Its APIs are unstable and can change at + * any time. + */ +public final class ClientAddressAndPortExtractor + implements AddressAndPortExtractor { + + private final ClientAttributesGetter getter; + private final AddressAndPortExtractor fallbackAddressAndPortExtractor; + + public ClientAddressAndPortExtractor( + ClientAttributesGetter getter, + AddressAndPortExtractor fallbackAddressAndPortExtractor) { + this.getter = getter; + this.fallbackAddressAndPortExtractor = fallbackAddressAndPortExtractor; + } + + @Override + public void extract(AddressPortSink sink, REQUEST request) { + String address = getter.getClientAddress(request); + Integer port = getter.getClientPort(request); + if (address == null && port == null) { + fallbackAddressAndPortExtractor.extract(sink, request); + } else { + sink.setAddress(address); + sink.setPort(port); + } + } +} 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 1a39f7c22139..eeb0e0348737 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 @@ -8,35 +8,31 @@ import static io.opentelemetry.instrumentation.api.internal.AttributesExtractorUtil.internalSet; import io.opentelemetry.api.common.AttributesBuilder; -import io.opentelemetry.instrumentation.api.instrumenter.network.ClientAttributesGetter; import io.opentelemetry.semconv.SemanticAttributes; -import javax.annotation.Nullable; /** * This class is internal and is hence not for public use. Its APIs are unstable and can change at * any time. */ -public final class InternalClientAttributesExtractor { +public final class InternalClientAttributesExtractor { - private final ClientAttributesGetter getter; - private final FallbackAddressPortExtractor fallbackAddressPortExtractor; + private final AddressAndPortExtractor addressAndPortExtractor; private final boolean emitStableUrlAttributes; private final boolean emitOldHttpAttributes; + // todo rework constructor public InternalClientAttributesExtractor( - ClientAttributesGetter getter, - FallbackAddressPortExtractor fallbackAddressPortExtractor, + AddressAndPortExtractor addressAndPortExtractor, boolean emitStableUrlAttributes, boolean emitOldHttpAttributes) { - this.getter = getter; - this.fallbackAddressPortExtractor = fallbackAddressPortExtractor; + this.addressAndPortExtractor = addressAndPortExtractor; this.emitStableUrlAttributes = emitStableUrlAttributes; this.emitOldHttpAttributes = emitOldHttpAttributes; } @SuppressWarnings("deprecation") // until old http semconv are dropped in 2.0 public void onStart(AttributesBuilder attributes, REQUEST request) { - AddressAndPort clientAddressAndPort = extractClientAddressAndPort(request); + AddressAndPort clientAddressAndPort = addressAndPortExtractor.extract(request); if (emitStableUrlAttributes) { internalSet(attributes, SemanticAttributes.CLIENT_ADDRESS, clientAddressAndPort.address); @@ -48,40 +44,4 @@ public void onStart(AttributesBuilder attributes, REQUEST request) { internalSet(attributes, SemanticAttributes.HTTP_CLIENT_IP, clientAddressAndPort.address); } } - - @SuppressWarnings("deprecation") // until old http semconv are dropped in 2.0 - public void onEnd(AttributesBuilder attributes, REQUEST request, @Nullable RESPONSE response) { - AddressAndPort clientAddressAndPort = extractClientAddressAndPort(request); - String clientSocketAddress = getter.getClientSocketAddress(request, response); - Integer clientSocketPort = getter.getClientSocketPort(request, response); - - if (clientSocketAddress != null && !clientSocketAddress.equals(clientAddressAndPort.address)) { - if (emitStableUrlAttributes) { - internalSet(attributes, SemanticAttributes.CLIENT_SOCKET_ADDRESS, clientSocketAddress); - } - if (emitOldHttpAttributes) { - internalSet(attributes, SemanticAttributes.NET_SOCK_PEER_ADDR, clientSocketAddress); - } - } - if (clientSocketPort != null && clientSocketPort > 0) { - if (emitStableUrlAttributes) { - if (!clientSocketPort.equals(clientAddressAndPort.port)) { - internalSet(attributes, SemanticAttributes.CLIENT_SOCKET_PORT, (long) clientSocketPort); - } - } - if (emitOldHttpAttributes) { - internalSet(attributes, SemanticAttributes.NET_SOCK_PEER_PORT, (long) clientSocketPort); - } - } - } - - private AddressAndPort extractClientAddressAndPort(REQUEST request) { - AddressAndPort addressAndPort = new AddressAndPort(); - addressAndPort.address = getter.getClientAddress(request); - addressAndPort.port = getter.getClientPort(request); - if (addressAndPort.address == null && addressAndPort.port == null) { - fallbackAddressPortExtractor.extract(addressAndPort, request); - } - return addressAndPort; - } } diff --git a/instrumentation-api-semconv/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/network/internal/InternalNetworkAttributesExtractor.java b/instrumentation-api-semconv/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/network/internal/InternalNetworkAttributesExtractor.java index 3a43fd3c9e7f..2f9eac0312dc 100644 --- a/instrumentation-api-semconv/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/network/internal/InternalNetworkAttributesExtractor.java +++ b/instrumentation-api-semconv/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/network/internal/InternalNetworkAttributesExtractor.java @@ -8,8 +8,10 @@ import static io.opentelemetry.instrumentation.api.internal.AttributesExtractorUtil.internalSet; import io.opentelemetry.api.common.AttributesBuilder; +import io.opentelemetry.instrumentation.api.instrumenter.net.internal.InetSocketAddressUtil; import io.opentelemetry.instrumentation.api.instrumenter.network.NetworkAttributesGetter; import io.opentelemetry.semconv.SemanticAttributes; +import java.net.InetSocketAddress; import java.util.Locale; import javax.annotation.Nullable; @@ -21,16 +23,28 @@ public final class InternalNetworkAttributesExtractor { private final NetworkAttributesGetter getter; private final NetworkTransportFilter networkTransportFilter; + private final AddressAndPortExtractor logicalLocalAddressAndPortExtractor; + private final AddressAndPortExtractor logicalPeerAddressAndPortExtractor; + private final boolean captureLocalSocketAttributes; + private final boolean captureOldPeerDomainAttribute; private final boolean emitStableUrlAttributes; private final boolean emitOldHttpAttributes; public InternalNetworkAttributesExtractor( NetworkAttributesGetter getter, NetworkTransportFilter networkTransportFilter, + AddressAndPortExtractor logicalLocalAddressAndPortExtractor, + AddressAndPortExtractor logicalPeerAddressAndPortExtractor, + boolean captureLocalSocketAttributes, + boolean captureOldPeerDomainAttribute, boolean emitStableUrlAttributes, boolean emitOldHttpAttributes) { this.getter = getter; this.networkTransportFilter = networkTransportFilter; + this.logicalLocalAddressAndPortExtractor = logicalLocalAddressAndPortExtractor; + this.logicalPeerAddressAndPortExtractor = logicalPeerAddressAndPortExtractor; + this.captureLocalSocketAttributes = captureLocalSocketAttributes; + this.captureOldPeerDomainAttribute = captureOldPeerDomainAttribute; this.emitStableUrlAttributes = emitStableUrlAttributes; this.emitOldHttpAttributes = emitOldHttpAttributes; } @@ -59,6 +73,59 @@ public void onEnd(AttributesBuilder attributes, REQUEST request, @Nullable RESPO internalSet(attributes, SemanticAttributes.NET_PROTOCOL_NAME, protocolName); internalSet(attributes, SemanticAttributes.NET_PROTOCOL_VERSION, protocolVersion); } + + String localAddress = getter.getNetworkLocalAddress(request, response); + String logicalLocalAddress = logicalLocalAddressAndPortExtractor.extract(request).address; + if (localAddress != null && !localAddress.equals(logicalLocalAddress)) { + if (emitStableUrlAttributes && captureLocalSocketAttributes) { + internalSet(attributes, NetworkAttributes.NETWORK_LOCAL_ADDRESS, localAddress); + } + if (emitOldHttpAttributes) { + internalSet(attributes, SemanticAttributes.NET_SOCK_HOST_ADDR, localAddress); + } + + Integer localPort = getter.getNetworkLocalPort(request, response); + if (localPort != null && localPort > 0) { + if (emitStableUrlAttributes && captureLocalSocketAttributes) { + internalSet(attributes, NetworkAttributes.NETWORK_LOCAL_PORT, (long) localPort); + } + if (emitOldHttpAttributes) { + internalSet(attributes, SemanticAttributes.NET_SOCK_HOST_PORT, (long) localPort); + } + } + } + + String peerAddress = getter.getNetworkPeerAddress(request, response); + String logicalPeerAddress = logicalPeerAddressAndPortExtractor.extract(request).address; + if (peerAddress != null && !peerAddress.equals(logicalPeerAddress)) { + if (emitStableUrlAttributes) { + internalSet(attributes, NetworkAttributes.NETWORK_PEER_ADDRESS, peerAddress); + } + if (emitOldHttpAttributes) { + internalSet(attributes, SemanticAttributes.NET_SOCK_PEER_ADDR, peerAddress); + } + + Integer peerPort = getter.getNetworkPeerPort(request, response); + if (peerPort != null && peerPort > 0) { + if (emitStableUrlAttributes) { + internalSet(attributes, NetworkAttributes.NETWORK_PEER_PORT, (long) peerPort); + } + if (emitOldHttpAttributes) { + internalSet(attributes, SemanticAttributes.NET_SOCK_PEER_PORT, (long) peerPort); + } + } + + if (emitOldHttpAttributes && captureOldPeerDomainAttribute) { + InetSocketAddress peerSocketAddress = + getter.getNetworkPeerInetSocketAddress(request, response); + if (peerSocketAddress != null) { + String peerSocketDomain = InetSocketAddressUtil.getDomainName(peerSocketAddress); + if (peerSocketDomain != null && !peerSocketDomain.equals(logicalPeerAddress)) { + internalSet(attributes, SemanticAttributes.NET_SOCK_PEER_NAME, peerSocketDomain); + } + } + } + } } @Nullable 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 b00fd5c7f5d5..e0611409c435 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 @@ -9,44 +9,36 @@ import io.opentelemetry.api.common.AttributeKey; import io.opentelemetry.api.common.AttributesBuilder; -import io.opentelemetry.instrumentation.api.instrumenter.network.ServerAttributesGetter; import io.opentelemetry.semconv.SemanticAttributes; import java.util.function.BiPredicate; -import javax.annotation.Nullable; /** * This class is internal and is hence not for public use. Its APIs are unstable and can change at * any time. */ -public final class InternalServerAttributesExtractor { +public final class InternalServerAttributesExtractor { - private final ServerAttributesGetter getter; private final BiPredicate captureServerPortCondition; - private final FallbackAddressPortExtractor fallbackAddressPortExtractor; + private final AddressAndPortExtractor addressAndPortExtractor; private final boolean emitStableUrlAttributes; private final boolean emitOldHttpAttributes; private final Mode oldSemconvMode; - private final boolean captureServerSocketAttributes; public InternalServerAttributesExtractor( - ServerAttributesGetter getter, BiPredicate captureServerPortCondition, - FallbackAddressPortExtractor fallbackAddressPortExtractor, + AddressAndPortExtractor addressAndPortExtractor, boolean emitStableUrlAttributes, boolean emitOldHttpAttributes, - Mode oldSemconvMode, - boolean captureServerSocketAttributes) { - this.getter = getter; + Mode oldSemconvMode) { this.captureServerPortCondition = captureServerPortCondition; - this.fallbackAddressPortExtractor = fallbackAddressPortExtractor; + this.addressAndPortExtractor = addressAndPortExtractor; this.emitStableUrlAttributes = emitStableUrlAttributes; this.emitOldHttpAttributes = emitOldHttpAttributes; this.oldSemconvMode = oldSemconvMode; - this.captureServerSocketAttributes = captureServerSocketAttributes; } public void onStart(AttributesBuilder attributes, REQUEST request) { - AddressAndPort serverAddressAndPort = extractServerAddressAndPort(request); + AddressAndPort serverAddressAndPort = addressAndPortExtractor.extract(request); if (emitStableUrlAttributes) { internalSet(attributes, SemanticAttributes.SERVER_ADDRESS, serverAddressAndPort.address); @@ -67,52 +59,6 @@ public void onStart(AttributesBuilder attributes, REQUEST request) { } } - public void onEnd(AttributesBuilder attributes, REQUEST request, @Nullable RESPONSE response) { - AddressAndPort serverAddressAndPort = extractServerAddressAndPort(request); - - String serverSocketAddress = getter.getServerSocketAddress(request, response); - if (serverSocketAddress != null && !serverSocketAddress.equals(serverAddressAndPort.address)) { - if (emitStableUrlAttributes && captureServerSocketAttributes) { - internalSet(attributes, SemanticAttributes.SERVER_SOCKET_ADDRESS, serverSocketAddress); - } - if (emitOldHttpAttributes) { - internalSet(attributes, oldSemconvMode.socketAddress, serverSocketAddress); - } - } - - Integer serverSocketPort = getter.getServerSocketPort(request, response); - if (serverSocketPort != null - && serverSocketPort > 0 - && !serverSocketPort.equals(serverAddressAndPort.port)) { - if (emitStableUrlAttributes && captureServerSocketAttributes) { - internalSet(attributes, SemanticAttributes.SERVER_SOCKET_PORT, (long) serverSocketPort); - } - if (emitOldHttpAttributes) { - internalSet(attributes, oldSemconvMode.socketPort, (long) serverSocketPort); - } - } - - String serverSocketDomain = getter.getServerSocketDomain(request, response); - if (serverSocketDomain != null && !serverSocketDomain.equals(serverAddressAndPort.address)) { - if (emitStableUrlAttributes && captureServerSocketAttributes) { - internalSet(attributes, SemanticAttributes.SERVER_SOCKET_DOMAIN, serverSocketDomain); - } - if (emitOldHttpAttributes && oldSemconvMode.socketDomain != null) { - internalSet(attributes, oldSemconvMode.socketDomain, serverSocketDomain); - } - } - } - - private AddressAndPort extractServerAddressAndPort(REQUEST request) { - AddressAndPort addressAndPort = new AddressAndPort(); - addressAndPort.address = getter.getServerAddress(request); - addressAndPort.port = getter.getServerPort(request); - if (addressAndPort.address == null && addressAndPort.port == null) { - fallbackAddressPortExtractor.extract(addressAndPort, request); - } - return addressAndPort; - } - /** * This class is internal and is hence not for public use. Its APIs are unstable and can change at * any time. @@ -122,37 +68,15 @@ private AddressAndPort extractServerAddressAndPort(REQUEST request) { "deprecation" }) // until old http semconv are dropped in 2.0 public enum Mode { - PEER( - SemanticAttributes.NET_PEER_NAME, - SemanticAttributes.NET_PEER_PORT, - SemanticAttributes.NET_SOCK_PEER_NAME, - SemanticAttributes.NET_SOCK_PEER_ADDR, - SemanticAttributes.NET_SOCK_PEER_PORT), - HOST( - SemanticAttributes.NET_HOST_NAME, - SemanticAttributes.NET_HOST_PORT, - // the old semconv does not have an attribute for this - null, - SemanticAttributes.NET_SOCK_HOST_ADDR, - SemanticAttributes.NET_SOCK_HOST_PORT); + PEER(SemanticAttributes.NET_PEER_NAME, SemanticAttributes.NET_PEER_PORT), + HOST(SemanticAttributes.NET_HOST_NAME, SemanticAttributes.NET_HOST_PORT); final AttributeKey address; final AttributeKey port; - @Nullable final AttributeKey socketDomain; - final AttributeKey socketAddress; - final AttributeKey socketPort; - Mode( - AttributeKey address, - AttributeKey port, - AttributeKey socketDomain, - AttributeKey socketAddress, - AttributeKey socketPort) { + Mode(AttributeKey address, AttributeKey port) { this.address = address; this.port = port; - this.socketDomain = socketDomain; - this.socketAddress = socketAddress; - this.socketPort = socketPort; } } } diff --git a/instrumentation-api-semconv/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/network/internal/NetworkAttributes.java b/instrumentation-api-semconv/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/network/internal/NetworkAttributes.java new file mode 100644 index 000000000000..4fa1ccd57fc8 --- /dev/null +++ b/instrumentation-api-semconv/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/network/internal/NetworkAttributes.java @@ -0,0 +1,29 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +package io.opentelemetry.instrumentation.api.instrumenter.network.internal; + +import static io.opentelemetry.api.common.AttributeKey.longKey; +import static io.opentelemetry.api.common.AttributeKey.stringKey; + +import io.opentelemetry.api.common.AttributeKey; + +/** + * This class is internal and is hence not for public use. Its APIs are unstable and can change at + * any time. + */ +public final class NetworkAttributes { + + public static final AttributeKey NETWORK_LOCAL_ADDRESS = + stringKey("network.local.address"); + + public static final AttributeKey NETWORK_LOCAL_PORT = longKey("network.local.port"); + + public static final AttributeKey NETWORK_PEER_ADDRESS = stringKey("network.peer.address"); + + public static final AttributeKey NETWORK_PEER_PORT = longKey("network.peer.port"); + + private NetworkAttributes() {} +} diff --git a/instrumentation-api-semconv/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/network/internal/ServerAddressAndPortExtractor.java b/instrumentation-api-semconv/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/network/internal/ServerAddressAndPortExtractor.java new file mode 100644 index 000000000000..41f4d3c6554e --- /dev/null +++ b/instrumentation-api-semconv/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/network/internal/ServerAddressAndPortExtractor.java @@ -0,0 +1,38 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +package io.opentelemetry.instrumentation.api.instrumenter.network.internal; + +import io.opentelemetry.instrumentation.api.instrumenter.network.ServerAttributesGetter; + +/** + * This class is internal and is hence not for public use. Its APIs are unstable and can change at + * any time. + */ +public final class ServerAddressAndPortExtractor + implements AddressAndPortExtractor { + + private final ServerAttributesGetter getter; + private final AddressAndPortExtractor fallbackAddressAndPortExtractor; + + public ServerAddressAndPortExtractor( + ServerAttributesGetter getter, + AddressAndPortExtractor fallbackAddressAndPortExtractor) { + this.getter = getter; + this.fallbackAddressAndPortExtractor = fallbackAddressAndPortExtractor; + } + + @Override + public void extract(AddressPortSink sink, REQUEST request) { + String address = getter.getServerAddress(request); + Integer port = getter.getServerPort(request); + if (address == null && port == null) { + fallbackAddressAndPortExtractor.extract(sink, request); + } else { + sink.setAddress(address); + sink.setPort(port); + } + } +} diff --git a/instrumentation-api-semconv/src/test/java/io/opentelemetry/instrumentation/api/instrumenter/http/ClientAddressAndPortExtractorTest.java b/instrumentation-api-semconv/src/test/java/io/opentelemetry/instrumentation/api/instrumenter/http/ForwardedAddressAndPortExtractorTest.java similarity index 96% rename from instrumentation-api-semconv/src/test/java/io/opentelemetry/instrumentation/api/instrumenter/http/ClientAddressAndPortExtractorTest.java rename to instrumentation-api-semconv/src/test/java/io/opentelemetry/instrumentation/api/instrumenter/http/ForwardedAddressAndPortExtractorTest.java index d1ae3f0b12f3..fe755c1606b5 100644 --- a/instrumentation-api-semconv/src/test/java/io/opentelemetry/instrumentation/api/instrumenter/http/ClientAddressAndPortExtractorTest.java +++ b/instrumentation-api-semconv/src/test/java/io/opentelemetry/instrumentation/api/instrumenter/http/ForwardedAddressAndPortExtractorTest.java @@ -12,7 +12,7 @@ import static org.junit.jupiter.params.provider.Arguments.arguments; import static org.mockito.Mockito.doReturn; -import io.opentelemetry.instrumentation.api.instrumenter.network.internal.FallbackAddressPortExtractor; +import io.opentelemetry.instrumentation.api.instrumenter.network.internal.AddressAndPortExtractor; import java.util.List; import java.util.stream.Stream; import javax.annotation.Nullable; @@ -27,11 +27,11 @@ import org.mockito.junit.jupiter.MockitoExtension; @ExtendWith(MockitoExtension.class) -class ClientAddressAndPortExtractorTest { +class ForwardedAddressAndPortExtractorTest { @Mock HttpServerAttributesGetter getter; - @InjectMocks ClientAddressAndPortExtractor underTest; + @InjectMocks ForwardedAddressAndPortExtractor underTest; @ParameterizedTest @ArgumentsSource(ForwardedArgs.class) @@ -148,7 +148,7 @@ public Stream provideArguments(ExtensionContext extensionCo } } - static final class AddressAndPort implements FallbackAddressPortExtractor.AddressPortSink { + static final class AddressAndPort implements AddressAndPortExtractor.AddressPortSink { @Nullable String address = null; @Nullable Integer port = null; @@ -159,7 +159,7 @@ public void setAddress(String address) { } @Override - public void setPort(int port) { + public void setPort(Integer port) { this.port = port; } } diff --git a/instrumentation-api-semconv/src/test/java/io/opentelemetry/instrumentation/api/instrumenter/http/HttpAddressPortExtractorTest.java b/instrumentation-api-semconv/src/test/java/io/opentelemetry/instrumentation/api/instrumenter/http/HostAddressAndPortExtractorTest.java similarity index 90% rename from instrumentation-api-semconv/src/test/java/io/opentelemetry/instrumentation/api/instrumenter/http/HttpAddressPortExtractorTest.java rename to instrumentation-api-semconv/src/test/java/io/opentelemetry/instrumentation/api/instrumenter/http/HostAddressAndPortExtractorTest.java index 10692f2be131..cee35b9e9f1b 100644 --- a/instrumentation-api-semconv/src/test/java/io/opentelemetry/instrumentation/api/instrumenter/http/HttpAddressPortExtractorTest.java +++ b/instrumentation-api-semconv/src/test/java/io/opentelemetry/instrumentation/api/instrumenter/http/HostAddressAndPortExtractorTest.java @@ -12,7 +12,7 @@ import static org.mockito.Mockito.verifyNoMoreInteractions; import static org.mockito.Mockito.when; -import io.opentelemetry.instrumentation.api.instrumenter.network.internal.FallbackAddressPortExtractor; +import io.opentelemetry.instrumentation.api.instrumenter.network.internal.AddressAndPortExtractor; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.InjectMocks; @@ -20,14 +20,14 @@ import org.mockito.junit.jupiter.MockitoExtension; @ExtendWith(MockitoExtension.class) -class HttpAddressPortExtractorTest { +class HostAddressAndPortExtractorTest { private static final String REQUEST = "request"; @Mock HttpCommonAttributesGetter getter; - @Mock FallbackAddressPortExtractor.AddressPortSink sink; + @Mock AddressAndPortExtractor.AddressPortSink sink; - @InjectMocks HttpAddressPortExtractor underTest; + @InjectMocks HostAddressAndPortExtractor underTest; @Test void noHostHeader() { diff --git a/instrumentation-api-semconv/src/test/java/io/opentelemetry/instrumentation/api/instrumenter/net/InetSocketAddressNetClientAttributesGetterTest.java b/instrumentation-api-semconv/src/test/java/io/opentelemetry/instrumentation/api/instrumenter/net/InetSocketAddressNetClientAttributesGetterTest.java index c02f4c74ace5..72341c84e7be 100644 --- a/instrumentation-api-semconv/src/test/java/io/opentelemetry/instrumentation/api/instrumenter/net/InetSocketAddressNetClientAttributesGetterTest.java +++ b/instrumentation-api-semconv/src/test/java/io/opentelemetry/instrumentation/api/instrumenter/net/InetSocketAddressNetClientAttributesGetterTest.java @@ -6,7 +6,6 @@ package io.opentelemetry.instrumentation.api.instrumenter.net; import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.assertThat; -import static org.assertj.core.api.Assertions.entry; import io.opentelemetry.api.common.Attributes; import io.opentelemetry.api.common.AttributesBuilder; @@ -105,10 +104,6 @@ void unresolved() { // then assertThat(startAttributes.build()).isEmpty(); - - assertThat(endAttributes.build()) - .containsOnly( - entry(SemanticAttributes.NET_SOCK_PEER_NAME, "api.github.com"), - entry(SemanticAttributes.NET_SOCK_PEER_PORT, 456L)); + assertThat(endAttributes.build()).isEmpty(); } } diff --git a/instrumentation-api-semconv/src/test/java/io/opentelemetry/instrumentation/api/instrumenter/net/InetSocketAddressNetServerAttributesGetterTest.java b/instrumentation-api-semconv/src/test/java/io/opentelemetry/instrumentation/api/instrumenter/net/InetSocketAddressNetServerAttributesGetterTest.java index 9c36ba973918..c06874fc80de 100644 --- a/instrumentation-api-semconv/src/test/java/io/opentelemetry/instrumentation/api/instrumenter/net/InetSocketAddressNetServerAttributesGetterTest.java +++ b/instrumentation-api-semconv/src/test/java/io/opentelemetry/instrumentation/api/instrumenter/net/InetSocketAddressNetServerAttributesGetterTest.java @@ -117,11 +117,7 @@ void unresolved() { // then assertThat(startAttributes.build()).isEmpty(); - - assertThat(endAttributes.build()) - .containsOnly( - entry(SemanticAttributes.NET_SOCK_PEER_PORT, 123L), - entry(SemanticAttributes.NET_SOCK_HOST_PORT, 456L)); + assertThat(endAttributes.build()).isEmpty(); } static final class Addresses { diff --git a/instrumentation-api-semconv/src/test/java/io/opentelemetry/instrumentation/api/instrumenter/net/NetClientAttributesExtractorTest.java b/instrumentation-api-semconv/src/test/java/io/opentelemetry/instrumentation/api/instrumenter/net/NetClientAttributesExtractorTest.java index 5331f44cb845..79110451086c 100644 --- a/instrumentation-api-semconv/src/test/java/io/opentelemetry/instrumentation/api/instrumenter/net/NetClientAttributesExtractorTest.java +++ b/instrumentation-api-semconv/src/test/java/io/opentelemetry/instrumentation/api/instrumenter/net/NetClientAttributesExtractorTest.java @@ -18,7 +18,6 @@ import java.util.HashMap; import java.util.Map; import javax.annotation.Nullable; -import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; @SuppressWarnings("deprecation") // testing deprecated class @@ -78,7 +77,9 @@ public String getSockFamily(Map request, Map res @Override public String getServerSocketDomain(Map request, Map response) { - return response.get("sockPeerName"); + // we only support the InetSocketAddress case here; nearly no instrumentations override this + // method anyway + return null; } @Override @@ -110,7 +111,6 @@ void normal() { map.put("peerPort", "42"); map.put("sockFamily", "inet6"); map.put("sockPeerAddr", "1:2:3:4::"); - map.put("sockPeerName", "proxy.opentelemetry.io"); map.put("sockPeerPort", "123"); Context context = Context.root(); @@ -135,7 +135,6 @@ void normal() { entry(SemanticAttributes.NET_PROTOCOL_VERSION, "1.1"), entry(SemanticAttributes.NET_SOCK_FAMILY, "inet6"), entry(SemanticAttributes.NET_SOCK_PEER_ADDR, "1:2:3:4::"), - entry(SemanticAttributes.NET_SOCK_PEER_NAME, "proxy.opentelemetry.io"), entry(SemanticAttributes.NET_SOCK_PEER_PORT, 123L)); } @@ -157,9 +156,7 @@ void empty() { } @Test - @DisplayName( - "does not set those net.sock.peer.* attributes that duplicate corresponding net.peer.* attributes") - void doesNotSetDuplicates1() { + void doesNotSetDuplicateSocketAddress() { // given Map map = new HashMap<>(); map.put("netTransport", IP_TCP); @@ -185,47 +182,7 @@ void doesNotSetDuplicates1() { entry(SemanticAttributes.NET_PEER_NAME, "1:2:3:4::"), entry(SemanticAttributes.NET_PEER_PORT, 42L)); - assertThat(endAttributes.build()) - .containsOnly( - entry(SemanticAttributes.NET_TRANSPORT, IP_TCP), - entry(SemanticAttributes.NET_SOCK_PEER_NAME, "proxy.opentelemetry.io"), - entry(SemanticAttributes.NET_SOCK_PEER_PORT, 123L)); - } - - @Test - @DisplayName( - "does not set net.sock.* attributes when they duplicate related net.peer.* attributes") - void doesNotSetDuplicates2() { - // given - Map map = new HashMap<>(); - map.put("netTransport", IP_TCP); - map.put("peerName", "opentelemetry.io"); - map.put("peerPort", "42"); - map.put("sockFamily", "inet6"); - map.put("sockPeerAddr", "1:2:3:4::"); - map.put("sockPeerName", "opentelemetry.io"); - map.put("sockPeerPort", "42"); - - Context context = Context.root(); - - // when - AttributesBuilder startAttributes = Attributes.builder(); - extractor.onStart(startAttributes, context, map); - - AttributesBuilder endAttributes = Attributes.builder(); - extractor.onEnd(endAttributes, context, map, map, null); - - // then - assertThat(startAttributes.build()) - .containsOnly( - entry(SemanticAttributes.NET_PEER_NAME, "opentelemetry.io"), - entry(SemanticAttributes.NET_PEER_PORT, 42L)); - - assertThat(endAttributes.build()) - .containsOnly( - entry(SemanticAttributes.NET_TRANSPORT, IP_TCP), - entry(SemanticAttributes.NET_SOCK_FAMILY, "inet6"), - entry(SemanticAttributes.NET_SOCK_PEER_ADDR, "1:2:3:4::")); + assertThat(endAttributes.build()).containsOnly(entry(SemanticAttributes.NET_TRANSPORT, IP_TCP)); } @Test diff --git a/instrumentation-api-semconv/src/test/java/io/opentelemetry/instrumentation/api/instrumenter/net/NetServerAttributesExtractorTest.java b/instrumentation-api-semconv/src/test/java/io/opentelemetry/instrumentation/api/instrumenter/net/NetServerAttributesExtractorTest.java index ec41cf0d197a..e4d9b443dc1f 100644 --- a/instrumentation-api-semconv/src/test/java/io/opentelemetry/instrumentation/api/instrumenter/net/NetServerAttributesExtractorTest.java +++ b/instrumentation-api-semconv/src/test/java/io/opentelemetry/instrumentation/api/instrumenter/net/NetServerAttributesExtractorTest.java @@ -18,7 +18,6 @@ import java.util.HashMap; import java.util.Map; import javax.annotation.Nullable; -import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; @SuppressWarnings("deprecation") // testing deprecated class @@ -165,9 +164,7 @@ void empty() { } @Test - @DisplayName( - "does not set those net.sock.host.* attributes that duplicate corresponding net.host.* attributes") - void doesNotSetDuplicates1() { + void doesNotSetDuplicatesSocketAddress() { // given Map map = new HashMap<>(); map.put("netTransport", IP_TCP); @@ -193,42 +190,7 @@ void doesNotSetDuplicates1() { entry(SemanticAttributes.NET_HOST_NAME, "4:3:2:1::"), entry(SemanticAttributes.NET_HOST_PORT, 80L)); - assertThat(endAttributes.build()) - .containsOnly(entry(SemanticAttributes.NET_SOCK_HOST_PORT, 8080L)); - } - - @Test - @DisplayName( - "does not set net.sock.host.* attributes when they duplicate related net.host.* attributes") - void doesNotSetDuplicates2() { - // given - Map map = new HashMap<>(); - map.put("netTransport", IP_TCP); - map.put("hostName", "opentelemetry.io"); - map.put("hostPort", "80"); - map.put("sockFamily", "inet6"); - map.put("sockHostAddr", "4:3:2:1::"); - map.put("sockHostPort", "80"); - - Context context = Context.root(); - - // when - AttributesBuilder startAttributes = Attributes.builder(); - extractor.onStart(startAttributes, context, map); - - AttributesBuilder endAttributes = Attributes.builder(); - extractor.onEnd(endAttributes, context, map, null, null); - - // then - assertThat(startAttributes.build()) - .containsOnly( - entry(SemanticAttributes.NET_TRANSPORT, IP_TCP), - entry(SemanticAttributes.NET_HOST_NAME, "opentelemetry.io"), - entry(SemanticAttributes.NET_HOST_PORT, 80L), - entry(SemanticAttributes.NET_SOCK_FAMILY, "inet6")); - - assertThat(endAttributes.build()) - .containsOnly(entry(SemanticAttributes.NET_SOCK_HOST_ADDR, "4:3:2:1::")); + assertThat(endAttributes.build()).isEmpty(); } @Test diff --git a/instrumentation-api-semconv/src/test/java/io/opentelemetry/instrumentation/api/instrumenter/network/ClientAttributesExtractorOldSemconvTest.java b/instrumentation-api-semconv/src/test/java/io/opentelemetry/instrumentation/api/instrumenter/network/ClientAttributesExtractorOldSemconvTest.java index 9c3837b28094..7fcc492c775e 100644 --- a/instrumentation-api-semconv/src/test/java/io/opentelemetry/instrumentation/api/instrumenter/network/ClientAttributesExtractorOldSemconvTest.java +++ b/instrumentation-api-semconv/src/test/java/io/opentelemetry/instrumentation/api/instrumenter/network/ClientAttributesExtractorOldSemconvTest.java @@ -37,19 +37,6 @@ public Integer getClientPort(Map request) { String value = request.get("port"); return value == null ? null : Integer.parseInt(value); } - - @Nullable - @Override - public String getClientSocketAddress(Map request, @Nullable Void response) { - return request.get("socketAddress"); - } - - @Nullable - @Override - public Integer getClientSocketPort(Map request, @Nullable Void response) { - String value = request.get("socketPort"); - return value == null ? null : Integer.parseInt(value); - } } @Test @@ -57,8 +44,6 @@ void allAttributes() { Map request = new HashMap<>(); request.put("address", "opentelemetry.io"); request.put("port", "80"); - request.put("socketAddress", "1.2.3.4"); - request.put("socketPort", "8080"); AttributesExtractor, Void> extractor = ClientAttributesExtractor.create(new TestClientAttributesGetter()); @@ -70,10 +55,7 @@ void allAttributes() { AttributesBuilder endAttributes = Attributes.builder(); extractor.onEnd(endAttributes, Context.root(), request, null, null); - assertThat(endAttributes.build()) - .containsOnly( - entry(SemanticAttributes.NET_SOCK_PEER_ADDR, "1.2.3.4"), - entry(SemanticAttributes.NET_SOCK_PEER_PORT, 8080L)); + assertThat(endAttributes.build()).isEmpty(); } @Test diff --git a/instrumentation-api-semconv/src/test/java/io/opentelemetry/instrumentation/api/instrumenter/network/NetworkAttributesExtractorOldSemconvTest.java b/instrumentation-api-semconv/src/test/java/io/opentelemetry/instrumentation/api/instrumenter/network/NetworkAttributesExtractorOldSemconvTest.java index 757ca7ceff3b..7ea4e0cf74a4 100644 --- a/instrumentation-api-semconv/src/test/java/io/opentelemetry/instrumentation/api/instrumenter/network/NetworkAttributesExtractorOldSemconvTest.java +++ b/instrumentation-api-semconv/src/test/java/io/opentelemetry/instrumentation/api/instrumenter/network/NetworkAttributesExtractorOldSemconvTest.java @@ -48,6 +48,32 @@ public String getNetworkProtocolName(Map request, @Nullable Void public String getNetworkProtocolVersion(Map request, @Nullable Void response) { return request.get("protocolVersion"); } + + @Nullable + @Override + public String getNetworkLocalAddress(Map request, @Nullable Void response) { + return request.get("localAddress"); + } + + @Nullable + @Override + public Integer getNetworkLocalPort(Map request, @Nullable Void response) { + String value = request.get("localPort"); + return value == null ? null : Integer.parseInt(value); + } + + @Nullable + @Override + public String getNetworkPeerAddress(Map request, @Nullable Void response) { + return request.get("peerAddress"); + } + + @Nullable + @Override + public Integer getNetworkPeerPort(Map request, @Nullable Void response) { + String value = request.get("peerPort"); + return value == null ? null : Integer.parseInt(value); + } } @Test @@ -57,6 +83,10 @@ void allAttributes() { request.put("type", "IPv4"); request.put("protocolName", "Http"); request.put("protocolVersion", "1.1"); + request.put("localAddress", "1.2.3.4"); + request.put("localPort", "8080"); + request.put("peerAddress", "4.3.2.1"); + request.put("peerPort", "9090"); AttributesExtractor, Void> extractor = NetworkAttributesExtractor.create(new TestNetworkAttributesGetter()); @@ -71,7 +101,11 @@ void allAttributes() { .containsOnly( // the NetworkAttributesExtractor can't emit old net.transport & net.sock.family entry(SemanticAttributes.NET_PROTOCOL_NAME, "http"), - entry(SemanticAttributes.NET_PROTOCOL_VERSION, "1.1")); + entry(SemanticAttributes.NET_PROTOCOL_VERSION, "1.1"), + entry(SemanticAttributes.NET_SOCK_HOST_ADDR, "1.2.3.4"), + entry(SemanticAttributes.NET_SOCK_HOST_PORT, 8080L), + entry(SemanticAttributes.NET_SOCK_PEER_ADDR, "4.3.2.1"), + entry(SemanticAttributes.NET_SOCK_PEER_PORT, 9090L)); } @Test diff --git a/instrumentation-api-semconv/src/test/java/io/opentelemetry/instrumentation/api/instrumenter/network/ServerAttributesExtractorOldSemconvTest.java b/instrumentation-api-semconv/src/test/java/io/opentelemetry/instrumentation/api/instrumenter/network/ServerAttributesExtractorOldSemconvTest.java index 916b875cbd30..a294ef229c5a 100644 --- a/instrumentation-api-semconv/src/test/java/io/opentelemetry/instrumentation/api/instrumenter/network/ServerAttributesExtractorOldSemconvTest.java +++ b/instrumentation-api-semconv/src/test/java/io/opentelemetry/instrumentation/api/instrumenter/network/ServerAttributesExtractorOldSemconvTest.java @@ -37,25 +37,6 @@ public Integer getServerPort(Map request) { String port = request.get("port"); return port == null ? null : Integer.parseInt(port); } - - @Nullable - @Override - public String getServerSocketDomain(Map request, @Nullable Void response) { - return request.get("socketDomain"); - } - - @Nullable - @Override - public String getServerSocketAddress(Map request, @Nullable Void response) { - return request.get("socketAddress"); - } - - @Nullable - @Override - public Integer getServerSocketPort(Map request, @Nullable Void response) { - String port = request.get("socketPort"); - return port == null ? null : Integer.parseInt(port); - } } @Test @@ -63,9 +44,6 @@ void allAttributes_peer() { Map request = new HashMap<>(); request.put("address", "opentelemetry.io"); request.put("port", "80"); - request.put("socketDomain", "proxy.opentelemetry.io"); - request.put("socketAddress", "1.2.3.4"); - request.put("socketPort", "8080"); AttributesExtractor, Void> extractor = ServerAttributesExtractor.create(new TestServerAttributesGetter()); @@ -79,11 +57,7 @@ void allAttributes_peer() { AttributesBuilder endAttributes = Attributes.builder(); extractor.onEnd(endAttributes, Context.root(), request, null, null); - assertThat(endAttributes.build()) - .containsOnly( - entry(SemanticAttributes.NET_SOCK_PEER_NAME, "proxy.opentelemetry.io"), - entry(SemanticAttributes.NET_SOCK_PEER_ADDR, "1.2.3.4"), - entry(SemanticAttributes.NET_SOCK_PEER_PORT, 8080L)); + assertThat(endAttributes.build()).isEmpty(); } @SuppressWarnings("deprecation") // need to test the old semconv too @@ -92,9 +66,6 @@ void allAttributes_host() { Map request = new HashMap<>(); request.put("address", "opentelemetry.io"); request.put("port", "80"); - request.put("socketDomain", "proxy.opentelemetry.io"); - request.put("socketAddress", "1.2.3.4"); - request.put("socketPort", "8080"); AttributesExtractor, Void> extractor = ServerAttributesExtractor.createForServerSide(new TestServerAttributesGetter()); @@ -108,10 +79,7 @@ void allAttributes_host() { AttributesBuilder endAttributes = Attributes.builder(); extractor.onEnd(endAttributes, Context.root(), request, null, null); - assertThat(endAttributes.build()) - .containsOnly( - entry(SemanticAttributes.NET_SOCK_HOST_ADDR, "1.2.3.4"), - entry(SemanticAttributes.NET_SOCK_HOST_PORT, 8080L)); + assertThat(endAttributes.build()).isEmpty(); } @Test diff --git a/instrumentation-api-semconv/src/testBothHttpSemconv/java/io/opentelemetry/instrumentation/api/instrumenter/net/NetClientAttributesExtractorBothSemconvTest.java b/instrumentation-api-semconv/src/testBothHttpSemconv/java/io/opentelemetry/instrumentation/api/instrumenter/net/NetClientAttributesExtractorBothSemconvTest.java index 2bb799eaee0a..bde4b7d32f11 100644 --- a/instrumentation-api-semconv/src/testBothHttpSemconv/java/io/opentelemetry/instrumentation/api/instrumenter/net/NetClientAttributesExtractorBothSemconvTest.java +++ b/instrumentation-api-semconv/src/testBothHttpSemconv/java/io/opentelemetry/instrumentation/api/instrumenter/net/NetClientAttributesExtractorBothSemconvTest.java @@ -13,6 +13,7 @@ import io.opentelemetry.api.common.AttributesBuilder; import io.opentelemetry.context.Context; import io.opentelemetry.instrumentation.api.instrumenter.AttributesExtractor; +import io.opentelemetry.instrumentation.api.instrumenter.network.internal.NetworkAttributes; import io.opentelemetry.semconv.SemanticAttributes; import java.util.HashMap; import java.util.Map; @@ -139,10 +140,8 @@ void normal() { entry(SemanticAttributes.NETWORK_PROTOCOL_VERSION, "1.1"), entry(SemanticAttributes.NET_SOCK_FAMILY, "inet6"), entry(SemanticAttributes.NET_SOCK_PEER_ADDR, "1:2:3:4::"), - entry(SemanticAttributes.NET_SOCK_PEER_NAME, "proxy.opentelemetry.io"), entry(SemanticAttributes.NET_SOCK_PEER_PORT, 123L), - entry(SemanticAttributes.SERVER_SOCKET_DOMAIN, "proxy.opentelemetry.io"), - entry(SemanticAttributes.SERVER_SOCKET_ADDRESS, "1:2:3:4::"), - entry(SemanticAttributes.SERVER_SOCKET_PORT, 123L)); + entry(NetworkAttributes.NETWORK_PEER_ADDRESS, "1:2:3:4::"), + entry(NetworkAttributes.NETWORK_PEER_PORT, 123L)); } } diff --git a/instrumentation-api-semconv/src/testBothHttpSemconv/java/io/opentelemetry/instrumentation/api/instrumenter/net/NetServerAttributesExtractorBothSemconvTest.java b/instrumentation-api-semconv/src/testBothHttpSemconv/java/io/opentelemetry/instrumentation/api/instrumenter/net/NetServerAttributesExtractorBothSemconvTest.java index f1301379eedc..7d938f131653 100644 --- a/instrumentation-api-semconv/src/testBothHttpSemconv/java/io/opentelemetry/instrumentation/api/instrumenter/net/NetServerAttributesExtractorBothSemconvTest.java +++ b/instrumentation-api-semconv/src/testBothHttpSemconv/java/io/opentelemetry/instrumentation/api/instrumenter/net/NetServerAttributesExtractorBothSemconvTest.java @@ -13,6 +13,7 @@ import io.opentelemetry.api.common.AttributesBuilder; import io.opentelemetry.context.Context; import io.opentelemetry.instrumentation.api.instrumenter.AttributesExtractor; +import io.opentelemetry.instrumentation.api.instrumenter.network.internal.NetworkAttributes; import io.opentelemetry.semconv.SemanticAttributes; import java.util.HashMap; import java.util.Map; @@ -141,12 +142,12 @@ void normal() { .containsOnly( entry(SemanticAttributes.NET_SOCK_HOST_ADDR, "4:3:2:1::"), entry(SemanticAttributes.NET_SOCK_HOST_PORT, 8080L), - entry(SemanticAttributes.SERVER_SOCKET_ADDRESS, "4:3:2:1::"), - entry(SemanticAttributes.SERVER_SOCKET_PORT, 8080L), + entry(NetworkAttributes.NETWORK_LOCAL_ADDRESS, "4:3:2:1::"), + entry(NetworkAttributes.NETWORK_LOCAL_PORT, 8080L), entry(SemanticAttributes.NET_SOCK_PEER_ADDR, "1:2:3:4::"), entry(SemanticAttributes.NET_SOCK_PEER_PORT, 42L), - entry(SemanticAttributes.CLIENT_SOCKET_ADDRESS, "1:2:3:4::"), - entry(SemanticAttributes.CLIENT_SOCKET_PORT, 42L), + entry(NetworkAttributes.NETWORK_PEER_ADDRESS, "1:2:3:4::"), + entry(NetworkAttributes.NETWORK_PEER_PORT, 42L), entry(SemanticAttributes.NETWORK_TRANSPORT, "tcp"), entry(SemanticAttributes.NETWORK_TYPE, "ipv6"), entry(SemanticAttributes.NETWORK_PROTOCOL_NAME, "http"), diff --git a/instrumentation-api-semconv/src/testStableHttpSemconv/java/io/opentelemetry/instrumentation/api/instrumenter/http/HttpClientAttributesExtractorStableSemconvTest.java b/instrumentation-api-semconv/src/testStableHttpSemconv/java/io/opentelemetry/instrumentation/api/instrumenter/http/HttpClientAttributesExtractorStableSemconvTest.java index a48cea562236..500b01819259 100644 --- a/instrumentation-api-semconv/src/testStableHttpSemconv/java/io/opentelemetry/instrumentation/api/instrumenter/http/HttpClientAttributesExtractorStableSemconvTest.java +++ b/instrumentation-api-semconv/src/testStableHttpSemconv/java/io/opentelemetry/instrumentation/api/instrumenter/http/HttpClientAttributesExtractorStableSemconvTest.java @@ -19,6 +19,7 @@ import io.opentelemetry.context.Context; import io.opentelemetry.instrumentation.api.instrumenter.AttributesExtractor; import io.opentelemetry.instrumentation.api.instrumenter.http.internal.HttpAttributes; +import io.opentelemetry.instrumentation.api.instrumenter.network.internal.NetworkAttributes; import io.opentelemetry.instrumentation.api.internal.HttpConstants; import io.opentelemetry.semconv.SemanticAttributes; import java.net.ConnectException; @@ -90,14 +91,29 @@ public String getNetworkType( @Override public String getNetworkProtocolName( Map request, @Nullable Map response) { - return request.get("protocolName"); + return request.get("networkProtocolName"); } @Nullable @Override public String getNetworkProtocolVersion( Map request, @Nullable Map response) { - return request.get("protocolVersion"); + return request.get("networkProtocolVersion"); + } + + @Nullable + @Override + public String getNetworkPeerAddress( + Map request, @Nullable Map response) { + return request.get("networkPeerAddress"); + } + + @Nullable + @Override + public Integer getNetworkPeerPort( + Map request, @Nullable Map response) { + String value = request.get("networkPeerPort"); + return value == null ? null : Integer.parseInt(value); } @Nullable @@ -133,8 +149,10 @@ void normal() { request.put("header.custom-request-header", "123,456"); request.put("networkTransport", "udp"); request.put("networkType", "ipv4"); - request.put("protocolName", "http"); - request.put("protocolVersion", "1.1"); + request.put("networkProtocolName", "http"); + request.put("networkProtocolVersion", "1.1"); + request.put("networkPeerAddress", "4.3.2.1"); + request.put("networkPeerPort", "456"); request.put("serverAddress", "github.com"); request.put("serverPort", "123"); @@ -179,7 +197,9 @@ void normal() { entry(SemanticAttributes.NETWORK_TRANSPORT, "udp"), entry(SemanticAttributes.NETWORK_TYPE, "ipv4"), entry(SemanticAttributes.NETWORK_PROTOCOL_NAME, "http"), - entry(SemanticAttributes.NETWORK_PROTOCOL_VERSION, "1.1")); + entry(SemanticAttributes.NETWORK_PROTOCOL_VERSION, "1.1"), + entry(NetworkAttributes.NETWORK_PEER_ADDRESS, "4.3.2.1"), + entry(NetworkAttributes.NETWORK_PEER_PORT, 456L)); } @ParameterizedTest @@ -190,8 +210,8 @@ void skipNetworkTransportIfDefaultForProtocol( String observedTransport, @Nullable String extractedTransport) { Map request = new HashMap<>(); - request.put("protocolName", observedProtocolName); - request.put("protocolVersion", observedProtocolVersion); + request.put("networkProtocolName", observedProtocolName); + request.put("networkProtocolVersion", observedProtocolVersion); request.put("networkTransport", observedTransport); AttributesExtractor, Map> extractor = @@ -376,4 +396,55 @@ void shouldExtractErrorType_other() { assertThat(attributes.build()).containsEntry(HttpAttributes.ERROR_TYPE, HttpConstants._OTHER); } + + @Test + void shouldExtractServerAddressAndPortFromHostHeader() { + Map request = new HashMap<>(); + request.put("header.host", "github.com:123"); + + Map response = new HashMap<>(); + response.put("statusCode", "200"); + + AttributesExtractor, Map> extractor = + HttpClientAttributesExtractor.create(new TestHttpClientAttributesGetter()); + + AttributesBuilder startAttributes = Attributes.builder(); + extractor.onStart(startAttributes, Context.root(), request); + assertThat(startAttributes.build()) + .containsOnly( + entry(SemanticAttributes.SERVER_ADDRESS, "github.com"), + entry(SemanticAttributes.SERVER_PORT, 123L)); + + AttributesBuilder endAttributes = Attributes.builder(); + extractor.onEnd(endAttributes, Context.root(), request, response, null); + assertThat(endAttributes.build()) + .containsOnly(entry(SemanticAttributes.HTTP_RESPONSE_STATUS_CODE, 200L)); + } + + @Test + void shouldNotExtractDuplicatePeerAddress() { + Map request = new HashMap<>(); + request.put("networkPeerAddress", "1.2.3.4"); + request.put("networkPeerPort", "456"); + request.put("serverAddress", "1.2.3.4"); + request.put("serverPort", "123"); + + Map response = new HashMap<>(); + response.put("statusCode", "200"); + + AttributesExtractor, Map> extractor = + HttpClientAttributesExtractor.create(new TestHttpClientAttributesGetter()); + + AttributesBuilder startAttributes = Attributes.builder(); + extractor.onStart(startAttributes, Context.root(), request); + assertThat(startAttributes.build()) + .containsOnly( + entry(SemanticAttributes.SERVER_ADDRESS, "1.2.3.4"), + entry(SemanticAttributes.SERVER_PORT, 123L)); + + AttributesBuilder endAttributes = Attributes.builder(); + extractor.onEnd(endAttributes, Context.root(), request, response, null); + assertThat(endAttributes.build()) + .containsOnly(entry(SemanticAttributes.HTTP_RESPONSE_STATUS_CODE, 200L)); + } } diff --git a/instrumentation-api-semconv/src/testStableHttpSemconv/java/io/opentelemetry/instrumentation/api/instrumenter/http/HttpClientExperimentalMetricsStableSemconvTest.java b/instrumentation-api-semconv/src/testStableHttpSemconv/java/io/opentelemetry/instrumentation/api/instrumenter/http/HttpClientExperimentalMetricsStableSemconvTest.java index f77c9119dd36..843dbe562736 100644 --- a/instrumentation-api-semconv/src/testStableHttpSemconv/java/io/opentelemetry/instrumentation/api/instrumenter/http/HttpClientExperimentalMetricsStableSemconvTest.java +++ b/instrumentation-api-semconv/src/testStableHttpSemconv/java/io/opentelemetry/instrumentation/api/instrumenter/http/HttpClientExperimentalMetricsStableSemconvTest.java @@ -16,6 +16,7 @@ import io.opentelemetry.context.Context; import io.opentelemetry.instrumentation.api.instrumenter.OperationListener; import io.opentelemetry.instrumentation.api.instrumenter.http.internal.HttpAttributes; +import io.opentelemetry.instrumentation.api.instrumenter.network.internal.NetworkAttributes; import io.opentelemetry.sdk.metrics.SdkMeterProvider; import io.opentelemetry.sdk.testing.exporter.InMemoryMetricReader; import io.opentelemetry.semconv.SemanticAttributes; @@ -51,9 +52,8 @@ void collectsMetrics() { .put(SemanticAttributes.HTTP_RESPONSE_BODY_SIZE, 200) .put(SemanticAttributes.NETWORK_PROTOCOL_NAME, "http") .put(SemanticAttributes.NETWORK_PROTOCOL_VERSION, "2.0") - .put(SemanticAttributes.SERVER_SOCKET_ADDRESS, "1.2.3.4") - .put(SemanticAttributes.SERVER_SOCKET_DOMAIN, "somehost20") - .put(SemanticAttributes.SERVER_SOCKET_PORT, 8080) + .put(NetworkAttributes.NETWORK_PEER_ADDRESS, "1.2.3.4") + .put(NetworkAttributes.NETWORK_PEER_PORT, 8080) .build(); Context parent = diff --git a/instrumentation-api-semconv/src/testStableHttpSemconv/java/io/opentelemetry/instrumentation/api/instrumenter/http/HttpClientMetricsStableSemconvTest.java b/instrumentation-api-semconv/src/testStableHttpSemconv/java/io/opentelemetry/instrumentation/api/instrumenter/http/HttpClientMetricsStableSemconvTest.java index 1d94a9bdc325..bffda6c5c2db 100644 --- a/instrumentation-api-semconv/src/testStableHttpSemconv/java/io/opentelemetry/instrumentation/api/instrumenter/http/HttpClientMetricsStableSemconvTest.java +++ b/instrumentation-api-semconv/src/testStableHttpSemconv/java/io/opentelemetry/instrumentation/api/instrumenter/http/HttpClientMetricsStableSemconvTest.java @@ -16,6 +16,7 @@ import io.opentelemetry.context.Context; import io.opentelemetry.instrumentation.api.instrumenter.OperationListener; import io.opentelemetry.instrumentation.api.instrumenter.http.internal.HttpAttributes; +import io.opentelemetry.instrumentation.api.instrumenter.network.internal.NetworkAttributes; import io.opentelemetry.sdk.metrics.SdkMeterProvider; import io.opentelemetry.sdk.testing.exporter.InMemoryMetricReader; import io.opentelemetry.semconv.SemanticAttributes; @@ -53,9 +54,8 @@ void collectsMetrics() { .put(SemanticAttributes.HTTP_RESPONSE_BODY_SIZE, 200) .put(SemanticAttributes.NETWORK_PROTOCOL_NAME, "http") .put(SemanticAttributes.NETWORK_PROTOCOL_VERSION, "2.0") - .put(SemanticAttributes.SERVER_SOCKET_ADDRESS, "1.2.3.4") - .put(SemanticAttributes.SERVER_SOCKET_DOMAIN, "somehost20") - .put(SemanticAttributes.SERVER_SOCKET_PORT, 8080) + .put(NetworkAttributes.NETWORK_PEER_ADDRESS, "1.2.3.4") + .put(NetworkAttributes.NETWORK_PEER_PORT, 8080) .build(); Context parent = diff --git a/instrumentation-api-semconv/src/testStableHttpSemconv/java/io/opentelemetry/instrumentation/api/instrumenter/http/HttpServerAttributesExtractorStableSemconvTest.java b/instrumentation-api-semconv/src/testStableHttpSemconv/java/io/opentelemetry/instrumentation/api/instrumenter/http/HttpServerAttributesExtractorStableSemconvTest.java index 5f57a669f202..fe905011a8e6 100644 --- a/instrumentation-api-semconv/src/testStableHttpSemconv/java/io/opentelemetry/instrumentation/api/instrumenter/http/HttpServerAttributesExtractorStableSemconvTest.java +++ b/instrumentation-api-semconv/src/testStableHttpSemconv/java/io/opentelemetry/instrumentation/api/instrumenter/http/HttpServerAttributesExtractorStableSemconvTest.java @@ -19,6 +19,7 @@ import io.opentelemetry.context.Context; import io.opentelemetry.instrumentation.api.instrumenter.AttributesExtractor; import io.opentelemetry.instrumentation.api.instrumenter.http.internal.HttpAttributes; +import io.opentelemetry.instrumentation.api.instrumenter.network.internal.NetworkAttributes; import io.opentelemetry.instrumentation.api.internal.HttpConstants; import io.opentelemetry.semconv.SemanticAttributes; import java.net.ConnectException; @@ -107,41 +108,56 @@ public String getNetworkType( @Override public String getNetworkProtocolName( Map request, Map response) { - return request.get("protocolName"); + return request.get("networkProtocolName"); } @Nullable @Override public String getNetworkProtocolVersion( Map request, Map response) { - return request.get("protocolVersion"); + return request.get("networkProtocolVersion"); } @Nullable @Override - public String getServerAddress(Map request) { - return request.get("serverAddress"); + public String getNetworkLocalAddress( + Map request, @Nullable Map response) { + return request.get("networkLocalAddress"); } @Nullable @Override - public Integer getServerPort(Map request) { - String value = request.get("serverPort"); + public Integer getNetworkLocalPort( + Map request, @Nullable Map response) { + String value = request.get("networkLocalPort"); return value == null ? null : Integer.parseInt(value); } @Nullable @Override - public String getServerSocketAddress( + public String getNetworkPeerAddress( Map request, @Nullable Map response) { - return request.get("serverSocketAddress"); + return request.get("networkPeerAddress"); } @Nullable @Override - public Integer getServerSocketPort( + public Integer getNetworkPeerPort( Map request, @Nullable Map response) { - String value = request.get("serverSocketPort"); + String value = request.get("networkPeerPort"); + return value == null ? null : Integer.parseInt(value); + } + + @Nullable + @Override + public String getServerAddress(Map request) { + return request.get("serverAddress"); + } + + @Nullable + @Override + public Integer getServerPort(Map request) { + String value = request.get("serverPort"); return value == null ? null : Integer.parseInt(value); } @@ -166,15 +182,19 @@ void normal() { request.put("header.content-length", "10"); request.put("route", "/repositories/{id}"); request.put("header.user-agent", "okhttp 3.x"); - request.put("header.host", "github.com"); + request.put("header.host", "www.github.com:456"); request.put("header.forwarded", "for=1.1.1.1;proto=https"); request.put("header.custom-request-header", "123,456"); request.put("networkTransport", "udp"); request.put("networkType", "ipv4"); - request.put("protocolName", "http"); - request.put("protocolVersion", "2.0"); - request.put("serverSocketAddress", "1.2.3.4"); - request.put("serverSocketPort", "42"); + request.put("networkProtocolName", "http"); + request.put("networkProtocolVersion", "2.0"); + request.put("networkLocalAddress", "1.2.3.4"); + request.put("networkLocalPort", "42"); + request.put("networkPeerAddress", "4.3.2.1"); + request.put("networkPeerPort", "456"); + request.put("serverAddress", "github.com"); + request.put("serverPort", "123"); Map response = new HashMap<>(); response.put("statusCode", "202"); @@ -195,6 +215,7 @@ void normal() { assertThat(startAttributes.build()) .containsOnly( entry(SemanticAttributes.SERVER_ADDRESS, "github.com"), + entry(SemanticAttributes.SERVER_PORT, 123L), entry(SemanticAttributes.HTTP_REQUEST_METHOD, "POST"), entry(SemanticAttributes.URL_SCHEME, "http"), entry(SemanticAttributes.URL_PATH, "/repositories/1"), @@ -214,6 +235,8 @@ void normal() { entry(SemanticAttributes.NETWORK_TYPE, "ipv4"), entry(SemanticAttributes.NETWORK_PROTOCOL_NAME, "http"), entry(SemanticAttributes.NETWORK_PROTOCOL_VERSION, "2.0"), + entry(NetworkAttributes.NETWORK_PEER_ADDRESS, "4.3.2.1"), + entry(NetworkAttributes.NETWORK_PEER_PORT, 456L), entry(SemanticAttributes.HTTP_ROUTE, "/repositories/{repoId}"), entry(SemanticAttributes.HTTP_REQUEST_BODY_SIZE, 10L), entry(SemanticAttributes.HTTP_RESPONSE_STATUS_CODE, 202L), @@ -231,8 +254,8 @@ void skipNetworkTransportIfDefaultForProtocol( String observedTransport, @Nullable String extractedTransport) { Map request = new HashMap<>(); - request.put("protocolName", observedProtocolName); - request.put("protocolVersion", observedProtocolVersion); + request.put("networkProtocolName", observedProtocolName); + request.put("networkProtocolVersion", observedProtocolVersion); request.put("networkTransport", observedTransport); AttributesExtractor, Map> extractor = @@ -417,4 +440,54 @@ void shouldExtractErrorType_other() { assertThat(attributes.build()).containsEntry(HttpAttributes.ERROR_TYPE, HttpConstants._OTHER); } + + @Test + void shouldExtractServerAddressAndPortFromHostHeader() { + Map request = new HashMap<>(); + request.put("header.host", "github.com:123"); + + Map response = new HashMap<>(); + response.put("statusCode", "200"); + + AttributesExtractor, Map> extractor = + HttpServerAttributesExtractor.create(new TestHttpServerAttributesGetter()); + + AttributesBuilder startAttributes = Attributes.builder(); + extractor.onStart(startAttributes, Context.root(), request); + assertThat(startAttributes.build()) + .containsOnly( + entry(SemanticAttributes.SERVER_ADDRESS, "github.com"), + entry(SemanticAttributes.SERVER_PORT, 123L)); + + AttributesBuilder endAttributes = Attributes.builder(); + extractor.onEnd(endAttributes, Context.root(), request, response, null); + assertThat(endAttributes.build()) + .containsOnly(entry(SemanticAttributes.HTTP_RESPONSE_STATUS_CODE, 200L)); + } + + @Test + void shouldNotExtractDuplicatePeerAddress() { + Map request = new HashMap<>(); + request.put("networkPeerAddress", "1.2.3.4"); + request.put("networkPeerPort", "456"); + request.put("header.forwarded", "for=1.2.3.4:123"); + + Map response = new HashMap<>(); + response.put("statusCode", "200"); + + AttributesExtractor, Map> extractor = + HttpServerAttributesExtractor.create(new TestHttpServerAttributesGetter()); + + AttributesBuilder startAttributes = Attributes.builder(); + extractor.onStart(startAttributes, Context.root(), request); + assertThat(startAttributes.build()) + .containsOnly( + entry(SemanticAttributes.CLIENT_ADDRESS, "1.2.3.4"), + entry(SemanticAttributes.CLIENT_PORT, 123L)); + + AttributesBuilder endAttributes = Attributes.builder(); + extractor.onEnd(endAttributes, Context.root(), request, response, null); + assertThat(endAttributes.build()) + .containsOnly(entry(SemanticAttributes.HTTP_RESPONSE_STATUS_CODE, 200L)); + } } diff --git a/instrumentation-api-semconv/src/testStableHttpSemconv/java/io/opentelemetry/instrumentation/api/instrumenter/http/HttpServerExperimentalMetricsStableSemconvTest.java b/instrumentation-api-semconv/src/testStableHttpSemconv/java/io/opentelemetry/instrumentation/api/instrumenter/http/HttpServerExperimentalMetricsStableSemconvTest.java index 32ce0239a6ce..b313794577c0 100644 --- a/instrumentation-api-semconv/src/testStableHttpSemconv/java/io/opentelemetry/instrumentation/api/instrumenter/http/HttpServerExperimentalMetricsStableSemconvTest.java +++ b/instrumentation-api-semconv/src/testStableHttpSemconv/java/io/opentelemetry/instrumentation/api/instrumenter/http/HttpServerExperimentalMetricsStableSemconvTest.java @@ -16,6 +16,7 @@ import io.opentelemetry.context.Context; import io.opentelemetry.instrumentation.api.instrumenter.OperationListener; import io.opentelemetry.instrumentation.api.instrumenter.http.internal.HttpAttributes; +import io.opentelemetry.instrumentation.api.instrumenter.network.internal.NetworkAttributes; import io.opentelemetry.sdk.metrics.SdkMeterProvider; import io.opentelemetry.sdk.testing.exporter.InMemoryMetricReader; import io.opentelemetry.semconv.SemanticAttributes; @@ -53,10 +54,10 @@ void collectsMetrics() { .put(HttpAttributes.ERROR_TYPE, "500") .put(SemanticAttributes.HTTP_REQUEST_BODY_SIZE, 100) .put(SemanticAttributes.HTTP_RESPONSE_BODY_SIZE, 200) - .put(SemanticAttributes.CLIENT_SOCKET_ADDRESS, "1.2.3.4") - .put(SemanticAttributes.CLIENT_SOCKET_PORT, 8080) - .put(SemanticAttributes.SERVER_SOCKET_ADDRESS, "4.3.2.1") - .put(SemanticAttributes.SERVER_SOCKET_PORT, 9090) + .put(NetworkAttributes.NETWORK_PEER_ADDRESS, "1.2.3.4") + .put(NetworkAttributes.NETWORK_PEER_PORT, 8080) + .put(NetworkAttributes.NETWORK_LOCAL_ADDRESS, "4.3.2.1") + .put(NetworkAttributes.NETWORK_LOCAL_PORT, 9090) .build(); SpanContext spanContext1 = diff --git a/instrumentation-api-semconv/src/testStableHttpSemconv/java/io/opentelemetry/instrumentation/api/instrumenter/http/HttpServerMetricsStableSemconvTest.java b/instrumentation-api-semconv/src/testStableHttpSemconv/java/io/opentelemetry/instrumentation/api/instrumenter/http/HttpServerMetricsStableSemconvTest.java index 2ea4be9d7f29..57776bf00e87 100644 --- a/instrumentation-api-semconv/src/testStableHttpSemconv/java/io/opentelemetry/instrumentation/api/instrumenter/http/HttpServerMetricsStableSemconvTest.java +++ b/instrumentation-api-semconv/src/testStableHttpSemconv/java/io/opentelemetry/instrumentation/api/instrumenter/http/HttpServerMetricsStableSemconvTest.java @@ -16,6 +16,7 @@ import io.opentelemetry.context.Context; import io.opentelemetry.instrumentation.api.instrumenter.OperationListener; import io.opentelemetry.instrumentation.api.instrumenter.http.internal.HttpAttributes; +import io.opentelemetry.instrumentation.api.instrumenter.network.internal.NetworkAttributes; import io.opentelemetry.sdk.metrics.SdkMeterProvider; import io.opentelemetry.sdk.testing.exporter.InMemoryMetricReader; import io.opentelemetry.semconv.SemanticAttributes; @@ -55,10 +56,10 @@ void collectsMetrics() { .put(HttpAttributes.ERROR_TYPE, "500") .put(SemanticAttributes.HTTP_REQUEST_BODY_SIZE, 100) .put(SemanticAttributes.HTTP_RESPONSE_BODY_SIZE, 200) - .put(SemanticAttributes.CLIENT_SOCKET_ADDRESS, "1.2.3.4") - .put(SemanticAttributes.CLIENT_SOCKET_PORT, 8080) - .put(SemanticAttributes.SERVER_SOCKET_ADDRESS, "4.3.2.1") - .put(SemanticAttributes.SERVER_SOCKET_PORT, 9090) + .put(NetworkAttributes.NETWORK_PEER_ADDRESS, "1.2.3.4") + .put(NetworkAttributes.NETWORK_PEER_PORT, 8080) + .put(NetworkAttributes.NETWORK_LOCAL_ADDRESS, "4.3.2.1") + .put(NetworkAttributes.NETWORK_LOCAL_PORT, 9090) .build(); SpanContext spanContext1 = diff --git a/instrumentation-api-semconv/src/testStableHttpSemconv/java/io/opentelemetry/instrumentation/api/instrumenter/net/NetClientAttributesExtractorStableSemconvTest.java b/instrumentation-api-semconv/src/testStableHttpSemconv/java/io/opentelemetry/instrumentation/api/instrumenter/net/NetClientAttributesExtractorStableSemconvTest.java index b27fdbe9bb96..65f0c7311e8a 100644 --- a/instrumentation-api-semconv/src/testStableHttpSemconv/java/io/opentelemetry/instrumentation/api/instrumenter/net/NetClientAttributesExtractorStableSemconvTest.java +++ b/instrumentation-api-semconv/src/testStableHttpSemconv/java/io/opentelemetry/instrumentation/api/instrumenter/net/NetClientAttributesExtractorStableSemconvTest.java @@ -13,6 +13,7 @@ import io.opentelemetry.api.common.AttributesBuilder; import io.opentelemetry.context.Context; import io.opentelemetry.instrumentation.api.instrumenter.AttributesExtractor; +import io.opentelemetry.instrumentation.api.instrumenter.network.internal.NetworkAttributes; import io.opentelemetry.semconv.SemanticAttributes; import java.util.HashMap; import java.util.Map; @@ -74,11 +75,6 @@ public String getSockFamily(Map request, Map res return response.get("sockFamily"); } - @Override - public String getServerSocketDomain(Map request, Map response) { - return response.get("sockPeerName"); - } - @Override public String getServerSocketAddress( Map request, Map response) { @@ -108,7 +104,6 @@ void normal() { map.put("peerPort", "42"); map.put("sockFamily", "inet6"); map.put("sockPeerAddr", "1:2:3:4::"); - map.put("sockPeerName", "proxy.opentelemetry.io"); map.put("sockPeerPort", "123"); Context context = Context.root(); @@ -132,8 +127,7 @@ void normal() { entry(SemanticAttributes.NETWORK_TYPE, "ipv6"), entry(SemanticAttributes.NETWORK_PROTOCOL_NAME, "http"), entry(SemanticAttributes.NETWORK_PROTOCOL_VERSION, "1.1"), - entry(SemanticAttributes.SERVER_SOCKET_DOMAIN, "proxy.opentelemetry.io"), - entry(SemanticAttributes.SERVER_SOCKET_ADDRESS, "1:2:3:4::"), - entry(SemanticAttributes.SERVER_SOCKET_PORT, 123L)); + entry(NetworkAttributes.NETWORK_PEER_ADDRESS, "1:2:3:4::"), + entry(NetworkAttributes.NETWORK_PEER_PORT, 123L)); } } diff --git a/instrumentation-api-semconv/src/testStableHttpSemconv/java/io/opentelemetry/instrumentation/api/instrumenter/net/NetServerAttributesExtractorStableSemconvTest.java b/instrumentation-api-semconv/src/testStableHttpSemconv/java/io/opentelemetry/instrumentation/api/instrumenter/net/NetServerAttributesExtractorStableSemconvTest.java index c44698dae7f1..9259e911151b 100644 --- a/instrumentation-api-semconv/src/testStableHttpSemconv/java/io/opentelemetry/instrumentation/api/instrumenter/net/NetServerAttributesExtractorStableSemconvTest.java +++ b/instrumentation-api-semconv/src/testStableHttpSemconv/java/io/opentelemetry/instrumentation/api/instrumenter/net/NetServerAttributesExtractorStableSemconvTest.java @@ -13,6 +13,7 @@ import io.opentelemetry.api.common.AttributesBuilder; import io.opentelemetry.context.Context; import io.opentelemetry.instrumentation.api.instrumenter.AttributesExtractor; +import io.opentelemetry.instrumentation.api.instrumenter.network.internal.NetworkAttributes; import io.opentelemetry.semconv.SemanticAttributes; import java.util.HashMap; import java.util.Map; @@ -139,9 +140,9 @@ void normal() { entry(SemanticAttributes.NETWORK_TYPE, "ipv6"), entry(SemanticAttributes.NETWORK_PROTOCOL_NAME, "http"), entry(SemanticAttributes.NETWORK_PROTOCOL_VERSION, "1.1"), - entry(SemanticAttributes.SERVER_SOCKET_ADDRESS, "4:3:2:1::"), - entry(SemanticAttributes.SERVER_SOCKET_PORT, 8080L), - entry(SemanticAttributes.CLIENT_SOCKET_ADDRESS, "1:2:3:4::"), - entry(SemanticAttributes.CLIENT_SOCKET_PORT, 42L)); + entry(NetworkAttributes.NETWORK_LOCAL_ADDRESS, "4:3:2:1::"), + entry(NetworkAttributes.NETWORK_LOCAL_PORT, 8080L), + entry(NetworkAttributes.NETWORK_PEER_ADDRESS, "1:2:3:4::"), + entry(NetworkAttributes.NETWORK_PEER_PORT, 42L)); } } diff --git a/instrumentation-api-semconv/src/testStableHttpSemconv/java/io/opentelemetry/instrumentation/api/instrumenter/network/ClientAttributesExtractorInetSocketAddressTest.java b/instrumentation-api-semconv/src/testStableHttpSemconv/java/io/opentelemetry/instrumentation/api/instrumenter/network/ClientAttributesExtractorInetSocketAddressTest.java deleted file mode 100644 index 70ecb02d7c00..000000000000 --- a/instrumentation-api-semconv/src/testStableHttpSemconv/java/io/opentelemetry/instrumentation/api/instrumenter/network/ClientAttributesExtractorInetSocketAddressTest.java +++ /dev/null @@ -1,81 +0,0 @@ -/* - * Copyright The OpenTelemetry Authors - * SPDX-License-Identifier: Apache-2.0 - */ - -package io.opentelemetry.instrumentation.api.instrumenter.network; - -import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.assertThat; -import static org.assertj.core.api.Assertions.entry; - -import io.opentelemetry.api.common.Attributes; -import io.opentelemetry.api.common.AttributesBuilder; -import io.opentelemetry.context.Context; -import io.opentelemetry.instrumentation.api.instrumenter.AttributesExtractor; -import io.opentelemetry.semconv.SemanticAttributes; -import java.net.InetSocketAddress; -import javax.annotation.Nullable; -import org.junit.jupiter.api.Test; - -class ClientAttributesExtractorInetSocketAddressTest { - - static class TestClientAttributesGetter - implements ClientAttributesGetter { - - @Nullable - @Override - public String getClientAddress(InetSocketAddress request) { - // covered in ClientAttributesExtractorTest - return null; - } - - @Nullable - @Override - public Integer getClientPort(InetSocketAddress request) { - // covered in ClientAttributesExtractorTest - return null; - } - - @Nullable - @Override - public InetSocketAddress getClientInetSocketAddress( - InetSocketAddress request, @Nullable Void response) { - return request; - } - } - - @Test - @SuppressWarnings("AddressSelection") - void fullAddress() { - InetSocketAddress address = new InetSocketAddress("api.github.com", 456); - assertThat(address.getAddress().getHostAddress()).isNotNull(); - - AttributesExtractor extractor = - ClientAttributesExtractor.create(new TestClientAttributesGetter()); - - AttributesBuilder startAttributes = Attributes.builder(); - extractor.onStart(startAttributes, Context.root(), address); - assertThat(startAttributes.build()).isEmpty(); - - AttributesBuilder endAttributes = Attributes.builder(); - extractor.onEnd(endAttributes, Context.root(), address, null, null); - assertThat(endAttributes.build()) - .containsOnly( - entry(SemanticAttributes.CLIENT_SOCKET_ADDRESS, address.getAddress().getHostAddress()), - entry(SemanticAttributes.CLIENT_SOCKET_PORT, 456L)); - } - - @Test - void noAttributes() { - AttributesExtractor extractor = - ClientAttributesExtractor.create(new TestClientAttributesGetter()); - - AttributesBuilder startAttributes = Attributes.builder(); - extractor.onStart(startAttributes, Context.root(), null); - assertThat(startAttributes.build()).isEmpty(); - - AttributesBuilder endAttributes = Attributes.builder(); - extractor.onEnd(endAttributes, Context.root(), null, null, null); - assertThat(endAttributes.build()).isEmpty(); - } -} 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 170d70fcc6c9..aa0ef6f785ba 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 @@ -36,19 +36,6 @@ public Integer getClientPort(Map request) { String value = request.get("port"); return value == null ? null : Integer.parseInt(value); } - - @Nullable - @Override - public String getClientSocketAddress(Map request, @Nullable Void response) { - return request.get("socketAddress"); - } - - @Nullable - @Override - public Integer getClientSocketPort(Map request, @Nullable Void response) { - String value = request.get("socketPort"); - return value == null ? null : Integer.parseInt(value); - } } @Test @@ -56,8 +43,6 @@ void allAttributes() { Map request = new HashMap<>(); request.put("address", "opentelemetry.io"); request.put("port", "80"); - request.put("socketAddress", "1.2.3.4"); - request.put("socketPort", "8080"); AttributesExtractor, Void> extractor = ClientAttributesExtractor.create(new TestClientAttributesGetter()); @@ -71,10 +56,7 @@ void allAttributes() { AttributesBuilder endAttributes = Attributes.builder(); extractor.onEnd(endAttributes, Context.root(), request, null, null); - assertThat(endAttributes.build()) - .containsOnly( - entry(SemanticAttributes.CLIENT_SOCKET_ADDRESS, "1.2.3.4"), - entry(SemanticAttributes.CLIENT_SOCKET_PORT, 8080L)); + assertThat(endAttributes.build()).isEmpty(); } @Test @@ -92,10 +74,9 @@ void noAttributes() { } @Test - void doesNotSetNegativePortValues() { + void doesNotSetNegativePortValue() { Map request = new HashMap<>(); request.put("port", "-12"); - request.put("socketPort", "-42"); AttributesExtractor, Void> extractor = ClientAttributesExtractor.create(new TestClientAttributesGetter()); @@ -108,27 +89,4 @@ void doesNotSetNegativePortValues() { extractor.onEnd(endAttributes, Context.root(), request, null, null); assertThat(endAttributes.build()).isEmpty(); } - - @Test - void doesNotSetDuplicates() { - Map request = new HashMap<>(); - request.put("address", "opentelemetry.io"); - request.put("port", "80"); - request.put("socketAddress", "opentelemetry.io"); - request.put("socketPort", "80"); - - AttributesExtractor, Void> extractor = - ClientAttributesExtractor.create(new TestClientAttributesGetter()); - - AttributesBuilder startAttributes = Attributes.builder(); - extractor.onStart(startAttributes, Context.root(), request); - assertThat(startAttributes.build()) - .containsOnly( - entry(SemanticAttributes.CLIENT_ADDRESS, "opentelemetry.io"), - entry(SemanticAttributes.CLIENT_PORT, 80L)); - - 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/NetworkAttributesExtractorInetSocketAddressTest.java b/instrumentation-api-semconv/src/testStableHttpSemconv/java/io/opentelemetry/instrumentation/api/instrumenter/network/NetworkAttributesExtractorInetSocketAddressTest.java new file mode 100644 index 000000000000..7f1ceaf9e9c6 --- /dev/null +++ b/instrumentation-api-semconv/src/testStableHttpSemconv/java/io/opentelemetry/instrumentation/api/instrumenter/network/NetworkAttributesExtractorInetSocketAddressTest.java @@ -0,0 +1,77 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +package io.opentelemetry.instrumentation.api.instrumenter.network; + +import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.assertThat; +import static org.assertj.core.api.Assertions.entry; + +import io.opentelemetry.api.common.Attributes; +import io.opentelemetry.api.common.AttributesBuilder; +import io.opentelemetry.context.Context; +import io.opentelemetry.instrumentation.api.instrumenter.AttributesExtractor; +import io.opentelemetry.instrumentation.api.instrumenter.network.internal.NetworkAttributes; +import io.opentelemetry.semconv.SemanticAttributes; +import java.net.InetSocketAddress; +import javax.annotation.Nullable; +import org.junit.jupiter.api.Test; + +class NetworkAttributesExtractorInetSocketAddressTest { + + static class TestNetworkAttributesGetter + implements NetworkAttributesGetter { + + @Nullable + @Override + public InetSocketAddress getNetworkLocalInetSocketAddress( + InetSocketAddress request, @Nullable InetSocketAddress response) { + return request; + } + + @Nullable + @Override + public InetSocketAddress getNetworkPeerInetSocketAddress( + InetSocketAddress request, @Nullable InetSocketAddress response) { + return response; + } + } + + @Test + void fullAddress() { + InetSocketAddress local = new InetSocketAddress("1.2.3.4", 8080); + InetSocketAddress peer = new InetSocketAddress("4.3.2.1", 9090); + + AttributesExtractor extractor = + NetworkAttributesExtractor.create(new TestNetworkAttributesGetter()); + + AttributesBuilder startAttributes = Attributes.builder(); + extractor.onStart(startAttributes, Context.root(), local); + assertThat(startAttributes.build()).isEmpty(); + + AttributesBuilder endAttributes = Attributes.builder(); + extractor.onEnd(endAttributes, Context.root(), local, peer, null); + assertThat(endAttributes.build()) + .containsOnly( + entry(SemanticAttributes.NETWORK_TYPE, "ipv4"), + entry(NetworkAttributes.NETWORK_LOCAL_ADDRESS, "1.2.3.4"), + entry(NetworkAttributes.NETWORK_LOCAL_PORT, 8080L), + entry(NetworkAttributes.NETWORK_PEER_ADDRESS, "4.3.2.1"), + entry(NetworkAttributes.NETWORK_PEER_PORT, 9090L)); + } + + @Test + void noAttributes() { + AttributesExtractor extractor = + NetworkAttributesExtractor.create(new TestNetworkAttributesGetter()); + + AttributesBuilder startAttributes = Attributes.builder(); + extractor.onStart(startAttributes, Context.root(), null); + assertThat(startAttributes.build()).isEmpty(); + + AttributesBuilder endAttributes = Attributes.builder(); + extractor.onEnd(endAttributes, Context.root(), null, null, null); + assertThat(endAttributes.build()).isEmpty(); + } +} diff --git a/instrumentation-api-semconv/src/testStableHttpSemconv/java/io/opentelemetry/instrumentation/api/instrumenter/network/NetworkAttributesExtractorTest.java b/instrumentation-api-semconv/src/testStableHttpSemconv/java/io/opentelemetry/instrumentation/api/instrumenter/network/NetworkAttributesExtractorTest.java index 4bc8edbf2c64..144973cf4254 100644 --- a/instrumentation-api-semconv/src/testStableHttpSemconv/java/io/opentelemetry/instrumentation/api/instrumenter/network/NetworkAttributesExtractorTest.java +++ b/instrumentation-api-semconv/src/testStableHttpSemconv/java/io/opentelemetry/instrumentation/api/instrumenter/network/NetworkAttributesExtractorTest.java @@ -13,6 +13,7 @@ import io.opentelemetry.api.common.AttributesBuilder; import io.opentelemetry.context.Context; import io.opentelemetry.instrumentation.api.instrumenter.AttributesExtractor; +import io.opentelemetry.instrumentation.api.instrumenter.network.internal.NetworkAttributes; import io.opentelemetry.semconv.SemanticAttributes; import java.util.HashMap; import java.util.Map; @@ -47,6 +48,32 @@ public String getNetworkProtocolName(Map request, @Nullable Void public String getNetworkProtocolVersion(Map request, @Nullable Void response) { return request.get("protocolVersion"); } + + @Nullable + @Override + public String getNetworkLocalAddress(Map request, @Nullable Void response) { + return request.get("localAddress"); + } + + @Nullable + @Override + public Integer getNetworkLocalPort(Map request, @Nullable Void response) { + String value = request.get("localPort"); + return value == null ? null : Integer.parseInt(value); + } + + @Nullable + @Override + public String getNetworkPeerAddress(Map request, @Nullable Void response) { + return request.get("peerAddress"); + } + + @Nullable + @Override + public Integer getNetworkPeerPort(Map request, @Nullable Void response) { + String value = request.get("peerPort"); + return value == null ? null : Integer.parseInt(value); + } } @Test @@ -56,6 +83,10 @@ void allAttributes() { request.put("type", "IPv4"); request.put("protocolName", "Http"); request.put("protocolVersion", "1.1"); + request.put("localAddress", "1.2.3.4"); + request.put("localPort", "8080"); + request.put("peerAddress", "4.3.2.1"); + request.put("peerPort", "9090"); AttributesExtractor, Void> extractor = NetworkAttributesExtractor.create(new TestNetworkAttributesGetter()); @@ -71,7 +102,11 @@ void allAttributes() { entry(SemanticAttributes.NETWORK_TRANSPORT, "tcp"), entry(SemanticAttributes.NETWORK_TYPE, "ipv4"), entry(SemanticAttributes.NETWORK_PROTOCOL_NAME, "http"), - entry(SemanticAttributes.NETWORK_PROTOCOL_VERSION, "1.1")); + entry(SemanticAttributes.NETWORK_PROTOCOL_VERSION, "1.1"), + entry(NetworkAttributes.NETWORK_LOCAL_ADDRESS, "1.2.3.4"), + entry(NetworkAttributes.NETWORK_LOCAL_PORT, 8080L), + entry(NetworkAttributes.NETWORK_PEER_ADDRESS, "4.3.2.1"), + entry(NetworkAttributes.NETWORK_PEER_PORT, 9090L)); } @Test @@ -87,4 +122,27 @@ void noAttributes() { extractor.onEnd(endAttributes, Context.root(), emptyMap(), null, null); assertThat(endAttributes.build()).isEmpty(); } + + @Test + void doesNotSetNegativePortValues() { + Map request = new HashMap<>(); + request.put("localAddress", "1.2.3.4"); + request.put("localPort", "-12"); + request.put("peerAddress", "4.3.2.1"); + request.put("peerPort", "-42"); + + AttributesExtractor, Void> extractor = + NetworkAttributesExtractor.create(new TestNetworkAttributesGetter()); + + 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()) + .containsOnly( + entry(NetworkAttributes.NETWORK_LOCAL_ADDRESS, "1.2.3.4"), + entry(NetworkAttributes.NETWORK_PEER_ADDRESS, "4.3.2.1")); + } } diff --git a/instrumentation-api-semconv/src/testStableHttpSemconv/java/io/opentelemetry/instrumentation/api/instrumenter/network/ServerAttributesExtractorInetSocketAddressTest.java b/instrumentation-api-semconv/src/testStableHttpSemconv/java/io/opentelemetry/instrumentation/api/instrumenter/network/ServerAttributesExtractorInetSocketAddressTest.java deleted file mode 100644 index 4926fd4a23fb..000000000000 --- a/instrumentation-api-semconv/src/testStableHttpSemconv/java/io/opentelemetry/instrumentation/api/instrumenter/network/ServerAttributesExtractorInetSocketAddressTest.java +++ /dev/null @@ -1,82 +0,0 @@ -/* - * Copyright The OpenTelemetry Authors - * SPDX-License-Identifier: Apache-2.0 - */ - -package io.opentelemetry.instrumentation.api.instrumenter.network; - -import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.assertThat; -import static org.assertj.core.api.Assertions.entry; - -import io.opentelemetry.api.common.Attributes; -import io.opentelemetry.api.common.AttributesBuilder; -import io.opentelemetry.context.Context; -import io.opentelemetry.instrumentation.api.instrumenter.AttributesExtractor; -import io.opentelemetry.semconv.SemanticAttributes; -import java.net.InetSocketAddress; -import javax.annotation.Nullable; -import org.junit.jupiter.api.Test; - -class ServerAttributesExtractorInetSocketAddressTest { - - static class TestServerAttributesGetter - implements ServerAttributesGetter { - - @Nullable - @Override - public String getServerAddress(InetSocketAddress request) { - // covered in ServerAttributesExtractorTest - return null; - } - - @Nullable - @Override - public Integer getServerPort(InetSocketAddress request) { - // covered in ServerAttributesExtractorTest - return null; - } - - @Nullable - @Override - public InetSocketAddress getServerInetSocketAddress( - InetSocketAddress request, @Nullable Void response) { - return request; - } - } - - @Test - @SuppressWarnings("AddressSelection") - void fullAddress() { - InetSocketAddress address = new InetSocketAddress("api.github.com", 456); - assertThat(address.getAddress().getHostAddress()).isNotNull(); - - AttributesExtractor extractor = - ServerAttributesExtractor.create(new TestServerAttributesGetter()); - - AttributesBuilder startAttributes = Attributes.builder(); - extractor.onStart(startAttributes, Context.root(), address); - assertThat(startAttributes.build()).isEmpty(); - - AttributesBuilder endAttributes = Attributes.builder(); - extractor.onEnd(endAttributes, Context.root(), address, null, null); - assertThat(endAttributes.build()) - .containsOnly( - entry(SemanticAttributes.SERVER_SOCKET_DOMAIN, "api.github.com"), - entry(SemanticAttributes.SERVER_SOCKET_ADDRESS, address.getAddress().getHostAddress()), - entry(SemanticAttributes.SERVER_SOCKET_PORT, 456L)); - } - - @Test - void noAttributes() { - AttributesExtractor extractor = - ServerAttributesExtractor.create(new TestServerAttributesGetter()); - - AttributesBuilder startAttributes = Attributes.builder(); - extractor.onStart(startAttributes, Context.root(), null); - assertThat(startAttributes.build()).isEmpty(); - - AttributesBuilder endAttributes = Attributes.builder(); - extractor.onEnd(endAttributes, Context.root(), null, 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 96d43e3b91d8..a7202943e5c1 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 @@ -36,25 +36,6 @@ public Integer getServerPort(Map request) { String port = request.get("port"); return port == null ? null : Integer.parseInt(port); } - - @Nullable - @Override - public String getServerSocketDomain(Map request, @Nullable Void response) { - return request.get("socketDomain"); - } - - @Nullable - @Override - public String getServerSocketAddress(Map request, @Nullable Void response) { - return request.get("socketAddress"); - } - - @Nullable - @Override - public Integer getServerSocketPort(Map request, @Nullable Void response) { - String port = request.get("socketPort"); - return port == null ? null : Integer.parseInt(port); - } } @Test @@ -62,9 +43,6 @@ void allAttributes() { Map request = new HashMap<>(); request.put("address", "opentelemetry.io"); request.put("port", "80"); - request.put("socketDomain", "proxy.opentelemetry.io"); - request.put("socketAddress", "1.2.3.4"); - request.put("socketPort", "8080"); AttributesExtractor, Void> extractor = ServerAttributesExtractor.create(new TestServerAttributesGetter()); @@ -78,11 +56,7 @@ void allAttributes() { AttributesBuilder endAttributes = Attributes.builder(); extractor.onEnd(endAttributes, Context.root(), request, null, null); - assertThat(endAttributes.build()) - .containsOnly( - entry(SemanticAttributes.SERVER_SOCKET_DOMAIN, "proxy.opentelemetry.io"), - entry(SemanticAttributes.SERVER_SOCKET_ADDRESS, "1.2.3.4"), - entry(SemanticAttributes.SERVER_SOCKET_PORT, 8080L)); + assertThat(endAttributes.build()).isEmpty(); } @Test @@ -100,12 +74,10 @@ void noAttributes() { } @Test - void doesNotSetNegativePortValues() { + void doesNotSetNegativePortValue() { Map request = new HashMap<>(); request.put("address", "opentelemetry.io"); request.put("port", "-12"); - request.put("socketAddress", "1.2.3.4"); - request.put("socketPort", "-42"); AttributesExtractor, Void> extractor = ServerAttributesExtractor.create(new TestServerAttributesGetter()); @@ -114,34 +86,5 @@ void doesNotSetNegativePortValues() { extractor.onStart(startAttributes, Context.root(), request); assertThat(startAttributes.build()) .containsOnly(entry(SemanticAttributes.SERVER_ADDRESS, "opentelemetry.io")); - - AttributesBuilder endAttributes = Attributes.builder(); - extractor.onEnd(endAttributes, Context.root(), request, null, null); - assertThat(endAttributes.build()) - .containsOnly(entry(SemanticAttributes.SERVER_SOCKET_ADDRESS, "1.2.3.4")); - } - - @Test - void doesNotSetDuplicates() { - Map request = new HashMap<>(); - request.put("address", "1.2.3.4"); - request.put("port", "80"); - request.put("socketDomain", "1.2.3.4"); - request.put("socketAddress", "1.2.3.4"); - request.put("socketPort", "80"); - - AttributesExtractor, Void> extractor = - ServerAttributesExtractor.create(new TestServerAttributesGetter()); - - AttributesBuilder startAttributes = Attributes.builder(); - extractor.onStart(startAttributes, Context.root(), request); - assertThat(startAttributes.build()) - .containsOnly( - entry(SemanticAttributes.SERVER_ADDRESS, "1.2.3.4"), - entry(SemanticAttributes.SERVER_PORT, 80L)); - - AttributesBuilder endAttributes = Attributes.builder(); - extractor.onEnd(endAttributes, Context.root(), request, null, null); - assertThat(endAttributes.build()).isEmpty(); } } diff --git a/instrumentation/netty/netty-4.0/javaagent/src/test/groovy/Netty40ClientSslTest.groovy b/instrumentation/netty/netty-4.0/javaagent/src/test/groovy/Netty40ClientSslTest.groovy index de637ac052a9..2c1140a583a1 100644 --- a/instrumentation/netty/netty-4.0/javaagent/src/test/groovy/Netty40ClientSslTest.groovy +++ b/instrumentation/netty/netty-4.0/javaagent/src/test/groovy/Netty40ClientSslTest.groovy @@ -18,7 +18,7 @@ import io.netty.handler.codec.http.HttpHeaders import io.netty.handler.codec.http.HttpMethod import io.netty.handler.codec.http.HttpVersion import io.netty.handler.ssl.SslHandler - +import io.opentelemetry.instrumentation.api.instrumenter.network.internal.NetworkAttributes import io.opentelemetry.instrumentation.api.internal.SemconvStability import io.opentelemetry.instrumentation.test.AgentInstrumentationSpecification import io.opentelemetry.instrumentation.testing.junit.http.HttpClientTestServer @@ -102,9 +102,8 @@ class Netty40ClientSslTest extends AgentInstrumentationSpecification { attributes { "$SemanticAttributes.NETWORK_TRANSPORT" "tcp" "$SemanticAttributes.NETWORK_TYPE" "ipv4" - "$SemanticAttributes.SERVER_ADDRESS" uri.host - "$SemanticAttributes.SERVER_PORT" uri.port - "$SemanticAttributes.SERVER_SOCKET_ADDRESS" "127.0.0.1" + "$NetworkAttributes.NETWORK_PEER_PORT" uri.port + "$NetworkAttributes.NETWORK_PEER_ADDRESS" "127.0.0.1" } } } @@ -127,9 +126,8 @@ class Netty40ClientSslTest extends AgentInstrumentationSpecification { attributes { "$SemanticAttributes.NETWORK_TRANSPORT" "tcp" "$SemanticAttributes.NETWORK_TYPE" "ipv4" - "$SemanticAttributes.SERVER_SOCKET_DOMAIN" uri.host - "$SemanticAttributes.SERVER_SOCKET_PORT" uri.port - "$SemanticAttributes.SERVER_SOCKET_ADDRESS" "127.0.0.1" + "$NetworkAttributes.NETWORK_PEER_PORT" uri.port + "$NetworkAttributes.NETWORK_PEER_ADDRESS" "127.0.0.1" } } } @@ -180,9 +178,8 @@ class Netty40ClientSslTest extends AgentInstrumentationSpecification { attributes { "$SemanticAttributes.NETWORK_TRANSPORT" "tcp" "$SemanticAttributes.NETWORK_TYPE" "ipv4" - "$SemanticAttributes.SERVER_ADDRESS" uri.host - "$SemanticAttributes.SERVER_PORT" uri.port - "$SemanticAttributes.SERVER_SOCKET_ADDRESS" "127.0.0.1" + "$NetworkAttributes.NETWORK_PEER_PORT" uri.port + "$NetworkAttributes.NETWORK_PEER_ADDRESS" "127.0.0.1" } } } @@ -202,9 +199,8 @@ class Netty40ClientSslTest extends AgentInstrumentationSpecification { attributes { "$SemanticAttributes.NETWORK_TRANSPORT" "tcp" "$SemanticAttributes.NETWORK_TYPE" "ipv4" - "$SemanticAttributes.SERVER_SOCKET_DOMAIN" uri.host - "$SemanticAttributes.SERVER_SOCKET_PORT" uri.port - "$SemanticAttributes.SERVER_SOCKET_ADDRESS" "127.0.0.1" + "$NetworkAttributes.NETWORK_PEER_PORT" uri.port + "$NetworkAttributes.NETWORK_PEER_ADDRESS" "127.0.0.1" } } } diff --git a/instrumentation/netty/netty-4.0/javaagent/src/test/groovy/Netty40ConnectionSpanTest.groovy b/instrumentation/netty/netty-4.0/javaagent/src/test/groovy/Netty40ConnectionSpanTest.groovy index b2bba7eb52b2..ddc938882eec 100644 --- a/instrumentation/netty/netty-4.0/javaagent/src/test/groovy/Netty40ConnectionSpanTest.groovy +++ b/instrumentation/netty/netty-4.0/javaagent/src/test/groovy/Netty40ConnectionSpanTest.groovy @@ -16,7 +16,7 @@ import io.netty.handler.codec.http.HttpClientCodec import io.netty.handler.codec.http.HttpHeaders import io.netty.handler.codec.http.HttpMethod import io.netty.handler.codec.http.HttpVersion - +import io.opentelemetry.instrumentation.api.instrumenter.network.internal.NetworkAttributes import io.opentelemetry.instrumentation.api.internal.SemconvStability import io.opentelemetry.instrumentation.test.AgentTestTrait import io.opentelemetry.instrumentation.test.InstrumentationSpecification @@ -118,9 +118,8 @@ class Netty40ConnectionSpanTest extends InstrumentationSpecification implements attributes { "$SemanticAttributes.NETWORK_TRANSPORT" "tcp" "$SemanticAttributes.NETWORK_TYPE" "ipv4" - "$SemanticAttributes.SERVER_ADDRESS" uri.host - "$SemanticAttributes.SERVER_PORT" uri.port - "$SemanticAttributes.SERVER_SOCKET_ADDRESS" "127.0.0.1" + "$NetworkAttributes.NETWORK_PEER_PORT" uri.port + "$NetworkAttributes.NETWORK_PEER_ADDRESS" "127.0.0.1" } } } @@ -177,9 +176,8 @@ class Netty40ConnectionSpanTest extends InstrumentationSpecification implements attributes { "$SemanticAttributes.NETWORK_TRANSPORT" "tcp" "$SemanticAttributes.NETWORK_TYPE" { it == "ipv4" || it == null } - "$SemanticAttributes.SERVER_ADDRESS" uri.host - "$SemanticAttributes.SERVER_PORT" uri.port - "$SemanticAttributes.SERVER_SOCKET_ADDRESS" { it == "127.0.0.1" || it == null } + "$NetworkAttributes.NETWORK_PEER_ADDRESS" { it == "127.0.0.1" || it == null } + "$NetworkAttributes.NETWORK_PEER_PORT" { it == uri.port || it == null } } } } diff --git a/instrumentation/netty/netty-4.1/javaagent/src/test/groovy/Netty41ClientSslTest.groovy b/instrumentation/netty/netty-4.1/javaagent/src/test/groovy/Netty41ClientSslTest.groovy index 84860ba077a7..d548fe9c82a9 100644 --- a/instrumentation/netty/netty-4.1/javaagent/src/test/groovy/Netty41ClientSslTest.groovy +++ b/instrumentation/netty/netty-4.1/javaagent/src/test/groovy/Netty41ClientSslTest.groovy @@ -20,7 +20,7 @@ import io.netty.handler.codec.http.HttpVersion import io.netty.handler.ssl.SslContext import io.netty.handler.ssl.SslContextBuilder import io.netty.handler.ssl.SslHandler - +import io.opentelemetry.instrumentation.api.instrumenter.network.internal.NetworkAttributes import io.opentelemetry.instrumentation.api.internal.SemconvStability import io.opentelemetry.instrumentation.netty.v4_1.ClientHandler import io.opentelemetry.instrumentation.test.AgentInstrumentationSpecification @@ -125,9 +125,8 @@ class Netty41ClientSslTest extends AgentInstrumentationSpecification { attributes { "$SemanticAttributes.NETWORK_TRANSPORT" "tcp" "$SemanticAttributes.NETWORK_TYPE" "ipv4" - "$SemanticAttributes.SERVER_ADDRESS" uri.host - "$SemanticAttributes.SERVER_PORT" uri.port - "$SemanticAttributes.SERVER_SOCKET_ADDRESS" "127.0.0.1" + "$NetworkAttributes.NETWORK_PEER_PORT" uri.port + "$NetworkAttributes.NETWORK_PEER_ADDRESS" "127.0.0.1" } } } @@ -150,9 +149,8 @@ class Netty41ClientSslTest extends AgentInstrumentationSpecification { attributes { "$SemanticAttributes.NETWORK_TRANSPORT" "tcp" "$SemanticAttributes.NETWORK_TYPE" "ipv4" - "$SemanticAttributes.SERVER_SOCKET_DOMAIN" uri.host - "$SemanticAttributes.SERVER_SOCKET_PORT" uri.port - "$SemanticAttributes.SERVER_SOCKET_ADDRESS" "127.0.0.1" + "$NetworkAttributes.NETWORK_PEER_PORT" uri.port + "$NetworkAttributes.NETWORK_PEER_ADDRESS" "127.0.0.1" } } } @@ -223,9 +221,8 @@ class Netty41ClientSslTest extends AgentInstrumentationSpecification { attributes { "$SemanticAttributes.NETWORK_TRANSPORT" "tcp" "$SemanticAttributes.NETWORK_TYPE" "ipv4" - "$SemanticAttributes.SERVER_ADDRESS" uri.host - "$SemanticAttributes.SERVER_PORT" uri.port - "$SemanticAttributes.SERVER_SOCKET_ADDRESS" "127.0.0.1" + "$NetworkAttributes.NETWORK_PEER_PORT" uri.port + "$NetworkAttributes.NETWORK_PEER_ADDRESS" "127.0.0.1" } } } @@ -245,9 +242,8 @@ class Netty41ClientSslTest extends AgentInstrumentationSpecification { attributes { "$SemanticAttributes.NETWORK_TRANSPORT" "tcp" "$SemanticAttributes.NETWORK_TYPE" "ipv4" - "$SemanticAttributes.SERVER_SOCKET_DOMAIN" uri.host - "$SemanticAttributes.SERVER_SOCKET_PORT" uri.port - "$SemanticAttributes.SERVER_SOCKET_ADDRESS" "127.0.0.1" + "$NetworkAttributes.NETWORK_PEER_PORT" uri.port + "$NetworkAttributes.NETWORK_PEER_ADDRESS" "127.0.0.1" } } } diff --git a/instrumentation/netty/netty-4.1/javaagent/src/test/groovy/Netty41ConnectionSpanTest.groovy b/instrumentation/netty/netty-4.1/javaagent/src/test/groovy/Netty41ConnectionSpanTest.groovy index 016ef996204b..6849bcb3d430 100644 --- a/instrumentation/netty/netty-4.1/javaagent/src/test/groovy/Netty41ConnectionSpanTest.groovy +++ b/instrumentation/netty/netty-4.1/javaagent/src/test/groovy/Netty41ConnectionSpanTest.groovy @@ -16,7 +16,7 @@ import io.netty.handler.codec.http.HttpClientCodec import io.netty.handler.codec.http.HttpHeaderNames import io.netty.handler.codec.http.HttpMethod import io.netty.handler.codec.http.HttpVersion - +import io.opentelemetry.instrumentation.api.instrumenter.network.internal.NetworkAttributes import io.opentelemetry.instrumentation.api.internal.SemconvStability import io.opentelemetry.instrumentation.netty.v4_1.ClientHandler import io.opentelemetry.instrumentation.test.AgentTestTrait @@ -139,9 +139,8 @@ class Netty41ConnectionSpanTest extends InstrumentationSpecification implements attributes { "$SemanticAttributes.NETWORK_TRANSPORT" "tcp" "$SemanticAttributes.NETWORK_TYPE" "ipv4" - "$SemanticAttributes.SERVER_ADDRESS" uri.host - "$SemanticAttributes.SERVER_PORT" uri.port - "$SemanticAttributes.SERVER_SOCKET_ADDRESS" "127.0.0.1" + "$NetworkAttributes.NETWORK_PEER_PORT" uri.port + "$NetworkAttributes.NETWORK_PEER_ADDRESS" "127.0.0.1" } } } @@ -218,9 +217,8 @@ class Netty41ConnectionSpanTest extends InstrumentationSpecification implements attributes { "$SemanticAttributes.NETWORK_TRANSPORT" "tcp" "$SemanticAttributes.NETWORK_TYPE" { it == "ipv4" || it == null } - "$SemanticAttributes.SERVER_ADDRESS" uri.host - "$SemanticAttributes.SERVER_PORT" uri.port - "$SemanticAttributes.SERVER_SOCKET_ADDRESS" { it == "127.0.0.1" || it == null } + "$NetworkAttributes.NETWORK_PEER_ADDRESS" { it == "127.0.0.1" || it == null } + "$NetworkAttributes.NETWORK_PEER_PORT" { it == uri.port || it == null } } } } diff --git a/instrumentation/undertow-1.4/javaagent/src/test/groovy/UndertowServerTest.groovy b/instrumentation/undertow-1.4/javaagent/src/test/groovy/UndertowServerTest.groovy index e6050d7c9c2b..d9c809cf1352 100644 --- a/instrumentation/undertow-1.4/javaagent/src/test/groovy/UndertowServerTest.groovy +++ b/instrumentation/undertow-1.4/javaagent/src/test/groovy/UndertowServerTest.groovy @@ -7,7 +7,7 @@ import io.opentelemetry.api.common.AttributeKey import io.opentelemetry.api.trace.Span import io.opentelemetry.api.trace.SpanKind import io.opentelemetry.api.trace.StatusCode - +import io.opentelemetry.instrumentation.api.instrumenter.network.internal.NetworkAttributes import io.opentelemetry.instrumentation.api.internal.SemconvStability import io.opentelemetry.instrumentation.test.AgentTestTrait import io.opentelemetry.instrumentation.test.base.HttpServerTest @@ -176,8 +176,8 @@ class UndertowServerTest extends HttpServerTest implements AgentTestTr "$SemanticAttributes.NETWORK_TYPE" "ipv4" "$SemanticAttributes.SERVER_ADDRESS" uri.host "$SemanticAttributes.SERVER_PORT" uri.port - "$SemanticAttributes.CLIENT_SOCKET_ADDRESS" "127.0.0.1" - "$SemanticAttributes.CLIENT_SOCKET_PORT" Long + "$NetworkAttributes.NETWORK_PEER_ADDRESS" "127.0.0.1" + "$NetworkAttributes.NETWORK_PEER_PORT" Long } } } @@ -248,8 +248,8 @@ class UndertowServerTest extends HttpServerTest implements AgentTestTr "$SemanticAttributes.NETWORK_TYPE" "ipv4" "$SemanticAttributes.SERVER_ADDRESS" uri.host "$SemanticAttributes.SERVER_PORT" uri.port - "$SemanticAttributes.CLIENT_SOCKET_ADDRESS" "127.0.0.1" - "$SemanticAttributes.CLIENT_SOCKET_PORT" Long + "$NetworkAttributes.NETWORK_PEER_ADDRESS" "127.0.0.1" + "$NetworkAttributes.NETWORK_PEER_PORT" Long } } } diff --git a/testing-common/src/main/java/io/opentelemetry/instrumentation/testing/junit/http/SemconvStabilityUtil.java b/testing-common/src/main/java/io/opentelemetry/instrumentation/testing/junit/http/SemconvStabilityUtil.java index 57eb82ad514c..0e7cf3ab5164 100644 --- a/testing-common/src/main/java/io/opentelemetry/instrumentation/testing/junit/http/SemconvStabilityUtil.java +++ b/testing-common/src/main/java/io/opentelemetry/instrumentation/testing/junit/http/SemconvStabilityUtil.java @@ -6,6 +6,7 @@ package io.opentelemetry.instrumentation.testing.junit.http; import io.opentelemetry.api.common.AttributeKey; +import io.opentelemetry.instrumentation.api.instrumenter.network.internal.NetworkAttributes; import io.opentelemetry.instrumentation.api.internal.SemconvStability; import io.opentelemetry.semconv.SemanticAttributes; import java.util.HashMap; @@ -27,11 +28,8 @@ public class SemconvStabilityUtil { addKey(oldToNewMap, SemanticAttributes.NET_PEER_NAME, SemanticAttributes.SERVER_ADDRESS); addKey(oldToNewMap, SemanticAttributes.NET_PEER_PORT, SemanticAttributes.SERVER_PORT); addKey( - oldToNewMap, - SemanticAttributes.NET_SOCK_PEER_ADDR, - SemanticAttributes.CLIENT_SOCKET_ADDRESS); - addKey( - oldToNewMap, SemanticAttributes.NET_SOCK_PEER_PORT, SemanticAttributes.CLIENT_SOCKET_PORT); + oldToNewMap, SemanticAttributes.NET_SOCK_PEER_ADDR, NetworkAttributes.NETWORK_PEER_ADDRESS); + addKey(oldToNewMap, SemanticAttributes.NET_SOCK_PEER_PORT, NetworkAttributes.NETWORK_PEER_PORT); addKey(oldToNewMap, SemanticAttributes.HTTP_URL, SemanticAttributes.URL_FULL); addKey(oldToNewMap, SemanticAttributes.HTTP_METHOD, SemanticAttributes.HTTP_REQUEST_METHOD); addKey( @@ -50,10 +48,6 @@ public class SemconvStabilityUtil { addKey(oldToNewMap, SemanticAttributes.NET_HOST_PORT, SemanticAttributes.SERVER_PORT); addKey(oldToNewMap, SemanticAttributes.HTTP_CLIENT_IP, SemanticAttributes.CLIENT_ADDRESS); addKey(oldToNewMap, SemanticAttributes.HTTP_SCHEME, SemanticAttributes.URL_SCHEME); - addKey( - oldToNewMap, - SemanticAttributes.NET_SOCK_HOST_ADDR, - SemanticAttributes.SERVER_SOCKET_ADDRESS); } private SemconvStabilityUtil() {}