From 3abaf5ed92a9cb1a72531e665c6e5e20c0ce7aad Mon Sep 17 00:00:00 2001 From: Mateusz Rzeszutek Date: Wed, 24 Aug 2022 15:06:38 +0200 Subject: [PATCH] Net attributes getters changes: instrumentation-api-semconv changes --- ...ocketAddressNetClientAttributesGetter.java | 42 ++-- ...ocketAddressNetServerAttributesGetter.java | 62 ++++- .../api/instrumenter/net/NetAttributes.java | 28 +++ .../net/NetClientAttributesExtractor.java | 21 +- .../net/NetClientAttributesGetter.java | 18 +- .../net/NetServerAttributesExtractor.java | 52 ++++- .../net/NetServerAttributesGetter.java | 20 +- ...tAddressNetClientAttributesGetterTest.java | 60 ++--- ...tAddressNetServerAttributesGetterTest.java | 111 ++++++--- .../net/NetClientAttributesExtractorTest.java | 201 +++++++++++----- .../net/NetServerAttributesExtractorTest.java | 220 ++++++++++++++---- 11 files changed, 598 insertions(+), 237 deletions(-) create mode 100644 instrumentation-api-semconv/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/net/NetAttributes.java diff --git a/instrumentation-api-semconv/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/net/InetSocketAddressNetClientAttributesGetter.java b/instrumentation-api-semconv/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/net/InetSocketAddressNetClientAttributesGetter.java index 1e4b3f44588e..ad49a6b9f230 100644 --- a/instrumentation-api-semconv/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/net/InetSocketAddressNetClientAttributesGetter.java +++ b/instrumentation-api-semconv/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/net/InetSocketAddressNetClientAttributesGetter.java @@ -21,32 +21,26 @@ public abstract class InetSocketAddressNetClientAttributesGetter { @Nullable - public abstract InetSocketAddress getAddress(REQUEST request, @Nullable RESPONSE response); + public abstract InetSocketAddress getPeerAddress(REQUEST request, @Nullable RESPONSE response); - @Override @Nullable - public final String peerName(REQUEST request, @Nullable RESPONSE response) { - InetSocketAddress address = getAddress(request, response); - if (address == null) { - return null; - } - return address.getHostString(); - } - @Override - @Nullable - public final Integer peerPort(REQUEST request, @Nullable RESPONSE response) { - InetSocketAddress address = getAddress(request, response); + public String sockFamily(REQUEST request, @Nullable RESPONSE response) { + InetSocketAddress address = getPeerAddress(request, response); if (address == null) { return null; } - return address.getPort(); + InetAddress remoteAddress = address.getAddress(); + if (remoteAddress instanceof Inet6Address) { + return "inet6"; + } + return null; } @Override @Nullable public final String sockPeerAddr(REQUEST request, @Nullable RESPONSE response) { - InetSocketAddress address = getAddress(request, response); + InetSocketAddress address = getPeerAddress(request, response); if (address == null) { return null; } @@ -57,27 +51,23 @@ public final String sockPeerAddr(REQUEST request, @Nullable RESPONSE response) { return null; } - @Nullable @Override - public Integer sockPeerPort(REQUEST request, @Nullable RESPONSE response) { - InetSocketAddress address = getAddress(request, response); + @Nullable + public String sockPeerName(REQUEST request, @Nullable RESPONSE response) { + InetSocketAddress address = getPeerAddress(request, response); if (address == null) { return null; } - return address.getPort(); + return address.getHostString(); } @Nullable @Override - public String sockFamily(REQUEST request, @Nullable RESPONSE response) { - InetSocketAddress address = getAddress(request, response); + public Integer sockPeerPort(REQUEST request, @Nullable RESPONSE response) { + InetSocketAddress address = getPeerAddress(request, response); if (address == null) { return null; } - InetAddress remoteAddress = address.getAddress(); - if (remoteAddress instanceof Inet6Address) { - return "inet6"; - } - return null; + return address.getPort(); } } diff --git a/instrumentation-api-semconv/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/net/InetSocketAddressNetServerAttributesGetter.java b/instrumentation-api-semconv/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/net/InetSocketAddressNetServerAttributesGetter.java index bc42a0b21540..0420a06e41f7 100644 --- a/instrumentation-api-semconv/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/net/InetSocketAddressNetServerAttributesGetter.java +++ b/instrumentation-api-semconv/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/net/InetSocketAddressNetServerAttributesGetter.java @@ -5,6 +5,7 @@ package io.opentelemetry.instrumentation.api.instrumenter.net; +import java.net.Inet6Address; import java.net.InetAddress; import java.net.InetSocketAddress; import javax.annotation.Nullable; @@ -20,22 +21,65 @@ public abstract class InetSocketAddressNetServerAttributesGetter implements NetServerAttributesGetter { @Nullable - public abstract InetSocketAddress getAddress(REQUEST request); + public abstract InetSocketAddress getPeerAddress(REQUEST request); + + // optional + @Nullable + public abstract InetSocketAddress getHostAddress(REQUEST request); + + @Nullable + @Override + public String sockFamily(REQUEST request) { + InetSocketAddress address = getPeerAddress(request); + if (address == null) { + address = getHostAddress(request); + } + if (address == null) { + return null; + } + InetAddress inetAddress = address.getAddress(); + if (inetAddress instanceof Inet6Address) { + return "inet6"; + } + return null; + } + + @Override + @Nullable + public final String sockPeerAddr(REQUEST request) { + return getAddress(getPeerAddress(request)); + } @Override @Nullable public final Integer sockPeerPort(REQUEST request) { - InetSocketAddress address = getAddress(request); + return getPort(getPeerAddress(request)); + } + + @Nullable + @Override + public String sockHostAddr(REQUEST request) { + return getAddress(getHostAddress(request)); + } + + @Nullable + @Override + public String sockHostName(REQUEST request) { + InetSocketAddress address = getHostAddress(request); if (address == null) { return null; } - return address.getPort(); + return address.getHostString(); } + @Nullable @Override + public Integer sockHostPort(REQUEST request) { + return getPort(getHostAddress(request)); + } + @Nullable - public final String sockPeerAddr(REQUEST request) { - InetSocketAddress address = getAddress(request); + private static String getAddress(InetSocketAddress address) { if (address == null) { return null; } @@ -45,4 +89,12 @@ public final String sockPeerAddr(REQUEST request) { } return null; } + + @Nullable + private static Integer getPort(InetSocketAddress address) { + if (address == null) { + return null; + } + return address.getPort(); + } } diff --git a/instrumentation-api-semconv/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/net/NetAttributes.java b/instrumentation-api-semconv/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/net/NetAttributes.java new file mode 100644 index 000000000000..6f8c449cacff --- /dev/null +++ b/instrumentation-api-semconv/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/net/NetAttributes.java @@ -0,0 +1,28 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +package io.opentelemetry.instrumentation.api.instrumenter.net; + +import io.opentelemetry.api.common.AttributeKey; + +// this class will be removed once SemanticAttributes contains all new net.* attributes +final class NetAttributes { + + static final AttributeKey NET_SOCK_FAMILY = AttributeKey.stringKey("net.sock.family"); + static final AttributeKey NET_SOCK_PEER_ADDR = + AttributeKey.stringKey("net.sock.peer.addr"); + static final AttributeKey NET_SOCK_PEER_NAME = + AttributeKey.stringKey("net.sock.peer.name"); + static final AttributeKey NET_SOCK_PEER_PORT = AttributeKey.longKey("net.sock.peer.port"); + static final AttributeKey NET_SOCK_HOST_ADDR = + AttributeKey.stringKey("net.sock.host.addr"); + static final AttributeKey NET_SOCK_HOST_NAME = + AttributeKey.stringKey("net.sock.host.name"); + static final AttributeKey NET_SOCK_HOST_PORT = AttributeKey.longKey("net.sock.host.port"); + + static final String SOCK_FAMILY_INET = "inet"; + + private NetAttributes() {} +} 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 520ae5c02205..9d4f6a0dc797 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 @@ -7,7 +7,6 @@ import static io.opentelemetry.instrumentation.api.internal.AttributesExtractorUtil.internalSet; -import io.opentelemetry.api.common.AttributeKey; import io.opentelemetry.api.common.AttributesBuilder; import io.opentelemetry.context.Context; import io.opentelemetry.instrumentation.api.instrumenter.AttributesExtractor; @@ -26,15 +25,6 @@ public final class NetClientAttributesExtractor implements AttributesExtractor { - private static final AttributeKey NET_SOCK_PEER_ADDR = - AttributeKey.stringKey("net.sock.peer.addr"); - public static final AttributeKey NET_SOCK_PEER_PORT = - AttributeKey.longKey("net.sock.peer.port"); - public static final AttributeKey NET_SOCK_FAMILY = - AttributeKey.stringKey("net.sock.family"); - public static final AttributeKey NET_SOCK_PEER_NAME = - AttributeKey.stringKey("net.sock.peer.name"); - private final NetClientAttributesGetter getter; public static NetClientAttributesExtractor create( @@ -70,18 +60,21 @@ public void onEnd( String sockPeerAddr = getter.sockPeerAddr(request, response); if (sockPeerAddr != null && !sockPeerAddr.equals(peerName)) { - internalSet(attributes, NET_SOCK_PEER_ADDR, sockPeerAddr); + internalSet(attributes, NetAttributes.NET_SOCK_PEER_ADDR, sockPeerAddr); Integer sockPeerPort = getter.sockPeerPort(request, response); if (sockPeerPort != null && sockPeerPort > 0 && !sockPeerPort.equals(peerPort)) { - internalSet(attributes, NET_SOCK_PEER_PORT, (long) sockPeerPort); + internalSet(attributes, NetAttributes.NET_SOCK_PEER_PORT, (long) sockPeerPort); } - internalSet(attributes, NET_SOCK_FAMILY, getter.sockFamily(request, response)); + String sockFamily = getter.sockFamily(request, response); + if (sockFamily != null && !NetAttributes.SOCK_FAMILY_INET.equals(sockFamily)) { + internalSet(attributes, NetAttributes.NET_SOCK_FAMILY, sockFamily); + } String sockPeerName = getter.sockPeerName(request, response); if (sockPeerName != null && !sockPeerName.equals(peerName)) { - internalSet(attributes, NET_SOCK_PEER_NAME, sockPeerName); + internalSet(attributes, NetAttributes.NET_SOCK_PEER_NAME, sockPeerName); } } } 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 9adc30a80f0d..ab05315f2deb 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 @@ -20,6 +20,8 @@ public interface NetClientAttributesGetter { @Nullable String transport(REQUEST request, @Nullable RESPONSE response); + // TODO: peerName and peerPort should be extracted onStart + @Nullable String peerName(REQUEST request, @Nullable RESPONSE response); @@ -27,22 +29,14 @@ public interface NetClientAttributesGetter { Integer peerPort(REQUEST request, @Nullable RESPONSE response); @Nullable - default String sockPeerAddr(REQUEST request, @Nullable RESPONSE response) { - return null; - } + String sockFamily(REQUEST request, @Nullable RESPONSE response); @Nullable - default Integer sockPeerPort(REQUEST request, @Nullable RESPONSE response) { - return null; - } + String sockPeerAddr(REQUEST request, @Nullable RESPONSE response); @Nullable - default String sockFamily(REQUEST request, @Nullable RESPONSE response) { - return null; - } + String sockPeerName(REQUEST request, @Nullable RESPONSE response); @Nullable - default String sockPeerName(REQUEST request, @Nullable RESPONSE response) { - return null; - } + Integer sockPeerPort(REQUEST request, @Nullable RESPONSE 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 6d6f2312d2f0..c0c06d48a5eb 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 @@ -7,7 +7,6 @@ import static io.opentelemetry.instrumentation.api.internal.AttributesExtractorUtil.internalSet; -import io.opentelemetry.api.common.AttributeKey; import io.opentelemetry.api.common.AttributesBuilder; import io.opentelemetry.context.Context; import io.opentelemetry.instrumentation.api.instrumenter.AttributesExtractor; @@ -23,10 +22,6 @@ public final class NetServerAttributesExtractor implements AttributesExtractor { - public static final AttributeKey NET_SOCK_PEER_ADDR = - AttributeKey.stringKey("net.sock.peer.addr"); - public static final AttributeKey NET_SOCK_PEER_PORT = - AttributeKey.longKey("net.sock.peer.port"); private final NetServerAttributesGetter getter; public static NetServerAttributesExtractor create( @@ -42,13 +37,52 @@ private NetServerAttributesExtractor(NetServerAttributesGetter getter) public void onStart(AttributesBuilder attributes, Context parentContext, REQUEST request) { internalSet(attributes, SemanticAttributes.NET_TRANSPORT, getter.transport(request)); + boolean setSockFamily = false; + String sockPeerAddr = getter.sockPeerAddr(request); + if (sockPeerAddr != null) { + setSockFamily = true; + + internalSet(attributes, NetAttributes.NET_SOCK_PEER_ADDR, sockPeerAddr); + + Integer sockPeerPort = getter.sockPeerPort(request); + if (sockPeerPort != null && sockPeerPort > 0) { + internalSet(attributes, NetAttributes.NET_SOCK_PEER_PORT, (long) sockPeerPort); + } + } + + String hostName = getter.hostName(request); + Integer hostPort = getter.hostPort(request); + if (hostName != null) { + internalSet(attributes, SemanticAttributes.NET_HOST_NAME, hostName); - internalSet(attributes, NET_SOCK_PEER_ADDR, sockPeerAddr); + if (hostPort != null && hostPort > 0) { + internalSet(attributes, SemanticAttributes.NET_HOST_PORT, (long) hostPort); + } + } + + String sockHostAddr = getter.sockHostAddr(request); + if (sockHostAddr != null && !sockHostAddr.equals(hostName)) { + setSockFamily = true; + + internalSet(attributes, NetAttributes.NET_SOCK_HOST_ADDR, sockHostAddr); + + Integer sockHostPort = getter.sockHostPort(request); + if (sockHostPort != null && sockHostPort > 0 && !sockHostPort.equals(hostPort)) { + internalSet(attributes, NetAttributes.NET_SOCK_HOST_PORT, (long) sockHostPort); + } + + String sockHostName = getter.sockHostName(request); + if (sockHostName != null && !sockHostName.equals(hostName)) { + internalSet(attributes, NetAttributes.NET_SOCK_HOST_NAME, sockHostName); + } + } - Integer sockPeerPort = getter.sockPeerPort(request); - if (sockPeerPort != null && sockPeerPort > 0) { - internalSet(attributes, NET_SOCK_PEER_PORT, (long) sockPeerPort); + if (setSockFamily) { + String sockFamily = getter.sockFamily(request); + if (sockFamily != null && !NetAttributes.SOCK_FAMILY_INET.equals(sockFamily)) { + internalSet(attributes, NetAttributes.NET_SOCK_FAMILY, sockFamily); + } } } 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 24ddd10feda6..eb41083702cc 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 @@ -21,8 +21,26 @@ public interface NetServerAttributesGetter { String transport(REQUEST request); @Nullable - Integer sockPeerPort(REQUEST request); + String hostName(REQUEST request); + + @Nullable + Integer hostPort(REQUEST request); + + @Nullable + String sockFamily(REQUEST request); @Nullable String sockPeerAddr(REQUEST request); + + @Nullable + Integer sockPeerPort(REQUEST request); + + @Nullable + String sockHostAddr(REQUEST request); + + @Nullable + String sockHostName(REQUEST request); + + @Nullable + Integer sockHostPort(REQUEST request); } 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 6cca61591a65..13ad7bf79c50 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 @@ -8,7 +8,6 @@ import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.assertThat; import static org.assertj.core.api.Assertions.entry; -import io.opentelemetry.api.common.AttributeKey; import io.opentelemetry.api.common.Attributes; import io.opentelemetry.api.common.AttributesBuilder; import io.opentelemetry.context.Context; @@ -26,14 +25,26 @@ class InetSocketAddressNetClientAttributesGetterTest { getter = new InetSocketAddressNetClientAttributesGetter() { @Override - public InetSocketAddress getAddress( - InetSocketAddress request, InetSocketAddress response) { - return response; + public String transport(InetSocketAddress request, InetSocketAddress response) { + return SemanticAttributes.NetTransportValues.IP_TCP; } @Override - public String transport(InetSocketAddress request, InetSocketAddress response) { - return SemanticAttributes.NetTransportValues.IP_TCP; + public String peerName(InetSocketAddress request, InetSocketAddress response) { + // net.peer.name and net.peer.port are tested in NetClientAttributesExtractorTest + return null; + } + + @Override + public Integer peerPort(InetSocketAddress request, InetSocketAddress response) { + // net.peer.name and net.peer.port are tested in NetClientAttributesExtractorTest + return null; + } + + @Override + public InetSocketAddress getPeerAddress( + InetSocketAddress request, InetSocketAddress response) { + return response; } }; private final NetClientAttributesExtractor extractor = @@ -52,35 +63,31 @@ void noInetSocketAddress() { @Test void fullAddress() { // given - InetSocketAddress request = new InetSocketAddress("github.com", 123); - assertThat(request.getAddress().getHostAddress()).isNotNull(); - - InetSocketAddress response = new InetSocketAddress("api.github.com", 456); - assertThat(request.getAddress().getHostAddress()).isNotNull(); + InetSocketAddress address = new InetSocketAddress("api.github.com", 456); + assertThat(address.getAddress().getHostAddress()).isNotNull(); - boolean ipv4 = response.getAddress() instanceof Inet4Address; + boolean ipv4 = address.getAddress() instanceof Inet4Address; Context context = Context.root(); // when AttributesBuilder startAttributes = Attributes.builder(); - extractor.onStart(startAttributes, context, request); + extractor.onStart(startAttributes, context, address); AttributesBuilder endAttributes = Attributes.builder(); - extractor.onEnd(endAttributes, context, request, response, null); + extractor.onEnd(endAttributes, context, address, address, null); // then assertThat(startAttributes.build()).isEmpty(); AttributesBuilder builder = Attributes.builder(); builder.put(SemanticAttributes.NET_TRANSPORT, SemanticAttributes.NetTransportValues.IP_TCP); - builder.put( - AttributeKey.stringKey("net.sock.peer.addr"), response.getAddress().getHostAddress()); + builder.put(NetAttributes.NET_SOCK_PEER_ADDR, address.getAddress().getHostAddress()); if (!ipv4) { - builder.put(AttributeKey.stringKey("net.sock.family"), "inet6"); + builder.put(NetAttributes.NET_SOCK_FAMILY, "inet6"); } - builder.put(SemanticAttributes.NET_PEER_NAME, "api.github.com"); - builder.put(SemanticAttributes.NET_PEER_PORT, 456L); + builder.put(NetAttributes.NET_SOCK_PEER_NAME, "api.github.com"); + builder.put(NetAttributes.NET_SOCK_PEER_PORT, 456L); assertThat(endAttributes.build()).isEqualTo(builder.build()); } @@ -88,28 +95,23 @@ void fullAddress() { @Test void unresolved() { // given - InetSocketAddress request = InetSocketAddress.createUnresolved("github.com", 123); - assertThat(request.getAddress()).isNull(); - - InetSocketAddress response = InetSocketAddress.createUnresolved("api.github.com", 456); - assertThat(request.getAddress()).isNull(); + InetSocketAddress address = InetSocketAddress.createUnresolved("api.github.com", 456); + assertThat(address.getAddress()).isNull(); Context context = Context.root(); // when AttributesBuilder startAttributes = Attributes.builder(); - extractor.onStart(startAttributes, context, request); + extractor.onStart(startAttributes, context, address); AttributesBuilder endAttributes = Attributes.builder(); - extractor.onEnd(endAttributes, context, request, response, null); + extractor.onEnd(endAttributes, context, address, address, null); // then assertThat(startAttributes.build()).isEmpty(); assertThat(endAttributes.build()) .containsOnly( - entry(SemanticAttributes.NET_TRANSPORT, SemanticAttributes.NetTransportValues.IP_TCP), - entry(SemanticAttributes.NET_PEER_NAME, "api.github.com"), - entry(SemanticAttributes.NET_PEER_PORT, 456L)); + entry(SemanticAttributes.NET_TRANSPORT, SemanticAttributes.NetTransportValues.IP_TCP)); } } 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 54c35bcae4b0..30681f852d3c 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 @@ -8,11 +8,11 @@ import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.assertThat; import static org.assertj.core.api.Assertions.entry; -import io.opentelemetry.api.common.AttributeKey; import io.opentelemetry.api.common.Attributes; import io.opentelemetry.api.common.AttributesBuilder; import io.opentelemetry.context.Context; import io.opentelemetry.semconv.trace.attributes.SemanticAttributes; +import java.net.Inet4Address; import java.net.InetSocketAddress; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; @@ -21,24 +21,43 @@ @ExtendWith(MockitoExtension.class) class InetSocketAddressNetServerAttributesGetterTest { - private final NetServerAttributesExtractor extractor = - NetServerAttributesExtractor.create( - new InetSocketAddressNetServerAttributesGetter() { - @Override - public InetSocketAddress getAddress(InetSocketAddress request) { - return request; - } - - @Override - public String transport(InetSocketAddress request) { - return SemanticAttributes.NetTransportValues.IP_TCP; - } - }); + final InetSocketAddressNetServerAttributesGetter getter = + new InetSocketAddressNetServerAttributesGetter() { + + @Override + public String transport(Addresses request) { + return SemanticAttributes.NetTransportValues.IP_TCP; + } + + @Override + public String hostName(Addresses request) { + // net.host.name and net.host.port are tested in NetClientAttributesExtractorTest + return null; + } + + @Override + public Integer hostPort(Addresses request) { + // net.host.name and net.host.port are tested in NetClientAttributesExtractorTest + return null; + } + + @Override + public InetSocketAddress getPeerAddress(Addresses request) { + return request.peer; + } + + @Override + public InetSocketAddress getHostAddress(Addresses request) { + return request.host; + } + }; + private final NetServerAttributesExtractor extractor = + NetServerAttributesExtractor.create(getter); @Test void noInetSocketAddress() { AttributesBuilder attributes = Attributes.builder(); - extractor.onStart(attributes, Context.root(), null); + extractor.onStart(attributes, Context.root(), new Addresses(null, null)); assertThat(attributes.build()) .containsOnly( entry(SemanticAttributes.NET_TRANSPORT, SemanticAttributes.NetTransportValues.IP_TCP)); @@ -47,11 +66,11 @@ void noInetSocketAddress() { @Test void fullAddress() { // given - InetSocketAddress request = new InetSocketAddress("github.com", 123); - assertThat(request.getAddress().getHostAddress()).isNotNull(); - - InetSocketAddress response = new InetSocketAddress("api.github.com", 456); - assertThat(request.getAddress().getHostAddress()).isNotNull(); + Addresses request = + new Addresses( + new InetSocketAddress("github.com", 123), new InetSocketAddress("api.github.com", 456)); + assertThat(request.peer.getAddress().getHostAddress()).isNotNull(); + assertThat(request.host.getAddress().getHostAddress()).isNotNull(); Context context = Context.root(); @@ -60,16 +79,21 @@ void fullAddress() { extractor.onStart(startAttributes, context, request); AttributesBuilder endAttributes = Attributes.builder(); - extractor.onEnd(endAttributes, context, request, response, null); + extractor.onEnd(endAttributes, context, request, request, null); // then - assertThat(startAttributes.build()) - .containsOnly( - entry(SemanticAttributes.NET_TRANSPORT, SemanticAttributes.NetTransportValues.IP_TCP), - entry( - AttributeKey.stringKey("net.sock.peer.addr"), - request.getAddress().getHostAddress()), - entry(AttributeKey.longKey("net.sock.peer.port"), 123L)); + AttributesBuilder builder = Attributes.builder(); + builder.put(SemanticAttributes.NET_TRANSPORT, SemanticAttributes.NetTransportValues.IP_TCP); + if (!request.isIpv4()) { + builder.put(NetAttributes.NET_SOCK_FAMILY, "inet6"); + } + builder.put(NetAttributes.NET_SOCK_PEER_ADDR, request.peer.getAddress().getHostAddress()); + builder.put(NetAttributes.NET_SOCK_PEER_PORT, 123L); + builder.put(NetAttributes.NET_SOCK_HOST_ADDR, request.host.getAddress().getHostAddress()); + builder.put(NetAttributes.NET_SOCK_HOST_NAME, "api.github.com"); + builder.put(NetAttributes.NET_SOCK_HOST_PORT, 456L); + + assertThat(startAttributes.build()).isEqualTo(builder.build()); assertThat(endAttributes.build()).isEmpty(); } @@ -77,11 +101,12 @@ void fullAddress() { @Test void unresolved() { // given - InetSocketAddress request = InetSocketAddress.createUnresolved("github.com", 123); - assertThat(request.getAddress()).isNull(); - - InetSocketAddress response = InetSocketAddress.createUnresolved("api.github.com", 456); - assertThat(request.getAddress()).isNull(); + Addresses request = + new Addresses( + InetSocketAddress.createUnresolved("github.com", 123), + InetSocketAddress.createUnresolved("api.github.com", 456)); + assertThat(request.peer.getAddress()).isNull(); + assertThat(request.host.getAddress()).isNull(); Context context = Context.root(); @@ -90,14 +115,28 @@ void unresolved() { extractor.onStart(startAttributes, context, request); AttributesBuilder endAttributes = Attributes.builder(); - extractor.onEnd(endAttributes, context, request, response, null); + extractor.onEnd(endAttributes, context, request, request, null); // then assertThat(startAttributes.build()) .containsOnly( - entry(SemanticAttributes.NET_TRANSPORT, SemanticAttributes.NetTransportValues.IP_TCP), - entry(AttributeKey.longKey("net.sock.peer.port"), 123L)); + entry(SemanticAttributes.NET_TRANSPORT, SemanticAttributes.NetTransportValues.IP_TCP)); assertThat(endAttributes.build()).isEmpty(); } + + static final class Addresses { + + private final InetSocketAddress peer; + private final InetSocketAddress host; + + Addresses(InetSocketAddress peer, InetSocketAddress host) { + this.peer = peer; + this.host = host; + } + + boolean isIpv4() { + return peer.getAddress() instanceof Inet4Address; + } + } } 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 4d7794edb83b..178ad068a33c 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 @@ -6,18 +6,20 @@ package io.opentelemetry.instrumentation.api.instrumenter.net; import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.assertThat; +import static io.opentelemetry.semconv.trace.attributes.SemanticAttributes.NetTransportValues.IP_TCP; +import static java.util.Collections.emptyMap; import static org.assertj.core.api.Assertions.entry; -import io.opentelemetry.api.common.AttributeKey; 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.trace.attributes.SemanticAttributes; import java.util.HashMap; import java.util.Map; +import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; -// TODO (trask) add more test coverage for #6268 class NetClientAttributesExtractorTest { static class TestNetClientAttributesGetter @@ -30,126 +32,209 @@ public String transport(Map request, Map respons @Override public String peerName(Map request, Map response) { - if (response != null) { - return response.get("peerName"); - } - return null; + return response.get("peerName"); } @Override public Integer peerPort(Map request, Map response) { - if (response != null) { - return Integer.valueOf(response.get("peerPort")); - } - return null; + String peerPort = response.get("peerPort"); + return peerPort == null ? null : Integer.valueOf(peerPort); + } + + @Override + public String sockFamily(Map request, Map response) { + return response.get("sockFamily"); } @Override public String sockPeerAddr(Map request, Map response) { - if (response != null) { - return response.get("sockPeerAddr"); - } - return null; + return response.get("sockPeerAddr"); + } + + @Override + public String sockPeerName(Map request, Map response) { + return response.get("sockPeerName"); + } + + @Override + public Integer sockPeerPort(Map request, Map response) { + String sockPeerPort = response.get("sockPeerPort"); + return sockPeerPort == null ? null : Integer.valueOf(sockPeerPort); } } + private final AttributesExtractor, Map> extractor = + NetClientAttributesExtractor.create(new TestNetClientAttributesGetter()); + @Test void normal() { // given - Map request = new HashMap<>(); - request.put("transport", "TCP"); - request.put("peerName", "github.com"); - request.put("peerPort", "123"); - request.put("sockPeerAddr", "1.2.3.4"); - - Map response = new HashMap<>(); - response.put("peerName", "opentelemetry.io"); - response.put("peerPort", "42"); - response.put("sockPeerAddr", "4.3.2.1"); - - TestNetClientAttributesGetter getter = new TestNetClientAttributesGetter(); - NetClientAttributesExtractor, Map> extractor = - NetClientAttributesExtractor.create(getter); + Map map = new HashMap<>(); + map.put("transport", 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", "proxy.opentelemetry.io"); + map.put("sockPeerPort", "123"); Context context = Context.root(); // when AttributesBuilder startAttributes = Attributes.builder(); - extractor.onStart(startAttributes, context, request); + extractor.onStart(startAttributes, context, map); AttributesBuilder endAttributes = Attributes.builder(); - extractor.onEnd(endAttributes, context, request, response, null); + extractor.onEnd(endAttributes, context, map, map, null); // then assertThat(startAttributes.build()).isEmpty(); assertThat(endAttributes.build()) .containsOnly( + entry(SemanticAttributes.NET_TRANSPORT, IP_TCP), entry(SemanticAttributes.NET_PEER_NAME, "opentelemetry.io"), entry(SemanticAttributes.NET_PEER_PORT, 42L), - entry(AttributeKey.stringKey("net.sock.peer.addr"), "4.3.2.1")); + entry(NetAttributes.NET_SOCK_FAMILY, "inet6"), + entry(NetAttributes.NET_SOCK_PEER_ADDR, "1:2:3:4::"), + entry(NetAttributes.NET_SOCK_PEER_NAME, "proxy.opentelemetry.io"), + entry(NetAttributes.NET_SOCK_PEER_PORT, 123L)); } @Test - public void doesNotSetDuplicateAttributes() { + void empty() { // given - Map request = new HashMap<>(); - request.put("transport", "TCP"); - request.put("peerName", "1.2.3.4"); - request.put("sockPeerAddr", "1.2.3.4"); - request.put("peerPort", "123"); + Context context = Context.root(); + + // when + AttributesBuilder startAttributes = Attributes.builder(); + extractor.onStart(startAttributes, context, emptyMap()); - Map response = new HashMap<>(); - response.put("peerName", "4.3.2.1"); - response.put("peerPort", "42"); - response.put("sockPeerAddr", "4.3.2.1"); + AttributesBuilder endAttributes = Attributes.builder(); + extractor.onEnd(endAttributes, context, emptyMap(), emptyMap(), null); + + // then + assertThat(startAttributes.build()).isEmpty(); + assertThat(endAttributes.build()).isEmpty(); + } - TestNetClientAttributesGetter getter = new TestNetClientAttributesGetter(); - NetClientAttributesExtractor, Map> extractor = - NetClientAttributesExtractor.create(getter); + @Test + @DisplayName("does not set any net.sock.* attributes when net.peer.name = net.sock.peer.addr") + void doesNotSetDuplicates1() { + // given + Map map = new HashMap<>(); + map.put("transport", IP_TCP); + map.put("peerName", "1:2:3:4::"); + 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(); // when AttributesBuilder startAttributes = Attributes.builder(); - extractor.onStart(startAttributes, context, request); + extractor.onStart(startAttributes, context, map); AttributesBuilder endAttributes = Attributes.builder(); - extractor.onEnd(endAttributes, context, request, response, null); + extractor.onEnd(endAttributes, context, map, map, null); // then assertThat(startAttributes.build()).isEmpty(); assertThat(endAttributes.build()) .containsOnly( + entry(SemanticAttributes.NET_TRANSPORT, IP_TCP), + entry(SemanticAttributes.NET_PEER_NAME, "1:2:3:4::"), + entry(SemanticAttributes.NET_PEER_PORT, 42L)); + } + + @Test + @DisplayName( + "does not set net.sock.* attributes when they duplicate related net.peer.* attributes") + void doesNotSetDuplicates2() { + // given + Map map = new HashMap<>(); + map.put("transport", 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()).isEmpty(); + + assertThat(endAttributes.build()) + .containsOnly( + entry(SemanticAttributes.NET_TRANSPORT, IP_TCP), + entry(SemanticAttributes.NET_PEER_NAME, "opentelemetry.io"), entry(SemanticAttributes.NET_PEER_PORT, 42L), - entry(SemanticAttributes.NET_PEER_NAME, "4.3.2.1")); + entry(NetAttributes.NET_SOCK_FAMILY, "inet6"), + entry(NetAttributes.NET_SOCK_PEER_ADDR, "1:2:3:4::")); } @Test - public void doesNotSetNegativePort() { + void doesNotSetNegativePortValues() { // given - Map request = new HashMap<>(); - request.put("peerPort", "-42"); + Map map = new HashMap<>(); + map.put("peerName", "opentelemetry.io"); + map.put("peerPort", "-12"); + map.put("sockPeerAddr", "1:2:3:4::"); + map.put("sockPeerPort", "-42"); + + Context context = Context.root(); - Map response = new HashMap<>(); - response.put("peerPort", "-1"); + // when + AttributesBuilder startAttributes = Attributes.builder(); + extractor.onStart(startAttributes, context, map); - TestNetClientAttributesGetter getter = new TestNetClientAttributesGetter(); - NetClientAttributesExtractor, Map> extractor = - NetClientAttributesExtractor.create(getter); + AttributesBuilder endAttributes = Attributes.builder(); + extractor.onEnd(endAttributes, context, map, map, null); + + // then + assertThat(startAttributes.build()).isEmpty(); + + assertThat(endAttributes.build()) + .containsOnly( + entry(SemanticAttributes.NET_PEER_NAME, "opentelemetry.io"), + entry(NetAttributes.NET_SOCK_PEER_ADDR, "1:2:3:4::")); + } + + @Test + void doesNotSetSockFamilyInet() { + // given + Map map = new HashMap<>(); + map.put("peerName", "opentelemetry.io"); + map.put("sockPeerAddr", "1.2.3.4"); + map.put("sockFamily", NetAttributes.SOCK_FAMILY_INET); Context context = Context.root(); // when AttributesBuilder startAttributes = Attributes.builder(); - extractor.onStart(startAttributes, context, request); + extractor.onStart(startAttributes, context, map); AttributesBuilder endAttributes = Attributes.builder(); - extractor.onEnd(endAttributes, context, request, response, null); + extractor.onEnd(endAttributes, context, map, map, null); // then assertThat(startAttributes.build()).isEmpty(); - assertThat(endAttributes.build()).isEmpty(); + + assertThat(endAttributes.build()) + .containsOnly( + entry(SemanticAttributes.NET_PEER_NAME, "opentelemetry.io"), + entry(NetAttributes.NET_SOCK_PEER_ADDR, "1.2.3.4")); } } 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 15a7a708e9d9..519ccbbda75d 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 @@ -6,15 +6,18 @@ package io.opentelemetry.instrumentation.api.instrumenter.net; import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.assertThat; +import static io.opentelemetry.semconv.trace.attributes.SemanticAttributes.NetTransportValues.IP_TCP; +import static java.util.Collections.emptyMap; import static org.assertj.core.api.Assertions.entry; -import io.opentelemetry.api.common.AttributeKey; import io.opentelemetry.api.common.Attributes; import io.opentelemetry.api.common.AttributesBuilder; import io.opentelemetry.context.Context; import io.opentelemetry.semconv.trace.attributes.SemanticAttributes; import java.util.HashMap; import java.util.Map; +import javax.annotation.Nullable; +import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; class NetServerAttributesExtractorTest { @@ -27,113 +30,236 @@ public String transport(Map request) { return request.get("transport"); } + @Nullable @Override - public Integer sockPeerPort(Map request) { - return Integer.valueOf(request.get("sockPeerPort")); + public String hostName(Map request) { + return request.get("hostName"); + } + + @Nullable + @Override + public Integer hostPort(Map request) { + String hostPort = request.get("hostPort"); + return hostPort == null ? null : Integer.valueOf(hostPort); + } + + @Nullable + @Override + public String sockFamily(Map request) { + return request.get("sockFamily"); } @Override public String sockPeerAddr(Map request) { return request.get("sockPeerAddr"); } + + @Override + public Integer sockPeerPort(Map request) { + String sockPeerPort = request.get("sockPeerPort"); + return sockPeerPort == null ? null : Integer.valueOf(sockPeerPort); + } + + @Nullable + @Override + public String sockHostAddr(Map request) { + return request.get("sockHostAddr"); + } + + @Nullable + @Override + public String sockHostName(Map request) { + return request.get("sockHostName"); + } + + @Nullable + @Override + public Integer sockHostPort(Map request) { + String sockHostPort = request.get("sockHostPort"); + return sockHostPort == null ? null : Integer.valueOf(sockHostPort); + } } + NetServerAttributesExtractor, Map> extractor = + NetServerAttributesExtractor.create(new TestNetServerAttributesGetter()); + @Test void normal() { // given - Map request = new HashMap<>(); - request.put("transport", "TCP"); - request.put("sockPeerPort", "123"); - request.put("sockPeerAddr", "1.2.3.4"); - - Map response = new HashMap<>(); - response.put("sockPeerPort", "42"); - response.put("sockPeerAddr", "4.3.2.1"); - - NetServerAttributesExtractor, Map> extractor = - createTestExtractor(); + Map map = new HashMap<>(); + map.put("transport", IP_TCP); + map.put("hostName", "opentelemetry.io"); + map.put("hostPort", "80"); + map.put("sockFamily", "inet6"); + map.put("sockPeerAddr", "1:2:3:4::"); + map.put("sockPeerPort", "42"); + map.put("sockHostAddr", "4:3:2:1::"); + map.put("sockHostName", "localhost"); + map.put("sockHostPort", "8080"); Context context = Context.root(); // when AttributesBuilder startAttributes = Attributes.builder(); - extractor.onStart(startAttributes, context, request); + extractor.onStart(startAttributes, context, map); AttributesBuilder endAttributes = Attributes.builder(); - extractor.onEnd(endAttributes, context, request, response, null); + extractor.onEnd(endAttributes, context, map, map, null); // then assertThat(startAttributes.build()) .containsOnly( - entry(SemanticAttributes.NET_TRANSPORT, "TCP"), - entry(AttributeKey.longKey("net.sock.peer.port"), 123L), - entry(AttributeKey.stringKey("net.sock.peer.addr"), "1.2.3.4")); + entry(SemanticAttributes.NET_TRANSPORT, IP_TCP), + entry(SemanticAttributes.NET_HOST_NAME, "opentelemetry.io"), + entry(SemanticAttributes.NET_HOST_PORT, 80L), + entry(NetAttributes.NET_SOCK_FAMILY, "inet6"), + entry(NetAttributes.NET_SOCK_PEER_ADDR, "1:2:3:4::"), + entry(NetAttributes.NET_SOCK_PEER_PORT, 42L), + entry(NetAttributes.NET_SOCK_HOST_ADDR, "4:3:2:1::"), + entry(NetAttributes.NET_SOCK_HOST_NAME, "localhost"), + entry(NetAttributes.NET_SOCK_HOST_PORT, 8080L)); assertThat(endAttributes.build()).isEmpty(); } @Test - public void doesNotSetDuplicateAttributes() { + void empty() { // given - Map request = new HashMap<>(); - request.put("transport", "TCP"); - request.put("sockPeerAddr", "1.2.3.4"); - request.put("sockPeerPort", "123"); + Context context = Context.root(); - Map response = new HashMap<>(); - response.put("sockPeerPort", "42"); - response.put("sockPeerAddr", "4.3.2.1"); + // when + AttributesBuilder startAttributes = Attributes.builder(); + extractor.onStart(startAttributes, context, emptyMap()); - NetServerAttributesExtractor, Map> extractor = - createTestExtractor(); + AttributesBuilder endAttributes = Attributes.builder(); + extractor.onEnd(endAttributes, context, emptyMap(), emptyMap(), null); + + // then + assertThat(startAttributes.build()).isEmpty(); + assertThat(endAttributes.build()).isEmpty(); + } + + @Test + @DisplayName( + "does not set any net.sock.host.* attributes when net.host.name = net.sock.host.addr") + void doesNotSetDuplicates1() { + // given + Map map = new HashMap<>(); + map.put("transport", IP_TCP); + map.put("hostName", "4:3:2:1::"); + map.put("hostPort", "80"); + map.put("sockFamily", "inet6"); + map.put("sockHostAddr", "4:3:2:1::"); + map.put("sockHostName", "localhost"); + map.put("sockHostPort", "8080"); Context context = Context.root(); // when AttributesBuilder startAttributes = Attributes.builder(); - extractor.onStart(startAttributes, context, request); + extractor.onStart(startAttributes, context, map); AttributesBuilder endAttributes = Attributes.builder(); - extractor.onEnd(endAttributes, context, request, response, null); + extractor.onEnd(endAttributes, context, map, map, null); // then assertThat(startAttributes.build()) .containsOnly( - entry(SemanticAttributes.NET_TRANSPORT, "TCP"), - entry(AttributeKey.longKey("net.sock.peer.port"), 123L), - entry(AttributeKey.stringKey("net.sock.peer.addr"), "1.2.3.4")); + entry(SemanticAttributes.NET_TRANSPORT, IP_TCP), + entry(SemanticAttributes.NET_HOST_NAME, "4:3:2:1::"), + entry(SemanticAttributes.NET_HOST_PORT, 80L)); assertThat(endAttributes.build()).isEmpty(); } @Test - public void doesNotSetNegativePort() { + @DisplayName( + "does not set net.sock.host.* attributes when they duplicate related net.host.* attributes") + void doesNotSetDuplicates2() { // given - Map request = new HashMap<>(); - request.put("sockPeerPort", "-42"); + Map map = new HashMap<>(); + map.put("transport", IP_TCP); + map.put("hostName", "opentelemetry.io"); + map.put("hostPort", "80"); + map.put("sockFamily", "inet6"); + map.put("sockHostAddr", "4:3:2:1::"); + map.put("sockHostName", "opentelemetry.io"); + map.put("sockHostPort", "80"); - Map response = new HashMap<>(); - response.put("sockPeerPort", "-1"); + Context context = Context.root(); - NetServerAttributesExtractor, Map> extractor = - createTestExtractor(); + // 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_TRANSPORT, IP_TCP), + entry(SemanticAttributes.NET_HOST_NAME, "opentelemetry.io"), + entry(SemanticAttributes.NET_HOST_PORT, 80L), + entry(NetAttributes.NET_SOCK_FAMILY, "inet6"), + entry(NetAttributes.NET_SOCK_HOST_ADDR, "4:3:2:1::")); + + assertThat(endAttributes.build()).isEmpty(); + } + + @Test + void doesNotSetNegativePort() { + // given + Map map = new HashMap<>(); + map.put("hostName", "opentelemetry.io"); + map.put("hostPort", "-80"); + map.put("sockPeerAddr", "1:2:3:4::"); + map.put("sockPeerPort", "-42"); + map.put("sockHostAddr", "4:3:2:1::"); + map.put("sockHostPort", "-8080"); Context context = Context.root(); // when AttributesBuilder startAttributes = Attributes.builder(); - extractor.onStart(startAttributes, context, request); + extractor.onStart(startAttributes, context, map); AttributesBuilder endAttributes = Attributes.builder(); - extractor.onEnd(endAttributes, context, request, response, null); + extractor.onEnd(endAttributes, context, map, map, null); // then - assertThat(startAttributes.build()).isEmpty(); + assertThat(startAttributes.build()) + .containsOnly( + entry(SemanticAttributes.NET_HOST_NAME, "opentelemetry.io"), + entry(NetAttributes.NET_SOCK_PEER_ADDR, "1:2:3:4::"), + entry(NetAttributes.NET_SOCK_HOST_ADDR, "4:3:2:1::")); + assertThat(endAttributes.build()).isEmpty(); } - private static NetServerAttributesExtractor, Map> - createTestExtractor() { - return NetServerAttributesExtractor.create(new TestNetServerAttributesGetter()); + @Test + void doesNotSetSockFamilyInet() { + // given + Map map = new HashMap<>(); + map.put("hostName", "opentelemetry.io"); + map.put("sockPeerAddr", "1.2.3.4"); + map.put("sockFamily", NetAttributes.SOCK_FAMILY_INET); + + 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_HOST_NAME, "opentelemetry.io"), + entry(NetAttributes.NET_SOCK_PEER_ADDR, "1.2.3.4")); + + assertThat(endAttributes.build()).isEmpty(); } }