Skip to content

Commit

Permalink
contrib/google.golang.org/grpc: add hostname tag (#2361)
Browse files Browse the repository at this point in the history
  • Loading branch information
rarguelloF authored Dec 11, 2023
1 parent ef3fbf1 commit aada6eb
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
18 changes: 11 additions & 7 deletions contrib/google.golang.org/grpc/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func StreamClientInterceptor(opts ...Option) grpc.StreamClientInterceptor {
span tracer.Span
err error
)
span, ctx, err = doClientRequest(ctx, cfg, method, methodKind, opts,
span, ctx, err = doClientRequest(ctx, cfg, method, methodKind, cc, opts,
func(ctx context.Context, opts []grpc.CallOption) error {
var err error
stream, err = streamer(ctx, desc, cc, method, opts...)
Expand Down Expand Up @@ -154,7 +154,7 @@ func UnaryClientInterceptor(opts ...Option) grpc.UnaryClientInterceptor {
if _, ok := cfg.untracedMethods[method]; ok {
return invoker(ctx, method, req, reply, cc, opts...)
}
span, _, err := doClientRequest(ctx, cfg, method, methodKindUnary, opts,
span, _, err := doClientRequest(ctx, cfg, method, methodKindUnary, cc, opts,
func(ctx context.Context, opts []grpc.CallOption) error {
return invoker(ctx, method, req, reply, cc, opts...)
})
Expand All @@ -166,7 +166,7 @@ func UnaryClientInterceptor(opts ...Option) grpc.UnaryClientInterceptor {
// doClientRequest starts a new span and invokes the handler with the new context
// and options. The span should be finished by the caller.
func doClientRequest(
ctx context.Context, cfg *config, method string, methodKind string, opts []grpc.CallOption,
ctx context.Context, cfg *config, method string, methodKind string, cc *grpc.ClientConn, opts []grpc.CallOption,
handler func(ctx context.Context, opts []grpc.CallOption) error,
) (ddtrace.Span, context.Context, error) {
// inject the trace id into the metadata
Expand All @@ -182,7 +182,11 @@ func doClientRequest(
if methodKind != "" {
span.SetTag(tagMethodKind, methodKind)
}

if cc != nil {
if host, _, err := net.SplitHostPort(cc.Target()); err == nil {
span.SetTag(ext.PeerHostname, host)
}
}
// fill in the peer so we can add it to the tags
var p peer.Peer
opts = append(opts, grpc.Peer(&p))
Expand All @@ -199,10 +203,10 @@ func doClientRequest(
func setSpanTargetFromPeer(span ddtrace.Span, p peer.Peer) {
// if the peer was set, set the tags
if p.Addr != nil {
host, port, err := net.SplitHostPort(p.Addr.String())
ip, port, err := net.SplitHostPort(p.Addr.String())
if err == nil {
if host != "" {
span.SetTag(ext.TargetHost, host)
if ip != "" {
span.SetTag(ext.TargetHost, ip)
}
span.SetTag(ext.TargetPort, port)
}
Expand Down
5 changes: 4 additions & 1 deletion contrib/google.golang.org/grpc/grpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,9 @@ func TestUnary(t *testing.T) {
assert.NotNil(clientSpan)
assert.NotNil(rootSpan)

// this tag always contains the resolved address
assert.Equal(clientSpan.Tag(ext.TargetHost), "127.0.0.1")
assert.Equal(clientSpan.Tag(ext.PeerHostname), "localhost")
assert.Equal(clientSpan.Tag(ext.TargetPort), rig.port)
assert.Equal(clientSpan.Tag(tagCode), tt.wantCode.String())
assert.Equal(clientSpan.TraceID(), rootSpan.TraceID())
Expand Down Expand Up @@ -173,6 +175,7 @@ func TestStreaming(t *testing.T) {
case "grpc.client":
assert.Equal(t, "127.0.0.1", span.Tag(ext.TargetHost),
"expected target host tag to be set in span: %v", span)
assert.Equal(t, "localhost", span.Tag(ext.PeerHostname))
assert.Equal(t, rig.port, span.Tag(ext.TargetPort),
"expected target host port to be set in span: %v", span)
fallthrough
Expand Down Expand Up @@ -617,7 +620,7 @@ func newRigWithInterceptors(
// start our test fixtureServer.
go server.Serve(li)

conn, err := grpc.Dial(li.Addr().String(), clientInterceptors...)
conn, err := grpc.Dial("localhost:"+port, clientInterceptors...)
if err != nil {
return nil, fmt.Errorf("error dialing: %s", err)
}
Expand Down

0 comments on commit aada6eb

Please sign in to comment.