-
Notifications
You must be signed in to change notification settings - Fork 443
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
contrib/google.golang.org/grpc: trace grpc encoding
- Loading branch information
1 parent
3cb8ab2
commit 6028bfe
Showing
8 changed files
with
183 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
// Unless explicitly stated otherwise all files in this repository are licensed | ||
// under the Apache License Version 2.0. | ||
// This product includes software developed at Datadog (https://www.datadoghq.com/). | ||
// Copyright 2016 Datadog, Inc. | ||
|
||
package grpc | ||
|
||
import ( | ||
"context" | ||
|
||
"google.golang.org/grpc/encoding" | ||
encodingproto "google.golang.org/grpc/encoding/proto" | ||
"google.golang.org/grpc/mem" | ||
|
||
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace/tracer" | ||
"gopkg.in/DataDog/dd-trace-go.v1/internal" | ||
) | ||
|
||
func init() { | ||
if !internal.BoolEnv("DD_TRACE_GRPC_TRACE_ENCODING", false) { | ||
return | ||
} | ||
_ = registerCodec() | ||
} | ||
|
||
func registerCodec() (unregister func()) { | ||
protoCodec := encoding.GetCodecV2(encodingproto.Name) | ||
encoding.RegisterCodecV2(&tracedCodec{protoCodec}) | ||
return func() { | ||
encoding.RegisterCodecV2(protoCodec) | ||
} | ||
} | ||
|
||
type tracedCodec struct { | ||
protoCodec encoding.CodecV2 | ||
} | ||
|
||
func (t *tracedCodec) Marshal(v any) (out mem.BufferSlice, err error) { | ||
ctx := context.Background() | ||
span, _ := tracer.StartSpanFromContext(ctx, "proto.Marshal") | ||
defer span.Finish(tracer.WithError(err)) | ||
return t.protoCodec.Marshal(v) | ||
} | ||
|
||
func (t *tracedCodec) Unmarshal(data mem.BufferSlice, v any) (err error) { | ||
ctx := context.Background() | ||
span, _ := tracer.StartSpanFromContext(ctx, "proto.Unmarshal") | ||
defer span.Finish(tracer.WithError(err)) | ||
return t.protoCodec.Unmarshal(data, v) | ||
} | ||
|
||
func (t *tracedCodec) Name() string { | ||
return encodingproto.Name | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
// Unless explicitly stated otherwise all files in this repository are licensed | ||
// under the Apache License Version 2.0. | ||
// This product includes software developed at Datadog (https://www.datadoghq.com/). | ||
// Copyright 2016 Datadog, Inc. | ||
|
||
package grpc | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
|
||
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace/mocktracer" | ||
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace/tracer" | ||
) | ||
|
||
func TestTraceEncoding(t *testing.T) { | ||
unregister := registerCodec() | ||
defer unregister() | ||
|
||
rig, err := newRig(true, WithServiceName("grpc"), WithRequestTags()) | ||
require.NoError(t, err, "error setting up rig") | ||
defer func() { assert.NoError(t, rig.Close()) }() | ||
client := rig.client | ||
|
||
mt := mocktracer.Start() | ||
defer mt.Stop() | ||
|
||
span, ctx := tracer.StartSpanFromContext(context.Background(), "root") | ||
_, err = client.Ping(ctx, &FixtureRequest{Name: "pass"}) | ||
require.NoError(t, err) | ||
span.Finish() | ||
|
||
spans := mt.FinishedSpans() | ||
require.Len(t, spans, 7) | ||
|
||
var ( | ||
clientMarshalSpan = spans[0] | ||
clientUnmarshalSpan = spans[1] | ||
serverSpan = spans[2] | ||
serverMarshalSpan = spans[3] | ||
serverUnmarshalSpan = spans[4] | ||
clientSpan = spans[5] | ||
rootSpan = spans[6] | ||
) | ||
assertParentAndChildSpans(t, rootSpan, clientSpan, "root and client") | ||
assertParentAndChildSpans(t, clientSpan, serverSpan, "client and server") | ||
|
||
assertSiblingSpans(t, clientSpan, clientMarshalSpan, "client and client.marshal") | ||
assertSiblingSpans(t, clientSpan, clientUnmarshalSpan, "client and client.unmarshal") | ||
|
||
assertSiblingSpans(t, serverSpan, serverMarshalSpan, "server and server.marshal") | ||
assertSiblingSpans(t, serverSpan, serverUnmarshalSpan, "server and server.unmarshal") | ||
} | ||
|
||
func assertParentAndChildSpans(t *testing.T, parent, child mocktracer.Span, msg string) { | ||
t.Helper() | ||
res := child.ParentID() == parent.SpanID() && child.TraceID() == parent.TraceID() | ||
assert.Truef(t, res, "%s: spans are not parent and child", msg) | ||
} | ||
|
||
func assertSiblingSpans(t *testing.T, s1, s2 mocktracer.Span, msg string) { | ||
t.Helper() | ||
res := s1.TraceID() == s2.TraceID() | ||
assert.Truef(t, res, "%s: spans are not siblings", msg) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.