Skip to content

Commit

Permalink
fix: add attributes of net.peer.* for grpc client span (open-telemetr…
Browse files Browse the repository at this point in the history
  • Loading branch information
ralphgj authored and RashmiRam committed May 23, 2022
1 parent 7ef981a commit 139bdf6
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
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;
Expand All @@ -21,7 +20,9 @@
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 {
Expand Down Expand Up @@ -55,8 +56,13 @@ public <REQUEST, RESPONSE> ClientCall<REQUEST, RESPONSE> interceptCall(
throw e;
}
}

SocketAddress address = result.getAttributes().get(Grpc.TRANSPORT_ATTR_REMOTE_ADDR);
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);

return new TracingClientCall<>(result, parentContext, context, request);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,8 @@ abstract class AbstractGrpcStreamingTest extends InstrumentationSpecification {
"$SemanticAttributes.RPC_METHOD" "Conversation"
"$SemanticAttributes.NET_TRANSPORT" SemanticAttributes.NetTransportValues.IP_TCP
"$SemanticAttributes.RPC_GRPC_STATUS_CODE" Status.OK.code.value()
"$SemanticAttributes.NET_PEER_NAME" "localhost"
"$SemanticAttributes.NET_PEER_PORT" port
}
(1..(clientMessageCount * serverMessageCount + clientMessageCount)).each {
def messageId = it
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,8 @@ abstract class AbstractGrpcTest extends InstrumentationSpecification {
"$SemanticAttributes.RPC_METHOD" "SayHello"
"$SemanticAttributes.NET_TRANSPORT" SemanticAttributes.NetTransportValues.IP_TCP
"$SemanticAttributes.RPC_GRPC_STATUS_CODE" Status.Code.OK.value()
"$SemanticAttributes.NET_PEER_NAME" "localhost"
"$SemanticAttributes.NET_PEER_PORT" port
}
}
span(2) {
Expand Down Expand Up @@ -236,6 +238,8 @@ abstract class AbstractGrpcTest extends InstrumentationSpecification {
"$SemanticAttributes.RPC_METHOD" "SayHello"
"$SemanticAttributes.NET_TRANSPORT" SemanticAttributes.NetTransportValues.IP_TCP
"$SemanticAttributes.RPC_GRPC_STATUS_CODE" Status.Code.OK.value()
"$SemanticAttributes.NET_PEER_NAME" "localhost"
"$SemanticAttributes.NET_PEER_PORT" port
}
}
span(2) {
Expand Down Expand Up @@ -369,6 +373,8 @@ abstract class AbstractGrpcTest extends InstrumentationSpecification {
"$SemanticAttributes.RPC_METHOD" "SayHello"
"$SemanticAttributes.NET_TRANSPORT" SemanticAttributes.NetTransportValues.IP_TCP
"$SemanticAttributes.RPC_GRPC_STATUS_CODE" Status.Code.OK.value()
"$SemanticAttributes.NET_PEER_NAME" "localhost"
"$SemanticAttributes.NET_PEER_PORT" port
}
}
span(2) {
Expand Down Expand Up @@ -465,6 +471,8 @@ abstract class AbstractGrpcTest extends InstrumentationSpecification {
"$SemanticAttributes.RPC_METHOD" "SayHello"
"$SemanticAttributes.NET_TRANSPORT" SemanticAttributes.NetTransportValues.IP_TCP
"$SemanticAttributes.RPC_GRPC_STATUS_CODE" grpcStatus.code.value()
"$SemanticAttributes.NET_PEER_NAME" "localhost"
"$SemanticAttributes.NET_PEER_PORT" port
}
}
span(1) {
Expand Down Expand Up @@ -565,6 +573,8 @@ abstract class AbstractGrpcTest extends InstrumentationSpecification {
"$SemanticAttributes.RPC_METHOD" "SayHello"
"$SemanticAttributes.NET_TRANSPORT" SemanticAttributes.NetTransportValues.IP_TCP
"$SemanticAttributes.RPC_GRPC_STATUS_CODE" Status.UNKNOWN.code.value()
"$SemanticAttributes.NET_PEER_NAME" "localhost"
"$SemanticAttributes.NET_PEER_PORT" port
}
}
span(1) {
Expand Down Expand Up @@ -740,6 +750,8 @@ abstract class AbstractGrpcTest extends InstrumentationSpecification {
"$SemanticAttributes.RPC_METHOD" "SayHello"
"$SemanticAttributes.NET_TRANSPORT" SemanticAttributes.NetTransportValues.IP_TCP
"$SemanticAttributes.RPC_GRPC_STATUS_CODE" Status.OK.code.value()
"$SemanticAttributes.NET_PEER_NAME" "localhost"
"$SemanticAttributes.NET_PEER_PORT" port
}
}
span(2) {
Expand Down Expand Up @@ -871,6 +883,8 @@ abstract class AbstractGrpcTest extends InstrumentationSpecification {
"$SemanticAttributes.RPC_METHOD" "SayHello"
"$SemanticAttributes.NET_TRANSPORT" SemanticAttributes.NetTransportValues.IP_TCP
"$SemanticAttributes.RPC_GRPC_STATUS_CODE" Status.CANCELLED.code.value()
"$SemanticAttributes.NET_PEER_NAME" "localhost"
"$SemanticAttributes.NET_PEER_PORT" port
}
}
span(2) {
Expand Down Expand Up @@ -985,6 +999,8 @@ abstract class AbstractGrpcTest extends InstrumentationSpecification {
"$SemanticAttributes.RPC_METHOD" "ServerReflectionInfo"
"$SemanticAttributes.NET_TRANSPORT" SemanticAttributes.NetTransportValues.IP_TCP
"$SemanticAttributes.RPC_GRPC_STATUS_CODE" Status.OK.code.value()
"$SemanticAttributes.NET_PEER_NAME" "localhost"
"$SemanticAttributes.NET_PEER_PORT" port
}
}
span(1) {
Expand Down Expand Up @@ -1093,6 +1109,8 @@ abstract class AbstractGrpcTest extends InstrumentationSpecification {
"$SemanticAttributes.RPC_METHOD" "SayHello"
"$SemanticAttributes.NET_TRANSPORT" SemanticAttributes.NetTransportValues.IP_TCP
"$SemanticAttributes.RPC_GRPC_STATUS_CODE" Status.Code.OK.value()
"$SemanticAttributes.NET_PEER_NAME" "localhost"
"$SemanticAttributes.NET_PEER_PORT" port
}
}
span(2) {
Expand Down

0 comments on commit 139bdf6

Please sign in to comment.