Skip to content

Commit

Permalink
Net attributes getters changes (in preparation for HTTP spec impl) (#…
Browse files Browse the repository at this point in the history
…6503)

* Net attributes getters changes: instrumentation-api-semconv changes

* Net attributes getters changes: getter implementations

* Net attributes getters changes: test fixes

* Remove net.sock.host.name

* code review comments

* default getter methods & getPeerSocketAddress() method name

* set authority in grpc earlier
  • Loading branch information
Mateusz Rzeszutek authored Sep 12, 2022
1 parent 24e92b2 commit cfdbe75
Show file tree
Hide file tree
Showing 127 changed files with 2,219 additions and 861 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,32 +21,27 @@ public abstract class InetSocketAddressNetClientAttributesGetter<REQUEST, RESPON
implements NetClientAttributesGetter<REQUEST, RESPONSE> {

@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;
}
Expand All @@ -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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -20,22 +21,55 @@ public abstract class InetSocketAddressNetServerAttributesGetter<REQUEST>
implements NetServerAttributesGetter<REQUEST> {

@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;
}
Expand All @@ -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();
}
}
Original file line number Diff line number Diff line change
@@ -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<String> NET_SOCK_FAMILY = AttributeKey.stringKey("net.sock.family");
static final AttributeKey<String> NET_SOCK_PEER_ADDR =
AttributeKey.stringKey("net.sock.peer.addr");
static final AttributeKey<String> NET_SOCK_PEER_NAME =
AttributeKey.stringKey("net.sock.peer.name");
static final AttributeKey<Long> NET_SOCK_PEER_PORT = AttributeKey.longKey("net.sock.peer.port");
static final AttributeKey<String> NET_SOCK_HOST_ADDR =
AttributeKey.stringKey("net.sock.host.addr");
static final AttributeKey<Long> NET_SOCK_HOST_PORT = AttributeKey.longKey("net.sock.host.port");

static final String SOCK_FAMILY_INET = "inet";

private NetAttributes() {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -26,15 +25,6 @@
public final class NetClientAttributesExtractor<REQUEST, RESPONSE>
implements AttributesExtractor<REQUEST, RESPONSE> {

private static final AttributeKey<String> NET_SOCK_PEER_ADDR =
AttributeKey.stringKey("net.sock.peer.addr");
public static final AttributeKey<Long> NET_SOCK_PEER_PORT =
AttributeKey.longKey("net.sock.peer.port");
public static final AttributeKey<String> NET_SOCK_FAMILY =
AttributeKey.stringKey("net.sock.family");
public static final AttributeKey<String> NET_SOCK_PEER_NAME =
AttributeKey.stringKey("net.sock.peer.name");

private final NetClientAttributesGetter<REQUEST, RESPONSE> getter;

public static <REQUEST, RESPONSE> NetClientAttributesExtractor<REQUEST, RESPONSE> create(
Expand Down Expand Up @@ -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);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,29 +20,31 @@ public interface NetClientAttributesGetter<REQUEST, RESPONSE> {
@Nullable
String transport(REQUEST request, @Nullable RESPONSE response);

// TODO: peerName and peerPort should be extracted onStart

@Nullable
String peerName(REQUEST request, @Nullable RESPONSE response);

@Nullable
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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -23,10 +22,6 @@
public final class NetServerAttributesExtractor<REQUEST, RESPONSE>
implements AttributesExtractor<REQUEST, RESPONSE> {

public static final AttributeKey<String> NET_SOCK_PEER_ADDR =
AttributeKey.stringKey("net.sock.peer.addr");
public static final AttributeKey<Long> NET_SOCK_PEER_PORT =
AttributeKey.longKey("net.sock.peer.port");
private final NetServerAttributesGetter<REQUEST> getter;

public static <REQUEST, RESPONSE> NetServerAttributesExtractor<REQUEST, RESPONSE> create(
Expand All @@ -42,13 +37,47 @@ private NetServerAttributesExtractor(NetServerAttributesGetter<REQUEST> 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);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,33 @@ public interface NetServerAttributesGetter<REQUEST> {
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;
}
}
Loading

0 comments on commit cfdbe75

Please sign in to comment.