-
Notifications
You must be signed in to change notification settings - Fork 438
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
WIP: contrib/google.golang.org/grpc: trace grpc encoding #2959
Draft
rarguelloF
wants to merge
2
commits into
main
Choose a base branch
from
rarguelloF/trace-grpc-encoding
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this order always stable? It looks like a potential flaky test.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it should be, the sequence of the operations should be this. The existing tests assume the order of the server / client spans and don't seem to be flaky, but if it turns out to be flaky we can change this to find the spans by name.