2020-05-01 13:43:38 +02:00
|
|
|
// Copyright The OpenTelemetry Authors
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
package grpctrace
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"google.golang.org/grpc"
|
|
|
|
|
2020-05-02 23:56:08 +02:00
|
|
|
"go.opentelemetry.io/otel/api/core"
|
2020-05-01 13:43:38 +02:00
|
|
|
export "go.opentelemetry.io/otel/sdk/export/trace"
|
|
|
|
sdktrace "go.opentelemetry.io/otel/sdk/trace"
|
|
|
|
)
|
|
|
|
|
|
|
|
type testExporter struct {
|
|
|
|
spanMap map[string][]*export.SpanData
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *testExporter) ExportSpan(ctx context.Context, s *export.SpanData) {
|
2020-05-02 14:50:11 +02:00
|
|
|
t.spanMap[s.Name] = append(t.spanMap[s.Name], s)
|
2020-05-01 13:43:38 +02:00
|
|
|
}
|
|
|
|
|
2020-05-02 23:56:08 +02:00
|
|
|
type mockUICInvoker struct {
|
2020-05-01 13:43:38 +02:00
|
|
|
ctx context.Context
|
|
|
|
}
|
|
|
|
|
2020-05-02 23:56:08 +02:00
|
|
|
func (mcuici *mockUICInvoker) invoker(ctx context.Context, method string, req, reply interface{}, cc *grpc.ClientConn, opts ...grpc.CallOption) error {
|
|
|
|
mcuici.ctx = ctx
|
2020-05-01 13:43:38 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-05-02 23:56:08 +02:00
|
|
|
type mockProtoMessage struct{}
|
2020-05-01 13:43:38 +02:00
|
|
|
|
2020-05-02 23:56:08 +02:00
|
|
|
func (mm *mockProtoMessage) Reset() {
|
2020-05-01 13:43:38 +02:00
|
|
|
}
|
|
|
|
|
2020-05-02 23:56:08 +02:00
|
|
|
func (mm *mockProtoMessage) String() string {
|
|
|
|
return "mock"
|
2020-05-01 13:43:38 +02:00
|
|
|
}
|
|
|
|
|
2020-05-02 23:56:08 +02:00
|
|
|
func (mm *mockProtoMessage) ProtoMessage() {
|
2020-05-01 13:43:38 +02:00
|
|
|
}
|
|
|
|
|
2020-05-02 23:56:08 +02:00
|
|
|
func TestUnaryClientInterceptor(t *testing.T) {
|
2020-05-01 13:43:38 +02:00
|
|
|
exp := &testExporter{make(map[string][]*export.SpanData)}
|
2020-05-04 10:02:07 +02:00
|
|
|
tp, _ := sdktrace.NewProvider(sdktrace.WithSyncer(exp),
|
2020-05-02 23:56:08 +02:00
|
|
|
sdktrace.WithConfig(sdktrace.Config{
|
|
|
|
DefaultSampler: sdktrace.AlwaysSample(),
|
|
|
|
},
|
|
|
|
))
|
2020-05-01 13:43:38 +02:00
|
|
|
|
|
|
|
clientConn, err := grpc.Dial("fake:connection", grpc.WithInsecure())
|
|
|
|
if err != nil {
|
2020-05-04 10:21:57 +02:00
|
|
|
t.Fatalf("failed to create client connection: %v", err)
|
2020-05-01 13:43:38 +02:00
|
|
|
}
|
|
|
|
|
2020-05-02 23:56:08 +02:00
|
|
|
tracer := tp.Tracer("grpctrace/client")
|
|
|
|
unaryInterceptor := UnaryClientInterceptor(tracer)
|
2020-05-01 13:43:38 +02:00
|
|
|
|
|
|
|
req := &mockProtoMessage{}
|
|
|
|
reply := &mockProtoMessage{}
|
2020-05-02 23:56:08 +02:00
|
|
|
uniInterceptorInvoker := &mockUICInvoker{}
|
2020-05-01 13:43:38 +02:00
|
|
|
|
2020-05-02 23:56:08 +02:00
|
|
|
checks := []struct {
|
|
|
|
name string
|
|
|
|
expectedAttr map[core.Key]core.Value
|
2020-05-05 22:50:01 +02:00
|
|
|
eventsAttr []map[core.Key]core.Value
|
2020-05-02 23:56:08 +02:00
|
|
|
}{
|
|
|
|
{
|
2020-05-05 22:50:01 +02:00
|
|
|
name: "/github.com.serviceName/bar",
|
2020-05-02 23:56:08 +02:00
|
|
|
expectedAttr: map[core.Key]core.Value{
|
|
|
|
rpcServiceKey: core.String("serviceName"),
|
|
|
|
netPeerIPKey: core.String("fake"),
|
|
|
|
netPeerPortKey: core.String("connection"),
|
|
|
|
},
|
2020-05-05 22:50:01 +02:00
|
|
|
eventsAttr: []map[core.Key]core.Value{
|
2020-05-02 23:56:08 +02:00
|
|
|
{
|
2020-05-05 22:50:01 +02:00
|
|
|
messageTypeKey: core.String("SENT"),
|
|
|
|
messageIDKey: core.Int(1),
|
2020-05-02 23:56:08 +02:00
|
|
|
},
|
|
|
|
{
|
2020-05-05 22:50:01 +02:00
|
|
|
messageTypeKey: core.String("RECEIVED"),
|
|
|
|
messageIDKey: core.Int(1),
|
2020-05-02 23:56:08 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2020-05-05 22:50:01 +02:00
|
|
|
{
|
|
|
|
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")},
|
|
|
|
},
|
2020-05-05 09:07:11 +02:00
|
|
|
}
|
|
|
|
|
2020-05-05 22:50:01 +02:00
|
|
|
for idx, check := range checks {
|
|
|
|
fmt.Println("================", idx, "==================")
|
2020-05-02 23:56:08 +02:00
|
|
|
err = unaryInterceptor(context.Background(), check.name, req, reply, clientConn, uniInterceptorInvoker.invoker)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to run unary interceptor: %v", err)
|
|
|
|
}
|
2020-05-01 13:43:38 +02:00
|
|
|
|
2020-05-05 22:50:01 +02:00
|
|
|
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
|
2020-05-02 23:56:08 +02:00
|
|
|
for _, attr := range attrs {
|
|
|
|
expectedAttr, ok := check.expectedAttr[attr.Key]
|
|
|
|
if ok {
|
|
|
|
if expectedAttr != attr.Value {
|
2020-05-05 22:50:01 +02:00
|
|
|
t.Errorf("name: %s invalid %s found. expected %s, actual %s", check.name, string(attr.Key),
|
2020-05-02 23:56:08 +02:00
|
|
|
expectedAttr.AsString(), attr.Value.AsString())
|
|
|
|
}
|
2020-05-05 22:50:01 +02:00
|
|
|
delete(check.expectedAttr, attr.Key)
|
2020-05-02 23:56:08 +02:00
|
|
|
}
|
2020-05-01 13:43:38 +02:00
|
|
|
}
|
|
|
|
|
2020-05-05 22:50:01 +02:00
|
|
|
// 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
|
2020-05-02 23:56:08 +02:00
|
|
|
for event := 0; event < len(check.eventsAttr); event++ {
|
2020-05-05 22:50:01 +02:00
|
|
|
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))
|
2020-05-02 23:56:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-05-01 13:43:38 +02:00
|
|
|
}
|
|
|
|
}
|