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"
|
|
|
|
|
|
|
|
"go.opentelemetry.io/otel/api/global"
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
type mockCCInvoker 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
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type mockProtoMessage struct {
|
|
|
|
}
|
|
|
|
|
|
|
|
func (mm *mockProtoMessage) Reset() {}
|
|
|
|
func (mm *mockProtoMessage) String() string { return "mock" }
|
|
|
|
func (mm *mockProtoMessage) ProtoMessage() {}
|
|
|
|
|
2020-05-04 10:02:07 +02:00
|
|
|
type nameAttributeTestCase struct {
|
|
|
|
testName string
|
|
|
|
expectedName string
|
|
|
|
fullNameFmt string
|
2020-05-01 13:43:38 +02:00
|
|
|
}
|
|
|
|
|
2020-05-04 10:02:07 +02:00
|
|
|
func (tc nameAttributeTestCase) fullName() string {
|
|
|
|
return fmt.Sprintf(tc.fullNameFmt, tc.expectedName)
|
2020-05-01 13:43:38 +02:00
|
|
|
}
|
|
|
|
|
2020-05-04 10:02:07 +02:00
|
|
|
func TestUCISetsExpectedServiceNameAttribute(t *testing.T) {
|
|
|
|
testCases := []nameAttributeTestCase{
|
|
|
|
{
|
2020-05-04 10:21:57 +02:00
|
|
|
"FullyQualifiedMethodName",
|
2020-05-04 10:02:07 +02:00
|
|
|
"serviceName",
|
|
|
|
"/github.com.foo.%s/bar",
|
|
|
|
},
|
|
|
|
{
|
2020-05-04 10:21:57 +02:00
|
|
|
"SimpleMethodName",
|
2020-05-04 10:02:07 +02:00
|
|
|
"serviceName",
|
|
|
|
"/%s/bar",
|
|
|
|
},
|
|
|
|
{
|
2020-05-04 10:21:57 +02:00
|
|
|
"MethodNameWithoutFullPath",
|
2020-05-04 10:02:07 +02:00
|
|
|
"serviceName",
|
|
|
|
"%s/bar",
|
|
|
|
},
|
|
|
|
{
|
2020-05-04 10:21:57 +02:00
|
|
|
"InvalidMethodName",
|
2020-05-04 10:02:07 +02:00
|
|
|
"",
|
|
|
|
"invalidName",
|
|
|
|
},
|
|
|
|
{
|
2020-05-04 10:21:57 +02:00
|
|
|
"NonAlhanumericMethodName",
|
2020-05-04 10:02:07 +02:00
|
|
|
"serviceName_123",
|
|
|
|
"/github.com.foo.%s/method",
|
|
|
|
},
|
|
|
|
}
|
2020-05-01 13:43:38 +02:00
|
|
|
|
2020-05-04 10:02:07 +02:00
|
|
|
for _, tc := range testCases {
|
2020-05-04 10:21:57 +02:00
|
|
|
t.Run(tc.testName, tc.testUCISetsExpectedNameAttribute)
|
2020-05-04 10:02:07 +02:00
|
|
|
}
|
2020-05-01 13:43:38 +02:00
|
|
|
}
|
|
|
|
|
2020-05-04 10:21:57 +02:00
|
|
|
func (tc nameAttributeTestCase) testUCISetsExpectedNameAttribute(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),
|
|
|
|
sdktrace.WithConfig(sdktrace.Config{DefaultSampler: sdktrace.AlwaysSample()}))
|
2020-05-01 13:43:38 +02:00
|
|
|
global.SetTraceProvider(tp)
|
|
|
|
|
|
|
|
tr := tp.Tracer("grpctrace/client")
|
2020-05-04 10:02:07 +02:00
|
|
|
ctx, span := tr.Start(context.Background(), tc.testName)
|
2020-05-01 13:43:38 +02:00
|
|
|
defer span.End()
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
unaryInt := UnaryClientInterceptor(tr)
|
|
|
|
|
|
|
|
req := &mockProtoMessage{}
|
|
|
|
reply := &mockProtoMessage{}
|
|
|
|
ccInvoker := &mockCCInvoker{}
|
|
|
|
|
2020-05-04 10:02:07 +02:00
|
|
|
err = unaryInt(ctx, tc.fullName(), req, reply, clientConn, ccInvoker.invoke)
|
2020-05-01 13:43:38 +02:00
|
|
|
if err != nil {
|
2020-05-04 10:21:57 +02:00
|
|
|
t.Fatalf("failed to run unary interceptor: %v", err)
|
2020-05-01 13:43:38 +02:00
|
|
|
}
|
|
|
|
|
2020-05-04 10:02:07 +02:00
|
|
|
attributes := exp.spanMap[tc.fullName()][0].Attributes
|
2020-05-01 13:43:38 +02:00
|
|
|
|
|
|
|
var actualServiceName string
|
|
|
|
for _, attr := range attributes {
|
|
|
|
if attr.Key == rpcServiceKey {
|
|
|
|
actualServiceName = attr.Value.AsString()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-04 10:02:07 +02:00
|
|
|
if tc.expectedName != actualServiceName {
|
2020-05-04 10:21:57 +02:00
|
|
|
t.Fatalf("invalid service name found. expected %s, actual %s",
|
|
|
|
tc.expectedName, actualServiceName)
|
2020-05-01 13:43:38 +02:00
|
|
|
}
|
|
|
|
}
|