Skip to content

Commit

Permalink
Support proto v2 message format, drop v1 format
Browse files Browse the repository at this point in the history
  • Loading branch information
ash2k committed Aug 12, 2022
1 parent bfa71cf commit 4ff60c3
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 45 deletions.
4 changes: 2 additions & 2 deletions instrumentation/google.golang.org/grpc/otelgrpc/go.mod
Expand Up @@ -3,21 +3,21 @@ module go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgr
go 1.17

require (
github.com/golang/protobuf v1.5.2
go.opentelemetry.io/otel v1.9.0
go.opentelemetry.io/otel/trace v1.9.0
google.golang.org/grpc v1.48.0
google.golang.org/protobuf v1.28.1
)

require (
cloud.google.com/go v0.34.0 // indirect
github.com/go-logr/logr v1.2.3 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/golang/protobuf v1.5.2 // indirect
golang.org/x/net v0.0.0-20201021035429-f5854403a974 // indirect
golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d // indirect
golang.org/x/sys v0.0.0-20210119212857-b64e53b001e4 // indirect
golang.org/x/text v0.3.3 // indirect
google.golang.org/appengine v1.4.0 // indirect
google.golang.org/genproto v0.0.0-20200526211855-cb27e3aa2013 // indirect
google.golang.org/protobuf v1.27.1 // indirect
)
5 changes: 4 additions & 1 deletion instrumentation/google.golang.org/grpc/otelgrpc/go.sum
Expand Up @@ -50,6 +50,7 @@ github.com/google/go-cmp v0.5.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.8 h1:e6P7q2lk1O+qJJb4BtCQXlK8vWEO8V1ZeuEdJNOqZyg=
github.com/google/go-cmp v0.5.8/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/grpc-ecosystem/grpc-gateway v1.16.0/go.mod h1:BDjrQk3hbvj6Nolgz8mAMFbcEtjT1g+wF4CSlocrBnw=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
Expand All @@ -60,6 +61,7 @@ github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.1 h1:5TQK59W5E3v0r2duFAb7P95B6hEeOyEnHRa8MjYSMTY=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
go.opentelemetry.io/otel v1.9.0 h1:8WZNQFIB2a71LnANS9JeyidJKKGOOremcUtb/OtHISw=
go.opentelemetry.io/otel v1.9.0/go.mod h1:np4EoPGzoPs3O67xUVNoPPcmSvsfOxNlNA4F4AC+0Eo=
go.opentelemetry.io/otel/trace v1.9.0 h1:oZaCNJUjWcg60VXWee8lJKlqhPbXAPB51URuR47pQYc=
Expand Down Expand Up @@ -131,8 +133,9 @@ google.golang.org/protobuf v1.23.1-0.20200526195155-81db48ad09cc/go.mod h1:EGpAD
google.golang.org/protobuf v1.25.0/go.mod h1:9JNX74DMeImyA3h4bdi1ymwjUzf21/xIlbajtzgsN7c=
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
google.golang.org/protobuf v1.27.1 h1:SnqbnDw1V7RiZcXPx5MEeqPv2s79L9i7BJUlG/+RurQ=
google.golang.org/protobuf v1.27.1/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
google.golang.org/protobuf v1.28.1 h1:d0NfwRgPtno5B1Wa6L2DAG+KivqkdutMf1UhdNx175w=
google.golang.org/protobuf v1.28.1/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.3/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
Expand Down
Expand Up @@ -21,13 +21,12 @@ import (
"io"
"net"

"github.com/golang/protobuf/proto" // nolint:staticcheck

"google.golang.org/grpc"
grpc_codes "google.golang.org/grpc/codes"
"google.golang.org/grpc/metadata"
"google.golang.org/grpc/peer"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/proto"

"go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc/internal"
"go.opentelemetry.io/otel/attribute"
Expand All @@ -43,6 +42,9 @@ type messageType attribute.KeyValue
// passed context with id and size (if message is a proto message).
func (m messageType) Event(ctx context.Context, id int, message interface{}) {
span := trace.SpanFromContext(ctx)
if !span.IsRecording() {
return
}
if p, ok := message.(proto.Message); ok {
span.AddEvent("message", trace.WithAttributes(
attribute.KeyValue(m),
Expand Down
4 changes: 2 additions & 2 deletions instrumentation/google.golang.org/grpc/otelgrpc/test/go.mod
Expand Up @@ -3,20 +3,21 @@ module go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgr
go 1.17

require (
github.com/golang/protobuf v1.5.2
github.com/stretchr/testify v1.8.0
go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.34.0
go.opentelemetry.io/otel v1.9.0
go.opentelemetry.io/otel/sdk v1.9.0
go.uber.org/goleak v1.1.12
google.golang.org/grpc v1.48.0
google.golang.org/protobuf v1.28.1
)

require (
cloud.google.com/go v0.34.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/go-logr/logr v1.2.3 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
go.opentelemetry.io/otel/trace v1.9.0 // indirect
golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4 // indirect
Expand All @@ -25,7 +26,6 @@ require (
golang.org/x/text v0.3.3 // indirect
google.golang.org/appengine v1.4.0 // indirect
google.golang.org/genproto v0.0.0-20200526211855-cb27e3aa2013 // indirect
google.golang.org/protobuf v1.27.1 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

Expand Down
5 changes: 4 additions & 1 deletion instrumentation/google.golang.org/grpc/otelgrpc/test/go.sum
Expand Up @@ -51,6 +51,7 @@ github.com/google/go-cmp v0.5.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.8 h1:e6P7q2lk1O+qJJb4BtCQXlK8vWEO8V1ZeuEdJNOqZyg=
github.com/google/go-cmp v0.5.8/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/grpc-ecosystem/grpc-gateway v1.16.0/go.mod h1:BDjrQk3hbvj6Nolgz8mAMFbcEtjT1g+wF4CSlocrBnw=
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
Expand Down Expand Up @@ -116,6 +117,7 @@ golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210119212857-b64e53b001e4/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210423185535-09eb48e85fd7/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210510120138-977fb7262007 h1:gG67DSER+11cZvqIMb8S8bt0vZtiN6xWYARwirrOSfE=
golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
Expand Down Expand Up @@ -161,8 +163,9 @@ google.golang.org/protobuf v1.23.1-0.20200526195155-81db48ad09cc/go.mod h1:EGpAD
google.golang.org/protobuf v1.25.0/go.mod h1:9JNX74DMeImyA3h4bdi1ymwjUzf21/xIlbajtzgsN7c=
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
google.golang.org/protobuf v1.27.1 h1:SnqbnDw1V7RiZcXPx5MEeqPv2s79L9i7BJUlG/+RurQ=
google.golang.org/protobuf v1.27.1/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
google.golang.org/protobuf v1.28.1 h1:d0NfwRgPtno5B1Wa6L2DAG+KivqkdutMf1UhdNx175w=
google.golang.org/protobuf v1.28.1/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
Expand Down
Expand Up @@ -30,15 +30,16 @@ import (
"go.opentelemetry.io/otel/sdk/trace/tracetest"
semconv "go.opentelemetry.io/otel/semconv/v1.12.0"

"github.com/golang/protobuf/proto" //nolint:staticcheck
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.uber.org/goleak"
"google.golang.org/grpc"
grpc_codes "google.golang.org/grpc/codes"
"google.golang.org/grpc/credentials/insecure"
"google.golang.org/grpc/interop/grpc_testing"
"google.golang.org/grpc/metadata"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/proto"
)

func getSpanFromRecorder(sr *tracetest.SpanRecorder, name string) (trace.ReadOnlySpan, bool) {
Expand All @@ -65,18 +66,6 @@ func (mcuici *mockUICInvoker) invoker(ctx context.Context, method string, req, r
return nil
}

type mockProtoMessage struct{}

func (mm *mockProtoMessage) Reset() {
}

func (mm *mockProtoMessage) String() string {
return "mock"
}

func (mm *mockProtoMessage) ProtoMessage() {
}

func TestUnaryClientInterceptor(t *testing.T) {
clientConn, err := grpc.Dial("fake:connection", grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
Expand All @@ -88,8 +77,8 @@ func TestUnaryClientInterceptor(t *testing.T) {
tp := trace.NewTracerProvider(trace.WithSpanProcessor(sr))
unaryInterceptor := otelgrpc.UnaryClientInterceptor(otelgrpc.WithTracerProvider(tp))

req := &mockProtoMessage{}
reply := &mockProtoMessage{}
req := &grpc_testing.SimpleRequest{}
reply := &grpc_testing.SimpleResponse{}
uniInterceptorInvoker := &mockUICInvoker{}

checks := []struct {
Expand All @@ -115,12 +104,12 @@ func TestUnaryClientInterceptor(t *testing.T) {
{
otelgrpc.RPCMessageTypeKey: attribute.StringValue("SENT"),
otelgrpc.RPCMessageIDKey: attribute.IntValue(1),
otelgrpc.RPCMessageUncompressedSizeKey: attribute.IntValue(proto.Size(proto.Message(req))),
otelgrpc.RPCMessageUncompressedSizeKey: attribute.IntValue(proto.Size(req)),
},
{
otelgrpc.RPCMessageTypeKey: attribute.StringValue("RECEIVED"),
otelgrpc.RPCMessageIDKey: attribute.IntValue(1),
otelgrpc.RPCMessageUncompressedSizeKey: attribute.IntValue(proto.Size(proto.Message(reply))),
otelgrpc.RPCMessageUncompressedSizeKey: attribute.IntValue(proto.Size(reply)),
},
},
},
Expand All @@ -139,12 +128,12 @@ func TestUnaryClientInterceptor(t *testing.T) {
{
otelgrpc.RPCMessageTypeKey: attribute.StringValue("SENT"),
otelgrpc.RPCMessageIDKey: attribute.IntValue(1),
otelgrpc.RPCMessageUncompressedSizeKey: attribute.IntValue(proto.Size(proto.Message(req))),
otelgrpc.RPCMessageUncompressedSizeKey: attribute.IntValue(proto.Size(req)),
},
{
otelgrpc.RPCMessageTypeKey: attribute.StringValue("RECEIVED"),
otelgrpc.RPCMessageIDKey: attribute.IntValue(1),
otelgrpc.RPCMessageUncompressedSizeKey: attribute.IntValue(proto.Size(proto.Message(reply))),
otelgrpc.RPCMessageUncompressedSizeKey: attribute.IntValue(proto.Size(reply)),
},
},
},
Expand All @@ -163,12 +152,12 @@ func TestUnaryClientInterceptor(t *testing.T) {
{
otelgrpc.RPCMessageTypeKey: attribute.StringValue("SENT"),
otelgrpc.RPCMessageIDKey: attribute.IntValue(1),
otelgrpc.RPCMessageUncompressedSizeKey: attribute.IntValue(proto.Size(proto.Message(req))),
otelgrpc.RPCMessageUncompressedSizeKey: attribute.IntValue(proto.Size(req)),
},
{
otelgrpc.RPCMessageTypeKey: attribute.StringValue("RECEIVED"),
otelgrpc.RPCMessageIDKey: attribute.IntValue(1),
otelgrpc.RPCMessageUncompressedSizeKey: attribute.IntValue(proto.Size(proto.Message(reply))),
otelgrpc.RPCMessageUncompressedSizeKey: attribute.IntValue(proto.Size(reply)),
},
},
},
Expand All @@ -188,12 +177,12 @@ func TestUnaryClientInterceptor(t *testing.T) {
{
otelgrpc.RPCMessageTypeKey: attribute.StringValue("SENT"),
otelgrpc.RPCMessageIDKey: attribute.IntValue(1),
otelgrpc.RPCMessageUncompressedSizeKey: attribute.IntValue(proto.Size(proto.Message(req))),
otelgrpc.RPCMessageUncompressedSizeKey: attribute.IntValue(proto.Size(req)),
},
{
otelgrpc.RPCMessageTypeKey: attribute.StringValue("RECEIVED"),
otelgrpc.RPCMessageIDKey: attribute.IntValue(1),
otelgrpc.RPCMessageUncompressedSizeKey: attribute.IntValue(proto.Size(proto.Message(reply))),
otelgrpc.RPCMessageUncompressedSizeKey: attribute.IntValue(proto.Size(reply)),
},
},
expectErr: true,
Expand All @@ -211,12 +200,12 @@ func TestUnaryClientInterceptor(t *testing.T) {
{
otelgrpc.RPCMessageTypeKey: attribute.StringValue("SENT"),
otelgrpc.RPCMessageIDKey: attribute.IntValue(1),
otelgrpc.RPCMessageUncompressedSizeKey: attribute.IntValue(proto.Size(proto.Message(req))),
otelgrpc.RPCMessageUncompressedSizeKey: attribute.IntValue(proto.Size(req)),
},
{
otelgrpc.RPCMessageTypeKey: attribute.StringValue("RECEIVED"),
otelgrpc.RPCMessageIDKey: attribute.IntValue(1),
otelgrpc.RPCMessageUncompressedSizeKey: attribute.IntValue(proto.Size(proto.Message(reply))),
otelgrpc.RPCMessageUncompressedSizeKey: attribute.IntValue(proto.Size(reply)),
},
},
},
Expand All @@ -235,12 +224,12 @@ func TestUnaryClientInterceptor(t *testing.T) {
{
otelgrpc.RPCMessageTypeKey: attribute.StringValue("SENT"),
otelgrpc.RPCMessageIDKey: attribute.IntValue(1),
otelgrpc.RPCMessageUncompressedSizeKey: attribute.IntValue(proto.Size(proto.Message(req))),
otelgrpc.RPCMessageUncompressedSizeKey: attribute.IntValue(proto.Size(req)),
},
{
otelgrpc.RPCMessageTypeKey: attribute.StringValue("RECEIVED"),
otelgrpc.RPCMessageIDKey: attribute.IntValue(1),
otelgrpc.RPCMessageUncompressedSizeKey: attribute.IntValue(proto.Size(proto.Message(reply))),
otelgrpc.RPCMessageUncompressedSizeKey: attribute.IntValue(proto.Size(reply)),
},
},
},
Expand Down Expand Up @@ -277,7 +266,7 @@ func eventAttrMap(events []trace.Event) []map[attribute.Key]attribute.Value {
type mockClientStream struct {
Desc *grpc.StreamDesc
Ctx context.Context
msgs []mockProtoMessage
msgs []grpc_testing.SimpleResponse
}

func (mockClientStream) SendMsg(m interface{}) error { return nil }
Expand All @@ -299,9 +288,9 @@ type clientStreamOpts struct {
}

func newMockClientStream(opts clientStreamOpts) *mockClientStream {
var msgs []mockProtoMessage
var msgs []grpc_testing.SimpleResponse
for i := 0; i < opts.NumRecvMsgs; i++ {
msgs = append(msgs, mockProtoMessage{})
msgs = append(msgs, grpc_testing.SimpleResponse{})
}
return &mockClientStream{msgs: msgs}
}
Expand Down Expand Up @@ -347,8 +336,8 @@ func TestStreamClientInterceptorOnBIDIStream(t *testing.T) {
_, ok := getSpanFromRecorder(sr, name)
require.False(t, ok, "span should not end while stream is open")

req := &mockProtoMessage{}
reply := &mockProtoMessage{}
req := &grpc_testing.SimpleRequest{}
reply := &grpc_testing.SimpleResponse{}

// send and receive fake data
for i := 0; i < 10; i++ {
Expand Down Expand Up @@ -414,8 +403,8 @@ func TestStreamClientInterceptorOnUnidirectionalClientServerStream(t *testing.T)
_, ok := getSpanFromRecorder(sr, name)
require.False(t, ok, "span should not end while stream is open")

req := &mockProtoMessage{}
reply := &mockProtoMessage{}
req := &grpc_testing.SimpleRequest{}
reply := &grpc_testing.SimpleResponse{}

// send fake data
for i := 0; i < 10; i++ {
Expand Down Expand Up @@ -509,8 +498,8 @@ func TestStreamClientInterceptorCancelContext(t *testing.T) {
_, ok := getSpanFromRecorder(sr, name)
require.False(t, ok, "span should not ended while stream is open")

req := &mockProtoMessage{}
reply := &mockProtoMessage{}
req := &grpc_testing.SimpleRequest{}
reply := &grpc_testing.SimpleResponse{}

// send and receive fake data
for i := 0; i < 10; i++ {
Expand Down Expand Up @@ -581,7 +570,7 @@ func TestServerInterceptorError(t *testing.T) {
handler := func(_ context.Context, _ interface{}) (interface{}, error) {
return nil, deniedErr
}
_, err := usi(context.Background(), &mockProtoMessage{}, &grpc.UnaryServerInfo{}, handler)
_, err := usi(context.Background(), &grpc_testing.SimpleRequest{}, &grpc.UnaryServerInfo{}, handler)
require.Error(t, err)
assert.Equal(t, err, deniedErr)

Expand Down

0 comments on commit 4ff60c3

Please sign in to comment.