From 6bfa16ecef9e41bcafbe4c4bd2f822bd7c2337df Mon Sep 17 00:00:00 2001 From: Ahmed Mujtaba Date: Sat, 2 May 2020 23:56:08 +0200 Subject: [PATCH 1/6] Added test case for grpc UrinaryInterceptorClient --- plugin/grpctrace/interceptor_test.go | 141 ++++++++++++--------------- 1 file changed, 65 insertions(+), 76 deletions(-) diff --git a/plugin/grpctrace/interceptor_test.go b/plugin/grpctrace/interceptor_test.go index d9360341d..8c5ec6f69 100644 --- a/plugin/grpctrace/interceptor_test.go +++ b/plugin/grpctrace/interceptor_test.go @@ -20,6 +20,8 @@ import ( "google.golang.org/grpc" + "go.opentelemetry.io/otel/api/core" + "go.opentelemetry.io/otel/api/global" export "go.opentelemetry.io/otel/sdk/export/trace" sdktrace "go.opentelemetry.io/otel/sdk/trace" ) @@ -32,110 +34,97 @@ func (t *testExporter) ExportSpan(ctx context.Context, s *export.SpanData) { t.spanMap[s.Name] = append(t.spanMap[s.Name], s) } -type mockCCInvoker struct { +type mockUICInvoker struct { ctx context.Context } -func (mcci *mockCCInvoker) invoke(ctx context.Context, method string, req, reply interface{}, cc *grpc.ClientConn, opts ...grpc.CallOption) error { - mcci.ctx = ctx +func (mcuici *mockUICInvoker) invoker(ctx context.Context, method string, req, reply interface{}, cc *grpc.ClientConn, opts ...grpc.CallOption) error { + mcuici.ctx = ctx return nil } -type mockProtoMessage struct { +type mockProtoMessage struct{} + +func (mm *mockProtoMessage) Reset() { } -func (mm *mockProtoMessage) Reset() {} -func (mm *mockProtoMessage) String() string { return "mock" } -func (mm *mockProtoMessage) ProtoMessage() {} - -type nameAttributeTestCase struct { - testName string - expectedName string - fullNameFmt string +func (mm *mockProtoMessage) String() string { + return "mock" } -func (tc nameAttributeTestCase) fullName() string { - return fmt.Sprintf(tc.fullNameFmt, tc.expectedName) +func (mm *mockProtoMessage) ProtoMessage() { } -func TestUCISetsExpectedServiceNameAttribute(t *testing.T) { - testCases := []nameAttributeTestCase{ - { - "FullyQualifiedMethodName", - "serviceName", - "/github.com.foo.%s/bar", - }, - { - "SimpleMethodName", - "serviceName", - "/%s/bar", - }, - { - "MethodNameWithoutFullPath", - "serviceName", - "%s/bar", - }, - { - "InvalidMethodName", - "", - "invalidName", - }, - { - "NonAlphanumericMethodName", - "serviceName_123", - "/github.com.foo.%s/method", - }, - } - - for _, tc := range testCases { - t.Run(tc.testName, tc.testUCISetsExpectedNameAttribute) - } -} - -func (tc nameAttributeTestCase) testUCISetsExpectedNameAttribute(t *testing.T) { +func TestUnaryClientInterceptor(t *testing.T) { exp := &testExporter{make(map[string][]*export.SpanData)} tp, _ := sdktrace.NewProvider(sdktrace.WithSyncer(exp), - sdktrace.WithConfig(sdktrace.Config{DefaultSampler: sdktrace.AlwaysSample()})) - - tr := tp.Tracer("grpctrace/client") - ctx, span := tr.Start(context.Background(), tc.testName) - defer span.End() + sdktrace.WithConfig(sdktrace.Config{ + DefaultSampler: sdktrace.AlwaysSample(), + }, + )) + global.SetTraceProvider(tp) clientConn, err := grpc.Dial("fake:connection", grpc.WithInsecure()) - if err != nil { t.Fatalf("failed to create client connection: %v", err) } - unaryInt := UnaryClientInterceptor(tr) + tracer := tp.Tracer("grpctrace/client") + unaryInterceptor := UnaryClientInterceptor(tracer) req := &mockProtoMessage{} reply := &mockProtoMessage{} - ccInvoker := &mockCCInvoker{} + uniInterceptorInvoker := &mockUICInvoker{} - err = unaryInt(ctx, tc.fullName(), req, reply, clientConn, ccInvoker.invoke) - if err != nil { - t.Fatalf("failed to run unary interceptor: %v", err) + checks := []struct { + name string + expectedAttr map[core.Key]core.Value + eventsAttr [][]core.KeyValue + }{ + { + name: fmt.Sprintf("/foo.%s/bar", "serviceName"), + expectedAttr: map[core.Key]core.Value{ + rpcServiceKey: core.String("serviceName"), + netPeerIPKey: core.String("fake"), + netPeerPortKey: core.String("connection"), + }, + eventsAttr: [][]core.KeyValue{ + { + core.KeyValue{Key: messageTypeKey, Value: core.String("SENT")}, + core.KeyValue{Key: messageIDKey, Value: core.Int(1)}, + }, + { + core.KeyValue{Key: messageTypeKey, Value: core.String("RECEIVED")}, + core.KeyValue{Key: messageIDKey, Value: core.Int(1)}, + }, + }, + }, } - spanData, hasSpanData := exp.spanMap[tc.fullName()] + for _, check := range checks { + err = unaryInterceptor(context.Background(), check.name, req, reply, clientConn, uniInterceptorInvoker.invoker) + if err != nil { + t.Fatalf("failed to run unary interceptor: %v", err) + } - if !hasSpanData || len(spanData) == 0 { - t.Fatalf("no span data found for name < %s >", tc.fullName()) - } + attrs := exp.spanMap[check.name][0].Attributes + for _, attr := range attrs { + expectedAttr, ok := check.expectedAttr[attr.Key] + if ok { + if expectedAttr != attr.Value { + t.Fatalf("invalid %s found. expected %s, actual %s", string(attr.Key), + expectedAttr.AsString(), attr.Value.AsString()) + } + } + } - attributes := spanData[0].Attributes - - var actualServiceName string - for _, attr := range attributes { - if attr.Key == rpcServiceKey { - actualServiceName = attr.Value.AsString() - break + events := exp.spanMap[check.name][0].MessageEvents + for event := 0; event < len(check.eventsAttr); event++ { + for attr := 0; attr < len(check.eventsAttr[event]); attr++ { + if events[event].Attributes[attr] != check.eventsAttr[event][attr] { + t.Fatalf("invalid attribute in events") + } + } } } - - if tc.expectedName != actualServiceName { - t.Fatalf("invalid service name found. expected %s, actual %s", - tc.expectedName, actualServiceName) - } } From 02ff1be72ccc4447c17b88816c20d71d43536419 Mon Sep 17 00:00:00 2001 From: Ahmed Mujtaba Date: Tue, 5 May 2020 22:50:01 +0200 Subject: [PATCH 2/6] Minor fixes and improvment in GRPC urinary interceptor test --- plugin/grpctrace/interceptor_test.go | 82 ++++++++++++++++++++++------ 1 file changed, 66 insertions(+), 16 deletions(-) diff --git a/plugin/grpctrace/interceptor_test.go b/plugin/grpctrace/interceptor_test.go index 8c5ec6f69..fac424933 100644 --- a/plugin/grpctrace/interceptor_test.go +++ b/plugin/grpctrace/interceptor_test.go @@ -21,7 +21,6 @@ import ( "google.golang.org/grpc" "go.opentelemetry.io/otel/api/core" - "go.opentelemetry.io/otel/api/global" export "go.opentelemetry.io/otel/sdk/export/trace" sdktrace "go.opentelemetry.io/otel/sdk/trace" ) @@ -62,7 +61,6 @@ func TestUnaryClientInterceptor(t *testing.T) { DefaultSampler: sdktrace.AlwaysSample(), }, )) - global.SetTraceProvider(tp) clientConn, err := grpc.Dial("fake:connection", grpc.WithInsecure()) if err != nil { @@ -79,50 +77,102 @@ func TestUnaryClientInterceptor(t *testing.T) { checks := []struct { name string expectedAttr map[core.Key]core.Value - eventsAttr [][]core.KeyValue + eventsAttr []map[core.Key]core.Value }{ { - name: fmt.Sprintf("/foo.%s/bar", "serviceName"), + name: "/github.com.serviceName/bar", expectedAttr: map[core.Key]core.Value{ rpcServiceKey: core.String("serviceName"), netPeerIPKey: core.String("fake"), netPeerPortKey: core.String("connection"), }, - eventsAttr: [][]core.KeyValue{ + eventsAttr: []map[core.Key]core.Value{ { - core.KeyValue{Key: messageTypeKey, Value: core.String("SENT")}, - core.KeyValue{Key: messageIDKey, Value: core.Int(1)}, + messageTypeKey: core.String("SENT"), + messageIDKey: core.Int(1), }, { - core.KeyValue{Key: messageTypeKey, Value: core.String("RECEIVED")}, - core.KeyValue{Key: messageIDKey, Value: core.Int(1)}, + messageTypeKey: core.String("RECEIVED"), + messageIDKey: core.Int(1), }, }, }, + { + name: "/serviceName/bar", + expectedAttr: map[core.Key]core.Value{ + rpcServiceKey: core.String("serviceName"), + }, + eventsAttr: []map[core.Key]core.Value{ + { + messageTypeKey: core.String("SENT"), + messageIDKey: core.Int(1), + }, + { + messageTypeKey: core.String("RECEIVED"), + messageIDKey: core.Int(1), + }, + }, + }, + { + name: "serviceName/bar", + expectedAttr: map[core.Key]core.Value{rpcServiceKey: core.String("serviceName")}, + }, + { + name: "invalidName", + expectedAttr: map[core.Key]core.Value{rpcServiceKey: core.String("")}, + }, + { + name: "/github.com.foo.serviceName_123/method", + expectedAttr: map[core.Key]core.Value{rpcServiceKey: core.String("serviceName_123")}, + }, } - for _, check := range checks { + for idx, check := range checks { + fmt.Println("================", idx, "==================") err = unaryInterceptor(context.Background(), check.name, req, reply, clientConn, uniInterceptorInvoker.invoker) if err != nil { t.Fatalf("failed to run unary interceptor: %v", err) } - attrs := exp.spanMap[check.name][0].Attributes + spanData, ok := exp.spanMap[check.name] + if !ok || len(spanData) == 0 { + t.Fatalf("no span data found for name < %s >", check.name) + } + + attrs := spanData[0].Attributes for _, attr := range attrs { expectedAttr, ok := check.expectedAttr[attr.Key] if ok { if expectedAttr != attr.Value { - t.Fatalf("invalid %s found. expected %s, actual %s", string(attr.Key), + t.Errorf("name: %s invalid %s found. expected %s, actual %s", check.name, string(attr.Key), expectedAttr.AsString(), attr.Value.AsString()) } + delete(check.expectedAttr, attr.Key) } } - events := exp.spanMap[check.name][0].MessageEvents + // Check if any expected attr not seen + if len(check.expectedAttr) > 0 { + for attr := range check.expectedAttr { + t.Errorf("missing attribute %s in span", string(attr)) + } + } + + events := spanData[0].MessageEvents for event := 0; event < len(check.eventsAttr); event++ { - for attr := 0; attr < len(check.eventsAttr[event]); attr++ { - if events[event].Attributes[attr] != check.eventsAttr[event][attr] { - t.Fatalf("invalid attribute in events") + for _, attr := range events[event].Attributes { + expectedAttr, ok := check.eventsAttr[event][attr.Key] + if ok { + if attr.Value != expectedAttr { + t.Errorf("invalid value for attribute %s in events, expected %s actual %s", + string(attr.Key), attr.Value.AsString(), expectedAttr.AsString()) + } + delete(check.eventsAttr[event], attr.Key) + } + } + if len(check.eventsAttr[event]) > 0 { + for attr := range check.eventsAttr[event] { + t.Errorf("missing attribute %s in span event", string(attr)) } } } From cffc57c907a4d002057e688ad4f62097eabc876d Mon Sep 17 00:00:00 2001 From: Ahmed Mujtaba Date: Sun, 10 May 2020 23:19:20 +0200 Subject: [PATCH 3/6] Added grpc stream interceptor client --- plugin/grpctrace/interceptor_test.go | 133 ++++++++++++++++++++++++++- 1 file changed, 129 insertions(+), 4 deletions(-) diff --git a/plugin/grpctrace/interceptor_test.go b/plugin/grpctrace/interceptor_test.go index fac424933..2dce41797 100644 --- a/plugin/grpctrace/interceptor_test.go +++ b/plugin/grpctrace/interceptor_test.go @@ -15,10 +15,12 @@ package grpctrace import ( "context" - "fmt" + "sync" "testing" + "time" "google.golang.org/grpc" + "google.golang.org/grpc/metadata" "go.opentelemetry.io/otel/api/core" export "go.opentelemetry.io/otel/sdk/export/trace" @@ -26,10 +28,13 @@ import ( ) type testExporter struct { + mu sync.Mutex spanMap map[string][]*export.SpanData } func (t *testExporter) ExportSpan(ctx context.Context, s *export.SpanData) { + t.mu.Lock() + defer t.mu.Unlock() t.spanMap[s.Name] = append(t.spanMap[s.Name], s) } @@ -55,7 +60,7 @@ func (mm *mockProtoMessage) ProtoMessage() { } func TestUnaryClientInterceptor(t *testing.T) { - exp := &testExporter{make(map[string][]*export.SpanData)} + exp := &testExporter{spanMap: make(map[string][]*export.SpanData)} tp, _ := sdktrace.NewProvider(sdktrace.WithSyncer(exp), sdktrace.WithConfig(sdktrace.Config{ DefaultSampler: sdktrace.AlwaysSample(), @@ -127,8 +132,7 @@ func TestUnaryClientInterceptor(t *testing.T) { }, } - for idx, check := range checks { - fmt.Println("================", idx, "==================") + for _, check := range checks { err = unaryInterceptor(context.Background(), check.name, req, reply, clientConn, uniInterceptorInvoker.invoker) if err != nil { t.Fatalf("failed to run unary interceptor: %v", err) @@ -178,3 +182,124 @@ func TestUnaryClientInterceptor(t *testing.T) { } } } + +type mockClientStream struct { + Desc *grpc.StreamDesc + Ctx context.Context +} + +func (mockClientStream) SendMsg(m interface{}) error { return nil } +func (mockClientStream) RecvMsg(m interface{}) error { return nil } +func (mockClientStream) CloseSend() error { return nil } +func (c mockClientStream) Context() context.Context { return c.Ctx } +func (mockClientStream) Header() (metadata.MD, error) { return nil, nil } +func (mockClientStream) Trailer() metadata.MD { return nil } + +func TestStreamClientInterceptor(t *testing.T) { + exp := &testExporter{spanMap: make(map[string][]*export.SpanData)} + tp, _ := sdktrace.NewProvider(sdktrace.WithSyncer(exp), + sdktrace.WithConfig(sdktrace.Config{ + DefaultSampler: sdktrace.AlwaysSample(), + }, + )) + clientConn, err := grpc.Dial("fake:connection", grpc.WithInsecure()) + if err != nil { + t.Fatalf("failed to create client connection: %v", err) + } + + // tracer + tracer := tp.Tracer("grpctrace/Server") + streamCI := StreamClientInterceptor(tracer) + + var mockClStr mockClientStream + methodName := "/github.com.serviceName/bar" + + streamClient, err := streamCI(context.Background(), + &grpc.StreamDesc{ServerStreams: true}, + clientConn, + methodName, + func(ctx context.Context, + desc *grpc.StreamDesc, + cc *grpc.ClientConn, + method string, + opts ...grpc.CallOption) (grpc.ClientStream, error) { + mockClStr = mockClientStream{Desc: desc, Ctx: ctx} + return mockClStr, nil + }) + + if err != nil { + t.Fatalf("failed to initialize grpc stream client: %v", err) + } + + // no span exported while stream is open + if _, ok := exp.spanMap[methodName]; ok { + t.Fatalf("span shouldn't end while stream is open") + } + + req := &mockProtoMessage{} + reply := &mockProtoMessage{} + + // send and receive fake data + for i := 0; i < 10; i++ { + _ = streamClient.SendMsg(req) + _ = streamClient.RecvMsg(reply) + } + + // close client and server stream + _ = streamClient.CloseSend() + mockClStr.Desc.ServerStreams = false + _ = streamClient.RecvMsg(reply) + + // added retry because span end is called in separate go routine + var spanData []*export.SpanData + for retry := 0; retry < 5; retry++ { + ok := false + exp.mu.Lock() + spanData, ok = exp.spanMap[methodName] + exp.mu.Unlock() + if ok { + break + } + time.Sleep(time.Second * 1) + } + if len(spanData) == 0 { + t.Fatalf("no span data found for name < %s >", methodName) + } + + attrs := spanData[0].Attributes + expectedAttr := map[core.Key]string{ + rpcServiceKey: "serviceName", + netPeerIPKey: "fake", + netPeerPortKey: "connection", + } + + for _, attr := range attrs { + expected, ok := expectedAttr[attr.Key] + if ok { + if expected != attr.Value.AsString() { + t.Errorf("name: %s invalid %s found. expected %s, actual %s", methodName, string(attr.Key), + expected, attr.Value.AsString()) + } + } + } + + events := spanData[0].MessageEvents + if len(events) != 20 { + t.Fatalf("incorrect number of events expected 20 got %d", len(events)) + } + for i := 0; i < 20; i += 2 { + msgID := i/2 + 1 + validate := func(eventName string, attrs []core.KeyValue) { + for _, attr := range attrs { + if attr.Key == messageTypeKey && attr.Value.AsString() != eventName { + t.Errorf("invalid event on index: %d expecting %s event, receive %s event", i, eventName, attr.Value.AsString()) + } + if attr.Key == messageIDKey && attr.Value != core.Int(msgID) { + t.Errorf("invalid id for message event expected %d received %d", msgID, attr.Value.AsInt32()) + } + } + } + validate("SENT", events[i].Attributes) + validate("RECEIVED", events[i+1].Attributes) + } +} From c40b3d47cf4aa0f024140013041607a41f184325 Mon Sep 17 00:00:00 2001 From: Ahmed Mujtaba Date: Wed, 13 May 2020 19:43:20 +0200 Subject: [PATCH 4/6] minor improvements in grpc interceptor test --- plugin/grpctrace/interceptor_test.go | 36 ++++++++++++++++++---------- 1 file changed, 23 insertions(+), 13 deletions(-) diff --git a/plugin/grpctrace/interceptor_test.go b/plugin/grpctrace/interceptor_test.go index 2dce41797..3667b9dbe 100644 --- a/plugin/grpctrace/interceptor_test.go +++ b/plugin/grpctrace/interceptor_test.go @@ -29,13 +29,13 @@ import ( type testExporter struct { mu sync.Mutex - spanMap map[string][]*export.SpanData + spanMap map[string]*export.SpanData } func (t *testExporter) ExportSpan(ctx context.Context, s *export.SpanData) { t.mu.Lock() defer t.mu.Unlock() - t.spanMap[s.Name] = append(t.spanMap[s.Name], s) + t.spanMap[s.Name] = s } type mockUICInvoker struct { @@ -60,7 +60,7 @@ func (mm *mockProtoMessage) ProtoMessage() { } func TestUnaryClientInterceptor(t *testing.T) { - exp := &testExporter{spanMap: make(map[string][]*export.SpanData)} + exp := &testExporter{spanMap: make(map[string]*export.SpanData)} tp, _ := sdktrace.NewProvider(sdktrace.WithSyncer(exp), sdktrace.WithConfig(sdktrace.Config{ DefaultSampler: sdktrace.AlwaysSample(), @@ -135,15 +135,21 @@ func TestUnaryClientInterceptor(t *testing.T) { for _, check := range checks { err = unaryInterceptor(context.Background(), check.name, req, reply, clientConn, uniInterceptorInvoker.invoker) if err != nil { - t.Fatalf("failed to run unary interceptor: %v", err) + t.Errorf("failed to run unary interceptor: %v", err) + continue } spanData, ok := exp.spanMap[check.name] - if !ok || len(spanData) == 0 { - t.Fatalf("no span data found for name < %s >", check.name) + if !ok { + t.Errorf("no span data found for name < %s >", check.name) + continue } - attrs := spanData[0].Attributes + attrs := spanData.Attributes + if len(check.expectedAttr) > len(attrs) { + t.Errorf("attributes received are less than expected attributes, received %d, expected %d", + len(attrs), len(check.expectedAttr)) + } for _, attr := range attrs { expectedAttr, ok := check.expectedAttr[attr.Key] if ok { @@ -162,7 +168,11 @@ func TestUnaryClientInterceptor(t *testing.T) { } } - events := spanData[0].MessageEvents + events := spanData.MessageEvents + if len(check.eventsAttr) > len(events) { + t.Errorf("events received are less than expected events, received %d, expected %d", + len(events), len(check.eventsAttr)) + } for event := 0; event < len(check.eventsAttr); event++ { for _, attr := range events[event].Attributes { expectedAttr, ok := check.eventsAttr[event][attr.Key] @@ -196,7 +206,7 @@ func (mockClientStream) Header() (metadata.MD, error) { return nil, nil } func (mockClientStream) Trailer() metadata.MD { return nil } func TestStreamClientInterceptor(t *testing.T) { - exp := &testExporter{spanMap: make(map[string][]*export.SpanData)} + exp := &testExporter{spanMap: make(map[string]*export.SpanData)} tp, _ := sdktrace.NewProvider(sdktrace.WithSyncer(exp), sdktrace.WithConfig(sdktrace.Config{ DefaultSampler: sdktrace.AlwaysSample(), @@ -251,7 +261,7 @@ func TestStreamClientInterceptor(t *testing.T) { _ = streamClient.RecvMsg(reply) // added retry because span end is called in separate go routine - var spanData []*export.SpanData + var spanData *export.SpanData for retry := 0; retry < 5; retry++ { ok := false exp.mu.Lock() @@ -262,11 +272,11 @@ func TestStreamClientInterceptor(t *testing.T) { } time.Sleep(time.Second * 1) } - if len(spanData) == 0 { + if spanData == nil { t.Fatalf("no span data found for name < %s >", methodName) } - attrs := spanData[0].Attributes + attrs := spanData.Attributes expectedAttr := map[core.Key]string{ rpcServiceKey: "serviceName", netPeerIPKey: "fake", @@ -283,7 +293,7 @@ func TestStreamClientInterceptor(t *testing.T) { } } - events := spanData[0].MessageEvents + events := spanData.MessageEvents if len(events) != 20 { t.Fatalf("incorrect number of events expected 20 got %d", len(events)) } From f4a25cf745a6d9fc24f5e9fdd8f47c80b03c4464 Mon Sep 17 00:00:00 2001 From: Ahmed Mujtaba Date: Fri, 15 May 2020 22:43:07 +0200 Subject: [PATCH 5/6] Added condition for missing attr received in attr expected --- plugin/grpctrace/interceptor_test.go | 89 +++++++++++++++++++++++----- 1 file changed, 74 insertions(+), 15 deletions(-) diff --git a/plugin/grpctrace/interceptor_test.go b/plugin/grpctrace/interceptor_test.go index 3667b9dbe..243bf776a 100644 --- a/plugin/grpctrace/interceptor_test.go +++ b/plugin/grpctrace/interceptor_test.go @@ -19,6 +19,7 @@ import ( "testing" "time" + "github.com/golang/protobuf/proto" "google.golang.org/grpc" "google.golang.org/grpc/metadata" @@ -93,42 +94,96 @@ func TestUnaryClientInterceptor(t *testing.T) { }, eventsAttr: []map[core.Key]core.Value{ { - messageTypeKey: core.String("SENT"), - messageIDKey: core.Int(1), + messageTypeKey: core.String("SENT"), + messageIDKey: core.Int(1), + messageUncompressedSizeKey: core.Int(proto.Size(proto.Message(req))), }, { - messageTypeKey: core.String("RECEIVED"), - messageIDKey: core.Int(1), + messageTypeKey: core.String("RECEIVED"), + messageIDKey: core.Int(1), + messageUncompressedSizeKey: core.Int(proto.Size(proto.Message(reply))), }, }, }, { name: "/serviceName/bar", expectedAttr: map[core.Key]core.Value{ - rpcServiceKey: core.String("serviceName"), + rpcServiceKey: core.String("serviceName"), + netPeerIPKey: core.String("fake"), + netPeerPortKey: core.String("connection"), }, eventsAttr: []map[core.Key]core.Value{ { - messageTypeKey: core.String("SENT"), - messageIDKey: core.Int(1), + messageTypeKey: core.String("SENT"), + messageIDKey: core.Int(1), + messageUncompressedSizeKey: core.Int(proto.Size(proto.Message(req))), }, { - messageTypeKey: core.String("RECEIVED"), - messageIDKey: core.Int(1), + messageTypeKey: core.String("RECEIVED"), + messageIDKey: core.Int(1), + messageUncompressedSizeKey: core.Int(proto.Size(proto.Message(reply))), }, }, }, { - name: "serviceName/bar", - expectedAttr: map[core.Key]core.Value{rpcServiceKey: core.String("serviceName")}, + name: "serviceName/bar", + expectedAttr: map[core.Key]core.Value{ + rpcServiceKey: core.String("serviceName"), + netPeerIPKey: core.String("fake"), + netPeerPortKey: core.String("connection"), + }, + eventsAttr: []map[core.Key]core.Value{ + { + messageTypeKey: core.String("SENT"), + messageIDKey: core.Int(1), + messageUncompressedSizeKey: core.Int(proto.Size(proto.Message(req))), + }, + { + messageTypeKey: core.String("RECEIVED"), + messageIDKey: core.Int(1), + messageUncompressedSizeKey: core.Int(proto.Size(proto.Message(reply))), + }, + }, }, { - name: "invalidName", - expectedAttr: map[core.Key]core.Value{rpcServiceKey: core.String("")}, + name: "invalidName", + expectedAttr: map[core.Key]core.Value{ + rpcServiceKey: core.String(""), + netPeerIPKey: core.String("fake"), + netPeerPortKey: core.String("connection"), + }, + eventsAttr: []map[core.Key]core.Value{ + { + messageTypeKey: core.String("SENT"), + messageIDKey: core.Int(1), + messageUncompressedSizeKey: core.Int(proto.Size(proto.Message(req))), + }, + { + messageTypeKey: core.String("RECEIVED"), + messageIDKey: core.Int(1), + messageUncompressedSizeKey: core.Int(proto.Size(proto.Message(reply))), + }, + }, }, { - name: "/github.com.foo.serviceName_123/method", - expectedAttr: map[core.Key]core.Value{rpcServiceKey: core.String("serviceName_123")}, + name: "/github.com.foo.serviceName_123/method", + expectedAttr: map[core.Key]core.Value{ + rpcServiceKey: core.String("serviceName_123"), + netPeerIPKey: core.String("fake"), + netPeerPortKey: core.String("connection"), + }, + eventsAttr: []map[core.Key]core.Value{ + { + messageTypeKey: core.String("SENT"), + messageIDKey: core.Int(1), + messageUncompressedSizeKey: core.Int(proto.Size(proto.Message(req))), + }, + { + messageTypeKey: core.String("RECEIVED"), + messageIDKey: core.Int(1), + messageUncompressedSizeKey: core.Int(proto.Size(proto.Message(reply))), + }, + }, }, } @@ -158,6 +213,8 @@ func TestUnaryClientInterceptor(t *testing.T) { expectedAttr.AsString(), attr.Value.AsString()) } delete(check.expectedAttr, attr.Key) + } else { + t.Errorf("attribute %s not found in expected attributes map", string(attr.Key)) } } @@ -182,6 +239,8 @@ func TestUnaryClientInterceptor(t *testing.T) { string(attr.Key), attr.Value.AsString(), expectedAttr.AsString()) } delete(check.eventsAttr[event], attr.Key) + } else { + t.Errorf("attribute in event %s not found in expected attributes map", string(attr.Key)) } } if len(check.eventsAttr[event]) > 0 { From 55d4f7c31f38758446f5c1171dfa272c549a33d8 Mon Sep 17 00:00:00 2001 From: Tyler Yahn Date: Fri, 15 May 2020 16:27:40 -0700 Subject: [PATCH 6/6] Upgrade to v0.5.0 --- plugin/grpctrace/interceptor_test.go | 123 ++++++++++++++------------- 1 file changed, 62 insertions(+), 61 deletions(-) diff --git a/plugin/grpctrace/interceptor_test.go b/plugin/grpctrace/interceptor_test.go index 243bf776a..a92c7177f 100644 --- a/plugin/grpctrace/interceptor_test.go +++ b/plugin/grpctrace/interceptor_test.go @@ -23,7 +23,8 @@ import ( "google.golang.org/grpc" "google.golang.org/grpc/metadata" - "go.opentelemetry.io/otel/api/core" + "go.opentelemetry.io/otel/api/kv" + "go.opentelemetry.io/otel/api/kv/value" export "go.opentelemetry.io/otel/sdk/export/trace" sdktrace "go.opentelemetry.io/otel/sdk/trace" ) @@ -82,106 +83,106 @@ func TestUnaryClientInterceptor(t *testing.T) { checks := []struct { name string - expectedAttr map[core.Key]core.Value - eventsAttr []map[core.Key]core.Value + expectedAttr map[kv.Key]value.Value + eventsAttr []map[kv.Key]value.Value }{ { name: "/github.com.serviceName/bar", - expectedAttr: map[core.Key]core.Value{ - rpcServiceKey: core.String("serviceName"), - netPeerIPKey: core.String("fake"), - netPeerPortKey: core.String("connection"), + expectedAttr: map[kv.Key]value.Value{ + rpcServiceKey: value.String("serviceName"), + netPeerIPKey: value.String("fake"), + netPeerPortKey: value.String("connection"), }, - eventsAttr: []map[core.Key]core.Value{ + eventsAttr: []map[kv.Key]value.Value{ { - messageTypeKey: core.String("SENT"), - messageIDKey: core.Int(1), - messageUncompressedSizeKey: core.Int(proto.Size(proto.Message(req))), + messageTypeKey: value.String("SENT"), + messageIDKey: value.Int(1), + messageUncompressedSizeKey: value.Int(proto.Size(proto.Message(req))), }, { - messageTypeKey: core.String("RECEIVED"), - messageIDKey: core.Int(1), - messageUncompressedSizeKey: core.Int(proto.Size(proto.Message(reply))), + messageTypeKey: value.String("RECEIVED"), + messageIDKey: value.Int(1), + messageUncompressedSizeKey: value.Int(proto.Size(proto.Message(reply))), }, }, }, { name: "/serviceName/bar", - expectedAttr: map[core.Key]core.Value{ - rpcServiceKey: core.String("serviceName"), - netPeerIPKey: core.String("fake"), - netPeerPortKey: core.String("connection"), + expectedAttr: map[kv.Key]value.Value{ + rpcServiceKey: value.String("serviceName"), + netPeerIPKey: value.String("fake"), + netPeerPortKey: value.String("connection"), }, - eventsAttr: []map[core.Key]core.Value{ + eventsAttr: []map[kv.Key]value.Value{ { - messageTypeKey: core.String("SENT"), - messageIDKey: core.Int(1), - messageUncompressedSizeKey: core.Int(proto.Size(proto.Message(req))), + messageTypeKey: value.String("SENT"), + messageIDKey: value.Int(1), + messageUncompressedSizeKey: value.Int(proto.Size(proto.Message(req))), }, { - messageTypeKey: core.String("RECEIVED"), - messageIDKey: core.Int(1), - messageUncompressedSizeKey: core.Int(proto.Size(proto.Message(reply))), + messageTypeKey: value.String("RECEIVED"), + messageIDKey: value.Int(1), + messageUncompressedSizeKey: value.Int(proto.Size(proto.Message(reply))), }, }, }, { name: "serviceName/bar", - expectedAttr: map[core.Key]core.Value{ - rpcServiceKey: core.String("serviceName"), - netPeerIPKey: core.String("fake"), - netPeerPortKey: core.String("connection"), + expectedAttr: map[kv.Key]value.Value{ + rpcServiceKey: value.String("serviceName"), + netPeerIPKey: value.String("fake"), + netPeerPortKey: value.String("connection"), }, - eventsAttr: []map[core.Key]core.Value{ + eventsAttr: []map[kv.Key]value.Value{ { - messageTypeKey: core.String("SENT"), - messageIDKey: core.Int(1), - messageUncompressedSizeKey: core.Int(proto.Size(proto.Message(req))), + messageTypeKey: value.String("SENT"), + messageIDKey: value.Int(1), + messageUncompressedSizeKey: value.Int(proto.Size(proto.Message(req))), }, { - messageTypeKey: core.String("RECEIVED"), - messageIDKey: core.Int(1), - messageUncompressedSizeKey: core.Int(proto.Size(proto.Message(reply))), + messageTypeKey: value.String("RECEIVED"), + messageIDKey: value.Int(1), + messageUncompressedSizeKey: value.Int(proto.Size(proto.Message(reply))), }, }, }, { name: "invalidName", - expectedAttr: map[core.Key]core.Value{ - rpcServiceKey: core.String(""), - netPeerIPKey: core.String("fake"), - netPeerPortKey: core.String("connection"), + expectedAttr: map[kv.Key]value.Value{ + rpcServiceKey: value.String(""), + netPeerIPKey: value.String("fake"), + netPeerPortKey: value.String("connection"), }, - eventsAttr: []map[core.Key]core.Value{ + eventsAttr: []map[kv.Key]value.Value{ { - messageTypeKey: core.String("SENT"), - messageIDKey: core.Int(1), - messageUncompressedSizeKey: core.Int(proto.Size(proto.Message(req))), + messageTypeKey: value.String("SENT"), + messageIDKey: value.Int(1), + messageUncompressedSizeKey: value.Int(proto.Size(proto.Message(req))), }, { - messageTypeKey: core.String("RECEIVED"), - messageIDKey: core.Int(1), - messageUncompressedSizeKey: core.Int(proto.Size(proto.Message(reply))), + messageTypeKey: value.String("RECEIVED"), + messageIDKey: value.Int(1), + messageUncompressedSizeKey: value.Int(proto.Size(proto.Message(reply))), }, }, }, { name: "/github.com.foo.serviceName_123/method", - expectedAttr: map[core.Key]core.Value{ - rpcServiceKey: core.String("serviceName_123"), - netPeerIPKey: core.String("fake"), - netPeerPortKey: core.String("connection"), + expectedAttr: map[kv.Key]value.Value{ + rpcServiceKey: value.String("serviceName_123"), + netPeerIPKey: value.String("fake"), + netPeerPortKey: value.String("connection"), }, - eventsAttr: []map[core.Key]core.Value{ + eventsAttr: []map[kv.Key]value.Value{ { - messageTypeKey: core.String("SENT"), - messageIDKey: core.Int(1), - messageUncompressedSizeKey: core.Int(proto.Size(proto.Message(req))), + messageTypeKey: value.String("SENT"), + messageIDKey: value.Int(1), + messageUncompressedSizeKey: value.Int(proto.Size(proto.Message(req))), }, { - messageTypeKey: core.String("RECEIVED"), - messageIDKey: core.Int(1), - messageUncompressedSizeKey: core.Int(proto.Size(proto.Message(reply))), + messageTypeKey: value.String("RECEIVED"), + messageIDKey: value.Int(1), + messageUncompressedSizeKey: value.Int(proto.Size(proto.Message(reply))), }, }, }, @@ -336,7 +337,7 @@ func TestStreamClientInterceptor(t *testing.T) { } attrs := spanData.Attributes - expectedAttr := map[core.Key]string{ + expectedAttr := map[kv.Key]string{ rpcServiceKey: "serviceName", netPeerIPKey: "fake", netPeerPortKey: "connection", @@ -358,12 +359,12 @@ func TestStreamClientInterceptor(t *testing.T) { } for i := 0; i < 20; i += 2 { msgID := i/2 + 1 - validate := func(eventName string, attrs []core.KeyValue) { + validate := func(eventName string, attrs []kv.KeyValue) { for _, attr := range attrs { if attr.Key == messageTypeKey && attr.Value.AsString() != eventName { t.Errorf("invalid event on index: %d expecting %s event, receive %s event", i, eventName, attr.Value.AsString()) } - if attr.Key == messageIDKey && attr.Value != core.Int(msgID) { + if attr.Key == messageIDKey && attr.Value != value.Int(msgID) { t.Errorf("invalid id for message event expected %d received %d", msgID, attr.Value.AsInt32()) } }