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..3f212247e521 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,27 @@ public abstract class InetSocketAddressNetClientAttributesGetter { @Nullable - public abstract InetSocketAddress getAddress(REQUEST request, @Nullable RESPONSE response); + protected abstract InetSocketAddress getPeerSocketAddress( + 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 = getPeerSocketAddress(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 = getPeerSocketAddress(request, response); if (address == null) { return null; } @@ -57,27 +52,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 = getPeerSocketAddress(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 = getPeerSocketAddress(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..fb2a5e4fa190 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,55 @@ public abstract class InetSocketAddressNetServerAttributesGetter implements NetServerAttributesGetter { @Nullable - public abstract InetSocketAddress getAddress(REQUEST request); + protected abstract InetSocketAddress getPeerSocketAddress(REQUEST request); - @Override + // optional @Nullable - public final Integer sockPeerPort(REQUEST request) { - InetSocketAddress address = getAddress(request); + protected abstract InetSocketAddress getHostSocketAddress(REQUEST request); + + @Nullable + @Override + public String sockFamily(REQUEST request) { + InetSocketAddress address = getPeerSocketAddress(request); + if (address == null) { + address = getHostSocketAddress(request); + } if (address == null) { return null; } - return address.getPort(); + InetAddress inetAddress = address.getAddress(); + if (inetAddress instanceof Inet6Address) { + return "inet6"; + } + return null; } @Override @Nullable public final String sockPeerAddr(REQUEST request) { - InetSocketAddress address = getAddress(request); + return getAddress(getPeerSocketAddress(request)); + } + + @Override + @Nullable + public final Integer sockPeerPort(REQUEST request) { + return getPort(getPeerSocketAddress(request)); + } + + @Nullable + @Override + public String sockHostAddr(REQUEST request) { + return getAddress(getHostSocketAddress(request)); + } + + @Nullable + @Override + public Integer sockHostPort(REQUEST request) { + return getPort(getHostSocketAddress(request)); + } + + @Nullable + private static String getAddress(InetSocketAddress address) { if (address == null) { return null; } @@ -45,4 +79,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..9bfefe1e10d5 --- /dev/null +++ b/instrumentation-api-semconv/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/net/NetAttributes.java @@ -0,0 +1,26 @@ +/* + * 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_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..b2af0ebb2792 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,22 @@ public interface NetClientAttributesGetter { Integer peerPort(REQUEST request, @Nullable RESPONSE response); @Nullable - default String sockPeerAddr(REQUEST request, @Nullable RESPONSE response) { + default String sockFamily(REQUEST request, @Nullable RESPONSE response) { return null; } @Nullable - default Integer sockPeerPort(REQUEST request, @Nullable RESPONSE response) { + default String sockPeerAddr(REQUEST request, @Nullable RESPONSE response) { return null; } @Nullable - default String sockFamily(REQUEST request, @Nullable RESPONSE response) { + default String sockPeerName(REQUEST request, @Nullable RESPONSE response) { return null; } @Nullable - default String sockPeerName(REQUEST request, @Nullable RESPONSE response) { + default Integer sockPeerPort(REQUEST request, @Nullable RESPONSE response) { return null; } } 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..9ae5c4ae33e2 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,47 @@ 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); + } + } - 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..72bd87e6b1b8 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,33 @@ public interface NetServerAttributesGetter { String transport(REQUEST request); @Nullable - Integer sockPeerPort(REQUEST request); + String hostName(REQUEST request); @Nullable - String sockPeerAddr(REQUEST request); + Integer hostPort(REQUEST request); + + @Nullable + default String sockFamily(REQUEST request) { + return null; + } + + @Nullable + default String sockPeerAddr(REQUEST request) { + return null; + } + + @Nullable + default Integer sockPeerPort(REQUEST request) { + return null; + } + + @Nullable + default String sockHostAddr(REQUEST request) { + return null; + } + + @Nullable + default Integer sockHostPort(REQUEST request) { + return null; + } } 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..abd6b7b424e6 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 + protected InetSocketAddress getPeerSocketAddress( + 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..bcbdfabbb65d 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 + protected InetSocketAddress getPeerSocketAddress(Addresses request) { + return request.peer; + } + + @Override + protected InetSocketAddress getHostSocketAddress(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,20 @@ 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_PORT, 456L); + + assertThat(startAttributes.build()).isEqualTo(builder.build()); assertThat(endAttributes.build()).isEmpty(); } @@ -77,11 +100,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 +114,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..fb4712e00e39 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,226 @@ 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 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("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_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(); + + // when + AttributesBuilder startAttributes = Attributes.builder(); + extractor.onStart(startAttributes, context, emptyMap()); + + AttributesBuilder endAttributes = Attributes.builder(); + extractor.onEnd(endAttributes, context, emptyMap(), emptyMap(), null); - Map response = new HashMap<>(); - response.put("sockPeerPort", "42"); - response.put("sockPeerAddr", "4.3.2.1"); + // then + assertThat(startAttributes.build()).isEmpty(); + assertThat(endAttributes.build()).isEmpty(); + } - NetServerAttributesExtractor, Map> extractor = - createTestExtractor(); + @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("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("sockHostPort", "80"); + + Context context = Context.root(); - Map response = new HashMap<>(); - response.put("sockPeerPort", "-1"); + // when + AttributesBuilder startAttributes = Attributes.builder(); + extractor.onStart(startAttributes, context, map); - NetServerAttributesExtractor, Map> extractor = - createTestExtractor(); + 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(); } } diff --git a/instrumentation-api/src/jmh/java/io/opentelemetry/instrumentation/api/instrumenter/InstrumenterBenchmark.java b/instrumentation-api/src/jmh/java/io/opentelemetry/instrumentation/api/instrumenter/InstrumenterBenchmark.java index 784fa9ad4791..4ea150d2011c 100644 --- a/instrumentation-api/src/jmh/java/io/opentelemetry/instrumentation/api/instrumenter/InstrumenterBenchmark.java +++ b/instrumentation-api/src/jmh/java/io/opentelemetry/instrumentation/api/instrumenter/InstrumenterBenchmark.java @@ -99,19 +99,39 @@ public List responseHeader(Void unused, Void unused2, String name) { static class ConstantNetAttributesGetter extends InetSocketAddressNetServerAttributesGetter { - private static final InetSocketAddress ADDRESS = + private static final InetSocketAddress PEER_ADDRESS = InetSocketAddress.createUnresolved("localhost", 8080); + private static final InetSocketAddress HOST_ADDRESS = + InetSocketAddress.createUnresolved("localhost", 80); @Override @Nullable - public InetSocketAddress getAddress(Void unused) { - return ADDRESS; + public String transport(Void unused) { + return SemanticAttributes.NetTransportValues.IP_TCP; } + @Nullable @Override + public String hostName(Void unused) { + return null; + } + @Nullable - public String transport(Void unused) { - return SemanticAttributes.NetTransportValues.IP_TCP; + @Override + public Integer hostPort(Void unused) { + return null; + } + + @Override + @Nullable + protected InetSocketAddress getPeerSocketAddress(Void unused) { + return PEER_ADDRESS; + } + + @Nullable + @Override + protected InetSocketAddress getHostSocketAddress(Void unused) { + return HOST_ADDRESS; } } } diff --git a/instrumentation/apache-camel-2.20/javaagent/src/test/groovy/io/opentelemetry/javaagent/instrumentation/apachecamel/RestCamelTest.groovy b/instrumentation/apache-camel-2.20/javaagent/src/test/groovy/io/opentelemetry/javaagent/instrumentation/apachecamel/RestCamelTest.groovy index 43b04b606088..142af24aed5b 100644 --- a/instrumentation/apache-camel-2.20/javaagent/src/test/groovy/io/opentelemetry/javaagent/instrumentation/apachecamel/RestCamelTest.groovy +++ b/instrumentation/apache-camel-2.20/javaagent/src/test/groovy/io/opentelemetry/javaagent/instrumentation/apachecamel/RestCamelTest.groovy @@ -95,10 +95,13 @@ class RestCamelTest extends AgentInstrumentationSpecification implements RetryOn "$SemanticAttributes.HTTP_USER_AGENT" String "$SemanticAttributes.HTTP_FLAVOR" "1.1" "$SemanticAttributes.HTTP_METHOD" "GET" + "$SemanticAttributes.HTTP_ROUTE" "/api/{module}/unit/{unitId}" + "$SemanticAttributes.NET_TRANSPORT" IP_TCP + "net.host.name" "localhost" + "net.host.port" port "net.sock.peer.addr" "127.0.0.1" "net.sock.peer.port" Long - "$SemanticAttributes.NET_TRANSPORT" IP_TCP - "$SemanticAttributes.HTTP_ROUTE" "/api/{module}/unit/{unitId}" + "net.sock.host.addr" "127.0.0.1" } } it.span(3) { diff --git a/instrumentation/apache-camel-2.20/javaagent/src/test/groovy/io/opentelemetry/javaagent/instrumentation/apachecamel/TwoServicesWithDirectClientCamelTest.groovy b/instrumentation/apache-camel-2.20/javaagent/src/test/groovy/io/opentelemetry/javaagent/instrumentation/apachecamel/TwoServicesWithDirectClientCamelTest.groovy index 3b4605337756..1e465b758b1c 100644 --- a/instrumentation/apache-camel-2.20/javaagent/src/test/groovy/io/opentelemetry/javaagent/instrumentation/apachecamel/TwoServicesWithDirectClientCamelTest.groovy +++ b/instrumentation/apache-camel-2.20/javaagent/src/test/groovy/io/opentelemetry/javaagent/instrumentation/apachecamel/TwoServicesWithDirectClientCamelTest.groovy @@ -128,13 +128,15 @@ class TwoServicesWithDirectClientCamelTest extends AgentInstrumentationSpecifica "$SemanticAttributes.HTTP_SCHEME" "http" "$SemanticAttributes.HTTP_HOST" "127.0.0.1:$portTwo" "$SemanticAttributes.HTTP_TARGET" "/serviceTwo" - "net.sock.peer.port" Number - "net.sock.peer.addr" "127.0.0.1" "$SemanticAttributes.HTTP_USER_AGENT" "Jakarta Commons-HttpClient/3.1" "$SemanticAttributes.HTTP_FLAVOR" "1.1" - "$SemanticAttributes.NET_TRANSPORT" IP_TCP "$SemanticAttributes.HTTP_REQUEST_CONTENT_LENGTH" Long "$SemanticAttributes.HTTP_ROUTE" "/serviceTwo" + "$SemanticAttributes.NET_TRANSPORT" IP_TCP + "net.host.name" "127.0.0.1" + "net.host.port" portTwo + "net.sock.peer.addr" "127.0.0.1" + "net.sock.peer.port" Long } } it.span(5) { diff --git a/instrumentation/apache-dubbo-2.7/library-autoconfigure/src/main/java/io/opentelemetry/instrumentation/apachedubbo/v2_7/DubboRequest.java b/instrumentation/apache-dubbo-2.7/library-autoconfigure/src/main/java/io/opentelemetry/instrumentation/apachedubbo/v2_7/DubboRequest.java index 8dd0b0dbe72b..0733b744c867 100644 --- a/instrumentation/apache-dubbo-2.7/library-autoconfigure/src/main/java/io/opentelemetry/instrumentation/apachedubbo/v2_7/DubboRequest.java +++ b/instrumentation/apache-dubbo-2.7/library-autoconfigure/src/main/java/io/opentelemetry/instrumentation/apachedubbo/v2_7/DubboRequest.java @@ -19,7 +19,11 @@ static DubboRequest create(RpcInvocation invocation, RpcContext context) { // In dubbo 3 RpcContext delegates to a ThreadLocal context. We copy the url and remote address // here to ensure we can access them from the thread that ends the span. return new AutoValue_DubboRequest( - invocation, context, context.getUrl(), context.getRemoteAddress()); + invocation, + context, + context.getUrl(), + context.getRemoteAddress(), + context.getLocalAddress()); } abstract RpcInvocation invocation(); @@ -30,4 +34,7 @@ static DubboRequest create(RpcInvocation invocation, RpcContext context) { @Nullable public abstract InetSocketAddress remoteAddress(); + + @Nullable + public abstract InetSocketAddress localAddress(); } diff --git a/instrumentation/apache-dubbo-2.7/library-autoconfigure/src/main/java/io/opentelemetry/instrumentation/apachedubbo/v2_7/internal/DubboNetClientAttributesGetter.java b/instrumentation/apache-dubbo-2.7/library-autoconfigure/src/main/java/io/opentelemetry/instrumentation/apachedubbo/v2_7/internal/DubboNetClientAttributesGetter.java index 7af5c97d1ce6..e0f67bed15fb 100644 --- a/instrumentation/apache-dubbo-2.7/library-autoconfigure/src/main/java/io/opentelemetry/instrumentation/apachedubbo/v2_7/internal/DubboNetClientAttributesGetter.java +++ b/instrumentation/apache-dubbo-2.7/library-autoconfigure/src/main/java/io/opentelemetry/instrumentation/apachedubbo/v2_7/internal/DubboNetClientAttributesGetter.java @@ -9,7 +9,6 @@ import io.opentelemetry.instrumentation.api.instrumenter.net.InetSocketAddressNetClientAttributesGetter; import java.net.InetSocketAddress; import javax.annotation.Nullable; -import org.apache.dubbo.common.URL; import org.apache.dubbo.rpc.Result; /** @@ -21,19 +20,25 @@ public final class DubboNetClientAttributesGetter @Override @Nullable - public InetSocketAddress getAddress(DubboRequest request, @Nullable Result response) { - InetSocketAddress address = request.remoteAddress(); - // dubbo 3 doesn't set remote address for client calls - if (address == null) { - URL url = request.url(); - address = InetSocketAddress.createUnresolved(url.getHost(), url.getPort()); - } - return address; + public String transport(DubboRequest request, @Nullable Result response) { + return null; + } + + @Nullable + @Override + public String peerName(DubboRequest request, @Nullable Result result) { + return request.url().getHost(); + } + + @Override + public Integer peerPort(DubboRequest request, @Nullable Result result) { + return request.url().getPort(); } @Override @Nullable - public String transport(DubboRequest request, @Nullable Result response) { - return null; + protected InetSocketAddress getPeerSocketAddress( + DubboRequest request, @Nullable Result response) { + return request.remoteAddress(); } } diff --git a/instrumentation/apache-dubbo-2.7/library-autoconfigure/src/main/java/io/opentelemetry/instrumentation/apachedubbo/v2_7/internal/DubboNetServerAttributesGetter.java b/instrumentation/apache-dubbo-2.7/library-autoconfigure/src/main/java/io/opentelemetry/instrumentation/apachedubbo/v2_7/internal/DubboNetServerAttributesGetter.java index 659587637b70..db5f707ab7d7 100644 --- a/instrumentation/apache-dubbo-2.7/library-autoconfigure/src/main/java/io/opentelemetry/instrumentation/apachedubbo/v2_7/internal/DubboNetServerAttributesGetter.java +++ b/instrumentation/apache-dubbo-2.7/library-autoconfigure/src/main/java/io/opentelemetry/instrumentation/apachedubbo/v2_7/internal/DubboNetServerAttributesGetter.java @@ -19,13 +19,31 @@ public final class DubboNetServerAttributesGetter @Override @Nullable - public InetSocketAddress getAddress(DubboRequest request) { - return request.remoteAddress(); + public String transport(DubboRequest request) { + return null; } + @Nullable @Override + public String hostName(DubboRequest request) { + return null; + } + @Nullable - public String transport(DubboRequest request) { + @Override + public Integer hostPort(DubboRequest request) { return null; } + + @Override + @Nullable + protected InetSocketAddress getPeerSocketAddress(DubboRequest request) { + return request.remoteAddress(); + } + + @Nullable + @Override + protected InetSocketAddress getHostSocketAddress(DubboRequest request) { + return request.localAddress(); + } } diff --git a/instrumentation/apache-httpasyncclient-4.1/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/apachehttpasyncclient/ApacheHttpAsyncClientNetAttributesGetter.java b/instrumentation/apache-httpasyncclient-4.1/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/apachehttpasyncclient/ApacheHttpAsyncClientNetAttributesGetter.java index da311b325e01..6c45782552d2 100644 --- a/instrumentation/apache-httpasyncclient-4.1/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/apachehttpasyncclient/ApacheHttpAsyncClientNetAttributesGetter.java +++ b/instrumentation/apache-httpasyncclient-4.1/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/apachehttpasyncclient/ApacheHttpAsyncClientNetAttributesGetter.java @@ -5,13 +5,14 @@ package io.opentelemetry.javaagent.instrumentation.apachehttpasyncclient; -import io.opentelemetry.instrumentation.api.instrumenter.net.NetClientAttributesGetter; +import io.opentelemetry.instrumentation.api.instrumenter.net.InetSocketAddressNetClientAttributesGetter; import io.opentelemetry.semconv.trace.attributes.SemanticAttributes; +import java.net.InetSocketAddress; import javax.annotation.Nullable; import org.apache.http.HttpResponse; final class ApacheHttpAsyncClientNetAttributesGetter - implements NetClientAttributesGetter { + extends InetSocketAddressNetClientAttributesGetter { @Override public String transport(ApacheHttpClientRequest request, @Nullable HttpResponse response) { @@ -28,4 +29,11 @@ public String peerName(ApacheHttpClientRequest request, @Nullable HttpResponse r public Integer peerPort(ApacheHttpClientRequest request, @Nullable HttpResponse response) { return request.getPeerPort(); } + + @Nullable + @Override + protected InetSocketAddress getPeerSocketAddress( + ApacheHttpClientRequest request, @Nullable HttpResponse response) { + return request.peerSocketAddress(); + } } diff --git a/instrumentation/apache-httpasyncclient-4.1/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/apachehttpasyncclient/ApacheHttpClientRequest.java b/instrumentation/apache-httpasyncclient-4.1/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/apachehttpasyncclient/ApacheHttpClientRequest.java index dd92339354dc..e9b1c6f27e0d 100644 --- a/instrumentation/apache-httpasyncclient-4.1/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/apachehttpasyncclient/ApacheHttpClientRequest.java +++ b/instrumentation/apache-httpasyncclient-4.1/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/apachehttpasyncclient/ApacheHttpClientRequest.java @@ -8,6 +8,8 @@ import static java.util.logging.Level.FINE; import io.opentelemetry.semconv.trace.attributes.SemanticAttributes; +import java.net.InetAddress; +import java.net.InetSocketAddress; import java.net.URI; import java.net.URISyntaxException; import java.util.ArrayList; @@ -27,8 +29,9 @@ public final class ApacheHttpClientRequest { @Nullable private final URI uri; private final HttpRequest delegate; + @Nullable private final HttpHost target; - public ApacheHttpClientRequest(HttpHost httpHost, HttpRequest httpRequest) { + public ApacheHttpClientRequest(@Nullable HttpHost httpHost, HttpRequest httpRequest) { URI calculatedUri = getUri(httpRequest); if (calculatedUri != null && httpHost != null) { uri = getCalculatedUri(httpHost, calculatedUri); @@ -36,6 +39,7 @@ public ApacheHttpClientRequest(HttpHost httpHost, HttpRequest httpRequest) { uri = calculatedUri; } delegate = httpRequest; + target = httpHost; } public List getHeader(String name) { @@ -143,4 +147,13 @@ private static URI getCalculatedUri(HttpHost httpHost, URI uri) { return null; } } + + @Nullable + public InetSocketAddress peerSocketAddress() { + if (target == null) { + return null; + } + InetAddress inetAddress = target.getAddress(); + return inetAddress == null ? null : new InetSocketAddress(inetAddress, target.getPort()); + } } diff --git a/instrumentation/apache-httpclient/apache-httpclient-4.3/library/src/main/java/io/opentelemetry/instrumentation/apachehttpclient/v4_3/ApacheHttpClientNetAttributesGetter.java b/instrumentation/apache-httpclient/apache-httpclient-4.3/library/src/main/java/io/opentelemetry/instrumentation/apachehttpclient/v4_3/ApacheHttpClientNetAttributesGetter.java index 27a4176a3384..88a5712cccda 100644 --- a/instrumentation/apache-httpclient/apache-httpclient-4.3/library/src/main/java/io/opentelemetry/instrumentation/apachehttpclient/v4_3/ApacheHttpClientNetAttributesGetter.java +++ b/instrumentation/apache-httpclient/apache-httpclient-4.3/library/src/main/java/io/opentelemetry/instrumentation/apachehttpclient/v4_3/ApacheHttpClientNetAttributesGetter.java @@ -5,13 +5,14 @@ package io.opentelemetry.instrumentation.apachehttpclient.v4_3; -import io.opentelemetry.instrumentation.api.instrumenter.net.NetClientAttributesGetter; +import io.opentelemetry.instrumentation.api.instrumenter.net.InetSocketAddressNetClientAttributesGetter; import io.opentelemetry.semconv.trace.attributes.SemanticAttributes; +import java.net.InetSocketAddress; import javax.annotation.Nullable; import org.apache.http.HttpResponse; final class ApacheHttpClientNetAttributesGetter - implements NetClientAttributesGetter { + extends InetSocketAddressNetClientAttributesGetter { @Override public String transport(ApacheHttpClientRequest request, @Nullable HttpResponse response) { @@ -29,4 +30,11 @@ public String peerName(ApacheHttpClientRequest request, @Nullable HttpResponse r public Integer peerPort(ApacheHttpClientRequest request, @Nullable HttpResponse response) { return request.getPeerPort(); } + + @Nullable + @Override + protected InetSocketAddress getPeerSocketAddress( + ApacheHttpClientRequest request, @Nullable HttpResponse response) { + return request.peerSocketAddress(); + } } diff --git a/instrumentation/apache-httpclient/apache-httpclient-4.3/library/src/main/java/io/opentelemetry/instrumentation/apachehttpclient/v4_3/ApacheHttpClientRequest.java b/instrumentation/apache-httpclient/apache-httpclient-4.3/library/src/main/java/io/opentelemetry/instrumentation/apachehttpclient/v4_3/ApacheHttpClientRequest.java index 4264c9035329..6ea15860e673 100644 --- a/instrumentation/apache-httpclient/apache-httpclient-4.3/library/src/main/java/io/opentelemetry/instrumentation/apachehttpclient/v4_3/ApacheHttpClientRequest.java +++ b/instrumentation/apache-httpclient/apache-httpclient-4.3/library/src/main/java/io/opentelemetry/instrumentation/apachehttpclient/v4_3/ApacheHttpClientRequest.java @@ -8,6 +8,8 @@ import static java.util.logging.Level.FINE; import io.opentelemetry.semconv.trace.attributes.SemanticAttributes; +import java.net.InetAddress; +import java.net.InetSocketAddress; import java.net.URI; import java.net.URISyntaxException; import java.util.ArrayList; @@ -27,6 +29,7 @@ public final class ApacheHttpClientRequest { @Nullable private final URI uri; private final HttpRequest delegate; + @Nullable private final HttpHost target; ApacheHttpClientRequest(@Nullable HttpHost httpHost, HttpRequest httpRequest) { URI calculatedUri = getUri(httpRequest); @@ -36,6 +39,7 @@ public final class ApacheHttpClientRequest { uri = calculatedUri; } delegate = httpRequest; + target = httpHost; } /** Returns the actual {@link HttpRequest} being executed by the client. */ @@ -146,4 +150,13 @@ private static URI getCalculatedUri(HttpHost httpHost, URI uri) { return null; } } + + @Nullable + public InetSocketAddress peerSocketAddress() { + if (target == null) { + return null; + } + InetAddress inetAddress = target.getAddress(); + return inetAddress == null ? null : new InetSocketAddress(inetAddress, target.getPort()); + } } diff --git a/instrumentation/armeria-1.3/library/src/main/java/io/opentelemetry/instrumentation/armeria/v1_3/ArmeriaNetServerAttributesGetter.java b/instrumentation/armeria-1.3/library/src/main/java/io/opentelemetry/instrumentation/armeria/v1_3/ArmeriaNetServerAttributesGetter.java index 6b3983a2b869..def4eeb71829 100644 --- a/instrumentation/armeria-1.3/library/src/main/java/io/opentelemetry/instrumentation/armeria/v1_3/ArmeriaNetServerAttributesGetter.java +++ b/instrumentation/armeria-1.3/library/src/main/java/io/opentelemetry/instrumentation/armeria/v1_3/ArmeriaNetServerAttributesGetter.java @@ -20,13 +20,35 @@ public String transport(RequestContext ctx) { return SemanticAttributes.NetTransportValues.IP_TCP; } + @Nullable + @Override + public String hostName(RequestContext ctx) { + return null; + } + + @Nullable + @Override + public Integer hostPort(RequestContext ctx) { + return null; + } + @Override @Nullable - public InetSocketAddress getAddress(RequestContext ctx) { + protected InetSocketAddress getPeerSocketAddress(RequestContext ctx) { SocketAddress address = ctx.remoteAddress(); if (address instanceof InetSocketAddress) { return (InetSocketAddress) address; } return null; } + + @Nullable + @Override + protected InetSocketAddress getHostSocketAddress(RequestContext ctx) { + SocketAddress address = ctx.localAddress(); + if (address instanceof InetSocketAddress) { + return (InetSocketAddress) address; + } + return null; + } } diff --git a/instrumentation/armeria-1.3/library/src/main/java/io/opentelemetry/instrumentation/armeria/v1_3/internal/ArmeriaNetClientAttributesGetter.java b/instrumentation/armeria-1.3/library/src/main/java/io/opentelemetry/instrumentation/armeria/v1_3/internal/ArmeriaNetClientAttributesGetter.java index 1e246b5af1c2..369770d93acc 100644 --- a/instrumentation/armeria-1.3/library/src/main/java/io/opentelemetry/instrumentation/armeria/v1_3/internal/ArmeriaNetClientAttributesGetter.java +++ b/instrumentation/armeria-1.3/library/src/main/java/io/opentelemetry/instrumentation/armeria/v1_3/internal/ArmeriaNetClientAttributesGetter.java @@ -5,6 +5,7 @@ package io.opentelemetry.instrumentation.armeria.v1_3.internal; +import com.linecorp.armeria.common.HttpRequest; import com.linecorp.armeria.common.RequestContext; import com.linecorp.armeria.common.logging.RequestLog; import io.opentelemetry.instrumentation.api.instrumenter.net.InetSocketAddressNetClientAttributesGetter; @@ -25,13 +26,34 @@ public String transport(RequestContext ctx, @Nullable RequestLog requestLog) { return SemanticAttributes.NetTransportValues.IP_TCP; } + @Nullable + @Override + public String peerName(RequestContext ctx, @Nullable RequestLog requestLog) { + return request(ctx).uri().getHost(); + } + + @Override + public Integer peerPort(RequestContext ctx, @Nullable RequestLog requestLog) { + return request(ctx).uri().getPort(); + } + @Override @Nullable - public InetSocketAddress getAddress(RequestContext ctx, @Nullable RequestLog requestLog) { + protected InetSocketAddress getPeerSocketAddress( + RequestContext ctx, @Nullable RequestLog requestLog) { SocketAddress address = ctx.remoteAddress(); if (address instanceof InetSocketAddress) { return (InetSocketAddress) address; } return null; } + + private static HttpRequest request(RequestContext ctx) { + HttpRequest request = ctx.request(); + if (request == null) { + throw new IllegalStateException( + "Context always has a request in decorators, this exception indicates a programming bug."); + } + return request; + } } diff --git a/instrumentation/async-http-client/async-http-client-2.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/asynchttpclient/v2_0/AsyncHttpClientNetAttributesGetter.java b/instrumentation/async-http-client/async-http-client-2.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/asynchttpclient/v2_0/AsyncHttpClientNetAttributesGetter.java index 3f720975e7bb..93692c417a2b 100644 --- a/instrumentation/async-http-client/async-http-client-2.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/asynchttpclient/v2_0/AsyncHttpClientNetAttributesGetter.java +++ b/instrumentation/async-http-client/async-http-client-2.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/asynchttpclient/v2_0/AsyncHttpClientNetAttributesGetter.java @@ -19,9 +19,21 @@ public String transport(RequestContext request, @Nullable Response response) { return SemanticAttributes.NetTransportValues.IP_TCP; } + @Nullable + @Override + public String peerName(RequestContext requestContext, @Nullable Response response) { + return requestContext.getRequest().getUri().getHost(); + } + + @Override + public Integer peerPort(RequestContext requestContext, @Nullable Response response) { + return requestContext.getRequest().getUri().getPort(); + } + @Override @Nullable - public InetSocketAddress getAddress(RequestContext request, @Nullable Response response) { + protected InetSocketAddress getPeerSocketAddress( + RequestContext request, @Nullable Response response) { if (response != null && response.getRemoteAddress() instanceof InetSocketAddress) { return (InetSocketAddress) response.getRemoteAddress(); } diff --git a/instrumentation/cassandra/cassandra-3.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/cassandra/v3_0/CassandraNetAttributesGetter.java b/instrumentation/cassandra/cassandra-3.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/cassandra/v3_0/CassandraNetAttributesGetter.java index c8b625e588c9..bce740dc9e09 100644 --- a/instrumentation/cassandra/cassandra-3.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/cassandra/v3_0/CassandraNetAttributesGetter.java +++ b/instrumentation/cassandra/cassandra-3.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/cassandra/v3_0/CassandraNetAttributesGetter.java @@ -19,9 +19,21 @@ public String transport(CassandraRequest request, @Nullable ExecutionInfo execut return null; } + @Nullable + @Override + public String peerName(CassandraRequest request, @Nullable ExecutionInfo executionInfo) { + return null; + } + + @Nullable + @Override + public Integer peerPort(CassandraRequest request, @Nullable ExecutionInfo executionInfo) { + return null; + } + @Override @Nullable - public InetSocketAddress getAddress( + protected InetSocketAddress getPeerSocketAddress( CassandraRequest request, @Nullable ExecutionInfo executionInfo) { return executionInfo == null ? null : executionInfo.getQueriedHost().getSocketAddress(); } diff --git a/instrumentation/cassandra/cassandra-3.0/javaagent/src/test/groovy/CassandraClientTest.groovy b/instrumentation/cassandra/cassandra-3.0/javaagent/src/test/groovy/CassandraClientTest.groovy index 6a94fa8b3de2..858d7c9c79be 100644 --- a/instrumentation/cassandra/cassandra-3.0/javaagent/src/test/groovy/CassandraClientTest.groovy +++ b/instrumentation/cassandra/cassandra-3.0/javaagent/src/test/groovy/CassandraClientTest.groovy @@ -147,9 +147,9 @@ class CassandraClientTest extends AgentInstrumentationSpecification { childOf((SpanData) parentSpan) } attributes { - "$SemanticAttributes.NET_PEER_NAME" "localhost" - "$SemanticAttributes.NET_PEER_PORT" cassandraPort "net.sock.peer.addr" "127.0.0.1" + "net.sock.peer.name" "localhost" + "net.sock.peer.port" cassandraPort "$SemanticAttributes.DB_SYSTEM" "cassandra" "$SemanticAttributes.DB_NAME" keyspace "$SemanticAttributes.DB_STATEMENT" statement diff --git a/instrumentation/cassandra/cassandra-4.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/cassandra/v4_0/CassandraNetAttributesGetter.java b/instrumentation/cassandra/cassandra-4.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/cassandra/v4_0/CassandraNetAttributesGetter.java index cb58a2a6d434..c4bb411ea0fe 100644 --- a/instrumentation/cassandra/cassandra-4.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/cassandra/v4_0/CassandraNetAttributesGetter.java +++ b/instrumentation/cassandra/cassandra-4.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/cassandra/v4_0/CassandraNetAttributesGetter.java @@ -21,9 +21,21 @@ public String transport(CassandraRequest request, @Nullable ExecutionInfo execut return null; } + @Nullable + @Override + public String peerName(CassandraRequest request, @Nullable ExecutionInfo executionInfo) { + return null; + } + + @Nullable + @Override + public Integer peerPort(CassandraRequest request, @Nullable ExecutionInfo executionInfo) { + return null; + } + @Override @Nullable - public InetSocketAddress getAddress( + protected InetSocketAddress getPeerSocketAddress( CassandraRequest request, @Nullable ExecutionInfo executionInfo) { if (executionInfo == null) { return null; diff --git a/instrumentation/cassandra/cassandra-4.0/javaagent/src/test/groovy/CassandraClientTest.groovy b/instrumentation/cassandra/cassandra-4.0/javaagent/src/test/groovy/CassandraClientTest.groovy index 0d5f345082a8..9f615175515d 100644 --- a/instrumentation/cassandra/cassandra-4.0/javaagent/src/test/groovy/CassandraClientTest.groovy +++ b/instrumentation/cassandra/cassandra-4.0/javaagent/src/test/groovy/CassandraClientTest.groovy @@ -120,9 +120,9 @@ class CassandraClientTest extends AgentInstrumentationSpecification { childOf((SpanData) parentSpan) } attributes { - "$SemanticAttributes.NET_PEER_NAME" "localhost" - "$SemanticAttributes.NET_PEER_PORT" cassandraPort "net.sock.peer.addr" "127.0.0.1" + "net.sock.peer.name" "localhost" + "net.sock.peer.port" cassandraPort "$SemanticAttributes.DB_SYSTEM" "cassandra" "$SemanticAttributes.DB_NAME" keyspace "$SemanticAttributes.DB_STATEMENT" statement diff --git a/instrumentation/couchbase/couchbase-2-common/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/couchbase/v2_0/CouchbaseRequestInfo.java b/instrumentation/couchbase/couchbase-2-common/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/couchbase/v2_0/CouchbaseRequestInfo.java index f7e01a33ff5c..992a8512f855 100644 --- a/instrumentation/couchbase/couchbase-2-common/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/couchbase/v2_0/CouchbaseRequestInfo.java +++ b/instrumentation/couchbase/couchbase-2-common/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/couchbase/v2_0/CouchbaseRequestInfo.java @@ -11,6 +11,7 @@ import io.opentelemetry.context.Context; import io.opentelemetry.context.ContextKey; import io.opentelemetry.instrumentation.api.db.SqlStatementInfo; +import java.net.SocketAddress; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; import javax.annotation.Nullable; @@ -29,10 +30,9 @@ protected Map computeValue(Class type) { } }; - private String peerName; - private Integer peerPort; private String localAddress; private String operationId; + private SocketAddress peerAddress; public static CouchbaseRequestInfo create( @Nullable String bucket, Class declaringClass, String methodName) { @@ -76,24 +76,6 @@ public static CouchbaseRequestInfo get(Context context) { public abstract boolean isMethodCall(); - @Nullable - public String getPeerName() { - return peerName; - } - - public void setPeerName(String peerName) { - this.peerName = peerName; - } - - @Nullable - public Integer getPeerPort() { - return peerPort; - } - - public void setPeerPort(Integer peerPort) { - this.peerPort = peerPort; - } - @Nullable public String getLocalAddress() { return localAddress; @@ -111,4 +93,13 @@ public String getOperationId() { public void setOperationId(String operationId) { this.operationId = operationId; } + + @Nullable + public SocketAddress getPeerAddress() { + return peerAddress; + } + + public void setPeerAddress(SocketAddress peerAddress) { + this.peerAddress = peerAddress; + } } diff --git a/instrumentation/couchbase/couchbase-2.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/couchbase/v2_0/CouchbaseNetAttributesGetter.java b/instrumentation/couchbase/couchbase-2.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/couchbase/v2_0/CouchbaseNetAttributesGetter.java index 582b8eeb2083..756ffb2c5470 100644 --- a/instrumentation/couchbase/couchbase-2.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/couchbase/v2_0/CouchbaseNetAttributesGetter.java +++ b/instrumentation/couchbase/couchbase-2.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/couchbase/v2_0/CouchbaseNetAttributesGetter.java @@ -5,16 +5,18 @@ package io.opentelemetry.javaagent.instrumentation.couchbase.v2_0; -import io.opentelemetry.instrumentation.api.instrumenter.net.NetClientAttributesGetter; +import io.opentelemetry.instrumentation.api.instrumenter.net.InetSocketAddressNetClientAttributesGetter; import io.opentelemetry.semconv.trace.attributes.SemanticAttributes; +import java.net.InetSocketAddress; +import java.net.SocketAddress; import javax.annotation.Nullable; public class CouchbaseNetAttributesGetter - implements NetClientAttributesGetter { + extends InetSocketAddressNetClientAttributesGetter { @Nullable @Override public String transport(CouchbaseRequestInfo couchbaseRequest, @Nullable Void unused) { - return couchbaseRequest.getPeerName() != null + return couchbaseRequest.getPeerAddress() != null ? SemanticAttributes.NetTransportValues.IP_TCP : null; } @@ -22,12 +24,23 @@ public String transport(CouchbaseRequestInfo couchbaseRequest, @Nullable Void un @Nullable @Override public String peerName(CouchbaseRequestInfo couchbaseRequest, @Nullable Void unused) { - return couchbaseRequest.getPeerName(); + return null; } @Nullable @Override public Integer peerPort(CouchbaseRequestInfo couchbaseRequest, @Nullable Void unused) { - return couchbaseRequest.getPeerPort(); + return null; + } + + @Nullable + @Override + protected InetSocketAddress getPeerSocketAddress( + CouchbaseRequestInfo couchbaseRequest, @Nullable Void unused) { + SocketAddress peerAddress = couchbaseRequest.getPeerAddress(); + if (peerAddress instanceof InetSocketAddress) { + return (InetSocketAddress) peerAddress; + } + return null; } } diff --git a/instrumentation/couchbase/couchbase-2.6/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/couchbase/v2_6/CouchbaseNetworkInstrumentation.java b/instrumentation/couchbase/couchbase-2.6/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/couchbase/v2_6/CouchbaseNetworkInstrumentation.java index 697403a14bb5..4794b9fcaf2a 100644 --- a/instrumentation/couchbase/couchbase-2.6/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/couchbase/v2_6/CouchbaseNetworkInstrumentation.java +++ b/instrumentation/couchbase/couchbase-2.6/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/couchbase/v2_6/CouchbaseNetworkInstrumentation.java @@ -11,6 +11,7 @@ import static net.bytebuddy.matcher.ElementMatchers.takesArguments; import com.couchbase.client.core.message.CouchbaseRequest; +import com.couchbase.client.deps.io.netty.channel.ChannelHandlerContext; import io.opentelemetry.instrumentation.api.util.VirtualField; import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation; import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer; @@ -46,26 +47,16 @@ public static class CouchbaseNetworkAdvice { @Advice.OnMethodEnter(suppress = Throwable.class) public static void addNetworkTagsToSpan( - @Advice.FieldValue("remoteHostname") String remoteHostname, - @Advice.FieldValue("remoteSocket") String remoteSocket, @Advice.FieldValue("localSocket") String localSocket, + @Advice.Argument(0) ChannelHandlerContext channelHandlerContext, @Advice.Argument(1) CouchbaseRequest request) { + VirtualField virtualField = VirtualField.find(CouchbaseRequest.class, CouchbaseRequestInfo.class); CouchbaseRequestInfo requestInfo = virtualField.get(request); if (requestInfo != null) { - if (remoteHostname != null) { - requestInfo.setPeerName(remoteHostname); - } - - if (remoteSocket != null) { - int splitIndex = remoteSocket.lastIndexOf(":"); - if (splitIndex != -1) { - requestInfo.setPeerPort(Integer.parseInt(remoteSocket.substring(splitIndex + 1))); - } - } - + requestInfo.setPeerAddress(channelHandlerContext.channel().remoteAddress()); requestInfo.setLocalAddress(localSocket); } } diff --git a/instrumentation/couchbase/couchbase-2.6/javaagent/src/test/groovy/CouchbaseSpanUtil.groovy b/instrumentation/couchbase/couchbase-2.6/javaagent/src/test/groovy/CouchbaseSpanUtil.groovy index 6cc47e47bc76..7ffd00ca489a 100644 --- a/instrumentation/couchbase/couchbase-2.6/javaagent/src/test/groovy/CouchbaseSpanUtil.groovy +++ b/instrumentation/couchbase/couchbase-2.6/javaagent/src/test/groovy/CouchbaseSpanUtil.groovy @@ -36,8 +36,9 @@ class CouchbaseSpanUtil { "$SemanticAttributes.NET_TRANSPORT" { it == null || it == IP_TCP } // Because of caching, not all requests hit the server so these attributes may be absent - "$SemanticAttributes.NET_PEER_NAME" { it == "localhost" || it == "127.0.0.1" || it == null } - "$SemanticAttributes.NET_PEER_PORT" { it == null || Number } + "net.sock.peer.addr" { it == "127.0.0.1" || it == null } + "net.sock.peer.name" { it == "localhost" || it == null } + "net.sock.peer.port" { it == null || Number } // Because of caching, not all requests hit the server so this tag may be absent "couchbase.local.address" { it == null || String } diff --git a/instrumentation/dropwizard/dropwizard-testing/src/test/groovy/DropwizardTest.groovy b/instrumentation/dropwizard/dropwizard-testing/src/test/groovy/DropwizardTest.groovy index b2d7e924ae70..a121252656d3 100644 --- a/instrumentation/dropwizard/dropwizard-testing/src/test/groovy/DropwizardTest.groovy +++ b/instrumentation/dropwizard/dropwizard-testing/src/test/groovy/DropwizardTest.groovy @@ -63,7 +63,7 @@ class DropwizardTest extends HttpServerTest implements Ag // this override is needed because dropwizard reports peer ip as the client ip @Override - String peerIp(ServerEndpoint endpoint) { + String sockPeerAddr(ServerEndpoint endpoint) { TEST_CLIENT_IP } diff --git a/instrumentation/elasticsearch/elasticsearch-transport-5.0/javaagent/src/test/groovy/Elasticsearch5TransportClientTest.groovy b/instrumentation/elasticsearch/elasticsearch-transport-5.0/javaagent/src/test/groovy/Elasticsearch5TransportClientTest.groovy index 90f1a1dec413..3c8379d6c80e 100644 --- a/instrumentation/elasticsearch/elasticsearch-transport-5.0/javaagent/src/test/groovy/Elasticsearch5TransportClientTest.groovy +++ b/instrumentation/elasticsearch/elasticsearch-transport-5.0/javaagent/src/test/groovy/Elasticsearch5TransportClientTest.groovy @@ -125,10 +125,10 @@ class Elasticsearch5TransportClientTest extends AbstractElasticsearchTransportCl name "ClusterHealthAction" kind CLIENT attributes { - "$SemanticAttributes.NET_PEER_NAME" tcpPublishAddress.address - "$SemanticAttributes.NET_PEER_PORT" tcpPublishAddress.port - "net.sock.peer.addr" tcpPublishAddress.host != tcpPublishAddress.address ? tcpPublishAddress.host : null "net.sock.family" { it == "inet6" || it == null } + "net.sock.peer.addr" tcpPublishAddress.address + "net.sock.peer.name" tcpPublishAddress.host + "net.sock.peer.port" tcpPublishAddress.port "$SemanticAttributes.DB_SYSTEM" "elasticsearch" "$SemanticAttributes.DB_OPERATION" "ClusterHealthAction" "elasticsearch.action" "ClusterHealthAction" @@ -243,10 +243,10 @@ class Elasticsearch5TransportClientTest extends AbstractElasticsearchTransportCl name "CreateIndexAction" kind CLIENT attributes { - "$SemanticAttributes.NET_PEER_NAME" tcpPublishAddress.address - "$SemanticAttributes.NET_PEER_PORT" tcpPublishAddress.port - "net.sock.peer.addr" tcpPublishAddress.host != tcpPublishAddress.address ? tcpPublishAddress.host : null "net.sock.family" { it == "inet6" || it == null } + "net.sock.peer.addr" tcpPublishAddress.address + "net.sock.peer.name" tcpPublishAddress.host + "net.sock.peer.port" tcpPublishAddress.port "$SemanticAttributes.DB_SYSTEM" "elasticsearch" "$SemanticAttributes.DB_OPERATION" "CreateIndexAction" "elasticsearch.action" "CreateIndexAction" @@ -260,10 +260,10 @@ class Elasticsearch5TransportClientTest extends AbstractElasticsearchTransportCl name "GetAction" kind CLIENT attributes { - "$SemanticAttributes.NET_PEER_NAME" tcpPublishAddress.address - "$SemanticAttributes.NET_PEER_PORT" tcpPublishAddress.port - "net.sock.peer.addr" tcpPublishAddress.host != tcpPublishAddress.address ? tcpPublishAddress.host : null "net.sock.family" { it == "inet6" || it == null } + "net.sock.peer.addr" tcpPublishAddress.address + "net.sock.peer.name" tcpPublishAddress.host + "net.sock.peer.port" tcpPublishAddress.port "$SemanticAttributes.DB_SYSTEM" "elasticsearch" "$SemanticAttributes.DB_OPERATION" "GetAction" "elasticsearch.action" "GetAction" @@ -292,10 +292,10 @@ class Elasticsearch5TransportClientTest extends AbstractElasticsearchTransportCl name "IndexAction" kind CLIENT attributes { - "$SemanticAttributes.NET_PEER_NAME" tcpPublishAddress.address - "$SemanticAttributes.NET_PEER_PORT" tcpPublishAddress.port - "net.sock.peer.addr" tcpPublishAddress.host != tcpPublishAddress.address ? tcpPublishAddress.host : null "net.sock.family" { it == "inet6" || it == null } + "net.sock.peer.addr" tcpPublishAddress.address + "net.sock.peer.name" tcpPublishAddress.host + "net.sock.peer.port" tcpPublishAddress.port "$SemanticAttributes.DB_SYSTEM" "elasticsearch" "$SemanticAttributes.DB_OPERATION" "IndexAction" "elasticsearch.action" "IndexAction" @@ -314,10 +314,10 @@ class Elasticsearch5TransportClientTest extends AbstractElasticsearchTransportCl name "GetAction" kind CLIENT attributes { - "$SemanticAttributes.NET_PEER_NAME" tcpPublishAddress.address - "$SemanticAttributes.NET_PEER_PORT" tcpPublishAddress.port - "net.sock.peer.addr" tcpPublishAddress.host != tcpPublishAddress.address ? tcpPublishAddress.address : null "net.sock.family" { it == "inet6" || it == null } + "net.sock.peer.addr" tcpPublishAddress.address + "net.sock.peer.name" tcpPublishAddress.host + "net.sock.peer.port" tcpPublishAddress.port "$SemanticAttributes.DB_SYSTEM" "elasticsearch" "$SemanticAttributes.DB_OPERATION" "GetAction" "elasticsearch.action" "GetAction" diff --git a/instrumentation/elasticsearch/elasticsearch-transport-5.3/javaagent/src/test/groovy/Elasticsearch53TransportClientTest.groovy b/instrumentation/elasticsearch/elasticsearch-transport-5.3/javaagent/src/test/groovy/Elasticsearch53TransportClientTest.groovy index 6f065708bf77..01ee7567e798 100644 --- a/instrumentation/elasticsearch/elasticsearch-transport-5.3/javaagent/src/test/groovy/Elasticsearch53TransportClientTest.groovy +++ b/instrumentation/elasticsearch/elasticsearch-transport-5.3/javaagent/src/test/groovy/Elasticsearch53TransportClientTest.groovy @@ -131,10 +131,10 @@ class Elasticsearch53TransportClientTest extends AbstractElasticsearchTransportC kind CLIENT childOf(span(0)) attributes { - "$SemanticAttributes.NET_PEER_NAME" tcpPublishAddress.address - "$SemanticAttributes.NET_PEER_PORT" tcpPublishAddress.port - "net.sock.peer.addr" tcpPublishAddress.host != tcpPublishAddress.address ? tcpPublishAddress.address : null "net.sock.family" { it == "inet6" || it == null } + "net.sock.peer.addr" tcpPublishAddress.address + "net.sock.peer.name" tcpPublishAddress.host + "net.sock.peer.port" tcpPublishAddress.port "$SemanticAttributes.DB_SYSTEM" "elasticsearch" "$SemanticAttributes.DB_OPERATION" "ClusterHealthAction" "elasticsearch.action" "ClusterHealthAction" @@ -248,10 +248,10 @@ class Elasticsearch53TransportClientTest extends AbstractElasticsearchTransportC name "CreateIndexAction" kind CLIENT attributes { - "$SemanticAttributes.NET_PEER_NAME" tcpPublishAddress.address - "$SemanticAttributes.NET_PEER_PORT" tcpPublishAddress.port - "net.sock.peer.addr" tcpPublishAddress.host != tcpPublishAddress.address ? tcpPublishAddress.address : null "net.sock.family" { it == "inet6" || it == null } + "net.sock.peer.addr" tcpPublishAddress.address + "net.sock.peer.name" tcpPublishAddress.host + "net.sock.peer.port" tcpPublishAddress.port "$SemanticAttributes.DB_SYSTEM" "elasticsearch" "$SemanticAttributes.DB_OPERATION" "CreateIndexAction" "elasticsearch.action" "CreateIndexAction" @@ -265,10 +265,10 @@ class Elasticsearch53TransportClientTest extends AbstractElasticsearchTransportC name "GetAction" kind CLIENT attributes { - "$SemanticAttributes.NET_PEER_NAME" tcpPublishAddress.address - "$SemanticAttributes.NET_PEER_PORT" tcpPublishAddress.port - "net.sock.peer.addr" tcpPublishAddress.host != tcpPublishAddress.address ? tcpPublishAddress.address : null "net.sock.family" { it == "inet6" || it == null } + "net.sock.peer.addr" tcpPublishAddress.address + "net.sock.peer.name" tcpPublishAddress.host + "net.sock.peer.port" tcpPublishAddress.port "$SemanticAttributes.DB_SYSTEM" "elasticsearch" "$SemanticAttributes.DB_OPERATION" "GetAction" "elasticsearch.action" "GetAction" @@ -297,10 +297,10 @@ class Elasticsearch53TransportClientTest extends AbstractElasticsearchTransportC name "IndexAction" kind CLIENT attributes { - "$SemanticAttributes.NET_PEER_NAME" tcpPublishAddress.address - "$SemanticAttributes.NET_PEER_PORT" tcpPublishAddress.port - "net.sock.peer.addr" tcpPublishAddress.host != tcpPublishAddress.address ? tcpPublishAddress.address : null "net.sock.family" { it == "inet6" || it == null } + "net.sock.peer.addr" tcpPublishAddress.address + "net.sock.peer.name" tcpPublishAddress.host + "net.sock.peer.port" tcpPublishAddress.port "$SemanticAttributes.DB_SYSTEM" "elasticsearch" "$SemanticAttributes.DB_OPERATION" "IndexAction" "elasticsearch.action" "IndexAction" @@ -320,10 +320,10 @@ class Elasticsearch53TransportClientTest extends AbstractElasticsearchTransportC name "GetAction" kind CLIENT attributes { - "$SemanticAttributes.NET_PEER_NAME" tcpPublishAddress.address - "$SemanticAttributes.NET_PEER_PORT" tcpPublishAddress.port - "net.sock.peer.addr" tcpPublishAddress.host != tcpPublishAddress.address ? tcpPublishAddress.address : null "net.sock.family" { it == "inet6" || it == null } + "net.sock.peer.addr" tcpPublishAddress.address + "net.sock.peer.name" tcpPublishAddress.host + "net.sock.peer.port" tcpPublishAddress.port "$SemanticAttributes.DB_SYSTEM" "elasticsearch" "$SemanticAttributes.DB_OPERATION" "GetAction" "elasticsearch.action" "GetAction" diff --git a/instrumentation/elasticsearch/elasticsearch-transport-6.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/elasticsearch/transport/v6_0/Elasticsearch6TransportNetAttributesGetter.java b/instrumentation/elasticsearch/elasticsearch-transport-6.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/elasticsearch/transport/v6_0/Elasticsearch6TransportNetAttributesGetter.java index 5374fe27fbab..00f971d61b61 100644 --- a/instrumentation/elasticsearch/elasticsearch-transport-6.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/elasticsearch/transport/v6_0/Elasticsearch6TransportNetAttributesGetter.java +++ b/instrumentation/elasticsearch/elasticsearch-transport-6.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/elasticsearch/transport/v6_0/Elasticsearch6TransportNetAttributesGetter.java @@ -19,9 +19,21 @@ public String transport(ElasticTransportRequest request, @Nullable ActionRespons return null; } + @Nullable + @Override + public String peerName(ElasticTransportRequest request, @Nullable ActionResponse response) { + return null; + } + + @Nullable + @Override + public Integer peerPort(ElasticTransportRequest request, @Nullable ActionResponse response) { + return null; + } + @Override @Nullable - public InetSocketAddress getAddress( + protected InetSocketAddress getPeerSocketAddress( ElasticTransportRequest request, @Nullable ActionResponse response) { if (response != null && response.remoteAddress() != null) { return response.remoteAddress().address(); diff --git a/instrumentation/elasticsearch/elasticsearch-transport-6.0/javaagent/src/test/groovy/Elasticsearch6TransportClientTest.groovy b/instrumentation/elasticsearch/elasticsearch-transport-6.0/javaagent/src/test/groovy/Elasticsearch6TransportClientTest.groovy index 9065777ad651..32c24833e8ce 100644 --- a/instrumentation/elasticsearch/elasticsearch-transport-6.0/javaagent/src/test/groovy/Elasticsearch6TransportClientTest.groovy +++ b/instrumentation/elasticsearch/elasticsearch-transport-6.0/javaagent/src/test/groovy/Elasticsearch6TransportClientTest.groovy @@ -105,8 +105,10 @@ class Elasticsearch6TransportClientTest extends AbstractElasticsearchTransportCl kind CLIENT childOf(span(0)) attributes { - "$SemanticAttributes.NET_PEER_NAME" tcpPublishAddress.address - "$SemanticAttributes.NET_PEER_PORT" tcpPublishAddress.port + "net.sock.family" { it == "inet6" || it == null } + "net.sock.peer.addr" tcpPublishAddress.address + "net.sock.peer.name" tcpPublishAddress.address().hostString + "net.sock.peer.port" tcpPublishAddress.port "$SemanticAttributes.DB_SYSTEM" "elasticsearch" "$SemanticAttributes.DB_OPERATION" "ClusterHealthAction" "elasticsearch.action" "ClusterHealthAction" @@ -223,8 +225,10 @@ class Elasticsearch6TransportClientTest extends AbstractElasticsearchTransportCl name "CreateIndexAction" kind CLIENT attributes { - "$SemanticAttributes.NET_PEER_NAME" tcpPublishAddress.address - "$SemanticAttributes.NET_PEER_PORT" tcpPublishAddress.port + "net.sock.family" { it == "inet6" || it == null } + "net.sock.peer.addr" tcpPublishAddress.address + "net.sock.peer.name" tcpPublishAddress.address().hostString + "net.sock.peer.port" tcpPublishAddress.port "$SemanticAttributes.DB_SYSTEM" "elasticsearch" "$SemanticAttributes.DB_OPERATION" "CreateIndexAction" "elasticsearch.action" "CreateIndexAction" @@ -238,8 +242,10 @@ class Elasticsearch6TransportClientTest extends AbstractElasticsearchTransportCl name "GetAction" kind CLIENT attributes { - "$SemanticAttributes.NET_PEER_NAME" tcpPublishAddress.address - "$SemanticAttributes.NET_PEER_PORT" tcpPublishAddress.port + "net.sock.family" { it == "inet6" || it == null } + "net.sock.peer.addr" tcpPublishAddress.address + "net.sock.peer.name" tcpPublishAddress.address().hostString + "net.sock.peer.port" tcpPublishAddress.port "$SemanticAttributes.DB_SYSTEM" "elasticsearch" "$SemanticAttributes.DB_OPERATION" "GetAction" "elasticsearch.action" "GetAction" @@ -268,8 +274,10 @@ class Elasticsearch6TransportClientTest extends AbstractElasticsearchTransportCl name "IndexAction" kind CLIENT attributes { - "$SemanticAttributes.NET_PEER_NAME" tcpPublishAddress.address - "$SemanticAttributes.NET_PEER_PORT" tcpPublishAddress.port + "net.sock.family" { it == "inet6" || it == null } + "net.sock.peer.addr" tcpPublishAddress.address + "net.sock.peer.name" tcpPublishAddress.address().hostString + "net.sock.peer.port" tcpPublishAddress.port "$SemanticAttributes.DB_SYSTEM" "elasticsearch" "$SemanticAttributes.DB_OPERATION" "IndexAction" "elasticsearch.action" "IndexAction" @@ -289,8 +297,10 @@ class Elasticsearch6TransportClientTest extends AbstractElasticsearchTransportCl name "GetAction" kind CLIENT attributes { - "$SemanticAttributes.NET_PEER_NAME" tcpPublishAddress.address - "$SemanticAttributes.NET_PEER_PORT" tcpPublishAddress.port + "net.sock.family" { it == "inet6" || it == null } + "net.sock.peer.addr" tcpPublishAddress.address + "net.sock.peer.name" tcpPublishAddress.address().hostString + "net.sock.peer.port" tcpPublishAddress.port "$SemanticAttributes.DB_SYSTEM" "elasticsearch" "$SemanticAttributes.DB_OPERATION" "GetAction" "elasticsearch.action" "GetAction" diff --git a/instrumentation/elasticsearch/elasticsearch-transport-common/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/elasticsearch/transport/ElasticTransportNetResponseAttributesGetter.java b/instrumentation/elasticsearch/elasticsearch-transport-common/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/elasticsearch/transport/ElasticTransportNetResponseAttributesGetter.java index 8203698d4a7a..bd4b7fe13fb1 100644 --- a/instrumentation/elasticsearch/elasticsearch-transport-common/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/elasticsearch/transport/ElasticTransportNetResponseAttributesGetter.java +++ b/instrumentation/elasticsearch/elasticsearch-transport-common/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/elasticsearch/transport/ElasticTransportNetResponseAttributesGetter.java @@ -21,18 +21,12 @@ public String transport(ElasticTransportRequest request, @Nullable ActionRespons @Override @Nullable public String peerName(ElasticTransportRequest request, @Nullable ActionResponse response) { - if (response != null && response.remoteAddress() != null) { - return response.remoteAddress().getHost(); - } return null; } @Override @Nullable public Integer peerPort(ElasticTransportRequest request, @Nullable ActionResponse response) { - if (response != null && response.remoteAddress() != null) { - return response.remoteAddress().getPort(); - } return null; } @@ -45,5 +39,21 @@ public String sockPeerAddr(ElasticTransportRequest request, @Nullable ActionResp return null; } - // TODO (trask) implement sockFamily + @Nullable + @Override + public String sockPeerName(ElasticTransportRequest request, @Nullable ActionResponse response) { + if (response != null && response.remoteAddress() != null) { + return response.remoteAddress().getHost(); + } + return null; + } + + @Nullable + @Override + public Integer sockPeerPort(ElasticTransportRequest request, @Nullable ActionResponse response) { + if (response != null && response.remoteAddress() != null) { + return response.remoteAddress().getPort(); + } + return null; + } } diff --git a/instrumentation/google-http-client-1.19/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/googlehttpclient/GoogleHttpClientNetAttributesGetter.java b/instrumentation/google-http-client-1.19/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/googlehttpclient/GoogleHttpClientNetAttributesGetter.java index b9decdb47878..701b6db144de 100644 --- a/instrumentation/google-http-client-1.19/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/googlehttpclient/GoogleHttpClientNetAttributesGetter.java +++ b/instrumentation/google-http-client-1.19/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/googlehttpclient/GoogleHttpClientNetAttributesGetter.java @@ -27,10 +27,6 @@ public String peerName(HttpRequest request, @Nullable HttpResponse response) { @Override public Integer peerPort(HttpRequest request, @Nullable HttpResponse response) { - int port = request.getUrl().getPort(); - if (port != -1) { - return port; - } - return null; + return request.getUrl().getPort(); } } diff --git a/instrumentation/grizzly-2.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/grizzly/GrizzlyNetAttributesGetter.java b/instrumentation/grizzly-2.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/grizzly/GrizzlyNetAttributesGetter.java index 8fe5900bc087..586775ff664a 100644 --- a/instrumentation/grizzly-2.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/grizzly/GrizzlyNetAttributesGetter.java +++ b/instrumentation/grizzly-2.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/grizzly/GrizzlyNetAttributesGetter.java @@ -5,21 +5,30 @@ package io.opentelemetry.javaagent.instrumentation.grizzly; +import static io.opentelemetry.semconv.trace.attributes.SemanticAttributes.NetTransportValues.IP_TCP; +import static io.opentelemetry.semconv.trace.attributes.SemanticAttributes.NetTransportValues.IP_UDP; + import io.opentelemetry.instrumentation.api.instrumenter.net.NetServerAttributesGetter; import javax.annotation.Nullable; import org.glassfish.grizzly.http.HttpRequestPacket; +import org.glassfish.grizzly.nio.transport.TCPNIOTransport; final class GrizzlyNetAttributesGetter implements NetServerAttributesGetter { - @Nullable @Override public String transport(HttpRequestPacket request) { - return null; + return request.getConnection().getTransport() instanceof TCPNIOTransport ? IP_TCP : IP_UDP; } + @Nullable @Override - public Integer sockPeerPort(HttpRequestPacket request) { - return request.getRemotePort(); + public String hostName(HttpRequestPacket request) { + return request.getLocalHost(); + } + + @Override + public Integer hostPort(HttpRequestPacket request) { + return request.getServerPort(); } @Nullable @@ -27,4 +36,20 @@ public Integer sockPeerPort(HttpRequestPacket request) { public String sockPeerAddr(HttpRequestPacket request) { return request.getRemoteAddress(); } + + @Override + public Integer sockPeerPort(HttpRequestPacket request) { + return request.getRemotePort(); + } + + @Nullable + @Override + public String sockHostAddr(HttpRequestPacket request) { + return request.getLocalAddress(); + } + + @Override + public Integer sockHostPort(HttpRequestPacket request) { + return request.getLocalPort(); + } } diff --git a/instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/GrpcRequest.java b/instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/GrpcRequest.java index dd1a49936a73..cd429d22e850 100644 --- a/instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/GrpcRequest.java +++ b/instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/GrpcRequest.java @@ -8,6 +8,7 @@ import io.grpc.Metadata; import io.grpc.MethodDescriptor; import java.net.SocketAddress; +import java.net.URI; import javax.annotation.Nullable; public final class GrpcRequest { @@ -16,15 +17,32 @@ public final class GrpcRequest { @Nullable private volatile Metadata metadata; - @Nullable private volatile SocketAddress remoteAddress; + @Nullable private volatile String logicalHost; + private volatile int logicalPort = -1; + @Nullable private volatile SocketAddress peerSocketAddress; GrpcRequest( MethodDescriptor method, @Nullable Metadata metadata, - @Nullable SocketAddress remoteAddress) { + @Nullable SocketAddress peerSocketAddress, + @Nullable String authority) { this.method = method; this.metadata = metadata; - this.remoteAddress = remoteAddress; + this.peerSocketAddress = peerSocketAddress; + setLogicalAddress(authority); + } + + private void setLogicalAddress(@Nullable String authority) { + if (authority == null) { + return; + } + try { + URI uri = new URI(null, authority, null, null, null); + logicalHost = uri.getHost(); + logicalPort = uri.getPort(); + } catch (Throwable e) { + // do nothing + } } public MethodDescriptor getMethod() { @@ -41,11 +59,20 @@ void setMetadata(Metadata metadata) { } @Nullable - public SocketAddress getRemoteAddress() { - return remoteAddress; + public String getLogicalHost() { + return logicalHost; + } + + public int getLogicalPort() { + return logicalPort; + } + + @Nullable + public SocketAddress getPeerSocketAddress() { + return peerSocketAddress; } - void setRemoteAddress(SocketAddress remoteAddress) { - this.remoteAddress = remoteAddress; + void setPeerSocketAddress(SocketAddress peerSocketAddress) { + this.peerSocketAddress = peerSocketAddress; } } diff --git a/instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/TracingClientInterceptor.java b/instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/TracingClientInterceptor.java index ead17c9a962d..e78b6896b3f3 100644 --- a/instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/TracingClientInterceptor.java +++ b/instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/TracingClientInterceptor.java @@ -11,6 +11,7 @@ import io.grpc.ClientInterceptor; import io.grpc.ForwardingClientCall; import io.grpc.ForwardingClientCallListener; +import io.grpc.Grpc; import io.grpc.Metadata; import io.grpc.MethodDescriptor; import io.grpc.Status; @@ -20,9 +21,6 @@ import io.opentelemetry.context.Scope; import io.opentelemetry.context.propagation.ContextPropagators; import io.opentelemetry.instrumentation.api.instrumenter.Instrumenter; -import java.net.InetSocketAddress; -import java.net.SocketAddress; -import java.net.URI; import java.util.concurrent.atomic.AtomicLongFieldUpdater; final class TracingClientInterceptor implements ClientInterceptor { @@ -43,7 +41,7 @@ final class TracingClientInterceptor implements ClientInterceptor { @Override public ClientCall interceptCall( MethodDescriptor method, CallOptions callOptions, Channel next) { - GrpcRequest request = new GrpcRequest(method, null, null); + GrpcRequest request = new GrpcRequest(method, null, null, next.authority()); Context parentContext = Context.current(); if (!instrumenter.shouldStart(parentContext, request)) { return next.newCall(method, callOptions); @@ -60,14 +58,8 @@ public ClientCall interceptCall( throw e; } } - SocketAddress address = null; - try { - URI uri = new URI(null, next.authority(), null, null, null); - address = InetSocketAddress.createUnresolved(uri.getHost(), uri.getPort()); - } catch (Throwable e) { - // do nothing - } - request.setRemoteAddress(address); + + request.setPeerSocketAddress(result.getAttributes().get(Grpc.TRANSPORT_ATTR_REMOTE_ADDR)); return new TracingClientCall<>(result, parentContext, context, request); } diff --git a/instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/TracingServerInterceptor.java b/instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/TracingServerInterceptor.java index 7aca5ff2779f..bd4175d1f62c 100644 --- a/instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/TracingServerInterceptor.java +++ b/instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/TracingServerInterceptor.java @@ -45,7 +45,8 @@ public ServerCall.Listener interceptCall( new GrpcRequest( call.getMethodDescriptor(), headers, - call.getAttributes().get(Grpc.TRANSPORT_ATTR_REMOTE_ADDR)); + call.getAttributes().get(Grpc.TRANSPORT_ATTR_REMOTE_ADDR), + call.getAuthority()); Context parentContext = Context.current(); if (!instrumenter.shouldStart(parentContext, request)) { return next.startCall(call, headers); diff --git a/instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/internal/GrpcNetClientAttributesGetter.java b/instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/internal/GrpcNetClientAttributesGetter.java index 37b2bb6589b1..0e54e7b52f37 100644 --- a/instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/internal/GrpcNetClientAttributesGetter.java +++ b/instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/internal/GrpcNetClientAttributesGetter.java @@ -19,18 +19,30 @@ */ public final class GrpcNetClientAttributesGetter extends InetSocketAddressNetClientAttributesGetter { + + @Override + public String transport(GrpcRequest request, @Nullable Status response) { + return SemanticAttributes.NetTransportValues.IP_TCP; + } + + @Nullable + @Override + public String peerName(GrpcRequest grpcRequest, @Nullable Status status) { + return grpcRequest.getLogicalHost(); + } + + @Override + public Integer peerPort(GrpcRequest grpcRequest, @Nullable Status status) { + return grpcRequest.getLogicalPort(); + } + @Override @Nullable - public InetSocketAddress getAddress(GrpcRequest request, @Nullable Status response) { - SocketAddress address = request.getRemoteAddress(); + protected InetSocketAddress getPeerSocketAddress(GrpcRequest request, @Nullable Status response) { + SocketAddress address = request.getPeerSocketAddress(); if (address instanceof InetSocketAddress) { return (InetSocketAddress) address; } return null; } - - @Override - public String transport(GrpcRequest request, @Nullable Status response) { - return SemanticAttributes.NetTransportValues.IP_TCP; - } } diff --git a/instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/internal/GrpcNetServerAttributesGetter.java b/instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/internal/GrpcNetServerAttributesGetter.java index f234e221484a..07c5d965a48a 100644 --- a/instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/internal/GrpcNetServerAttributesGetter.java +++ b/instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/internal/GrpcNetServerAttributesGetter.java @@ -18,18 +18,37 @@ */ public final class GrpcNetServerAttributesGetter extends InetSocketAddressNetServerAttributesGetter { + @Override + public String transport(GrpcRequest request) { + return SemanticAttributes.NetTransportValues.IP_TCP; + } + @Nullable - public InetSocketAddress getAddress(GrpcRequest request) { - SocketAddress address = request.getRemoteAddress(); + @Override + public String hostName(GrpcRequest grpcRequest) { + return grpcRequest.getLogicalHost(); + } + + @Override + public Integer hostPort(GrpcRequest grpcRequest) { + return grpcRequest.getLogicalPort(); + } + + @Override + @Nullable + protected InetSocketAddress getPeerSocketAddress(GrpcRequest request) { + SocketAddress address = request.getPeerSocketAddress(); if (address instanceof InetSocketAddress) { return (InetSocketAddress) address; } return null; } + @Nullable @Override - public String transport(GrpcRequest request) { - return SemanticAttributes.NetTransportValues.IP_TCP; + protected InetSocketAddress getHostSocketAddress(GrpcRequest grpcRequest) { + // TODO: later version introduces TRANSPORT_ATTR_LOCAL_ADDR, might be a good idea to use it + return null; } } diff --git a/instrumentation/grpc-1.6/testing/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/AbstractGrpcStreamingTest.java b/instrumentation/grpc-1.6/testing/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/AbstractGrpcStreamingTest.java index c7f194afe059..322364b164c8 100644 --- a/instrumentation/grpc-1.6/testing/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/AbstractGrpcStreamingTest.java +++ b/instrumentation/grpc-1.6/testing/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/AbstractGrpcStreamingTest.java @@ -196,16 +196,18 @@ public void onCompleted() { equalTo(SemanticAttributes.RPC_SYSTEM, "grpc"), equalTo(SemanticAttributes.RPC_SERVICE, "example.Greeter"), equalTo(SemanticAttributes.RPC_METHOD, "Conversation"), - equalTo(AttributeKey.stringKey("net.sock.peer.addr"), "127.0.0.1"), - satisfies( - AttributeKey.longKey("net.sock.peer.port"), - val -> assertThat(val).isNotNull()), + equalTo( + SemanticAttributes.RPC_GRPC_STATUS_CODE, + (long) Status.Code.OK.value()), equalTo( SemanticAttributes.NET_TRANSPORT, SemanticAttributes.NetTransportValues.IP_TCP), - equalTo( - SemanticAttributes.RPC_GRPC_STATUS_CODE, - (long) Status.Code.OK.value())) + equalTo(SemanticAttributes.NET_HOST_NAME, "localhost"), + equalTo(SemanticAttributes.NET_HOST_PORT, server.getPort()), + equalTo(AttributeKey.stringKey("net.sock.peer.addr"), "127.0.0.1"), + satisfies( + AttributeKey.longKey("net.sock.peer.port"), + val -> assertThat(val).isNotNull())) .hasEventsSatisfyingExactly(events.toArray(new Consumer[0])))); testing() .waitAndAssertMetrics( @@ -222,6 +224,8 @@ public void onCompleted() { point -> point.hasAttributesSatisfying( equalTo(SemanticAttributes.NET_TRANSPORT, "ip_tcp"), + equalTo( + SemanticAttributes.NET_HOST_NAME, "localhost"), equalTo( SemanticAttributes.RPC_METHOD, "Conversation"), equalTo( diff --git a/instrumentation/grpc-1.6/testing/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/AbstractGrpcTest.java b/instrumentation/grpc-1.6/testing/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/AbstractGrpcTest.java index 6c48247ac432..18fe6e1ff625 100644 --- a/instrumentation/grpc-1.6/testing/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/AbstractGrpcTest.java +++ b/instrumentation/grpc-1.6/testing/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/AbstractGrpcTest.java @@ -164,16 +164,18 @@ public void sayHello( equalTo(SemanticAttributes.RPC_SYSTEM, "grpc"), equalTo(SemanticAttributes.RPC_SERVICE, "example.Greeter"), equalTo(SemanticAttributes.RPC_METHOD, "SayHello"), - equalTo(AttributeKey.stringKey("net.sock.peer.addr"), "127.0.0.1"), - satisfies( - AttributeKey.longKey("net.sock.peer.port"), - val -> assertThat(val).isNotNull()), + equalTo( + SemanticAttributes.RPC_GRPC_STATUS_CODE, + (long) Status.Code.OK.value()), equalTo( SemanticAttributes.NET_TRANSPORT, SemanticAttributes.NetTransportValues.IP_TCP), - equalTo( - SemanticAttributes.RPC_GRPC_STATUS_CODE, - (long) Status.Code.OK.value())) + equalTo(SemanticAttributes.NET_HOST_NAME, "localhost"), + equalTo(SemanticAttributes.NET_HOST_PORT, server.getPort()), + equalTo(AttributeKey.stringKey("net.sock.peer.addr"), "127.0.0.1"), + satisfies( + AttributeKey.longKey("net.sock.peer.port"), + val -> assertThat(val).isNotNull())) .hasEventsSatisfyingExactly( event -> event @@ -213,6 +215,8 @@ public void sayHello( point -> point.hasAttributesSatisfying( equalTo(SemanticAttributes.NET_TRANSPORT, "ip_tcp"), + equalTo( + SemanticAttributes.NET_HOST_NAME, "localhost"), equalTo(SemanticAttributes.RPC_METHOD, "SayHello"), equalTo( SemanticAttributes.RPC_SERVICE, @@ -348,16 +352,18 @@ public void sayHello( equalTo(SemanticAttributes.RPC_SYSTEM, "grpc"), equalTo(SemanticAttributes.RPC_SERVICE, "example.Greeter"), equalTo(SemanticAttributes.RPC_METHOD, "SayHello"), - equalTo(AttributeKey.stringKey("net.sock.peer.addr"), "127.0.0.1"), - satisfies( - AttributeKey.longKey("net.sock.peer.port"), - val -> assertThat(val).isNotNull()), + equalTo( + SemanticAttributes.RPC_GRPC_STATUS_CODE, + (long) Status.Code.OK.value()), equalTo( SemanticAttributes.NET_TRANSPORT, SemanticAttributes.NetTransportValues.IP_TCP), - equalTo( - SemanticAttributes.RPC_GRPC_STATUS_CODE, - (long) Status.Code.OK.value())) + equalTo(SemanticAttributes.NET_HOST_NAME, "localhost"), + equalTo(SemanticAttributes.NET_HOST_PORT, server.getPort()), + equalTo(AttributeKey.stringKey("net.sock.peer.addr"), "127.0.0.1"), + satisfies( + AttributeKey.longKey("net.sock.peer.port"), + val -> assertThat(val).isNotNull())) .hasEventsSatisfyingExactly( event -> event @@ -400,6 +406,8 @@ public void sayHello( point -> point.hasAttributesSatisfying( equalTo(SemanticAttributes.NET_TRANSPORT, "ip_tcp"), + equalTo( + SemanticAttributes.NET_HOST_NAME, "localhost"), equalTo(SemanticAttributes.RPC_METHOD, "SayHello"), equalTo( SemanticAttributes.RPC_SERVICE, @@ -543,16 +551,18 @@ public void onCompleted() { equalTo(SemanticAttributes.RPC_SYSTEM, "grpc"), equalTo(SemanticAttributes.RPC_SERVICE, "example.Greeter"), equalTo(SemanticAttributes.RPC_METHOD, "SayHello"), - equalTo(AttributeKey.stringKey("net.sock.peer.addr"), "127.0.0.1"), - satisfies( - AttributeKey.longKey("net.sock.peer.port"), - val -> assertThat(val).isNotNull()), + equalTo( + SemanticAttributes.RPC_GRPC_STATUS_CODE, + (long) Status.Code.OK.value()), equalTo( SemanticAttributes.NET_TRANSPORT, SemanticAttributes.NetTransportValues.IP_TCP), - equalTo( - SemanticAttributes.RPC_GRPC_STATUS_CODE, - (long) Status.Code.OK.value())) + equalTo(SemanticAttributes.NET_HOST_NAME, "localhost"), + equalTo(SemanticAttributes.NET_HOST_PORT, server.getPort()), + equalTo(AttributeKey.stringKey("net.sock.peer.addr"), "127.0.0.1"), + satisfies( + AttributeKey.longKey("net.sock.peer.port"), + val -> assertThat(val).isNotNull())) .hasEventsSatisfyingExactly( event -> event @@ -595,6 +605,8 @@ public void onCompleted() { point -> point.hasAttributesSatisfying( equalTo(SemanticAttributes.NET_TRANSPORT, "ip_tcp"), + equalTo( + SemanticAttributes.NET_HOST_NAME, "localhost"), equalTo(SemanticAttributes.RPC_METHOD, "SayHello"), equalTo( SemanticAttributes.RPC_SERVICE, @@ -702,16 +714,18 @@ public void sayHello( equalTo(SemanticAttributes.RPC_SYSTEM, "grpc"), equalTo(SemanticAttributes.RPC_SERVICE, "example.Greeter"), equalTo(SemanticAttributes.RPC_METHOD, "SayHello"), - equalTo(AttributeKey.stringKey("net.sock.peer.addr"), "127.0.0.1"), - satisfies( - AttributeKey.longKey("net.sock.peer.port"), - val -> assertThat(val).isNotNull()), + equalTo( + SemanticAttributes.RPC_GRPC_STATUS_CODE, + (long) status.getCode().value()), equalTo( SemanticAttributes.NET_TRANSPORT, SemanticAttributes.NetTransportValues.IP_TCP), - equalTo( - SemanticAttributes.RPC_GRPC_STATUS_CODE, - (long) status.getCode().value())) + equalTo(SemanticAttributes.NET_HOST_NAME, "localhost"), + equalTo(SemanticAttributes.NET_HOST_PORT, server.getPort()), + equalTo(AttributeKey.stringKey("net.sock.peer.addr"), "127.0.0.1"), + satisfies( + AttributeKey.longKey("net.sock.peer.port"), + val -> assertThat(val).isNotNull())) .hasEventsSatisfying( events -> { assertThat(events).isNotEmpty(); @@ -747,6 +761,8 @@ public void sayHello( point -> point.hasAttributesSatisfying( equalTo(SemanticAttributes.NET_TRANSPORT, "ip_tcp"), + equalTo( + SemanticAttributes.NET_HOST_NAME, "localhost"), equalTo(SemanticAttributes.RPC_METHOD, "SayHello"), equalTo( SemanticAttributes.RPC_SERVICE, @@ -860,16 +876,18 @@ public void sayHello( equalTo(SemanticAttributes.RPC_SYSTEM, "grpc"), equalTo(SemanticAttributes.RPC_SERVICE, "example.Greeter"), equalTo(SemanticAttributes.RPC_METHOD, "SayHello"), - equalTo(AttributeKey.stringKey("net.sock.peer.addr"), "127.0.0.1"), - satisfies( - AttributeKey.longKey("net.sock.peer.port"), - val -> assertThat(val).isNotNull()), + equalTo( + SemanticAttributes.RPC_GRPC_STATUS_CODE, + (long) Status.Code.UNKNOWN.value()), equalTo( SemanticAttributes.NET_TRANSPORT, SemanticAttributes.NetTransportValues.IP_TCP), - equalTo( - SemanticAttributes.RPC_GRPC_STATUS_CODE, - (long) Status.Code.UNKNOWN.value())) + equalTo(SemanticAttributes.NET_HOST_NAME, "localhost"), + equalTo(SemanticAttributes.NET_HOST_PORT, server.getPort()), + equalTo(AttributeKey.stringKey("net.sock.peer.addr"), "127.0.0.1"), + satisfies( + AttributeKey.longKey("net.sock.peer.port"), + val -> assertThat(val).isNotNull())) .hasEventsSatisfying( events -> { assertThat(events).hasSize(2); @@ -900,6 +918,8 @@ public void sayHello( point -> point.hasAttributesSatisfying( equalTo(SemanticAttributes.NET_TRANSPORT, "ip_tcp"), + equalTo( + SemanticAttributes.NET_HOST_NAME, "localhost"), equalTo(SemanticAttributes.RPC_METHOD, "SayHello"), equalTo( SemanticAttributes.RPC_SERVICE, @@ -1119,16 +1139,18 @@ public void onCompleted() { equalTo(SemanticAttributes.RPC_SYSTEM, "grpc"), equalTo(SemanticAttributes.RPC_SERVICE, "example.Greeter"), equalTo(SemanticAttributes.RPC_METHOD, "SayHello"), - equalTo(AttributeKey.stringKey("net.sock.peer.addr"), "127.0.0.1"), - satisfies( - AttributeKey.longKey("net.sock.peer.port"), - val -> assertThat(val).isNotNull()), + equalTo( + SemanticAttributes.RPC_GRPC_STATUS_CODE, + (long) Status.Code.OK.value()), equalTo( SemanticAttributes.NET_TRANSPORT, SemanticAttributes.NetTransportValues.IP_TCP), - equalTo( - SemanticAttributes.RPC_GRPC_STATUS_CODE, - (long) Status.Code.OK.value())) + equalTo(SemanticAttributes.NET_HOST_NAME, "localhost"), + equalTo(SemanticAttributes.NET_HOST_PORT, server.getPort()), + equalTo(AttributeKey.stringKey("net.sock.peer.addr"), "127.0.0.1"), + satisfies( + AttributeKey.longKey("net.sock.peer.port"), + val -> assertThat(val).isNotNull())) .hasEventsSatisfyingExactly( event -> event @@ -1260,16 +1282,18 @@ public void onCompleted() { equalTo(SemanticAttributes.RPC_SYSTEM, "grpc"), equalTo(SemanticAttributes.RPC_SERVICE, "example.Greeter"), equalTo(SemanticAttributes.RPC_METHOD, "SayMultipleHello"), - equalTo(AttributeKey.stringKey("net.sock.peer.addr"), "127.0.0.1"), - satisfies( - AttributeKey.longKey("net.sock.peer.port"), - val -> assertThat(val).isNotNull()), + equalTo( + SemanticAttributes.RPC_GRPC_STATUS_CODE, + (long) Status.Code.CANCELLED.value()), equalTo( SemanticAttributes.NET_TRANSPORT, SemanticAttributes.NetTransportValues.IP_TCP), - equalTo( - SemanticAttributes.RPC_GRPC_STATUS_CODE, - (long) Status.Code.CANCELLED.value())) + equalTo(SemanticAttributes.NET_HOST_NAME, "localhost"), + equalTo(SemanticAttributes.NET_HOST_PORT, server.getPort()), + equalTo(AttributeKey.stringKey("net.sock.peer.addr"), "127.0.0.1"), + satisfies( + AttributeKey.longKey("net.sock.peer.port"), + val -> assertThat(val).isNotNull())) .hasEventsSatisfyingExactly( event -> event @@ -1401,16 +1425,18 @@ public void onCompleted() { SemanticAttributes.RPC_SERVICE, "grpc.reflection.v1alpha.ServerReflection"), equalTo(SemanticAttributes.RPC_METHOD, "ServerReflectionInfo"), - equalTo(AttributeKey.stringKey("net.sock.peer.addr"), "127.0.0.1"), - satisfies( - AttributeKey.longKey("net.sock.peer.port"), - val -> assertThat(val).isNotNull()), + equalTo( + SemanticAttributes.RPC_GRPC_STATUS_CODE, + (long) Status.Code.OK.value()), equalTo( SemanticAttributes.NET_TRANSPORT, SemanticAttributes.NetTransportValues.IP_TCP), - equalTo( - SemanticAttributes.RPC_GRPC_STATUS_CODE, - (long) Status.Code.OK.value())) + equalTo(SemanticAttributes.NET_HOST_NAME, "localhost"), + equalTo(SemanticAttributes.NET_HOST_PORT, server.getPort()), + equalTo(AttributeKey.stringKey("net.sock.peer.addr"), "127.0.0.1"), + satisfies( + AttributeKey.longKey("net.sock.peer.port"), + val -> assertThat(val).isNotNull())) .hasEventsSatisfyingExactly( event -> event @@ -1525,16 +1551,18 @@ public void sayHello( equalTo(SemanticAttributes.RPC_SYSTEM, "grpc"), equalTo(SemanticAttributes.RPC_SERVICE, "example.Greeter"), equalTo(SemanticAttributes.RPC_METHOD, "SayHello"), - equalTo(AttributeKey.stringKey("net.sock.peer.addr"), "127.0.0.1"), - satisfies( - AttributeKey.longKey("net.sock.peer.port"), - val -> assertThat(val).isNotNull()), + equalTo( + SemanticAttributes.RPC_GRPC_STATUS_CODE, + (long) Status.Code.OK.value()), equalTo( SemanticAttributes.NET_TRANSPORT, SemanticAttributes.NetTransportValues.IP_TCP), - equalTo( - SemanticAttributes.RPC_GRPC_STATUS_CODE, - (long) Status.Code.OK.value())) + equalTo(SemanticAttributes.NET_HOST_NAME, "localhost"), + equalTo(SemanticAttributes.NET_HOST_PORT, server.getPort()), + equalTo(AttributeKey.stringKey("net.sock.peer.addr"), "127.0.0.1"), + satisfies( + AttributeKey.longKey("net.sock.peer.port"), + val -> assertThat(val).isNotNull())) .hasEventsSatisfyingExactly( event -> event diff --git a/instrumentation/jaxrs-client/jaxrs-client-1.1/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/jaxrsclient/v1_1/JaxRsClientNetAttributesGetter.java b/instrumentation/jaxrs-client/jaxrs-client-1.1/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/jaxrsclient/v1_1/JaxRsClientNetAttributesGetter.java index 66a69e60c9a7..d822db30859b 100644 --- a/instrumentation/jaxrs-client/jaxrs-client-1.1/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/jaxrsclient/v1_1/JaxRsClientNetAttributesGetter.java +++ b/instrumentation/jaxrs-client/jaxrs-client-1.1/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/jaxrsclient/v1_1/JaxRsClientNetAttributesGetter.java @@ -27,10 +27,6 @@ public String peerName(ClientRequest request, @Nullable ClientResponse response) @Override public Integer peerPort(ClientRequest request, @Nullable ClientResponse response) { - int port = request.getURI().getPort(); - if (port != -1) { - return port; - } - return null; + return request.getURI().getPort(); } } diff --git a/instrumentation/jaxrs/jaxrs-common/testing/src/main/groovy/AbstractJaxRsHttpServerTest.groovy b/instrumentation/jaxrs/jaxrs-common/testing/src/main/groovy/AbstractJaxRsHttpServerTest.groovy index 91acbad24ab9..f67fa2a23bfe 100644 --- a/instrumentation/jaxrs/jaxrs-common/testing/src/main/groovy/AbstractJaxRsHttpServerTest.groovy +++ b/instrumentation/jaxrs/jaxrs-common/testing/src/main/groovy/AbstractJaxRsHttpServerTest.groovy @@ -268,8 +268,11 @@ abstract class AbstractJaxRsHttpServerTest extends HttpServerTest implemen hasNoParent() } attributes { + "net.host.name" fullUrl.host + "net.host.port" fullUrl.port "net.sock.peer.addr" "127.0.0.1" "net.sock.peer.port" Long + "net.sock.host.addr" "127.0.0.1" "$SemanticAttributes.HTTP_SCHEME" fullUrl.getScheme() "$SemanticAttributes.HTTP_HOST" fullUrl.getHost() + ":" + fullUrl.getPort() "$SemanticAttributes.HTTP_TARGET" fullUrl.getPath() + (fullUrl.getQuery() != null ? "?" + fullUrl.getQuery() : "") diff --git a/instrumentation/jedis/jedis-3.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/jedis/v3_0/JedisNetAttributesGetter.java b/instrumentation/jedis/jedis-3.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/jedis/v3_0/JedisNetAttributesGetter.java index 395626a1d6ba..0cfc0b474de0 100644 --- a/instrumentation/jedis/jedis-3.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/jedis/v3_0/JedisNetAttributesGetter.java +++ b/instrumentation/jedis/jedis-3.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/jedis/v3_0/JedisNetAttributesGetter.java @@ -15,17 +15,29 @@ final class JedisNetAttributesGetter extends InetSocketAddressNetClientAttributesGetter { @Override + public String transport(JedisRequest jedisRequest, @Nullable Void unused) { + return SemanticAttributes.NetTransportValues.IP_TCP; + } + @Nullable - public InetSocketAddress getAddress(JedisRequest jedisRequest, @Nullable Void unused) { + @Override + public String peerName(JedisRequest jedisRequest, @Nullable Void unused) { + return jedisRequest.getConnection().getHost(); + } + + @Override + public Integer peerPort(JedisRequest jedisRequest, @Nullable Void unused) { + return jedisRequest.getConnection().getPort(); + } + + @Override + @Nullable + protected InetSocketAddress getPeerSocketAddress( + JedisRequest jedisRequest, @Nullable Void unused) { Socket socket = jedisRequest.getConnection().getSocket(); if (socket != null && socket.getRemoteSocketAddress() instanceof InetSocketAddress) { return (InetSocketAddress) socket.getRemoteSocketAddress(); } return null; } - - @Override - public String transport(JedisRequest jedisRequest, @Nullable Void unused) { - return SemanticAttributes.NetTransportValues.IP_TCP; - } } diff --git a/instrumentation/jedis/jedis-4.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/jedis/v4_0/JedisNetAttributesGetter.java b/instrumentation/jedis/jedis-4.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/jedis/v4_0/JedisNetAttributesGetter.java index 873daf066ec0..458c6b45c1b4 100644 --- a/instrumentation/jedis/jedis-4.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/jedis/v4_0/JedisNetAttributesGetter.java +++ b/instrumentation/jedis/jedis-4.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/jedis/v4_0/JedisNetAttributesGetter.java @@ -15,17 +15,30 @@ final class JedisNetAttributesGetter extends InetSocketAddressNetClientAttributesGetter { @Override + public String transport(JedisRequest jedisRequest, @Nullable Void unused) { + return SemanticAttributes.NetTransportValues.IP_TCP; + } + @Nullable - public InetSocketAddress getAddress(JedisRequest jedisRequest, @Nullable Void unused) { - SocketAddress socketAddress = jedisRequest.getRemoteSocketAddress(); - if (socketAddress != null && socketAddress instanceof InetSocketAddress) { - return (InetSocketAddress) socketAddress; - } + @Override + public String peerName(JedisRequest jedisRequest, @Nullable Void unused) { return null; } + @Nullable @Override - public String transport(JedisRequest jedisRequest, @Nullable Void unused) { - return SemanticAttributes.NetTransportValues.IP_TCP; + public Integer peerPort(JedisRequest jedisRequest, @Nullable Void unused) { + return null; + } + + @Override + @Nullable + protected InetSocketAddress getPeerSocketAddress( + JedisRequest jedisRequest, @Nullable Void unused) { + SocketAddress socketAddress = jedisRequest.getRemoteSocketAddress(); + if (socketAddress instanceof InetSocketAddress) { + return (InetSocketAddress) socketAddress; + } + return null; } } diff --git a/instrumentation/jedis/jedis-4.0/javaagent/src/test/groovy/Jedis40ClientTest.groovy b/instrumentation/jedis/jedis-4.0/javaagent/src/test/groovy/Jedis40ClientTest.groovy index 94ea0be4cfff..28fed6057bcd 100644 --- a/instrumentation/jedis/jedis-4.0/javaagent/src/test/groovy/Jedis40ClientTest.groovy +++ b/instrumentation/jedis/jedis-4.0/javaagent/src/test/groovy/Jedis40ClientTest.groovy @@ -24,7 +24,7 @@ class Jedis40ClientTest extends AgentInstrumentationSpecification { def setupSpec() { redisServer.start() port = redisServer.getMappedPort(6379) - jedis = new Jedis("127.0.0.1", port) + jedis = new Jedis("localhost", port) } def cleanupSpec() { @@ -51,9 +51,10 @@ class Jedis40ClientTest extends AgentInstrumentationSpecification { "$SemanticAttributes.DB_SYSTEM" "redis" "$SemanticAttributes.DB_STATEMENT" "SET foo ?" "$SemanticAttributes.DB_OPERATION" "SET" - "$SemanticAttributes.NET_PEER_NAME" "127.0.0.1" - "$SemanticAttributes.NET_PEER_PORT" port "$SemanticAttributes.NET_TRANSPORT" SemanticAttributes.NetTransportValues.IP_TCP + "net.sock.peer.addr" "127.0.0.1" + "net.sock.peer.name" "localhost" + "net.sock.peer.port" port } } } @@ -77,9 +78,10 @@ class Jedis40ClientTest extends AgentInstrumentationSpecification { "$SemanticAttributes.DB_SYSTEM" "redis" "$SemanticAttributes.DB_STATEMENT" "SET foo ?" "$SemanticAttributes.DB_OPERATION" "SET" - "$SemanticAttributes.NET_PEER_NAME" "127.0.0.1" - "$SemanticAttributes.NET_PEER_PORT" port "$SemanticAttributes.NET_TRANSPORT" SemanticAttributes.NetTransportValues.IP_TCP + "net.sock.peer.addr" "127.0.0.1" + "net.sock.peer.name" "localhost" + "net.sock.peer.port" port } } } @@ -91,9 +93,10 @@ class Jedis40ClientTest extends AgentInstrumentationSpecification { "$SemanticAttributes.DB_SYSTEM" "redis" "$SemanticAttributes.DB_STATEMENT" "GET foo" "$SemanticAttributes.DB_OPERATION" "GET" - "$SemanticAttributes.NET_PEER_NAME" "127.0.0.1" - "$SemanticAttributes.NET_PEER_PORT" port "$SemanticAttributes.NET_TRANSPORT" SemanticAttributes.NetTransportValues.IP_TCP + "net.sock.peer.addr" "127.0.0.1" + "net.sock.peer.name" "localhost" + "net.sock.peer.port" port } } } @@ -117,9 +120,10 @@ class Jedis40ClientTest extends AgentInstrumentationSpecification { "$SemanticAttributes.DB_SYSTEM" "redis" "$SemanticAttributes.DB_STATEMENT" "SET foo ?" "$SemanticAttributes.DB_OPERATION" "SET" - "$SemanticAttributes.NET_PEER_NAME" "127.0.0.1" - "$SemanticAttributes.NET_PEER_PORT" port "$SemanticAttributes.NET_TRANSPORT" SemanticAttributes.NetTransportValues.IP_TCP + "net.sock.peer.addr" "127.0.0.1" + "net.sock.peer.name" "localhost" + "net.sock.peer.port" port } } } @@ -131,9 +135,10 @@ class Jedis40ClientTest extends AgentInstrumentationSpecification { "$SemanticAttributes.DB_SYSTEM" "redis" "$SemanticAttributes.DB_STATEMENT" "RANDOMKEY" "$SemanticAttributes.DB_OPERATION" "RANDOMKEY" - "$SemanticAttributes.NET_PEER_NAME" "127.0.0.1" - "$SemanticAttributes.NET_PEER_PORT" port "$SemanticAttributes.NET_TRANSPORT" SemanticAttributes.NetTransportValues.IP_TCP + "net.sock.peer.addr" "127.0.0.1" + "net.sock.peer.name" "localhost" + "net.sock.peer.port" port } } } diff --git a/instrumentation/jsf/jsf-common/testing/src/main/groovy/BaseJsfTest.groovy b/instrumentation/jsf/jsf-common/testing/src/main/groovy/BaseJsfTest.groovy index 6f28adecabef..1f39b4b39448 100644 --- a/instrumentation/jsf/jsf-common/testing/src/main/groovy/BaseJsfTest.groovy +++ b/instrumentation/jsf/jsf-common/testing/src/main/groovy/BaseJsfTest.groovy @@ -98,8 +98,11 @@ abstract class BaseJsfTest extends AgentInstrumentationSpecification implements hasNoParent() attributes { "$SemanticAttributes.NET_TRANSPORT" SemanticAttributes.NetTransportValues.IP_TCP + "$SemanticAttributes.NET_HOST_NAME" "localhost" + "$SemanticAttributes.NET_HOST_PORT" port "net.sock.peer.addr" "127.0.0.1" "net.sock.peer.port" Long + "net.sock.host.addr" "127.0.0.1" "$SemanticAttributes.HTTP_METHOD" "GET" "$SemanticAttributes.HTTP_SCHEME" "http" "$SemanticAttributes.HTTP_HOST" { it == "localhost" || it == "localhost:$port" } diff --git a/instrumentation/jsp-2.3/javaagent/src/test/groovy/JspInstrumentationBasicTests.groovy b/instrumentation/jsp-2.3/javaagent/src/test/groovy/JspInstrumentationBasicTests.groovy index f292a4f1a74c..5b793aa7f0ba 100644 --- a/instrumentation/jsp-2.3/javaagent/src/test/groovy/JspInstrumentationBasicTests.groovy +++ b/instrumentation/jsp-2.3/javaagent/src/test/groovy/JspInstrumentationBasicTests.groovy @@ -90,8 +90,6 @@ class JspInstrumentationBasicTests extends AgentInstrumentationSpecification { name route kind SERVER attributes { - "net.sock.peer.addr" "127.0.0.1" - "net.sock.peer.port" Long "$SemanticAttributes.HTTP_SCHEME" "http" "$SemanticAttributes.HTTP_HOST" "localhost:$port" "$SemanticAttributes.HTTP_TARGET" route @@ -99,10 +97,14 @@ class JspInstrumentationBasicTests extends AgentInstrumentationSpecification { "$SemanticAttributes.HTTP_STATUS_CODE" 200 "$SemanticAttributes.HTTP_FLAVOR" "1.1" "$SemanticAttributes.HTTP_USER_AGENT" String - "$SemanticAttributes.NET_TRANSPORT" IP_TCP - // Optional "$SemanticAttributes.HTTP_REQUEST_CONTENT_LENGTH" { it == null || it instanceof Long } "$SemanticAttributes.HTTP_ROUTE" route + "$SemanticAttributes.NET_TRANSPORT" IP_TCP + "$SemanticAttributes.NET_HOST_NAME" "localhost" + "$SemanticAttributes.NET_HOST_PORT" port + "net.sock.peer.addr" "127.0.0.1" + "net.sock.peer.port" Long + "net.sock.host.addr" "127.0.0.1" } } span(1) { @@ -148,8 +150,6 @@ class JspInstrumentationBasicTests extends AgentInstrumentationSpecification { name route kind SERVER attributes { - "net.sock.peer.addr" "127.0.0.1" - "net.sock.peer.port" Long "$SemanticAttributes.HTTP_SCHEME" "http" "$SemanticAttributes.HTTP_HOST" "localhost:$port" "$SemanticAttributes.HTTP_TARGET" "$route?$queryString" @@ -157,8 +157,13 @@ class JspInstrumentationBasicTests extends AgentInstrumentationSpecification { "$SemanticAttributes.HTTP_STATUS_CODE" 200 "$SemanticAttributes.HTTP_FLAVOR" "1.1" "$SemanticAttributes.HTTP_USER_AGENT" String - "$SemanticAttributes.NET_TRANSPORT" IP_TCP "$SemanticAttributes.HTTP_ROUTE" route + "$SemanticAttributes.NET_TRANSPORT" IP_TCP + "$SemanticAttributes.NET_HOST_NAME" "localhost" + "$SemanticAttributes.NET_HOST_PORT" port + "net.sock.peer.addr" "127.0.0.1" + "net.sock.peer.port" Long + "net.sock.host.addr" "127.0.0.1" } } span(1) { @@ -200,8 +205,6 @@ class JspInstrumentationBasicTests extends AgentInstrumentationSpecification { name route kind SERVER attributes { - "net.sock.peer.addr" "127.0.0.1" - "net.sock.peer.port" Long "$SemanticAttributes.HTTP_SCHEME" "http" "$SemanticAttributes.HTTP_HOST" "localhost:$port" "$SemanticAttributes.HTTP_TARGET" route @@ -209,9 +212,14 @@ class JspInstrumentationBasicTests extends AgentInstrumentationSpecification { "$SemanticAttributes.HTTP_STATUS_CODE" 200 "$SemanticAttributes.HTTP_FLAVOR" "1.1" "$SemanticAttributes.HTTP_USER_AGENT" String - "$SemanticAttributes.NET_TRANSPORT" IP_TCP "$SemanticAttributes.HTTP_REQUEST_CONTENT_LENGTH" Long "$SemanticAttributes.HTTP_ROUTE" route + "$SemanticAttributes.NET_TRANSPORT" IP_TCP + "$SemanticAttributes.NET_HOST_NAME" "localhost" + "$SemanticAttributes.NET_HOST_PORT" port + "net.sock.peer.addr" "127.0.0.1" + "net.sock.peer.port" Long + "net.sock.host.addr" "127.0.0.1" } } span(1) { @@ -262,8 +270,6 @@ class JspInstrumentationBasicTests extends AgentInstrumentationSpecification { } } attributes { - "net.sock.peer.addr" "127.0.0.1" - "net.sock.peer.port" Long "$SemanticAttributes.HTTP_SCHEME" "http" "$SemanticAttributes.HTTP_HOST" "localhost:$port" "$SemanticAttributes.HTTP_TARGET" route @@ -271,8 +277,13 @@ class JspInstrumentationBasicTests extends AgentInstrumentationSpecification { "$SemanticAttributes.HTTP_STATUS_CODE" 500 "$SemanticAttributes.HTTP_FLAVOR" "1.1" "$SemanticAttributes.HTTP_USER_AGENT" String - "$SemanticAttributes.NET_TRANSPORT" IP_TCP "$SemanticAttributes.HTTP_ROUTE" route + "$SemanticAttributes.NET_TRANSPORT" IP_TCP + "$SemanticAttributes.NET_HOST_NAME" "localhost" + "$SemanticAttributes.NET_HOST_PORT" port + "net.sock.peer.addr" "127.0.0.1" + "net.sock.peer.port" Long + "net.sock.host.addr" "127.0.0.1" } } span(1) { @@ -328,8 +339,6 @@ class JspInstrumentationBasicTests extends AgentInstrumentationSpecification { name route kind SERVER attributes { - "net.sock.peer.addr" "127.0.0.1" - "net.sock.peer.port" Long "$SemanticAttributes.HTTP_SCHEME" "http" "$SemanticAttributes.HTTP_HOST" "localhost:$port" "$SemanticAttributes.HTTP_TARGET" route @@ -337,8 +346,13 @@ class JspInstrumentationBasicTests extends AgentInstrumentationSpecification { "$SemanticAttributes.HTTP_STATUS_CODE" 200 "$SemanticAttributes.HTTP_FLAVOR" "1.1" "$SemanticAttributes.HTTP_USER_AGENT" String - "$SemanticAttributes.NET_TRANSPORT" IP_TCP "$SemanticAttributes.HTTP_ROUTE" route + "$SemanticAttributes.NET_TRANSPORT" IP_TCP + "$SemanticAttributes.NET_HOST_NAME" "localhost" + "$SemanticAttributes.NET_HOST_PORT" port + "net.sock.peer.addr" "127.0.0.1" + "net.sock.peer.port" Long + "net.sock.host.addr" "127.0.0.1" } } span(1) { @@ -375,8 +389,6 @@ class JspInstrumentationBasicTests extends AgentInstrumentationSpecification { name route kind SERVER attributes { - "net.sock.peer.addr" "127.0.0.1" - "net.sock.peer.port" Long "$SemanticAttributes.HTTP_SCHEME" "http" "$SemanticAttributes.HTTP_HOST" "localhost:$port" "$SemanticAttributes.HTTP_TARGET" route @@ -384,8 +396,13 @@ class JspInstrumentationBasicTests extends AgentInstrumentationSpecification { "$SemanticAttributes.HTTP_STATUS_CODE" 200 "$SemanticAttributes.HTTP_FLAVOR" "1.1" "$SemanticAttributes.HTTP_USER_AGENT" String - "$SemanticAttributes.NET_TRANSPORT" IP_TCP "$SemanticAttributes.HTTP_ROUTE" route + "$SemanticAttributes.NET_TRANSPORT" IP_TCP + "$SemanticAttributes.NET_HOST_NAME" "localhost" + "$SemanticAttributes.NET_HOST_PORT" port + "net.sock.peer.addr" "127.0.0.1" + "net.sock.peer.port" Long + "net.sock.host.addr" "127.0.0.1" } } span(1) { @@ -454,8 +471,6 @@ class JspInstrumentationBasicTests extends AgentInstrumentationSpecification { status ERROR errorEvent(JasperException, String) attributes { - "net.sock.peer.addr" "127.0.0.1" - "net.sock.peer.port" Long "$SemanticAttributes.HTTP_SCHEME" "http" "$SemanticAttributes.HTTP_HOST" "localhost:$port" "$SemanticAttributes.HTTP_TARGET" route @@ -463,8 +478,13 @@ class JspInstrumentationBasicTests extends AgentInstrumentationSpecification { "$SemanticAttributes.HTTP_STATUS_CODE" 500 "$SemanticAttributes.HTTP_FLAVOR" "1.1" "$SemanticAttributes.HTTP_USER_AGENT" String - "$SemanticAttributes.NET_TRANSPORT" IP_TCP "$SemanticAttributes.HTTP_ROUTE" route + "$SemanticAttributes.NET_TRANSPORT" IP_TCP + "$SemanticAttributes.NET_HOST_NAME" "localhost" + "$SemanticAttributes.NET_HOST_PORT" port + "net.sock.peer.addr" "127.0.0.1" + "net.sock.peer.port" Long + "net.sock.host.addr" "127.0.0.1" } } span(1) { @@ -502,8 +522,6 @@ class JspInstrumentationBasicTests extends AgentInstrumentationSpecification { name route kind SERVER attributes { - "net.sock.peer.addr" "127.0.0.1" - "net.sock.peer.port" Long "$SemanticAttributes.HTTP_SCHEME" "http" "$SemanticAttributes.HTTP_HOST" "localhost:$port" "$SemanticAttributes.HTTP_TARGET" "/$jspWebappContext/$staticFile" @@ -511,8 +529,13 @@ class JspInstrumentationBasicTests extends AgentInstrumentationSpecification { "$SemanticAttributes.HTTP_STATUS_CODE" 200 "$SemanticAttributes.HTTP_FLAVOR" "1.1" "$SemanticAttributes.HTTP_USER_AGENT" String - "$SemanticAttributes.NET_TRANSPORT" IP_TCP "$SemanticAttributes.HTTP_ROUTE" route + "$SemanticAttributes.NET_TRANSPORT" IP_TCP + "$SemanticAttributes.NET_HOST_NAME" "localhost" + "$SemanticAttributes.NET_HOST_PORT" port + "net.sock.peer.addr" "127.0.0.1" + "net.sock.peer.port" Long + "net.sock.host.addr" "127.0.0.1" } } } diff --git a/instrumentation/jsp-2.3/javaagent/src/test/groovy/JspInstrumentationForwardTests.groovy b/instrumentation/jsp-2.3/javaagent/src/test/groovy/JspInstrumentationForwardTests.groovy index 754a23ac7c23..90dce4d1ef61 100644 --- a/instrumentation/jsp-2.3/javaagent/src/test/groovy/JspInstrumentationForwardTests.groovy +++ b/instrumentation/jsp-2.3/javaagent/src/test/groovy/JspInstrumentationForwardTests.groovy @@ -88,8 +88,6 @@ class JspInstrumentationForwardTests extends AgentInstrumentationSpecification { name route kind SERVER attributes { - "net.sock.peer.addr" "127.0.0.1" - "net.sock.peer.port" Long "$SemanticAttributes.HTTP_SCHEME" "http" "$SemanticAttributes.HTTP_HOST" "localhost:$port" "$SemanticAttributes.HTTP_TARGET" route @@ -97,8 +95,13 @@ class JspInstrumentationForwardTests extends AgentInstrumentationSpecification { "$SemanticAttributes.HTTP_STATUS_CODE" 200 "$SemanticAttributes.HTTP_FLAVOR" "1.1" "$SemanticAttributes.HTTP_USER_AGENT" String - "$SemanticAttributes.NET_TRANSPORT" IP_TCP "$SemanticAttributes.HTTP_ROUTE" route + "$SemanticAttributes.NET_TRANSPORT" IP_TCP + "$SemanticAttributes.NET_HOST_NAME" "localhost" + "$SemanticAttributes.NET_HOST_PORT" port + "net.sock.peer.addr" "127.0.0.1" + "net.sock.peer.port" Long + "net.sock.host.addr" "127.0.0.1" } } span(1) { @@ -156,8 +159,6 @@ class JspInstrumentationForwardTests extends AgentInstrumentationSpecification { name route kind SERVER attributes { - "net.sock.peer.addr" "127.0.0.1" - "net.sock.peer.port" Long "$SemanticAttributes.HTTP_SCHEME" "http" "$SemanticAttributes.HTTP_HOST" "localhost:$port" "$SemanticAttributes.HTTP_TARGET" route @@ -165,8 +166,13 @@ class JspInstrumentationForwardTests extends AgentInstrumentationSpecification { "$SemanticAttributes.HTTP_STATUS_CODE" 200 "$SemanticAttributes.HTTP_FLAVOR" "1.1" "$SemanticAttributes.HTTP_USER_AGENT" String - "$SemanticAttributes.NET_TRANSPORT" IP_TCP "$SemanticAttributes.HTTP_ROUTE" route + "$SemanticAttributes.NET_TRANSPORT" IP_TCP + "$SemanticAttributes.NET_HOST_NAME" "localhost" + "$SemanticAttributes.NET_HOST_PORT" port + "net.sock.peer.addr" "127.0.0.1" + "net.sock.peer.port" Long + "net.sock.host.addr" "127.0.0.1" } } span(1) { @@ -203,8 +209,6 @@ class JspInstrumentationForwardTests extends AgentInstrumentationSpecification { name route kind SERVER attributes { - "net.sock.peer.addr" "127.0.0.1" - "net.sock.peer.port" Long "$SemanticAttributes.HTTP_SCHEME" "http" "$SemanticAttributes.HTTP_HOST" "localhost:$port" "$SemanticAttributes.HTTP_TARGET" route @@ -212,8 +216,13 @@ class JspInstrumentationForwardTests extends AgentInstrumentationSpecification { "$SemanticAttributes.HTTP_STATUS_CODE" 200 "$SemanticAttributes.HTTP_FLAVOR" "1.1" "$SemanticAttributes.HTTP_USER_AGENT" String - "$SemanticAttributes.NET_TRANSPORT" IP_TCP "$SemanticAttributes.HTTP_ROUTE" route + "$SemanticAttributes.NET_TRANSPORT" IP_TCP + "$SemanticAttributes.NET_HOST_NAME" "localhost" + "$SemanticAttributes.NET_HOST_PORT" port + "net.sock.peer.addr" "127.0.0.1" + "net.sock.peer.port" Long + "net.sock.host.addr" "127.0.0.1" } } span(1) { @@ -298,8 +307,6 @@ class JspInstrumentationForwardTests extends AgentInstrumentationSpecification { name route kind SERVER attributes { - "net.sock.peer.addr" "127.0.0.1" - "net.sock.peer.port" Long "$SemanticAttributes.HTTP_SCHEME" "http" "$SemanticAttributes.HTTP_HOST" "localhost:$port" "$SemanticAttributes.HTTP_TARGET" route @@ -307,8 +314,13 @@ class JspInstrumentationForwardTests extends AgentInstrumentationSpecification { "$SemanticAttributes.HTTP_STATUS_CODE" 200 "$SemanticAttributes.HTTP_FLAVOR" "1.1" "$SemanticAttributes.HTTP_USER_AGENT" String - "$SemanticAttributes.NET_TRANSPORT" IP_TCP "$SemanticAttributes.HTTP_ROUTE" route + "$SemanticAttributes.NET_TRANSPORT" IP_TCP + "$SemanticAttributes.NET_HOST_NAME" "localhost" + "$SemanticAttributes.NET_HOST_PORT" port + "net.sock.peer.addr" "127.0.0.1" + "net.sock.peer.port" Long + "net.sock.host.addr" "127.0.0.1" } } span(1) { @@ -379,8 +391,6 @@ class JspInstrumentationForwardTests extends AgentInstrumentationSpecification { status ERROR errorEvent(JasperException, String) attributes { - "net.sock.peer.addr" "127.0.0.1" - "net.sock.peer.port" Long "$SemanticAttributes.HTTP_SCHEME" "http" "$SemanticAttributes.HTTP_HOST" "localhost:$port" "$SemanticAttributes.HTTP_TARGET" route @@ -388,8 +398,13 @@ class JspInstrumentationForwardTests extends AgentInstrumentationSpecification { "$SemanticAttributes.HTTP_STATUS_CODE" 500 "$SemanticAttributes.HTTP_FLAVOR" "1.1" "$SemanticAttributes.HTTP_USER_AGENT" String - "$SemanticAttributes.NET_TRANSPORT" IP_TCP "$SemanticAttributes.HTTP_ROUTE" route + "$SemanticAttributes.NET_TRANSPORT" IP_TCP + "$SemanticAttributes.NET_HOST_NAME" "localhost" + "$SemanticAttributes.NET_HOST_PORT" port + "net.sock.peer.addr" "127.0.0.1" + "net.sock.peer.port" Long + "net.sock.host.addr" "127.0.0.1" } } span(1) { @@ -439,8 +454,6 @@ class JspInstrumentationForwardTests extends AgentInstrumentationSpecification { kind SERVER status UNSET attributes { - "net.sock.peer.addr" "127.0.0.1" - "net.sock.peer.port" Long "$SemanticAttributes.HTTP_SCHEME" "http" "$SemanticAttributes.HTTP_HOST" "localhost:$port" "$SemanticAttributes.HTTP_TARGET" route @@ -448,8 +461,13 @@ class JspInstrumentationForwardTests extends AgentInstrumentationSpecification { "$SemanticAttributes.HTTP_STATUS_CODE" 404 "$SemanticAttributes.HTTP_FLAVOR" "1.1" "$SemanticAttributes.HTTP_USER_AGENT" String - "$SemanticAttributes.NET_TRANSPORT" IP_TCP "$SemanticAttributes.HTTP_ROUTE" route + "$SemanticAttributes.NET_TRANSPORT" IP_TCP + "$SemanticAttributes.NET_HOST_NAME" "localhost" + "$SemanticAttributes.NET_HOST_PORT" port + "net.sock.peer.addr" "127.0.0.1" + "net.sock.peer.port" Long + "net.sock.host.addr" "127.0.0.1" } } span(1) { diff --git a/instrumentation/ktor/ktor-1.0/library/src/main/kotlin/io/opentelemetry/instrumentation/ktor/v1_0/KtorNetServerAttributesGetter.kt b/instrumentation/ktor/ktor-1.0/library/src/main/kotlin/io/opentelemetry/instrumentation/ktor/v1_0/KtorNetServerAttributesGetter.kt index 01222d7e2475..8f82a1e46969 100644 --- a/instrumentation/ktor/ktor-1.0/library/src/main/kotlin/io/opentelemetry/instrumentation/ktor/v1_0/KtorNetServerAttributesGetter.kt +++ b/instrumentation/ktor/ktor-1.0/library/src/main/kotlin/io/opentelemetry/instrumentation/ktor/v1_0/KtorNetServerAttributesGetter.kt @@ -15,15 +15,19 @@ internal class KtorNetServerAttributesGetter : NetServerAttributesGetter getAllHeaderNames(); + + String getURLHost(); + + int getURLPort(); } diff --git a/instrumentation/liberty/liberty-dispatcher-20.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/liberty/dispatcher/LibertyDispatcherNetAttributesGetter.java b/instrumentation/liberty/liberty-dispatcher-20.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/liberty/dispatcher/LibertyDispatcherNetAttributesGetter.java index 95f447ac41a3..86652e6c5503 100644 --- a/instrumentation/liberty/liberty-dispatcher-20.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/liberty/dispatcher/LibertyDispatcherNetAttributesGetter.java +++ b/instrumentation/liberty/liberty-dispatcher-20.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/liberty/dispatcher/LibertyDispatcherNetAttributesGetter.java @@ -13,19 +13,41 @@ public class LibertyDispatcherNetAttributesGetter implements NetServerAttributesGetter { @Override - public String transport(LibertyRequest libertyRequest) { + public String transport(LibertyRequest request) { return SemanticAttributes.NetTransportValues.IP_TCP; } + @Nullable + @Override + public String hostName(LibertyRequest request) { + return request.request().getURLHost(); + } + + @Override + public Integer hostPort(LibertyRequest request) { + return request.request().getURLPort(); + } + @Override @Nullable - public Integer sockPeerPort(LibertyRequest libertyRequest) { - return libertyRequest.peerPort(); + public String sockPeerAddr(LibertyRequest request) { + return request.dispatcher().getRemoteHostAddress(); } @Override + public Integer sockPeerPort(LibertyRequest request) { + return request.dispatcher().getRemotePort(); + } + @Nullable - public String sockPeerAddr(LibertyRequest libertyRequest) { - return libertyRequest.peerIp(); + @Override + public String sockHostAddr(LibertyRequest request) { + return request.dispatcher().getLocalHostAddress(); + } + + @Nullable + @Override + public Integer sockHostPort(LibertyRequest request) { + return request.dispatcher().getLocalPort(); } } diff --git a/instrumentation/liberty/liberty-dispatcher-20.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/liberty/dispatcher/LibertyRequest.java b/instrumentation/liberty/liberty-dispatcher-20.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/liberty/dispatcher/LibertyRequest.java index 8edc81bee185..245e77e6f229 100644 --- a/instrumentation/liberty/liberty-dispatcher-20.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/liberty/dispatcher/LibertyRequest.java +++ b/instrumentation/liberty/liberty-dispatcher-20.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/liberty/dispatcher/LibertyRequest.java @@ -38,10 +38,6 @@ public String getQueryString() { return httpRequestMessage.getQueryString(); } - public int getServerPort() { - return httpDispatcherLink.getRequestedPort(); - } - public List getAllHeaderNames() { return httpRequestMessage.getAllHeaderNames(); } @@ -63,15 +59,15 @@ public List getHeaderValues(String name) { return stringHeaders; } - public int peerPort() { - return httpDispatcherLink.getRemotePort(); + public String getProtocol() { + return httpRequestMessage.getVersion(); } - public String peerIp() { - return httpDispatcherLink.getRemoteHostAddress(); + public HttpDispatcherLink dispatcher() { + return httpDispatcherLink; } - public String getProtocol() { - return httpRequestMessage.getVersion(); + public HttpRequestMessage request() { + return httpRequestMessage; } } diff --git a/instrumentation/mongo/mongo-3.1/library/src/main/java/io/opentelemetry/instrumentation/mongo/v3_1/MongoNetAttributesGetter.java b/instrumentation/mongo/mongo-3.1/library/src/main/java/io/opentelemetry/instrumentation/mongo/v3_1/MongoNetAttributesGetter.java index 787ec3272cc1..70cd421ae7ef 100644 --- a/instrumentation/mongo/mongo-3.1/library/src/main/java/io/opentelemetry/instrumentation/mongo/v3_1/MongoNetAttributesGetter.java +++ b/instrumentation/mongo/mongo-3.1/library/src/main/java/io/opentelemetry/instrumentation/mongo/v3_1/MongoNetAttributesGetter.java @@ -6,26 +6,34 @@ package io.opentelemetry.instrumentation.mongo.v3_1; import com.mongodb.event.CommandStartedEvent; -import io.opentelemetry.instrumentation.api.instrumenter.net.InetSocketAddressNetClientAttributesGetter; -import java.net.InetSocketAddress; +import io.opentelemetry.instrumentation.api.instrumenter.net.NetClientAttributesGetter; import javax.annotation.Nullable; -class MongoNetAttributesGetter - extends InetSocketAddressNetClientAttributesGetter { +class MongoNetAttributesGetter implements NetClientAttributesGetter { + @Override @Nullable - public InetSocketAddress getAddress(CommandStartedEvent event, @Nullable Void unused) { + public String transport(CommandStartedEvent event, @Nullable Void unused) { + return null; + } + + @Nullable + @Override + public String peerName(CommandStartedEvent event, @Nullable Void unused) { if (event.getConnectionDescription() != null && event.getConnectionDescription().getServerAddress() != null) { - return event.getConnectionDescription().getServerAddress().getSocketAddress(); - } else { - return null; + return event.getConnectionDescription().getServerAddress().getHost(); } + return null; } - @Override @Nullable - public String transport(CommandStartedEvent commandStartedEvent, @Nullable Void unused) { + @Override + public Integer peerPort(CommandStartedEvent event, @Nullable Void unused) { + if (event.getConnectionDescription() != null + && event.getConnectionDescription().getServerAddress() != null) { + return event.getConnectionDescription().getServerAddress().getPort(); + } return null; } } diff --git a/instrumentation/mongo/mongo-common/testing/src/main/groovy/io/opentelemetry/instrumentation/mongo/testing/AbstractMongoClientTest.groovy b/instrumentation/mongo/mongo-common/testing/src/main/groovy/io/opentelemetry/instrumentation/mongo/testing/AbstractMongoClientTest.groovy index 2166382aa421..4d12f5aad546 100644 --- a/instrumentation/mongo/mongo-common/testing/src/main/groovy/io/opentelemetry/instrumentation/mongo/testing/AbstractMongoClientTest.groovy +++ b/instrumentation/mongo/mongo-common/testing/src/main/groovy/io/opentelemetry/instrumentation/mongo/testing/AbstractMongoClientTest.groovy @@ -389,7 +389,6 @@ abstract class AbstractMongoClientTest extends InstrumentationSpecification { attributes { "$SemanticAttributes.NET_PEER_NAME" "localhost" "$SemanticAttributes.NET_PEER_PORT" port - "net.sock.peer.addr" "127.0.0.1" "$SemanticAttributes.DB_STATEMENT" { statementEval.call(it.replaceAll(" ", "")) } diff --git a/instrumentation/netty/netty-3.8/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/netty/v3_8/client/NettyConnectNetAttributesGetter.java b/instrumentation/netty/netty-3.8/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/netty/v3_8/client/NettyConnectNetAttributesGetter.java index e15feaa5c129..072dc10ccca3 100644 --- a/instrumentation/netty/netty-3.8/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/netty/v3_8/client/NettyConnectNetAttributesGetter.java +++ b/instrumentation/netty/netty-3.8/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/netty/v3_8/client/NettyConnectNetAttributesGetter.java @@ -19,25 +19,42 @@ final class NettyConnectNetAttributesGetter extends InetSocketAddressNetClientAttributesGetter { + @Override + public String transport(NettyConnectionRequest request, @Nullable Channel channel) { + return channel instanceof DatagramChannel ? IP_UDP : IP_TCP; + } + @Nullable @Override - public InetSocketAddress getAddress(NettyConnectionRequest request, @Nullable Channel channel) { - SocketAddress remoteAddress = null; - if (channel != null) { - remoteAddress = channel.getRemoteAddress(); - } - // remote address on end() may be null when connection hasn't been established - if (remoteAddress == null) { - remoteAddress = request.remoteAddressOnStart(); + public String peerName(NettyConnectionRequest request, @Nullable Channel channel) { + SocketAddress requestedAddress = request.remoteAddressOnStart(); + if (requestedAddress instanceof InetSocketAddress) { + return ((InetSocketAddress) requestedAddress).getHostString(); } - if (remoteAddress instanceof InetSocketAddress) { - return (InetSocketAddress) remoteAddress; + return null; + } + + @Nullable + @Override + public Integer peerPort(NettyConnectionRequest request, @Nullable Channel channel) { + SocketAddress requestedAddress = request.remoteAddressOnStart(); + if (requestedAddress instanceof InetSocketAddress) { + return ((InetSocketAddress) requestedAddress).getPort(); } return null; } + @Nullable @Override - public String transport(NettyConnectionRequest request, @Nullable Channel channel) { - return channel instanceof DatagramChannel ? IP_UDP : IP_TCP; + protected InetSocketAddress getPeerSocketAddress( + NettyConnectionRequest request, @Nullable Channel channel) { + if (channel == null) { + return null; + } + SocketAddress remoteAddress = channel.getRemoteAddress(); + if (remoteAddress instanceof InetSocketAddress) { + return (InetSocketAddress) remoteAddress; + } + return null; } } diff --git a/instrumentation/netty/netty-3.8/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/netty/v3_8/client/NettyNetClientAttributesGetter.java b/instrumentation/netty/netty-3.8/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/netty/v3_8/client/NettyNetClientAttributesGetter.java index 8364398b0618..e31dbedeff1c 100644 --- a/instrumentation/netty/netty-3.8/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/netty/v3_8/client/NettyNetClientAttributesGetter.java +++ b/instrumentation/netty/netty-3.8/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/netty/v3_8/client/NettyNetClientAttributesGetter.java @@ -5,31 +5,48 @@ package io.opentelemetry.javaagent.instrumentation.netty.v3_8.client; +import static io.opentelemetry.semconv.trace.attributes.SemanticAttributes.NetTransportValues.IP_TCP; +import static io.opentelemetry.semconv.trace.attributes.SemanticAttributes.NetTransportValues.IP_UDP; + import io.opentelemetry.instrumentation.api.instrumenter.net.InetSocketAddressNetClientAttributesGetter; import io.opentelemetry.javaagent.instrumentation.netty.v3_8.HttpRequestAndChannel; import java.net.InetSocketAddress; import java.net.SocketAddress; import javax.annotation.Nullable; +import org.jboss.netty.channel.socket.DatagramChannel; import org.jboss.netty.handler.codec.http.HttpResponse; final class NettyNetClientAttributesGetter extends InetSocketAddressNetClientAttributesGetter { @Override - @Nullable - public InetSocketAddress getAddress( + public String transport( HttpRequestAndChannel requestAndChannel, @Nullable HttpResponse response) { - SocketAddress address = requestAndChannel.channel().getRemoteAddress(); - if (address instanceof InetSocketAddress) { - return (InetSocketAddress) address; - } + return requestAndChannel.channel() instanceof DatagramChannel ? IP_UDP : IP_TCP; + } + + @Nullable + @Override + public String peerName( + HttpRequestAndChannel requestAndChannel, @Nullable HttpResponse httpResponse) { + return null; + } + + @Nullable + @Override + public Integer peerPort( + HttpRequestAndChannel requestAndChannel, @Nullable HttpResponse httpResponse) { return null; } @Override @Nullable - public String transport( + protected InetSocketAddress getPeerSocketAddress( HttpRequestAndChannel requestAndChannel, @Nullable HttpResponse response) { + SocketAddress address = requestAndChannel.channel().getRemoteAddress(); + if (address instanceof InetSocketAddress) { + return (InetSocketAddress) address; + } return null; } } diff --git a/instrumentation/netty/netty-3.8/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/netty/v3_8/server/NettyNetServerAttributesGetter.java b/instrumentation/netty/netty-3.8/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/netty/v3_8/server/NettyNetServerAttributesGetter.java index bed71eb95b02..25be717c9fb7 100644 --- a/instrumentation/netty/netty-3.8/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/netty/v3_8/server/NettyNetServerAttributesGetter.java +++ b/instrumentation/netty/netty-3.8/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/netty/v3_8/server/NettyNetServerAttributesGetter.java @@ -19,18 +19,39 @@ final class NettyNetServerAttributesGetter extends InetSocketAddressNetServerAttributesGetter { @Override - @Nullable public String transport(HttpRequestAndChannel requestAndChannel) { return requestAndChannel.channel() instanceof DatagramChannel ? IP_UDP : IP_TCP; } + @Nullable @Override + public String hostName(HttpRequestAndChannel requestAndChannel) { + return null; + } + @Nullable - public InetSocketAddress getAddress(HttpRequestAndChannel requestAndChannel) { + @Override + public Integer hostPort(HttpRequestAndChannel requestAndChannel) { + return null; + } + + @Override + @Nullable + protected InetSocketAddress getPeerSocketAddress(HttpRequestAndChannel requestAndChannel) { SocketAddress address = requestAndChannel.channel().getRemoteAddress(); if (address instanceof InetSocketAddress) { return (InetSocketAddress) address; } return null; } + + @Nullable + @Override + protected InetSocketAddress getHostSocketAddress(HttpRequestAndChannel requestAndChannel) { + SocketAddress address = requestAndChannel.channel().getLocalAddress(); + if (address instanceof InetSocketAddress) { + return (InetSocketAddress) address; + } + return null; + } } diff --git a/instrumentation/netty/netty-3.8/javaagent/src/test/groovy/Netty38ClientTest.groovy b/instrumentation/netty/netty-3.8/javaagent/src/test/groovy/Netty38ClientTest.groovy index a199eb4d7852..71991eb3ce81 100644 --- a/instrumentation/netty/netty-3.8/javaagent/src/test/groovy/Netty38ClientTest.groovy +++ b/instrumentation/netty/netty-3.8/javaagent/src/test/groovy/Netty38ClientTest.groovy @@ -15,6 +15,7 @@ import io.opentelemetry.context.Scope import io.opentelemetry.instrumentation.test.AgentTestTrait import io.opentelemetry.instrumentation.test.base.HttpClientTest import io.opentelemetry.instrumentation.testing.junit.http.AbstractHttpClientTest +import io.opentelemetry.semconv.trace.attributes.SemanticAttributes import spock.lang.AutoCleanup import spock.lang.Shared @@ -128,7 +129,10 @@ class Netty38ClientTest extends HttpClientTest implements AgentTestTrai case "http://192.0.2.1/": // non routable address return [] } - return super.httpAttributes(uri) + def attributes = super.httpAttributes(uri) + attributes.remove(SemanticAttributes.NET_PEER_NAME) + attributes.remove(SemanticAttributes.NET_PEER_PORT) + return attributes } @Override diff --git a/instrumentation/netty/netty-4-common/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/netty/v4/common/client/NettyConnectNetAttributesGetter.java b/instrumentation/netty/netty-4-common/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/netty/v4/common/client/NettyConnectNetAttributesGetter.java index e4eed780c643..dada5cb6f81d 100644 --- a/instrumentation/netty/netty-4-common/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/netty/v4/common/client/NettyConnectNetAttributesGetter.java +++ b/instrumentation/netty/netty-4-common/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/netty/v4/common/client/NettyConnectNetAttributesGetter.java @@ -19,25 +19,42 @@ final class NettyConnectNetAttributesGetter extends InetSocketAddressNetClientAttributesGetter { + @Override + public String transport(NettyConnectionRequest request, @Nullable Channel channel) { + return channel instanceof DatagramChannel ? IP_UDP : IP_TCP; + } + @Nullable @Override - public InetSocketAddress getAddress(NettyConnectionRequest request, @Nullable Channel channel) { - SocketAddress remoteAddress = null; - if (channel != null) { - remoteAddress = channel.remoteAddress(); - } - // remote address on end() may be null when connection hasn't been established - if (remoteAddress == null) { - remoteAddress = request.remoteAddressOnStart(); + public String peerName(NettyConnectionRequest request, @Nullable Channel channel) { + SocketAddress requestedAddress = request.remoteAddressOnStart(); + if (requestedAddress instanceof InetSocketAddress) { + return ((InetSocketAddress) requestedAddress).getHostString(); } - if (remoteAddress instanceof InetSocketAddress) { - return (InetSocketAddress) remoteAddress; + return null; + } + + @Nullable + @Override + public Integer peerPort(NettyConnectionRequest request, @Nullable Channel channel) { + SocketAddress requestedAddress = request.remoteAddressOnStart(); + if (requestedAddress instanceof InetSocketAddress) { + return ((InetSocketAddress) requestedAddress).getPort(); } return null; } + @Nullable @Override - public String transport(NettyConnectionRequest request, @Nullable Channel channel) { - return channel instanceof DatagramChannel ? IP_UDP : IP_TCP; + protected InetSocketAddress getPeerSocketAddress( + NettyConnectionRequest request, @Nullable Channel channel) { + if (channel == null) { + return null; + } + SocketAddress remoteAddress = channel.remoteAddress(); + if (remoteAddress instanceof InetSocketAddress) { + return (InetSocketAddress) remoteAddress; + } + return null; } } diff --git a/instrumentation/netty/netty-4-common/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/netty/v4/common/client/NettyNetClientAttributesGetter.java b/instrumentation/netty/netty-4-common/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/netty/v4/common/client/NettyNetClientAttributesGetter.java index eaa541fd7b5b..c36b802de3aa 100644 --- a/instrumentation/netty/netty-4-common/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/netty/v4/common/client/NettyNetClientAttributesGetter.java +++ b/instrumentation/netty/netty-4-common/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/netty/v4/common/client/NettyNetClientAttributesGetter.java @@ -5,6 +5,10 @@ package io.opentelemetry.javaagent.instrumentation.netty.v4.common.client; +import static io.opentelemetry.semconv.trace.attributes.SemanticAttributes.NetTransportValues.IP_TCP; +import static io.opentelemetry.semconv.trace.attributes.SemanticAttributes.NetTransportValues.IP_UDP; + +import io.netty.channel.socket.DatagramChannel; import io.netty.handler.codec.http.HttpResponse; import io.opentelemetry.instrumentation.api.instrumenter.net.InetSocketAddressNetClientAttributesGetter; import io.opentelemetry.javaagent.instrumentation.netty.v4.common.HttpRequestAndChannel; @@ -16,20 +20,33 @@ final class NettyNetClientAttributesGetter extends InetSocketAddressNetClientAttributesGetter { @Override - @Nullable - public InetSocketAddress getAddress( + public String transport( HttpRequestAndChannel requestAndChannel, @Nullable HttpResponse response) { - SocketAddress address = requestAndChannel.remoteAddress(); - if (address instanceof InetSocketAddress) { - return (InetSocketAddress) address; - } + return requestAndChannel.channel() instanceof DatagramChannel ? IP_UDP : IP_TCP; + } + + @Nullable + @Override + public String peerName( + HttpRequestAndChannel requestAndChannel, @Nullable HttpResponse httpResponse) { + return null; + } + + @Nullable + @Override + public Integer peerPort( + HttpRequestAndChannel requestAndChannel, @Nullable HttpResponse httpResponse) { return null; } @Override @Nullable - public String transport( + protected InetSocketAddress getPeerSocketAddress( HttpRequestAndChannel requestAndChannel, @Nullable HttpResponse response) { + SocketAddress address = requestAndChannel.remoteAddress(); + if (address instanceof InetSocketAddress) { + return (InetSocketAddress) address; + } return null; } } diff --git a/instrumentation/netty/netty-4-common/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/netty/v4/common/client/NettySslNetAttributesGetter.java b/instrumentation/netty/netty-4-common/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/netty/v4/common/client/NettySslNetAttributesGetter.java index a836893583d9..a9462cb0da8b 100644 --- a/instrumentation/netty/netty-4-common/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/netty/v4/common/client/NettySslNetAttributesGetter.java +++ b/instrumentation/netty/netty-4-common/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/netty/v4/common/client/NettySslNetAttributesGetter.java @@ -16,17 +16,29 @@ final class NettySslNetAttributesGetter extends InetSocketAddressNetClientAttributesGetter { + @Override + public String transport(NettySslRequest request, @Nullable Void unused) { + return request.channel() instanceof DatagramChannel ? IP_UDP : IP_TCP; + } + @Nullable @Override - public InetSocketAddress getAddress(NettySslRequest request, @Nullable Void unused) { - if (request.remoteAddress() instanceof InetSocketAddress) { - return (InetSocketAddress) request.remoteAddress(); - } + public String peerName(NettySslRequest nettySslRequest, @Nullable Void unused) { return null; } + @Nullable @Override - public String transport(NettySslRequest request, @Nullable Void unused) { - return request.channel() instanceof DatagramChannel ? IP_UDP : IP_TCP; + public Integer peerPort(NettySslRequest nettySslRequest, @Nullable Void unused) { + return null; + } + + @Nullable + @Override + protected InetSocketAddress getPeerSocketAddress(NettySslRequest request, @Nullable Void unused) { + if (request.remoteAddress() instanceof InetSocketAddress) { + return (InetSocketAddress) request.remoteAddress(); + } + return null; } } diff --git a/instrumentation/netty/netty-4-common/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/netty/v4/common/server/NettyNetServerAttributesGetter.java b/instrumentation/netty/netty-4-common/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/netty/v4/common/server/NettyNetServerAttributesGetter.java index c0247d1ef672..d179254e8d3f 100644 --- a/instrumentation/netty/netty-4-common/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/netty/v4/common/server/NettyNetServerAttributesGetter.java +++ b/instrumentation/netty/netty-4-common/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/netty/v4/common/server/NettyNetServerAttributesGetter.java @@ -19,18 +19,39 @@ final class NettyNetServerAttributesGetter extends InetSocketAddressNetServerAttributesGetter { @Override - @Nullable public String transport(HttpRequestAndChannel requestAndChannel) { return requestAndChannel.channel() instanceof DatagramChannel ? IP_UDP : IP_TCP; } + @Nullable @Override + public String hostName(HttpRequestAndChannel requestAndChannel) { + return null; + } + @Nullable - public InetSocketAddress getAddress(HttpRequestAndChannel requestAndChannel) { + @Override + public Integer hostPort(HttpRequestAndChannel requestAndChannel) { + return null; + } + + @Override + @Nullable + protected InetSocketAddress getPeerSocketAddress(HttpRequestAndChannel requestAndChannel) { SocketAddress address = requestAndChannel.remoteAddress(); if (address instanceof InetSocketAddress) { return (InetSocketAddress) address; } return null; } + + @Nullable + @Override + protected InetSocketAddress getHostSocketAddress(HttpRequestAndChannel requestAndChannel) { + SocketAddress address = requestAndChannel.channel().localAddress(); + if (address instanceof InetSocketAddress) { + return (InetSocketAddress) address; + } + return null; + } } 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 eab3a9f3d217..58a6f8ad3bde 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 @@ -104,9 +104,9 @@ class Netty40ClientSslTest extends AgentInstrumentationSpecification { errorEventWithAnyMessage(SSLHandshakeException) attributes { "$SemanticAttributes.NET_TRANSPORT" IP_TCP - "$SemanticAttributes.NET_PEER_NAME" uri.host - "$SemanticAttributes.NET_PEER_PORT" uri.port "net.sock.peer.addr" { it == "127.0.0.1" || it == null } + "net.sock.peer.name" uri.host + "net.sock.peer.port" uri.port } } } @@ -157,9 +157,9 @@ class Netty40ClientSslTest extends AgentInstrumentationSpecification { childOf span(0) attributes { "$SemanticAttributes.NET_TRANSPORT" IP_TCP - "$SemanticAttributes.NET_PEER_NAME" uri.host - "$SemanticAttributes.NET_PEER_PORT" uri.port "net.sock.peer.addr" { it == "127.0.0.1" || it == null } + "net.sock.peer.name" uri.host + "net.sock.peer.port" uri.port } } span(3) { diff --git a/instrumentation/netty/netty-4.0/javaagent/src/test/groovy/Netty40ClientTest.groovy b/instrumentation/netty/netty-4.0/javaagent/src/test/groovy/Netty40ClientTest.groovy index 37b5707c2ea3..1fe13741b222 100644 --- a/instrumentation/netty/netty-4.0/javaagent/src/test/groovy/Netty40ClientTest.groovy +++ b/instrumentation/netty/netty-4.0/javaagent/src/test/groovy/Netty40ClientTest.groovy @@ -23,6 +23,7 @@ import io.opentelemetry.api.common.AttributeKey import io.opentelemetry.instrumentation.test.AgentTestTrait import io.opentelemetry.instrumentation.test.base.HttpClientTest import io.opentelemetry.instrumentation.testing.junit.http.AbstractHttpClientTest +import io.opentelemetry.semconv.trace.attributes.SemanticAttributes import spock.lang.Shared import java.util.concurrent.CompletableFuture @@ -126,7 +127,10 @@ class Netty40ClientTest extends HttpClientTest implement case "http://192.0.2.1/": // non routable address return [] } - return super.httpAttributes(uri) + def attributes = super.httpAttributes(uri) + attributes.remove(SemanticAttributes.NET_PEER_NAME) + attributes.remove(SemanticAttributes.NET_PEER_PORT) + return attributes } @Override 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 3f760bac6f3d..40de84ee8bf3 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 @@ -118,9 +118,9 @@ class Netty41ClientSslTest extends AgentInstrumentationSpecification { errorEventWithAnyMessage(SSLHandshakeException) attributes { "$SemanticAttributes.NET_TRANSPORT" IP_TCP - "$SemanticAttributes.NET_PEER_NAME" uri.host - "$SemanticAttributes.NET_PEER_PORT" uri.port "net.sock.peer.addr" { it == "127.0.0.1" || it == null } + "net.sock.peer.name" uri.host + "net.sock.peer.port" uri.port } } } @@ -183,9 +183,9 @@ class Netty41ClientSslTest extends AgentInstrumentationSpecification { childOf span(0) attributes { "$SemanticAttributes.NET_TRANSPORT" IP_TCP - "$SemanticAttributes.NET_PEER_NAME" uri.host - "$SemanticAttributes.NET_PEER_PORT" uri.port "net.sock.peer.addr" { it == "127.0.0.1" || it == null } + "net.sock.peer.name" uri.host + "net.sock.peer.port" uri.port } } span(4) { diff --git a/instrumentation/netty/netty-4.1/javaagent/src/test/groovy/Netty41ClientTest.groovy b/instrumentation/netty/netty-4.1/javaagent/src/test/groovy/Netty41ClientTest.groovy index 99268b5c84ba..f4a822216d0a 100644 --- a/instrumentation/netty/netty-4.1/javaagent/src/test/groovy/Netty41ClientTest.groovy +++ b/instrumentation/netty/netty-4.1/javaagent/src/test/groovy/Netty41ClientTest.groovy @@ -31,6 +31,7 @@ import io.opentelemetry.instrumentation.test.base.HttpClientTest import io.opentelemetry.instrumentation.testing.junit.http.AbstractHttpClientTest import io.opentelemetry.instrumentation.testing.junit.http.SingleConnection import io.opentelemetry.javaagent.instrumentation.netty.v4_1.client.HttpClientTracingHandler +import io.opentelemetry.semconv.trace.attributes.SemanticAttributes import spock.lang.Shared import spock.lang.Unroll @@ -154,7 +155,10 @@ class Netty41ClientTest extends HttpClientTest implement case "https://192.0.2.1/": // non routable address return [] } - return super.httpAttributes(uri) + def attributes = super.httpAttributes(uri) + attributes.remove(SemanticAttributes.NET_PEER_NAME) + attributes.remove(SemanticAttributes.NET_PEER_PORT) + return attributes } @Override diff --git a/instrumentation/play/play-ws/play-ws-common/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/playws/PlayWsClientNetAttributesGetter.java b/instrumentation/play/play-ws/play-ws-common/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/playws/PlayWsClientNetAttributesGetter.java index 96df0b34088f..56d4d9ceb0c5 100644 --- a/instrumentation/play/play-ws/play-ws-common/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/playws/PlayWsClientNetAttributesGetter.java +++ b/instrumentation/play/play-ws/play-ws-common/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/playws/PlayWsClientNetAttributesGetter.java @@ -20,9 +20,20 @@ public String transport(Request request, @Nullable Response response) { return SemanticAttributes.NetTransportValues.IP_TCP; } + @Nullable + @Override + public String peerName(Request request, @Nullable Response response) { + return request.getUri().getHost(); + } + + @Override + public Integer peerPort(Request request, @Nullable Response response) { + return request.getUri().getPort(); + } + @Override @Nullable - public InetSocketAddress getAddress(Request request, @Nullable Response response) { + protected InetSocketAddress getPeerSocketAddress(Request request, @Nullable Response response) { if (response != null && response.getRemoteAddress() instanceof InetSocketAddress) { return (InetSocketAddress) response.getRemoteAddress(); } diff --git a/instrumentation/rabbitmq-2.7/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/rabbitmq/RabbitChannelNetAttributesGetter.java b/instrumentation/rabbitmq-2.7/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/rabbitmq/RabbitChannelNetAttributesGetter.java index 3b30d16be2b6..b53c279f753f 100644 --- a/instrumentation/rabbitmq-2.7/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/rabbitmq/RabbitChannelNetAttributesGetter.java +++ b/instrumentation/rabbitmq-2.7/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/rabbitmq/RabbitChannelNetAttributesGetter.java @@ -21,14 +21,13 @@ public String transport(ChannelAndMethod channelAndMethod, @Nullable Void unused @Nullable @Override public String peerName(ChannelAndMethod channelAndMethod, @Nullable Void unused) { - // not using InetAddress.getHostName() since that can trigger reverse name lookup - return channelAndMethod.getChannel().getConnection().getAddress().getHostAddress(); + return null; } @Nullable @Override public Integer peerPort(ChannelAndMethod channelAndMethod, @Nullable Void unused) { - return channelAndMethod.getChannel().getConnection().getPort(); + return null; } @Nullable @@ -37,6 +36,11 @@ public String sockPeerAddr(ChannelAndMethod channelAndMethod, @Nullable Void unu return channelAndMethod.getChannel().getConnection().getAddress().getHostAddress(); } + @Override + public Integer sockPeerPort(ChannelAndMethod channelAndMethod, @Nullable Void unused) { + return channelAndMethod.getChannel().getConnection().getPort(); + } + @Nullable @Override public String sockFamily(ChannelAndMethod channelAndMethod, @Nullable Void unused) { diff --git a/instrumentation/rabbitmq-2.7/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/rabbitmq/RabbitReceiveNetAttributesGetter.java b/instrumentation/rabbitmq-2.7/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/rabbitmq/RabbitReceiveNetAttributesGetter.java index a34f22c4bb9a..00001c74b7fc 100644 --- a/instrumentation/rabbitmq-2.7/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/rabbitmq/RabbitReceiveNetAttributesGetter.java +++ b/instrumentation/rabbitmq-2.7/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/rabbitmq/RabbitReceiveNetAttributesGetter.java @@ -22,14 +22,13 @@ public String transport(ReceiveRequest request, @Nullable GetResponse response) @Nullable @Override public String peerName(ReceiveRequest request, @Nullable GetResponse response) { - // not using InetAddress.getHostName() since that can trigger reverse name lookup - return request.getConnection().getAddress().getHostAddress(); + return null; } @Nullable @Override public Integer peerPort(ReceiveRequest request, @Nullable GetResponse response) { - return request.getConnection().getPort(); + return null; } @Nullable @@ -38,6 +37,12 @@ public String sockPeerAddr(ReceiveRequest request, @Nullable GetResponse respons return request.getConnection().getAddress().getHostAddress(); } + @Nullable + @Override + public Integer sockPeerPort(ReceiveRequest request, @Nullable GetResponse response) { + return request.getConnection().getPort(); + } + @Nullable @Override public String sockFamily(ReceiveRequest request, @Nullable GetResponse response) { diff --git a/instrumentation/rabbitmq-2.7/javaagent/src/test/groovy/RabbitMqTest.groovy b/instrumentation/rabbitmq-2.7/javaagent/src/test/groovy/RabbitMqTest.groovy index dd951341523c..6a4ea6d5a6d6 100644 --- a/instrumentation/rabbitmq-2.7/javaagent/src/test/groovy/RabbitMqTest.groovy +++ b/instrumentation/rabbitmq-2.7/javaagent/src/test/groovy/RabbitMqTest.groovy @@ -394,11 +394,9 @@ class RabbitMqTest extends AgentInstrumentationSpecification implements WithRabb } attributes { - // "localhost" on linux, "127.0.0.1" on windows if (spanKind != CONSUMER) { - "$SemanticAttributes.NET_PEER_NAME" { it == "localhost" || it == "127.0.0.1" || it == "0:0:0:0:0:0:0:1" } - "$SemanticAttributes.NET_PEER_PORT" Long "net.sock.peer.addr" { it == "127.0.0.1" || it == "0:0:0:0:0:0:0:1" || it == null } + "net.sock.peer.port" Long "net.sock.family" { it == null || it == "inet6" } } diff --git a/instrumentation/rabbitmq-2.7/javaagent/src/test/groovy/ReactorRabbitMqTest.groovy b/instrumentation/rabbitmq-2.7/javaagent/src/test/groovy/ReactorRabbitMqTest.groovy index d56fbcee72b8..e9e8012c3d0e 100644 --- a/instrumentation/rabbitmq-2.7/javaagent/src/test/groovy/ReactorRabbitMqTest.groovy +++ b/instrumentation/rabbitmq-2.7/javaagent/src/test/groovy/ReactorRabbitMqTest.groovy @@ -37,10 +37,8 @@ class ReactorRabbitMqTest extends AgentInstrumentationSpecification implements W name 'exchange.declare' kind SpanKind.CLIENT attributes { - // "localhost" on linux, "127.0.0.1" on windows - "$SemanticAttributes.NET_PEER_NAME" { it == "localhost" || it == "127.0.0.1" || it == "0:0:0:0:0:0:0:1" } - "$SemanticAttributes.NET_PEER_PORT" Long "net.sock.peer.addr" { it == "127.0.0.1" || it == "0:0:0:0:0:0:0:1" || it == null } + "net.sock.peer.port" Long "net.sock.family" { it == null || it == "inet6" } "$SemanticAttributes.MESSAGING_SYSTEM" "rabbitmq" "$SemanticAttributes.MESSAGING_DESTINATION_KIND" "queue" diff --git a/instrumentation/ratpack/ratpack-1.4/javaagent/src/test/groovy/server/RatpackRoutesTest.groovy b/instrumentation/ratpack/ratpack-1.4/javaagent/src/test/groovy/server/RatpackRoutesTest.groovy index d2846e0d0f40..e52bd4f93c1e 100644 --- a/instrumentation/ratpack/ratpack-1.4/javaagent/src/test/groovy/server/RatpackRoutesTest.groovy +++ b/instrumentation/ratpack/ratpack-1.4/javaagent/src/test/groovy/server/RatpackRoutesTest.groovy @@ -5,7 +5,6 @@ package server - import io.opentelemetry.instrumentation.ratpack.server.AbstractRatpackRoutesTest import io.opentelemetry.instrumentation.test.AgentTestTrait import ratpack.server.RatpackServerSpec diff --git a/instrumentation/ratpack/ratpack-1.4/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/ratpack/RatpackForkedHttpClientTest.java b/instrumentation/ratpack/ratpack-1.4/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/ratpack/RatpackForkedHttpClientTest.java index ed453607a20d..84dee00acc4e 100644 --- a/instrumentation/ratpack/ratpack-1.4/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/ratpack/RatpackForkedHttpClientTest.java +++ b/instrumentation/ratpack/ratpack-1.4/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/ratpack/RatpackForkedHttpClientTest.java @@ -5,13 +5,27 @@ package io.opentelemetry.javaagent.instrumentation.ratpack; +import io.opentelemetry.api.common.AttributeKey; import io.opentelemetry.instrumentation.ratpack.client.AbstractRatpackForkedHttpClientTest; import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension; import io.opentelemetry.instrumentation.testing.junit.http.HttpClientInstrumentationExtension; +import io.opentelemetry.semconv.trace.attributes.SemanticAttributes; +import java.net.URI; +import java.util.HashSet; +import java.util.Set; import org.junit.jupiter.api.extension.RegisterExtension; class RatpackForkedHttpClientTest extends AbstractRatpackForkedHttpClientTest { @RegisterExtension static final InstrumentationExtension testing = HttpClientInstrumentationExtension.forAgent(); + + @Override + protected Set> computeHttpAttributes(URI uri) { + Set> attributes = new HashSet<>(super.computeHttpAttributes(uri)); + // underlying netty instrumentation does not provide these + attributes.remove(SemanticAttributes.NET_PEER_NAME); + attributes.remove(SemanticAttributes.NET_PEER_PORT); + return attributes; + } } diff --git a/instrumentation/ratpack/ratpack-1.4/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/ratpack/RatpackHttpClientTest.java b/instrumentation/ratpack/ratpack-1.4/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/ratpack/RatpackHttpClientTest.java index 13b941a21879..19ec05c26707 100644 --- a/instrumentation/ratpack/ratpack-1.4/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/ratpack/RatpackHttpClientTest.java +++ b/instrumentation/ratpack/ratpack-1.4/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/ratpack/RatpackHttpClientTest.java @@ -5,13 +5,27 @@ package io.opentelemetry.javaagent.instrumentation.ratpack; +import io.opentelemetry.api.common.AttributeKey; import io.opentelemetry.instrumentation.ratpack.client.AbstractRatpackHttpClientTest; import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension; import io.opentelemetry.instrumentation.testing.junit.http.HttpClientInstrumentationExtension; +import io.opentelemetry.semconv.trace.attributes.SemanticAttributes; +import java.net.URI; +import java.util.HashSet; +import java.util.Set; import org.junit.jupiter.api.extension.RegisterExtension; class RatpackHttpClientTest extends AbstractRatpackHttpClientTest { @RegisterExtension static final InstrumentationExtension testing = HttpClientInstrumentationExtension.forAgent(); + + @Override + protected Set> computeHttpAttributes(URI uri) { + Set> attributes = new HashSet<>(super.computeHttpAttributes(uri)); + // underlying netty instrumentation does not provide these + attributes.remove(SemanticAttributes.NET_PEER_NAME); + attributes.remove(SemanticAttributes.NET_PEER_PORT); + return attributes; + } } diff --git a/instrumentation/ratpack/ratpack-1.4/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/ratpack/RatpackPooledHttpClientTest.java b/instrumentation/ratpack/ratpack-1.4/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/ratpack/RatpackPooledHttpClientTest.java index 1b5863d5b0b1..4ebc83c89b26 100644 --- a/instrumentation/ratpack/ratpack-1.4/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/ratpack/RatpackPooledHttpClientTest.java +++ b/instrumentation/ratpack/ratpack-1.4/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/ratpack/RatpackPooledHttpClientTest.java @@ -5,13 +5,27 @@ package io.opentelemetry.javaagent.instrumentation.ratpack; +import io.opentelemetry.api.common.AttributeKey; import io.opentelemetry.instrumentation.ratpack.client.AbstractRatpackPooledHttpClientTest; import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension; import io.opentelemetry.instrumentation.testing.junit.http.HttpClientInstrumentationExtension; +import io.opentelemetry.semconv.trace.attributes.SemanticAttributes; +import java.net.URI; +import java.util.HashSet; +import java.util.Set; import org.junit.jupiter.api.extension.RegisterExtension; class RatpackPooledHttpClientTest extends AbstractRatpackPooledHttpClientTest { @RegisterExtension static final InstrumentationExtension testing = HttpClientInstrumentationExtension.forAgent(); + + @Override + protected Set> computeHttpAttributes(URI uri) { + Set> attributes = new HashSet<>(super.computeHttpAttributes(uri)); + // underlying netty instrumentation does not provide these + attributes.remove(SemanticAttributes.NET_PEER_NAME); + attributes.remove(SemanticAttributes.NET_PEER_PORT); + return attributes; + } } diff --git a/instrumentation/ratpack/ratpack-1.4/testing/src/main/groovy/io/opentelemetry/instrumentation/ratpack/server/AbstractRatpackRoutesTest.groovy b/instrumentation/ratpack/ratpack-1.4/testing/src/main/groovy/io/opentelemetry/instrumentation/ratpack/server/AbstractRatpackRoutesTest.groovy index 7dc4910c903b..9025bb3a8ea7 100644 --- a/instrumentation/ratpack/ratpack-1.4/testing/src/main/groovy/io/opentelemetry/instrumentation/ratpack/server/AbstractRatpackRoutesTest.groovy +++ b/instrumentation/ratpack/ratpack-1.4/testing/src/main/groovy/io/opentelemetry/instrumentation/ratpack/server/AbstractRatpackRoutesTest.groovy @@ -96,9 +96,12 @@ abstract class AbstractRatpackRoutesTest extends InstrumentationSpecification { hasNoParent() attributes { "$SemanticAttributes.NET_TRANSPORT" IP_TCP + "$SemanticAttributes.NET_HOST_NAME" { it == "localhost" || it == null } + "$SemanticAttributes.NET_HOST_PORT" { it instanceof Long || it == null } "net.sock.peer.addr" { it == "127.0.0.1" || it == null } - "net.sock.peer.port" Long - + "net.sock.peer.port" { it instanceof Long || it == null } + "net.sock.host.addr" { it == "127.0.0.1" || it == null } + "net.sock.host.port" { it instanceof Long || it == null } "$SemanticAttributes.HTTP_METHOD" "GET" "$SemanticAttributes.HTTP_STATUS_CODE" 200 "$SemanticAttributes.HTTP_FLAVOR" "1.1" diff --git a/instrumentation/ratpack/ratpack-1.4/testing/src/main/java/io/opentelemetry/instrumentation/ratpack/client/AbstractRatpackHttpClientTest.java b/instrumentation/ratpack/ratpack-1.4/testing/src/main/java/io/opentelemetry/instrumentation/ratpack/client/AbstractRatpackHttpClientTest.java index 010bcb1ece39..53d0df256874 100644 --- a/instrumentation/ratpack/ratpack-1.4/testing/src/main/java/io/opentelemetry/instrumentation/ratpack/client/AbstractRatpackHttpClientTest.java +++ b/instrumentation/ratpack/ratpack-1.4/testing/src/main/java/io/opentelemetry/instrumentation/ratpack/client/AbstractRatpackHttpClientTest.java @@ -7,12 +7,14 @@ import io.netty.channel.ConnectTimeoutException; import io.netty.handler.timeout.ReadTimeoutException; +import io.opentelemetry.api.common.AttributeKey; import io.opentelemetry.instrumentation.testing.junit.http.AbstractHttpClientTest; import io.opentelemetry.instrumentation.testing.junit.http.HttpClientTestOptions; import java.net.URI; import java.time.Duration; import java.util.Collections; import java.util.Map; +import java.util.Set; import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.condition.OS; @@ -139,16 +141,7 @@ protected void configure(HttpClientTestOptions options) { return exception; }); - options.setHttpAttributes( - uri -> { - switch (uri.toString()) { - case "http://localhost:61/": // unopened port - case "https://192.0.2.1/": // non routable address - return Collections.emptySet(); - default: - return HttpClientTestOptions.DEFAULT_HTTP_ATTRIBUTES; - } - }); + options.setHttpAttributes(this::computeHttpAttributes); options.disableTestRedirects(); @@ -157,4 +150,14 @@ protected void configure(HttpClientTestOptions options) { options.enableTestReadTimeout(); } + + protected Set> computeHttpAttributes(URI uri) { + switch (uri.toString()) { + case "http://localhost:61/": // unopened port + case "https://192.0.2.1/": // non routable address + return Collections.emptySet(); + default: + return HttpClientTestOptions.DEFAULT_HTTP_ATTRIBUTES; + } + } } diff --git a/instrumentation/ratpack/ratpack-1.7/library/src/main/java/io/opentelemetry/instrumentation/ratpack/internal/RatpackHttpNetAttributesGetter.java b/instrumentation/ratpack/ratpack-1.7/library/src/main/java/io/opentelemetry/instrumentation/ratpack/internal/RatpackHttpNetAttributesGetter.java index 8d5cfb8674ea..71843670198c 100644 --- a/instrumentation/ratpack/ratpack-1.7/library/src/main/java/io/opentelemetry/instrumentation/ratpack/internal/RatpackHttpNetAttributesGetter.java +++ b/instrumentation/ratpack/ratpack-1.7/library/src/main/java/io/opentelemetry/instrumentation/ratpack/internal/RatpackHttpNetAttributesGetter.java @@ -17,6 +17,7 @@ */ public final class RatpackHttpNetAttributesGetter implements NetClientAttributesGetter { + @Override public String transport(RequestSpec request, @Nullable HttpResponse response) { return SemanticAttributes.NetTransportValues.IP_TCP; diff --git a/instrumentation/ratpack/ratpack-1.7/library/src/main/java/io/opentelemetry/instrumentation/ratpack/internal/RatpackNetAttributesGetter.java b/instrumentation/ratpack/ratpack-1.7/library/src/main/java/io/opentelemetry/instrumentation/ratpack/internal/RatpackNetAttributesGetter.java index 4c24558fba46..d52e1ee15e66 100644 --- a/instrumentation/ratpack/ratpack-1.7/library/src/main/java/io/opentelemetry/instrumentation/ratpack/internal/RatpackNetAttributesGetter.java +++ b/instrumentation/ratpack/ratpack-1.7/library/src/main/java/io/opentelemetry/instrumentation/ratpack/internal/RatpackNetAttributesGetter.java @@ -8,7 +8,9 @@ import io.opentelemetry.instrumentation.api.instrumenter.net.NetServerAttributesGetter; import io.opentelemetry.semconv.trace.attributes.SemanticAttributes; import javax.annotation.Nullable; +import ratpack.handling.Context; import ratpack.http.Request; +import ratpack.server.PublicAddress; /** * This class is internal and is hence not for public use. Its APIs are unstable and can change at @@ -20,14 +22,30 @@ public String transport(Request request) { return SemanticAttributes.NetTransportValues.IP_TCP; } + @Nullable @Override - public Integer sockPeerPort(Request request) { - return request.getRemoteAddress().getPort(); + public String hostName(Request request) { + PublicAddress publicAddress = getPublicAddress(request); + return publicAddress == null ? null : publicAddress.get().getHost(); } - @Override @Nullable - public String sockPeerAddr(Request request) { - return null; + @Override + public Integer hostPort(Request request) { + PublicAddress publicAddress = getPublicAddress(request); + return publicAddress == null ? null : publicAddress.get().getPort(); + } + + private static PublicAddress getPublicAddress(Request request) { + Context ratpackContext = request.get(Context.class); + if (ratpackContext == null) { + return null; + } + return ratpackContext.get(PublicAddress.class); + } + + @Override + public Integer sockPeerPort(Request request) { + return request.getRemoteAddress().getPort(); } } diff --git a/instrumentation/ratpack/ratpack-1.7/library/src/test/groovy/io/opentelemetry/instrumentation/ratpack/server/RatpackRoutesTest.groovy b/instrumentation/ratpack/ratpack-1.7/library/src/test/groovy/io/opentelemetry/instrumentation/ratpack/server/RatpackRoutesTest.groovy index 0498f3bd2171..e714669f29ea 100644 --- a/instrumentation/ratpack/ratpack-1.7/library/src/test/groovy/io/opentelemetry/instrumentation/ratpack/server/RatpackRoutesTest.groovy +++ b/instrumentation/ratpack/ratpack-1.7/library/src/test/groovy/io/opentelemetry/instrumentation/ratpack/server/RatpackRoutesTest.groovy @@ -5,7 +5,6 @@ package io.opentelemetry.instrumentation.ratpack.server - import io.opentelemetry.instrumentation.ratpack.RatpackTelemetry import io.opentelemetry.instrumentation.test.LibraryTestTrait import ratpack.server.RatpackServerSpec diff --git a/instrumentation/reactor/reactor-netty/reactor-netty-0.9/javaagent/src/test/groovy/io/opentelemetry/javaagent/instrumentation/reactornetty/v0_9/AbstractReactorNettyHttpClientTest.groovy b/instrumentation/reactor/reactor-netty/reactor-netty-0.9/javaagent/src/test/groovy/io/opentelemetry/javaagent/instrumentation/reactornetty/v0_9/AbstractReactorNettyHttpClientTest.groovy index bd2b38e5c902..42e99ae013f1 100644 --- a/instrumentation/reactor/reactor-netty/reactor-netty-0.9/javaagent/src/test/groovy/io/opentelemetry/javaagent/instrumentation/reactornetty/v0_9/AbstractReactorNettyHttpClientTest.groovy +++ b/instrumentation/reactor/reactor-netty/reactor-netty-0.9/javaagent/src/test/groovy/io/opentelemetry/javaagent/instrumentation/reactornetty/v0_9/AbstractReactorNettyHttpClientTest.groovy @@ -13,6 +13,7 @@ import io.opentelemetry.instrumentation.test.AgentTestTrait import io.opentelemetry.instrumentation.test.base.HttpClientTest import io.opentelemetry.instrumentation.testing.junit.http.AbstractHttpClientTest import io.opentelemetry.sdk.trace.data.SpanData +import io.opentelemetry.semconv.trace.attributes.SemanticAttributes import reactor.netty.http.client.HttpClient import java.util.concurrent.CountDownLatch @@ -100,7 +101,10 @@ abstract class AbstractReactorNettyHttpClientTest extends HttpClientTest { - // unopened port or non routable address - if ("http://localhost:61/".equals(uri.toString()) - || "https://192.0.2.1/".equals(uri.toString())) { - return emptySet(); - } + options.setHttpAttributes(this::getHttpAttributes); + } - Set> attributes = - new HashSet<>(HttpClientTestOptions.DEFAULT_HTTP_ATTRIBUTES); - if (uri.toString().contains("/read-timeout")) { - attributes.remove(SemanticAttributes.NET_PEER_NAME); - attributes.remove(SemanticAttributes.NET_PEER_PORT); - attributes.remove(SemanticAttributes.HTTP_FLAVOR); - } - return attributes; - }); + protected Set> getHttpAttributes(URI uri) { + // unopened port or non routable address + if ("http://localhost:61/".equals(uri.toString()) + || "https://192.0.2.1/".equals(uri.toString())) { + return emptySet(); + } + + Set> attributes = new HashSet<>(HttpClientTestOptions.DEFAULT_HTTP_ATTRIBUTES); + if (uri.toString().contains("/read-timeout")) { + attributes.remove(SemanticAttributes.NET_PEER_NAME); + attributes.remove(SemanticAttributes.NET_PEER_PORT); + attributes.remove(SemanticAttributes.HTTP_FLAVOR); + } + return attributes; } @Test diff --git a/instrumentation/reactor/reactor-netty/reactor-netty-1.0/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/reactornetty/v1_0/ReactorNettyClientSslTest.java b/instrumentation/reactor/reactor-netty/reactor-netty-1.0/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/reactornetty/v1_0/ReactorNettyClientSslTest.java index 962793afc732..ebd77be51e24 100644 --- a/instrumentation/reactor/reactor-netty/reactor-netty-1.0/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/reactornetty/v1_0/ReactorNettyClientSslTest.java +++ b/instrumentation/reactor/reactor-netty/reactor-netty-1.0/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/reactornetty/v1_0/ReactorNettyClientSslTest.java @@ -5,6 +5,7 @@ package io.opentelemetry.javaagent.instrumentation.reactornetty.v1_0; +import static io.opentelemetry.api.common.AttributeKey.longKey; import static io.opentelemetry.api.common.AttributeKey.stringKey; import static io.opentelemetry.api.trace.SpanKind.CLIENT; import static io.opentelemetry.api.trace.SpanKind.INTERNAL; @@ -92,7 +93,9 @@ void shouldFailSslHandshake() throws SSLException { .hasEventsSatisfying(ReactorNettyClientSslTest::isSslHandshakeException) .hasAttributesSatisfyingExactly( equalTo(SemanticAttributes.HTTP_METHOD, "GET"), - equalTo(SemanticAttributes.HTTP_URL, uri)), + equalTo(SemanticAttributes.HTTP_URL, uri), + equalTo(SemanticAttributes.NET_PEER_NAME, "localhost"), + equalTo(SemanticAttributes.NET_PEER_PORT, server.httpsPort())), span -> span.hasName("RESOLVE") .hasKind(INTERNAL) @@ -120,9 +123,9 @@ void shouldFailSslHandshake() throws SSLException { .hasEventsSatisfying(ReactorNettyClientSslTest::isSslHandshakeException) .hasAttributesSatisfyingExactly( equalTo(SemanticAttributes.NET_TRANSPORT, IP_TCP), - equalTo(SemanticAttributes.NET_PEER_NAME, "localhost"), - equalTo(SemanticAttributes.NET_PEER_PORT, server.httpsPort()), - equalTo(stringKey("net.sock.peer.addr"), "127.0.0.1")))); + equalTo(stringKey("net.sock.peer.addr"), "127.0.0.1"), + equalTo(stringKey("net.sock.peer.name"), "localhost"), + equalTo(longKey("net.sock.peer.port"), server.httpsPort())))); } @Test @@ -184,9 +187,9 @@ void shouldSuccessfullyEstablishSslHandshake() throws SSLException { .hasParent(trace.getSpan(1)) .hasAttributesSatisfyingExactly( equalTo(SemanticAttributes.NET_TRANSPORT, IP_TCP), - equalTo(SemanticAttributes.NET_PEER_NAME, "localhost"), - equalTo(SemanticAttributes.NET_PEER_PORT, server.httpsPort()), - equalTo(stringKey("net.sock.peer.addr"), "127.0.0.1")), + equalTo(stringKey("net.sock.peer.addr"), "127.0.0.1"), + equalTo(stringKey("net.sock.peer.name"), "localhost"), + equalTo(longKey("net.sock.peer.port"), server.httpsPort())), span -> span.hasName("test-http-server").hasKind(SERVER).hasParent(trace.getSpan(1)))); } diff --git a/instrumentation/reactor/reactor-netty/reactor-netty-1.0/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/reactornetty/v1_0/ReactorNettyConnectionSpanTest.java b/instrumentation/reactor/reactor-netty/reactor-netty-1.0/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/reactornetty/v1_0/ReactorNettyConnectionSpanTest.java index 8da1c1cd61de..1eadacd3d9ea 100644 --- a/instrumentation/reactor/reactor-netty/reactor-netty-1.0/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/reactornetty/v1_0/ReactorNettyConnectionSpanTest.java +++ b/instrumentation/reactor/reactor-netty/reactor-netty-1.0/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/reactornetty/v1_0/ReactorNettyConnectionSpanTest.java @@ -153,7 +153,9 @@ void testFailingRequest() { .hasException(connectException) .hasAttributesSatisfyingExactly( equalTo(SemanticAttributes.HTTP_METHOD, "GET"), - equalTo(SemanticAttributes.HTTP_URL, uri)), + equalTo(SemanticAttributes.HTTP_URL, uri), + equalTo(SemanticAttributes.NET_PEER_NAME, "localhost"), + equalTo(SemanticAttributes.NET_PEER_PORT, PortUtils.UNUSABLE_PORT)), span -> span.hasName("RESOLVE") .hasKind(INTERNAL) @@ -172,6 +174,8 @@ void testFailingRequest() { equalTo(SemanticAttributes.NET_TRANSPORT, IP_TCP), equalTo(SemanticAttributes.NET_PEER_NAME, "localhost"), equalTo(SemanticAttributes.NET_PEER_PORT, PortUtils.UNUSABLE_PORT), - equalTo(stringKey("net.sock.peer.addr"), "127.0.0.1")))); + satisfies( + stringKey("net.sock.peer.addr"), + val -> val.isIn(null, "127.0.0.1"))))); } } diff --git a/instrumentation/reactor/reactor-netty/reactor-netty-1.0/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/reactornetty/v1_0/ReactorNettyHttpClientTest.java b/instrumentation/reactor/reactor-netty/reactor-netty-1.0/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/reactornetty/v1_0/ReactorNettyHttpClientTest.java index 9fe4971bc058..02b368fd23e7 100644 --- a/instrumentation/reactor/reactor-netty/reactor-netty-1.0/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/reactornetty/v1_0/ReactorNettyHttpClientTest.java +++ b/instrumentation/reactor/reactor-netty/reactor-netty-1.0/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/reactornetty/v1_0/ReactorNettyHttpClientTest.java @@ -6,8 +6,13 @@ package io.opentelemetry.javaagent.instrumentation.reactornetty.v1_0; import io.netty.channel.ChannelOption; +import io.opentelemetry.api.common.AttributeKey; import io.opentelemetry.instrumentation.testing.junit.http.HttpClientTestOptions; +import io.opentelemetry.semconv.trace.attributes.SemanticAttributes; import io.opentelemetry.testing.internal.armeria.common.HttpHeaderNames; +import java.net.URI; +import java.util.HashSet; +import java.util.Set; import reactor.netty.http.client.HttpClient; class ReactorNettyHttpClientTest extends AbstractReactorNettyHttpClientTest { @@ -48,4 +53,18 @@ protected void configure(HttpClientTestOptions options) { .code(); }); } + + @Override + protected Set> getHttpAttributes(URI uri) { + if (uri.toString().contains("/success")) { + // the single connection test does not report net.peer.* attributes; it only reports the + // net.peer.sock.* attributes + Set> attributes = + new HashSet<>(HttpClientTestOptions.DEFAULT_HTTP_ATTRIBUTES); + attributes.remove(SemanticAttributes.NET_PEER_NAME); + attributes.remove(SemanticAttributes.NET_PEER_PORT); + return attributes; + } + return super.getHttpAttributes(uri); + } } diff --git a/instrumentation/redisson/redisson-common/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/redisson/RedissonNetAttributesGetter.java b/instrumentation/redisson/redisson-common/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/redisson/RedissonNetAttributesGetter.java index 0700622183e0..b20329993edf 100644 --- a/instrumentation/redisson/redisson-common/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/redisson/RedissonNetAttributesGetter.java +++ b/instrumentation/redisson/redisson-common/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/redisson/RedissonNetAttributesGetter.java @@ -12,14 +12,26 @@ final class RedissonNetAttributesGetter extends InetSocketAddressNetClientAttributesGetter { + @Nullable @Override - public InetSocketAddress getAddress(RedissonRequest request, @Nullable Void unused) { - return request.getAddress(); + public String transport(RedissonRequest request, @Nullable Void unused) { + return null; } @Nullable @Override - public String transport(RedissonRequest request, @Nullable Void unused) { + public String peerName(RedissonRequest redissonRequest, @Nullable Void unused) { + return null; + } + + @Nullable + @Override + public Integer peerPort(RedissonRequest redissonRequest, @Nullable Void unused) { return null; } + + @Override + protected InetSocketAddress getPeerSocketAddress(RedissonRequest request, @Nullable Void unused) { + return request.getAddress(); + } } diff --git a/instrumentation/redisson/redisson-common/testing/src/main/groovy/AbstractRedissonAsyncClientTest.groovy b/instrumentation/redisson/redisson-common/testing/src/main/groovy/AbstractRedissonAsyncClientTest.groovy index a6fdb2e60222..0e80fd983a7f 100644 --- a/instrumentation/redisson/redisson-common/testing/src/main/groovy/AbstractRedissonAsyncClientTest.groovy +++ b/instrumentation/redisson/redisson-common/testing/src/main/groovy/AbstractRedissonAsyncClientTest.groovy @@ -78,10 +78,10 @@ abstract class AbstractRedissonAsyncClientTest extends AgentInstrumentationSpeci name "SET" kind CLIENT attributes { - "$SemanticAttributes.DB_SYSTEM" "redis" - "$SemanticAttributes.NET_PEER_NAME" "localhost" - "$SemanticAttributes.NET_PEER_PORT" port "net.sock.peer.addr" "127.0.0.1" + "net.sock.peer.name" "localhost" + "net.sock.peer.port" port + "$SemanticAttributes.DB_SYSTEM" "redis" "$SemanticAttributes.DB_STATEMENT" "SET foo ?" "$SemanticAttributes.DB_OPERATION" "SET" } @@ -118,10 +118,10 @@ abstract class AbstractRedissonAsyncClientTest extends AgentInstrumentationSpeci kind CLIENT childOf(span(0)) attributes { - "$SemanticAttributes.DB_SYSTEM" "redis" - "$SemanticAttributes.NET_PEER_NAME" "localhost" - "$SemanticAttributes.NET_PEER_PORT" port "net.sock.peer.addr" "127.0.0.1" + "net.sock.peer.name" "localhost" + "net.sock.peer.port" port + "$SemanticAttributes.DB_SYSTEM" "redis" "$SemanticAttributes.DB_STATEMENT" "SADD set1 ?" "$SemanticAttributes.DB_OPERATION" "SADD" } diff --git a/instrumentation/redisson/redisson-common/testing/src/main/groovy/AbstractRedissonClientTest.groovy b/instrumentation/redisson/redisson-common/testing/src/main/groovy/AbstractRedissonClientTest.groovy index 40a2163073d8..d8b79b39f13b 100644 --- a/instrumentation/redisson/redisson-common/testing/src/main/groovy/AbstractRedissonClientTest.groovy +++ b/instrumentation/redisson/redisson-common/testing/src/main/groovy/AbstractRedissonClientTest.groovy @@ -77,10 +77,10 @@ abstract class AbstractRedissonClientTest extends AgentInstrumentationSpecificat name "SET" kind CLIENT attributes { - "$SemanticAttributes.DB_SYSTEM" "redis" - "$SemanticAttributes.NET_PEER_NAME" "localhost" - "$SemanticAttributes.NET_PEER_PORT" port "net.sock.peer.addr" "127.0.0.1" + "net.sock.peer.name" "localhost" + "net.sock.peer.port" port + "$SemanticAttributes.DB_SYSTEM" "redis" "$SemanticAttributes.DB_STATEMENT" "SET foo ?" "$SemanticAttributes.DB_OPERATION" "SET" } @@ -91,10 +91,10 @@ abstract class AbstractRedissonClientTest extends AgentInstrumentationSpecificat name "GET" kind CLIENT attributes { - "$SemanticAttributes.DB_SYSTEM" "redis" - "$SemanticAttributes.NET_PEER_NAME" "localhost" - "$SemanticAttributes.NET_PEER_PORT" port "net.sock.peer.addr" "127.0.0.1" + "net.sock.peer.name" "localhost" + "net.sock.peer.port" port + "$SemanticAttributes.DB_SYSTEM" "redis" "$SemanticAttributes.DB_STATEMENT" "GET foo" "$SemanticAttributes.DB_OPERATION" "GET" } @@ -117,10 +117,10 @@ abstract class AbstractRedissonClientTest extends AgentInstrumentationSpecificat name "DB Query" kind CLIENT attributes { - "$SemanticAttributes.DB_SYSTEM" "redis" - "$SemanticAttributes.NET_PEER_NAME" "localhost" - "$SemanticAttributes.NET_PEER_PORT" port "net.sock.peer.addr" "127.0.0.1" + "net.sock.peer.name" "localhost" + "net.sock.peer.port" port + "$SemanticAttributes.DB_SYSTEM" "redis" "$SemanticAttributes.DB_STATEMENT" "SET batch1 ?;SET batch2 ?" "$SemanticAttributes.DB_OPERATION" null } @@ -141,10 +141,10 @@ abstract class AbstractRedissonClientTest extends AgentInstrumentationSpecificat name "RPUSH" kind CLIENT attributes { - "$SemanticAttributes.DB_SYSTEM" "redis" - "$SemanticAttributes.NET_PEER_NAME" "localhost" - "$SemanticAttributes.NET_PEER_PORT" port "net.sock.peer.addr" "127.0.0.1" + "net.sock.peer.name" "localhost" + "net.sock.peer.port" port + "$SemanticAttributes.DB_SYSTEM" "redis" "$SemanticAttributes.DB_STATEMENT" "RPUSH list1 ?" "$SemanticAttributes.DB_OPERATION" "RPUSH" } @@ -168,10 +168,10 @@ abstract class AbstractRedissonClientTest extends AgentInstrumentationSpecificat name "EVAL" kind CLIENT attributes { - "$SemanticAttributes.DB_SYSTEM" "redis" - "$SemanticAttributes.NET_PEER_NAME" "localhost" - "$SemanticAttributes.NET_PEER_PORT" port "net.sock.peer.addr" "127.0.0.1" + "net.sock.peer.name" "localhost" + "net.sock.peer.port" port + "$SemanticAttributes.DB_SYSTEM" "redis" "$SemanticAttributes.DB_STATEMENT" "EVAL $script 1 map1 ? ?" "$SemanticAttributes.DB_OPERATION" "EVAL" } @@ -182,10 +182,10 @@ abstract class AbstractRedissonClientTest extends AgentInstrumentationSpecificat name "HGET" kind CLIENT attributes { - "$SemanticAttributes.DB_SYSTEM" "redis" - "$SemanticAttributes.NET_PEER_NAME" "localhost" - "$SemanticAttributes.NET_PEER_PORT" port "net.sock.peer.addr" "127.0.0.1" + "net.sock.peer.name" "localhost" + "net.sock.peer.port" port + "$SemanticAttributes.DB_SYSTEM" "redis" "$SemanticAttributes.DB_STATEMENT" "HGET map1 key1" "$SemanticAttributes.DB_OPERATION" "HGET" } @@ -206,10 +206,10 @@ abstract class AbstractRedissonClientTest extends AgentInstrumentationSpecificat name "SADD" kind CLIENT attributes { - "$SemanticAttributes.DB_SYSTEM" "redis" - "$SemanticAttributes.NET_PEER_NAME" "localhost" - "$SemanticAttributes.NET_PEER_PORT" port "net.sock.peer.addr" "127.0.0.1" + "net.sock.peer.name" "localhost" + "net.sock.peer.port" port + "$SemanticAttributes.DB_SYSTEM" "redis" "$SemanticAttributes.DB_STATEMENT" "SADD set1 ?" "$SemanticAttributes.DB_OPERATION" "SADD" } @@ -234,10 +234,10 @@ abstract class AbstractRedissonClientTest extends AgentInstrumentationSpecificat name "ZADD" kind CLIENT attributes { - "$SemanticAttributes.DB_SYSTEM" "redis" - "$SemanticAttributes.NET_PEER_NAME" "localhost" - "$SemanticAttributes.NET_PEER_PORT" port "net.sock.peer.addr" "127.0.0.1" + "net.sock.peer.name" "localhost" + "net.sock.peer.port" port + "$SemanticAttributes.DB_SYSTEM" "redis" "$SemanticAttributes.DB_STATEMENT" "ZADD sort_set1 ? ? ? ? ? ?" "$SemanticAttributes.DB_OPERATION" "ZADD" } @@ -258,10 +258,10 @@ abstract class AbstractRedissonClientTest extends AgentInstrumentationSpecificat name "INCR" kind CLIENT attributes { - "$SemanticAttributes.DB_SYSTEM" "redis" - "$SemanticAttributes.NET_PEER_NAME" "localhost" - "$SemanticAttributes.NET_PEER_PORT" port "net.sock.peer.addr" "127.0.0.1" + "net.sock.peer.name" "localhost" + "net.sock.peer.port" port + "$SemanticAttributes.DB_SYSTEM" "redis" "$SemanticAttributes.DB_STATEMENT" "INCR AtomicLong" "$SemanticAttributes.DB_OPERATION" "INCR" } @@ -287,10 +287,10 @@ abstract class AbstractRedissonClientTest extends AgentInstrumentationSpecificat name "EVAL" kind CLIENT attributes { - "$SemanticAttributes.DB_SYSTEM" "redis" - "$SemanticAttributes.NET_PEER_NAME" "localhost" - "$SemanticAttributes.NET_PEER_PORT" port "net.sock.peer.addr" "127.0.0.1" + "net.sock.peer.name" "localhost" + "net.sock.peer.port" port + "$SemanticAttributes.DB_SYSTEM" "redis" "$SemanticAttributes.DB_STATEMENT" { lockScriptPattern.matcher(it).matches() } "$SemanticAttributes.DB_OPERATION" "EVAL" } @@ -303,10 +303,10 @@ abstract class AbstractRedissonClientTest extends AgentInstrumentationSpecificat name "EVAL" kind CLIENT attributes { - "$SemanticAttributes.DB_SYSTEM" "redis" - "$SemanticAttributes.NET_PEER_NAME" "localhost" - "$SemanticAttributes.NET_PEER_PORT" port "net.sock.peer.addr" "127.0.0.1" + "net.sock.peer.name" "localhost" + "net.sock.peer.port" port + "$SemanticAttributes.DB_SYSTEM" "redis" "$SemanticAttributes.DB_STATEMENT" { lockScriptPattern.matcher(it).matches() } "$SemanticAttributes.DB_OPERATION" "EVAL" } diff --git a/instrumentation/restlet/restlet-1.0/library/src/main/java/io/opentelemetry/instrumentation/restlet/v1_0/RestletNetAttributesGetter.java b/instrumentation/restlet/restlet-1.0/library/src/main/java/io/opentelemetry/instrumentation/restlet/v1_0/RestletNetAttributesGetter.java index e94f1c6d11e1..d9f8b2ff7598 100644 --- a/instrumentation/restlet/restlet-1.0/library/src/main/java/io/opentelemetry/instrumentation/restlet/v1_0/RestletNetAttributesGetter.java +++ b/instrumentation/restlet/restlet-1.0/library/src/main/java/io/opentelemetry/instrumentation/restlet/v1_0/RestletNetAttributesGetter.java @@ -5,20 +5,32 @@ package io.opentelemetry.instrumentation.restlet.v1_0; +import com.noelios.restlet.http.HttpCall; +import com.noelios.restlet.http.HttpRequest; import io.opentelemetry.instrumentation.api.instrumenter.net.NetServerAttributesGetter; import io.opentelemetry.semconv.trace.attributes.SemanticAttributes; import javax.annotation.Nullable; import org.restlet.data.Request; final class RestletNetAttributesGetter implements NetServerAttributesGetter { + @Override public String transport(Request request) { return SemanticAttributes.NetTransportValues.IP_TCP; } + @Nullable @Override - public Integer sockPeerPort(Request request) { - return request.getClientInfo().getPort(); + public String hostName(Request request) { + HttpCall call = httpCall(request); + return call == null ? null : call.getHostDomain(); + } + + @Nullable + @Override + public Integer hostPort(Request request) { + HttpCall call = httpCall(request); + return call == null ? null : call.getServerPort(); } @Override @@ -26,4 +38,24 @@ public Integer sockPeerPort(Request request) { public String sockPeerAddr(Request request) { return request.getClientInfo().getAddress(); } + + @Override + public Integer sockPeerPort(Request request) { + return request.getClientInfo().getPort(); + } + + @Nullable + @Override + public String sockHostAddr(Request request) { + HttpCall call = httpCall(request); + return call == null ? null : call.getServerAddress(); + } + + @Nullable + private static HttpCall httpCall(Request request) { + if (request instanceof HttpRequest) { + return ((HttpRequest) request).getHttpCall(); + } + return null; + } } diff --git a/instrumentation/restlet/restlet-2.0/library/src/main/java/io/opentelemetry/instrumentation/restlet/v2_0/internal/RestletNetAttributesGetter.java b/instrumentation/restlet/restlet-2.0/library/src/main/java/io/opentelemetry/instrumentation/restlet/v2_0/internal/RestletNetAttributesGetter.java index d883e5e4df3a..6a4f22a48be9 100644 --- a/instrumentation/restlet/restlet-2.0/library/src/main/java/io/opentelemetry/instrumentation/restlet/v2_0/internal/RestletNetAttributesGetter.java +++ b/instrumentation/restlet/restlet-2.0/library/src/main/java/io/opentelemetry/instrumentation/restlet/v2_0/internal/RestletNetAttributesGetter.java @@ -9,16 +9,28 @@ import io.opentelemetry.semconv.trace.attributes.SemanticAttributes; import javax.annotation.Nullable; import org.restlet.Request; +import org.restlet.engine.http.HttpRequest; +import org.restlet.engine.http.ServerCall; final class RestletNetAttributesGetter implements NetServerAttributesGetter { + @Override public String transport(Request request) { return SemanticAttributes.NetTransportValues.IP_TCP; } + @Nullable @Override - public Integer sockPeerPort(Request request) { - return request.getClientInfo().getPort(); + public String hostName(Request request) { + ServerCall call = serverCall(request); + return call == null ? null : call.getHostDomain(); + } + + @Nullable + @Override + public Integer hostPort(Request request) { + ServerCall call = serverCall(request); + return call == null ? null : call.getServerPort(); } @Override @@ -26,4 +38,24 @@ public Integer sockPeerPort(Request request) { public String sockPeerAddr(Request request) { return request.getClientInfo().getAddress(); } + + @Override + public Integer sockPeerPort(Request request) { + return request.getClientInfo().getPort(); + } + + @Nullable + @Override + public String sockHostAddr(Request request) { + ServerCall call = serverCall(request); + return call == null ? null : call.getServerAddress(); + } + + @Nullable + private static ServerCall serverCall(Request request) { + if (request instanceof HttpRequest) { + return ((HttpRequest) request).getHttpCall(); + } + return null; + } } diff --git a/instrumentation/servlet/servlet-2.2/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/servlet/v2_2/Servlet2Accessor.java b/instrumentation/servlet/servlet-2.2/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/servlet/v2_2/Servlet2Accessor.java index 6325f265e1b5..91e1c278e7f1 100644 --- a/instrumentation/servlet/servlet-2.2/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/servlet/v2_2/Servlet2Accessor.java +++ b/instrumentation/servlet/servlet-2.2/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/servlet/v2_2/Servlet2Accessor.java @@ -22,6 +22,16 @@ public Integer getRequestRemotePort(HttpServletRequest httpServletRequest) { return null; } + @Override + public String getRequestLocalAddr(HttpServletRequest request) { + return null; + } + + @Override + public Integer getRequestLocalPort(HttpServletRequest request) { + return null; + } + @Override public void addRequestAsyncListener( HttpServletRequest httpServletRequest, diff --git a/instrumentation/servlet/servlet-3.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/servlet/v3_0/Servlet3Accessor.java b/instrumentation/servlet/servlet-3.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/servlet/v3_0/Servlet3Accessor.java index 68514bdc659a..a5a22189764d 100644 --- a/instrumentation/servlet/servlet-3.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/servlet/v3_0/Servlet3Accessor.java +++ b/instrumentation/servlet/servlet-3.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/servlet/v3_0/Servlet3Accessor.java @@ -26,6 +26,16 @@ public Integer getRequestRemotePort(HttpServletRequest request) { return request.getRemotePort(); } + @Override + public String getRequestLocalAddr(HttpServletRequest request) { + return request.getLocalAddr(); + } + + @Override + public Integer getRequestLocalPort(HttpServletRequest request) { + return request.getLocalPort(); + } + @Override public void addRequestAsyncListener( HttpServletRequest request, diff --git a/instrumentation/servlet/servlet-5.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/servlet/v5_0/Servlet5Accessor.java b/instrumentation/servlet/servlet-5.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/servlet/v5_0/Servlet5Accessor.java index 7d56896c38a7..7fe4de161043 100644 --- a/instrumentation/servlet/servlet-5.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/servlet/v5_0/Servlet5Accessor.java +++ b/instrumentation/servlet/servlet-5.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/servlet/v5_0/Servlet5Accessor.java @@ -65,11 +65,36 @@ public String getRequestMethod(HttpServletRequest request) { return request.getMethod(); } + @Override + public String getRequestServerName(HttpServletRequest request) { + return request.getServerName(); + } + + @Override + public Integer getRequestServerPort(HttpServletRequest request) { + return request.getServerPort(); + } + @Override public String getRequestRemoteAddr(HttpServletRequest request) { return request.getRemoteAddr(); } + @Override + public Integer getRequestRemotePort(HttpServletRequest request) { + return request.getRemotePort(); + } + + @Override + public String getRequestLocalAddr(HttpServletRequest request) { + return request.getLocalAddr(); + } + + @Override + public Integer getRequestLocalPort(HttpServletRequest request) { + return request.getLocalPort(); + } + @Override public String getRequestHeader(HttpServletRequest request, String name) { return request.getHeader(name); @@ -108,11 +133,6 @@ public Principal getRequestUserPrincipal(HttpServletRequest request) { return request.getUserPrincipal(); } - @Override - public Integer getRequestRemotePort(HttpServletRequest request) { - return request.getRemotePort(); - } - @Override public void addRequestAsyncListener( HttpServletRequest request, diff --git a/instrumentation/servlet/servlet-common/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/servlet/ServletAccessor.java b/instrumentation/servlet/servlet-common/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/servlet/ServletAccessor.java index 6d24c11fb321..1b1f49b8e0c9 100644 --- a/instrumentation/servlet/servlet-common/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/servlet/ServletAccessor.java +++ b/instrumentation/servlet/servlet-common/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/servlet/ServletAccessor.java @@ -35,8 +35,18 @@ public interface ServletAccessor { String getRequestMethod(REQUEST request); + String getRequestServerName(REQUEST request); + + Integer getRequestServerPort(REQUEST request); + String getRequestRemoteAddr(REQUEST request); + Integer getRequestRemotePort(REQUEST request); + + String getRequestLocalAddr(REQUEST request); + + Integer getRequestLocalPort(REQUEST request); + String getRequestHeader(REQUEST request, String name); List getRequestHeaderValues(REQUEST request, String name); @@ -51,8 +61,6 @@ public interface ServletAccessor { Principal getRequestUserPrincipal(REQUEST request); - Integer getRequestRemotePort(REQUEST request); - void addRequestAsyncListener( REQUEST request, ServletAsyncListener listener, Object response); diff --git a/instrumentation/servlet/servlet-common/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/servlet/ServletNetAttributesGetter.java b/instrumentation/servlet/servlet-common/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/servlet/ServletNetAttributesGetter.java index ec0ff1bbab1e..7410628c5d01 100644 --- a/instrumentation/servlet/servlet-common/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/servlet/ServletNetAttributesGetter.java +++ b/instrumentation/servlet/servlet-common/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/servlet/ServletNetAttributesGetter.java @@ -11,6 +11,7 @@ public class ServletNetAttributesGetter implements NetServerAttributesGetter> { + private final ServletAccessor accessor; public ServletNetAttributesGetter(ServletAccessor accessor) { @@ -23,10 +24,16 @@ public String transport(ServletRequestContext requestContext) { return SemanticAttributes.NetTransportValues.IP_TCP; } + @Nullable @Override + public String hostName(ServletRequestContext requestContext) { + return accessor.getRequestServerName(requestContext.request()); + } + @Nullable - public Integer sockPeerPort(ServletRequestContext requestContext) { - return accessor.getRequestRemotePort(requestContext.request()); + @Override + public Integer hostPort(ServletRequestContext requestContext) { + return accessor.getRequestServerPort(requestContext.request()); } @Override @@ -34,4 +41,22 @@ public Integer sockPeerPort(ServletRequestContext requestContext) { public String sockPeerAddr(ServletRequestContext requestContext) { return accessor.getRequestRemoteAddr(requestContext.request()); } + + @Override + @Nullable + public Integer sockPeerPort(ServletRequestContext requestContext) { + return accessor.getRequestRemotePort(requestContext.request()); + } + + @Nullable + @Override + public String sockHostAddr(ServletRequestContext requestContext) { + return accessor.getRequestLocalAddr(requestContext.request()); + } + + @Nullable + @Override + public Integer sockHostPort(ServletRequestContext requestContext) { + return accessor.getRequestLocalPort(requestContext.request()); + } } diff --git a/instrumentation/servlet/servlet-javax-common/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/servlet/javax/JavaxServletAccessor.java b/instrumentation/servlet/servlet-javax-common/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/servlet/javax/JavaxServletAccessor.java index db1a6198092a..7a300c16313f 100644 --- a/instrumentation/servlet/servlet-javax-common/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/servlet/javax/JavaxServletAccessor.java +++ b/instrumentation/servlet/servlet-javax-common/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/servlet/javax/JavaxServletAccessor.java @@ -55,6 +55,16 @@ public String getRequestMethod(HttpServletRequest request) { return request.getMethod(); } + @Override + public String getRequestServerName(HttpServletRequest request) { + return request.getServerName(); + } + + @Override + public Integer getRequestServerPort(HttpServletRequest request) { + return request.getServerPort(); + } + @Override public String getRequestRemoteAddr(HttpServletRequest request) { return request.getRemoteAddr(); diff --git a/instrumentation/spark-2.3/javaagent/src/test/groovy/SparkJavaBasedTest.groovy b/instrumentation/spark-2.3/javaagent/src/test/groovy/SparkJavaBasedTest.groovy index 3395cc74e68c..d95809f0927e 100644 --- a/instrumentation/spark-2.3/javaagent/src/test/groovy/SparkJavaBasedTest.groovy +++ b/instrumentation/spark-2.3/javaagent/src/test/groovy/SparkJavaBasedTest.groovy @@ -47,9 +47,6 @@ class SparkJavaBasedTest extends AgentInstrumentationSpecification { kind SERVER hasNoParent() attributes { - "$SemanticAttributes.NET_TRANSPORT" IP_TCP - "net.sock.peer.addr" "127.0.0.1" - "net.sock.peer.port" Long "$SemanticAttributes.HTTP_SCHEME" "http" "$SemanticAttributes.HTTP_HOST" "localhost:$port" "$SemanticAttributes.HTTP_TARGET" "/param/asdf1234" @@ -58,6 +55,12 @@ class SparkJavaBasedTest extends AgentInstrumentationSpecification { "$SemanticAttributes.HTTP_FLAVOR" "1.1" "$SemanticAttributes.HTTP_USER_AGENT" String "$SemanticAttributes.HTTP_ROUTE" "/param/:param" + "$SemanticAttributes.NET_TRANSPORT" IP_TCP + "$SemanticAttributes.NET_HOST_NAME" "localhost" + "$SemanticAttributes.NET_HOST_PORT" port + "net.sock.peer.addr" "127.0.0.1" + "net.sock.peer.port" Long + "net.sock.host.addr" "127.0.0.1" } } } diff --git a/instrumentation/spring/spring-integration-4.1/javaagent/src/test/groovy/SpringIntegrationAndRabbitTest.groovy b/instrumentation/spring/spring-integration-4.1/javaagent/src/test/groovy/SpringIntegrationAndRabbitTest.groovy index 96cf998ce7e0..67ef09e2b9ba 100644 --- a/instrumentation/spring/spring-integration-4.1/javaagent/src/test/groovy/SpringIntegrationAndRabbitTest.groovy +++ b/instrumentation/spring/spring-integration-4.1/javaagent/src/test/groovy/SpringIntegrationAndRabbitTest.groovy @@ -46,10 +46,8 @@ class SpringIntegrationAndRabbitTest extends AgentInstrumentationSpecification i childOf span(1) kind CLIENT attributes { - // "localhost" on linux, "127.0.0.1" on windows - "$SemanticAttributes.NET_PEER_NAME" { it == "localhost" || it == "127.0.0.1" || it == "0:0:0:0:0:0:0:1" } - "$SemanticAttributes.NET_PEER_PORT" Long "net.sock.peer.addr" { it == "127.0.0.1" || it == "0:0:0:0:0:0:0:1" || it == null } + "net.sock.peer.port" Long "net.sock.family" { it == null || it == "inet6" } "$SemanticAttributes.MESSAGING_SYSTEM" "rabbitmq" "$SemanticAttributes.MESSAGING_DESTINATION_KIND" "queue" @@ -61,10 +59,8 @@ class SpringIntegrationAndRabbitTest extends AgentInstrumentationSpecification i childOf span(1) kind PRODUCER attributes { - // "localhost" on linux, "127.0.0.1" on windows - "$SemanticAttributes.NET_PEER_NAME" { it == "localhost" || it == "127.0.0.1" || it == "0:0:0:0:0:0:0:1" } - "$SemanticAttributes.NET_PEER_PORT" Long "net.sock.peer.addr" { it == "127.0.0.1" || it == "0:0:0:0:0:0:0:1" || it == null } + "net.sock.peer.port" Long "net.sock.family" { it == null || it == "inet6" } "$SemanticAttributes.MESSAGING_SYSTEM" "rabbitmq" "$SemanticAttributes.MESSAGING_DESTINATION" "testTopic" @@ -117,10 +113,8 @@ class SpringIntegrationAndRabbitTest extends AgentInstrumentationSpecification i name "basic.ack" kind CLIENT attributes { - // "localhost" on linux, "127.0.0.1" on windows - "$SemanticAttributes.NET_PEER_NAME" { it == "localhost" || it == "127.0.0.1" || it == "0:0:0:0:0:0:0:1" } - "$SemanticAttributes.NET_PEER_PORT" Long "net.sock.peer.addr" { it == "127.0.0.1" || it == "0:0:0:0:0:0:0:1" || it == null } + "net.sock.peer.port" Long "net.sock.family" { it == null || it == "inet6" } "$SemanticAttributes.MESSAGING_SYSTEM" "rabbitmq" "$SemanticAttributes.MESSAGING_DESTINATION_KIND" "queue" diff --git a/instrumentation/spring/spring-rabbit-1.0/javaagent/src/test/groovy/ContextPropagationTest.groovy b/instrumentation/spring/spring-rabbit-1.0/javaagent/src/test/groovy/ContextPropagationTest.groovy index e69fc60bbd38..cdb076e92493 100644 --- a/instrumentation/spring/spring-rabbit-1.0/javaagent/src/test/groovy/ContextPropagationTest.groovy +++ b/instrumentation/spring/spring-rabbit-1.0/javaagent/src/test/groovy/ContextPropagationTest.groovy @@ -100,10 +100,8 @@ class ContextPropagationTest extends AgentInstrumentationSpecification { kind PRODUCER childOf span(0) attributes { - // "localhost" on linux, "127.0.0.1" on windows - "$SemanticAttributes.NET_PEER_NAME" { it == "localhost" || it == "127.0.0.1" } - "$SemanticAttributes.NET_PEER_PORT" Long "net.sock.peer.addr" { it == "127.0.0.1" || it == null } + "net.sock.peer.port" Long "$SemanticAttributes.MESSAGING_SYSTEM" "rabbitmq" "$SemanticAttributes.MESSAGING_DESTINATION" "" "$SemanticAttributes.MESSAGING_DESTINATION_KIND" "queue" @@ -160,10 +158,8 @@ class ContextPropagationTest extends AgentInstrumentationSpecification { name "basic.ack" kind CLIENT attributes { - // "localhost" on linux, "127.0.0.1" on windows - "$SemanticAttributes.NET_PEER_NAME" { it == "localhost" || it == "127.0.0.1" } - "$SemanticAttributes.NET_PEER_PORT" Long "net.sock.peer.addr" { it == "127.0.0.1" || it == null } + "net.sock.peer.port" Long "$SemanticAttributes.MESSAGING_SYSTEM" "rabbitmq" "$SemanticAttributes.MESSAGING_DESTINATION_KIND" "queue" } diff --git a/instrumentation/spring/spring-web-3.1/library/src/main/java/io/opentelemetry/instrumentation/spring/web/SpringWebNetAttributesGetter.java b/instrumentation/spring/spring-web-3.1/library/src/main/java/io/opentelemetry/instrumentation/spring/web/SpringWebNetAttributesGetter.java index 2996c93fbe13..4a005c628b82 100644 --- a/instrumentation/spring/spring-web-3.1/library/src/main/java/io/opentelemetry/instrumentation/spring/web/SpringWebNetAttributesGetter.java +++ b/instrumentation/spring/spring-web-3.1/library/src/main/java/io/opentelemetry/instrumentation/spring/web/SpringWebNetAttributesGetter.java @@ -13,6 +13,7 @@ final class SpringWebNetAttributesGetter implements NetClientAttributesGetter { + @Override public String transport(HttpRequest httpRequest, @Nullable ClientHttpResponse response) { return SemanticAttributes.NetTransportValues.IP_TCP; diff --git a/instrumentation/spring/spring-webflux-5.0/javaagent/src/test/groovy/SpringWebfluxTest.groovy b/instrumentation/spring/spring-webflux-5.0/javaagent/src/test/groovy/SpringWebfluxTest.groovy index 8368933ee666..9793d8bf97e6 100644 --- a/instrumentation/spring/spring-webflux-5.0/javaagent/src/test/groovy/SpringWebfluxTest.groovy +++ b/instrumentation/spring/spring-webflux-5.0/javaagent/src/test/groovy/SpringWebfluxTest.groovy @@ -83,6 +83,8 @@ class SpringWebfluxTest extends AgentInstrumentationSpecification { "$SemanticAttributes.NET_TRANSPORT" IP_TCP "net.sock.peer.addr" "127.0.0.1" "net.sock.peer.port" Long + "net.sock.host.addr" "127.0.0.1" + "net.sock.host.port" Long "$SemanticAttributes.HTTP_HOST" { it == "localhost" || it == "localhost:${port}" } "$SemanticAttributes.HTTP_TARGET" urlPath "$SemanticAttributes.HTTP_METHOD" "GET" @@ -151,6 +153,8 @@ class SpringWebfluxTest extends AgentInstrumentationSpecification { "$SemanticAttributes.NET_TRANSPORT" IP_TCP "net.sock.peer.addr" "127.0.0.1" "net.sock.peer.port" Long + "net.sock.host.addr" "127.0.0.1" + "net.sock.host.port" Long "$SemanticAttributes.HTTP_HOST" { it == "localhost" || it == "localhost:${port}" } "$SemanticAttributes.HTTP_TARGET" urlPath "$SemanticAttributes.HTTP_METHOD" "GET" @@ -239,6 +243,8 @@ class SpringWebfluxTest extends AgentInstrumentationSpecification { "$SemanticAttributes.NET_TRANSPORT" IP_TCP "net.sock.peer.addr" "127.0.0.1" "net.sock.peer.port" Long + "net.sock.host.addr" "127.0.0.1" + "net.sock.host.port" Long "$SemanticAttributes.HTTP_HOST" { it == "localhost" || it == "localhost:${port}" } "$SemanticAttributes.HTTP_TARGET" urlPath "$SemanticAttributes.HTTP_METHOD" "GET" @@ -305,6 +311,8 @@ class SpringWebfluxTest extends AgentInstrumentationSpecification { "$SemanticAttributes.NET_TRANSPORT" IP_TCP "net.sock.peer.addr" "127.0.0.1" "net.sock.peer.port" Long + "net.sock.host.addr" "127.0.0.1" + "net.sock.host.port" Long "$SemanticAttributes.HTTP_HOST" { it == "localhost" || it == "localhost:${port}" } "$SemanticAttributes.HTTP_TARGET" "/notfoundgreet" "$SemanticAttributes.HTTP_METHOD" "GET" @@ -350,6 +358,8 @@ class SpringWebfluxTest extends AgentInstrumentationSpecification { "$SemanticAttributes.NET_TRANSPORT" IP_TCP "net.sock.peer.addr" "127.0.0.1" "net.sock.peer.port" Long + "net.sock.host.addr" "127.0.0.1" + "net.sock.host.port" Long "$SemanticAttributes.HTTP_HOST" { it == "localhost" || it == "localhost:${port}" } "$SemanticAttributes.HTTP_TARGET" "/echo" "$SemanticAttributes.HTTP_METHOD" "POST" @@ -400,6 +410,8 @@ class SpringWebfluxTest extends AgentInstrumentationSpecification { "$SemanticAttributes.NET_TRANSPORT" IP_TCP "net.sock.peer.addr" "127.0.0.1" "net.sock.peer.port" Long + "net.sock.host.addr" "127.0.0.1" + "net.sock.host.port" Long "$SemanticAttributes.HTTP_HOST" { it == "localhost" || it == "localhost:${port}" } "$SemanticAttributes.HTTP_TARGET" urlPath "$SemanticAttributes.HTTP_METHOD" "GET" @@ -465,6 +477,8 @@ class SpringWebfluxTest extends AgentInstrumentationSpecification { "$SemanticAttributes.NET_TRANSPORT" IP_TCP "net.sock.peer.addr" "127.0.0.1" "net.sock.peer.port" Long + "net.sock.host.addr" "127.0.0.1" + "net.sock.host.port" Long "$SemanticAttributes.HTTP_HOST" { it == "localhost" || it == "localhost:${port}" } "$SemanticAttributes.HTTP_TARGET" "/double-greet-redirect" "$SemanticAttributes.HTTP_METHOD" "GET" @@ -498,6 +512,8 @@ class SpringWebfluxTest extends AgentInstrumentationSpecification { "$SemanticAttributes.NET_TRANSPORT" IP_TCP "net.sock.peer.addr" "127.0.0.1" "net.sock.peer.port" Long + "net.sock.host.addr" "127.0.0.1" + "net.sock.host.port" Long "$SemanticAttributes.HTTP_HOST" { it == "localhost" || it == "localhost:${port}" } "$SemanticAttributes.HTTP_TARGET" "/double-greet" "$SemanticAttributes.HTTP_METHOD" "GET" @@ -546,6 +562,8 @@ class SpringWebfluxTest extends AgentInstrumentationSpecification { "$SemanticAttributes.NET_TRANSPORT" IP_TCP "net.sock.peer.addr" "127.0.0.1" "net.sock.peer.port" Long + "net.sock.host.addr" "127.0.0.1" + "net.sock.host.port" Long "$SemanticAttributes.HTTP_HOST" { it == "localhost" || it == "localhost:${port}" } "$SemanticAttributes.HTTP_TARGET" urlPath "$SemanticAttributes.HTTP_METHOD" "GET" diff --git a/instrumentation/spring/spring-webmvc-5.3/library/src/main/java/io/opentelemetry/instrumentation/spring/webmvc/v5_3/SpringWebMvcNetAttributesGetter.java b/instrumentation/spring/spring-webmvc-5.3/library/src/main/java/io/opentelemetry/instrumentation/spring/webmvc/v5_3/SpringWebMvcNetAttributesGetter.java index f961ab636a37..79a9f82daef3 100644 --- a/instrumentation/spring/spring-webmvc-5.3/library/src/main/java/io/opentelemetry/instrumentation/spring/webmvc/v5_3/SpringWebMvcNetAttributesGetter.java +++ b/instrumentation/spring/spring-webmvc-5.3/library/src/main/java/io/opentelemetry/instrumentation/spring/webmvc/v5_3/SpringWebMvcNetAttributesGetter.java @@ -18,9 +18,15 @@ public String transport(HttpServletRequest request) { return SemanticAttributes.NetTransportValues.IP_TCP; } + @Nullable @Override - public Integer sockPeerPort(HttpServletRequest request) { - return request.getRemotePort(); + public String hostName(HttpServletRequest request) { + return request.getServerName(); + } + + @Override + public Integer hostPort(HttpServletRequest request) { + return request.getServerPort(); } @Override @@ -28,4 +34,20 @@ public Integer sockPeerPort(HttpServletRequest request) { public String sockPeerAddr(HttpServletRequest request) { return request.getRemoteAddr(); } + + @Override + public Integer sockPeerPort(HttpServletRequest request) { + return request.getRemotePort(); + } + + @Nullable + @Override + public String sockHostAddr(HttpServletRequest request) { + return request.getLocalAddr(); + } + + @Override + public Integer sockHostPort(HttpServletRequest request) { + return request.getLocalPort(); + } } diff --git a/instrumentation/tomcat/tomcat-common/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/tomcat/common/TomcatNetAttributesGetter.java b/instrumentation/tomcat/tomcat-common/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/tomcat/common/TomcatNetAttributesGetter.java index 2573db0127bc..30c8ffb01164 100644 --- a/instrumentation/tomcat/tomcat-common/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/tomcat/common/TomcatNetAttributesGetter.java +++ b/instrumentation/tomcat/tomcat-common/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/tomcat/common/TomcatNetAttributesGetter.java @@ -19,6 +19,24 @@ public String transport(Request request) { return SemanticAttributes.NetTransportValues.IP_TCP; } + @Nullable + @Override + public String hostName(Request request) { + return request.serverName().toString(); + } + + @Override + public Integer hostPort(Request request) { + return request.getServerPort(); + } + + @Override + @Nullable + public String sockPeerAddr(Request request) { + request.action(ActionCode.REQ_HOST_ADDR_ATTRIBUTE, request); + return request.remoteAddr().toString(); + } + @Override @Nullable public Integer sockPeerPort(Request request) { @@ -26,10 +44,17 @@ public Integer sockPeerPort(Request request) { return request.getRemotePort(); } + @Nullable @Override + public String sockHostAddr(Request request) { + request.action(ActionCode.REQ_LOCAL_ADDR_ATTRIBUTE, request); + return request.localAddr().toString(); + } + @Nullable - public String sockPeerAddr(Request request) { - request.action(ActionCode.REQ_HOST_ADDR_ATTRIBUTE, request); - return request.remoteAddr().toString(); + @Override + public Integer sockHostPort(Request request) { + request.action(ActionCode.REQ_LOCALPORT_ATTRIBUTE, request); + return request.getLocalPort(); } } diff --git a/instrumentation/undertow-1.4/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/undertow/UndertowNetAttributesGetter.java b/instrumentation/undertow-1.4/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/undertow/UndertowNetAttributesGetter.java index f4fe5e4a61cf..73044ace65b8 100644 --- a/instrumentation/undertow-1.4/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/undertow/UndertowNetAttributesGetter.java +++ b/instrumentation/undertow-1.4/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/undertow/UndertowNetAttributesGetter.java @@ -15,13 +15,31 @@ public class UndertowNetAttributesGetter extends InetSocketAddressNetServerAttributesGetter { @Override + public String transport(HttpServerExchange exchange) { + return SemanticAttributes.NetTransportValues.IP_TCP; + } + + @Nullable + @Override + public String hostName(HttpServerExchange exchange) { + return exchange.getHostName(); + } + @Nullable - public InetSocketAddress getAddress(HttpServerExchange exchange) { + @Override + public Integer hostPort(HttpServerExchange exchange) { + return exchange.getHostPort(); + } + + @Override + @Nullable + protected InetSocketAddress getPeerSocketAddress(HttpServerExchange exchange) { return exchange.getConnection().getPeerAddress(InetSocketAddress.class); } + @Nullable @Override - public String transport(HttpServerExchange exchange) { - return SemanticAttributes.NetTransportValues.IP_TCP; + protected InetSocketAddress getHostSocketAddress(HttpServerExchange exchange) { + return exchange.getConnection().getLocalAddress(InetSocketAddress.class); } } 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 95931cdaad3c..27a27514449a 100644 --- a/instrumentation/undertow-1.4/javaagent/src/test/groovy/UndertowServerTest.groovy +++ b/instrumentation/undertow-1.4/javaagent/src/test/groovy/UndertowServerTest.groovy @@ -138,8 +138,6 @@ class UndertowServerTest extends HttpServerTest implements AgentTestTr } attributes { - "net.sock.peer.addr" "127.0.0.1" - "net.sock.peer.port" Long "$SemanticAttributes.HTTP_CLIENT_IP" TEST_CLIENT_IP "$SemanticAttributes.HTTP_SCHEME" uri.getScheme() "$SemanticAttributes.HTTP_HOST" uri.getHost() + ":" + uri.getPort() @@ -153,6 +151,11 @@ class UndertowServerTest extends HttpServerTest implements AgentTestTr "$SemanticAttributes.HTTP_SCHEME" "http" "$SemanticAttributes.HTTP_TARGET" "/sendResponse" "$SemanticAttributes.NET_TRANSPORT" SemanticAttributes.NetTransportValues.IP_TCP + "$SemanticAttributes.NET_HOST_NAME" uri.host + "$SemanticAttributes.NET_HOST_PORT" uri.port + "net.sock.peer.addr" "127.0.0.1" + "net.sock.peer.port" Long + "net.sock.host.addr" "127.0.0.1" } } span(1) { @@ -191,8 +194,6 @@ class UndertowServerTest extends HttpServerTest implements AgentTestTr errorEvent(Exception, "exception after sending response", 2) attributes { - "net.sock.peer.addr" "127.0.0.1" - "net.sock.peer.port" Long "$SemanticAttributes.HTTP_CLIENT_IP" TEST_CLIENT_IP "$SemanticAttributes.HTTP_SCHEME" uri.getScheme() "$SemanticAttributes.HTTP_HOST" uri.getHost() + ":" + uri.getPort() @@ -206,6 +207,11 @@ class UndertowServerTest extends HttpServerTest implements AgentTestTr "$SemanticAttributes.HTTP_SCHEME" "http" "$SemanticAttributes.HTTP_TARGET" "/sendResponseWithException" "$SemanticAttributes.NET_TRANSPORT" SemanticAttributes.NetTransportValues.IP_TCP + "$SemanticAttributes.NET_HOST_NAME" uri.host + "$SemanticAttributes.NET_HOST_PORT" uri.port + "net.sock.peer.addr" "127.0.0.1" + "net.sock.peer.port" Long + "net.sock.host.addr" "127.0.0.1" } } span(1) { diff --git a/instrumentation/vertx/vertx-http-client/vertx-http-client-4.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/vertx/v4_0/client/Vertx4NetAttributesGetter.java b/instrumentation/vertx/vertx-http-client/vertx-http-client-4.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/vertx/v4_0/client/Vertx4NetAttributesGetter.java index b7b8154d9a3e..b5c6b0f38ef9 100644 --- a/instrumentation/vertx/vertx-http-client/vertx-http-client-4.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/vertx/v4_0/client/Vertx4NetAttributesGetter.java +++ b/instrumentation/vertx/vertx-http-client/vertx-http-client-4.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/vertx/v4_0/client/Vertx4NetAttributesGetter.java @@ -5,14 +5,16 @@ package io.opentelemetry.javaagent.instrumentation.vertx.v4_0.client; -import io.opentelemetry.instrumentation.api.instrumenter.net.NetClientAttributesGetter; +import io.opentelemetry.instrumentation.api.instrumenter.net.InetSocketAddressNetClientAttributesGetter; import io.opentelemetry.semconv.trace.attributes.SemanticAttributes; import io.vertx.core.http.HttpClientRequest; import io.vertx.core.http.HttpClientResponse; +import io.vertx.core.net.SocketAddress; +import java.net.InetSocketAddress; import javax.annotation.Nullable; final class Vertx4NetAttributesGetter - implements NetClientAttributesGetter { + extends InetSocketAddressNetClientAttributesGetter { @Override public String transport(HttpClientRequest request, @Nullable HttpClientResponse response) { @@ -29,4 +31,18 @@ public String peerName(HttpClientRequest request, @Nullable HttpClientResponse r public Integer peerPort(HttpClientRequest request, @Nullable HttpClientResponse response) { return request.getPort(); } + + @Nullable + @Override + protected InetSocketAddress getPeerSocketAddress( + HttpClientRequest request, @Nullable HttpClientResponse response) { + if (response == null) { + return null; + } + SocketAddress address = response.netSocket().remoteAddress(); + if (address instanceof InetSocketAddress) { + return (InetSocketAddress) address; + } + return null; + } } diff --git a/instrumentation/vertx/vertx-rx-java-3.5/javaagent/src/latestDepTest/groovy/VertxReactivePropagationTest.groovy b/instrumentation/vertx/vertx-rx-java-3.5/javaagent/src/latestDepTest/groovy/VertxReactivePropagationTest.groovy index 83d2150fe6fa..051c093a632a 100644 --- a/instrumentation/vertx/vertx-rx-java-3.5/javaagent/src/latestDepTest/groovy/VertxReactivePropagationTest.groovy +++ b/instrumentation/vertx/vertx-rx-java-3.5/javaagent/src/latestDepTest/groovy/VertxReactivePropagationTest.groovy @@ -66,6 +66,8 @@ class VertxReactivePropagationTest extends AgentInstrumentationSpecification { "$SemanticAttributes.NET_TRANSPORT" IP_TCP "net.sock.peer.addr" "127.0.0.1" "net.sock.peer.port" Long + "net.sock.host.addr" "127.0.0.1" + "net.sock.host.port" Long "$SemanticAttributes.HTTP_HOST" { it == "localhost" || it == "localhost:${port}" } "$SemanticAttributes.HTTP_TARGET" "/listProducts" "$SemanticAttributes.HTTP_METHOD" "GET" @@ -157,6 +159,8 @@ class VertxReactivePropagationTest extends AgentInstrumentationSpecification { "$SemanticAttributes.NET_TRANSPORT" IP_TCP "net.sock.peer.addr" "127.0.0.1" "net.sock.peer.port" Long + "net.sock.host.addr" "127.0.0.1" + "net.sock.host.port" Long "$SemanticAttributes.HTTP_HOST" { it == "localhost" || it == "localhost:${port}" } "$SemanticAttributes.HTTP_TARGET" "$baseUrl?$TEST_REQUEST_ID_PARAMETER=$requestId" "$SemanticAttributes.HTTP_METHOD" "GET" diff --git a/instrumentation/vertx/vertx-rx-java-3.5/javaagent/src/version35Test/groovy/VertxReactivePropagationTest.groovy b/instrumentation/vertx/vertx-rx-java-3.5/javaagent/src/version35Test/groovy/VertxReactivePropagationTest.groovy index 83d2150fe6fa..051c093a632a 100644 --- a/instrumentation/vertx/vertx-rx-java-3.5/javaagent/src/version35Test/groovy/VertxReactivePropagationTest.groovy +++ b/instrumentation/vertx/vertx-rx-java-3.5/javaagent/src/version35Test/groovy/VertxReactivePropagationTest.groovy @@ -66,6 +66,8 @@ class VertxReactivePropagationTest extends AgentInstrumentationSpecification { "$SemanticAttributes.NET_TRANSPORT" IP_TCP "net.sock.peer.addr" "127.0.0.1" "net.sock.peer.port" Long + "net.sock.host.addr" "127.0.0.1" + "net.sock.host.port" Long "$SemanticAttributes.HTTP_HOST" { it == "localhost" || it == "localhost:${port}" } "$SemanticAttributes.HTTP_TARGET" "/listProducts" "$SemanticAttributes.HTTP_METHOD" "GET" @@ -157,6 +159,8 @@ class VertxReactivePropagationTest extends AgentInstrumentationSpecification { "$SemanticAttributes.NET_TRANSPORT" IP_TCP "net.sock.peer.addr" "127.0.0.1" "net.sock.peer.port" Long + "net.sock.host.addr" "127.0.0.1" + "net.sock.host.port" Long "$SemanticAttributes.HTTP_HOST" { it == "localhost" || it == "localhost:${port}" } "$SemanticAttributes.HTTP_TARGET" "$baseUrl?$TEST_REQUEST_ID_PARAMETER=$requestId" "$SemanticAttributes.HTTP_METHOD" "GET" diff --git a/testing-common/src/main/groovy/io/opentelemetry/instrumentation/test/base/HttpServerTest.groovy b/testing-common/src/main/groovy/io/opentelemetry/instrumentation/test/base/HttpServerTest.groovy index 6db401d956e8..f4545fb9c6cc 100644 --- a/testing-common/src/main/groovy/io/opentelemetry/instrumentation/test/base/HttpServerTest.groovy +++ b/testing-common/src/main/groovy/io/opentelemetry/instrumentation/test/base/HttpServerTest.groovy @@ -100,7 +100,7 @@ abstract class HttpServerTest extends InstrumentationSpecification imple false } - String peerIp(ServerEndpoint endpoint) { + String sockPeerAddr(ServerEndpoint endpoint) { "127.0.0.1" } @@ -199,6 +199,9 @@ abstract class HttpServerTest extends InstrumentationSpecification imple options.hasExceptionOnServerSpan = { endpoint -> HttpServerTest.this.hasExceptionOnServerSpan(endpoint) } + options.sockPeerAddr = { endpoint -> + HttpServerTest.this.sockPeerAddr(endpoint) + } options.testRedirect = testRedirect() options.testError = testError() diff --git a/testing-common/src/main/java/io/opentelemetry/instrumentation/testing/junit/http/AbstractHttpClientTest.java b/testing-common/src/main/java/io/opentelemetry/instrumentation/testing/junit/http/AbstractHttpClientTest.java index 911042ffd414..77183b3df044 100644 --- a/testing-common/src/main/java/io/opentelemetry/instrumentation/testing/junit/http/AbstractHttpClientTest.java +++ b/testing-common/src/main/java/io/opentelemetry/instrumentation/testing/junit/http/AbstractHttpClientTest.java @@ -6,6 +6,7 @@ package io.opentelemetry.instrumentation.testing.junit.http; import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.assertThat; +import static io.opentelemetry.semconv.trace.attributes.SemanticAttributes.NetTransportValues.IP_TCP; import static org.assertj.core.api.Assertions.catchThrowable; import static org.junit.jupiter.api.Assumptions.assumeTrue; @@ -912,10 +913,14 @@ SpanDataAssert assertClientSpan( .hasKind(SpanKind.CLIENT) .hasAttributesSatisfying( attrs -> { + // TODO: Move to test knob rather than always treating as optional + if (attrs.get(SemanticAttributes.NET_TRANSPORT) != null) { + assertThat(attrs).containsEntry(SemanticAttributes.NET_TRANSPORT, IP_TCP); + } + if (uri.getPort() == PortUtils.UNUSABLE_PORT || uri.getHost().equals("192.0.2.1")) { - // TODO(anuraaga): For theses cases, there isn't actually a peer so we shouldn't be - // filling in peer information but some instrumentation does so based on the URL - // itself which is present in HTTP attributes. We should fix this. + // TODO: net.peer.name and net.peer.port should always be populated from the URI or + // the Host header, verify these assertions below if (attrs.asMap().containsKey(SemanticAttributes.NET_PEER_NAME)) { assertThat(attrs).containsEntry(SemanticAttributes.NET_PEER_NAME, uri.getHost()); } @@ -937,6 +942,17 @@ SpanDataAssert assertClientSpan( }); } } + + // In these cases the peer connection is not established, so the HTTP client should + // not report any socket-level attributes + // TODO https://github.com/open-telemetry/opentelemetry-java/pull/4723 + assertThat(attrs.asMap()) + .doesNotContainKey(AttributeKey.stringKey("net.sock.family")) + // TODO netty sometimes reports net.sock.peer.addr in connection error test + // .doesNotContainKey(AttributeKey.stringKey("net.sock.peer.addr")) + .doesNotContainKey(AttributeKey.stringKey("net.sock.peer.name")) + .doesNotContainKey(AttributeKey.stringKey("net.sock.peer.port")); + } else { if (httpClientAttributes.contains(SemanticAttributes.NET_PEER_NAME)) { assertThat(attrs).containsEntry(SemanticAttributes.NET_PEER_NAME, uri.getHost()); @@ -944,19 +960,18 @@ SpanDataAssert assertClientSpan( if (httpClientAttributes.contains(SemanticAttributes.NET_PEER_PORT)) { assertThat(attrs).containsEntry(SemanticAttributes.NET_PEER_PORT, uri.getPort()); } - } - // TODO(anuraaga): Move to test knob rather than always treating as optional - if (attrs.asMap().containsKey(AttributeKey.stringKey("net.sock.peer.addr"))) { - if (uri.getHost().equals("192.0.2.1")) { - // NB(anuraaga): This branch seems to currently only be exercised on Java 15. - // It would be good to understand how the JVM version is impacting this check. - assertThat(attrs) - .containsEntry(AttributeKey.stringKey("net.sock.peer.addr"), "192.0.2.1"); - } else { + // TODO: Move to test knob rather than always treating as optional + if (attrs.asMap().containsKey(AttributeKey.stringKey("net.sock.peer.addr"))) { assertThat(attrs) .containsEntry(AttributeKey.stringKey("net.sock.peer.addr"), "127.0.0.1"); } + if (attrs.asMap().containsKey(AttributeKey.stringKey("net.sock.peer.port"))) { + assertThat(attrs) + .containsEntry( + AttributeKey.longKey("net.sock.peer.port"), + "https".equals(uri.getScheme()) ? server.httpsPort() : server.httpPort()); + } } if (httpClientAttributes.contains(SemanticAttributes.HTTP_URL)) { diff --git a/testing-common/src/main/java/io/opentelemetry/instrumentation/testing/junit/http/AbstractHttpServerTest.java b/testing-common/src/main/java/io/opentelemetry/instrumentation/testing/junit/http/AbstractHttpServerTest.java index 4c14cd588737..34626a340041 100644 --- a/testing-common/src/main/java/io/opentelemetry/instrumentation/testing/junit/http/AbstractHttpServerTest.java +++ b/testing-common/src/main/java/io/opentelemetry/instrumentation/testing/junit/http/AbstractHttpServerTest.java @@ -551,7 +551,15 @@ protected SpanDataAssert assertServerSpan( .containsEntry( SemanticAttributes.NET_TRANSPORT, SemanticAttributes.NetTransportValues.IP_TCP); } - if (httpAttributes.contains(AttributeKey.longKey("net.sock.peer.port"))) { + + // TODO: Move to test knob rather than always treating as optional + if (attrs.get(SemanticAttributes.NET_HOST_NAME) != null) { + assertThat(attrs).containsEntry(SemanticAttributes.NET_HOST_NAME, "localhost"); + } + if (attrs.get(SemanticAttributes.NET_HOST_PORT) != null) { + assertThat(attrs).containsEntry(SemanticAttributes.NET_HOST_PORT, port); + } + if (attrs.get(AttributeKey.longKey("net.sock.peer.port")) != null) { assertThat(attrs) .hasEntrySatisfying( AttributeKey.longKey("net.sock.peer.port"), @@ -560,12 +568,16 @@ protected SpanDataAssert assertServerSpan( .isInstanceOf(Long.class) .isNotEqualTo(Long.valueOf(port))); } - if (httpAttributes.contains(AttributeKey.stringKey("net.sock.peer.addr"))) { + if (attrs.get(AttributeKey.stringKey("net.sock.peer.addr")) != null) { assertThat(attrs) .containsEntry( AttributeKey.stringKey("net.sock.peer.addr"), options.sockPeerAddr.apply(endpoint)); } + if (attrs.get(AttributeKey.stringKey("net.sock.host.addr")) != null) { + assertThat(attrs) + .containsEntry(AttributeKey.stringKey("net.sock.host.addr"), "127.0.0.1"); + } assertThat(attrs) .hasEntrySatisfying(