Skip to content
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

fix interceptor regexp #683

Merged
merged 18 commits into from
May 7, 2020
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 4 additions & 5 deletions plugin/grpctrace/interceptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -426,16 +426,15 @@ func peerInfoFromContext(ctx context.Context) []core.KeyValue {
return peerInfoFromTarget(p.Addr.String())
}

var fullMethodRegexp = regexp.MustCompile(`^/\S*\.(\S*)/\S*$`)
var fullMethodRegexp = regexp.MustCompile(`^\/?(?:\S+\.)?(\S+)\/\S+$`)

func serviceFromFullMethod(method string) string {
match := fullMethodRegexp.FindAllStringSubmatch(method, 1)

if len(match) != 1 && len(match[1]) != 2 {
match := fullMethodRegexp.FindStringSubmatch(method)
if len(match) == 0 {
return ""
}

return match[0][1]
return match[1]
}

func addEventForMessageReceived(ctx context.Context, id int, m interface{}) {
Expand Down
136 changes: 136 additions & 0 deletions plugin/grpctrace/interceptor_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
// Copyright The OpenTelemetry Authors
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was just playing around a bit to see how to test this part. Should still wait on #684 and then merge these tests with those.

//
// 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) {
t.spanMap[s.Name] = append(t.spanMap[s.Name], s)
}

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() {}

type nameAttributeTestCase struct {
testName string
expectedName string
fullNameFmt string
}

func (tc nameAttributeTestCase) fullName() string {
return fmt.Sprintf(tc.fullNameFmt, tc.expectedName)
}

func TestUCISetsExpectedServiceNameAttribute(t *testing.T) {
testCases := []nameAttributeTestCase{
{
"FullyQualifiedMethodName",
"serviceName",
"/github.com.foo.%s/bar",
},
{
"SimpleMethodName",
"serviceName",
"/%s/bar",
},
{
"MethodNameWithoutFullPath",
"serviceName",
"%s/bar",
},
{
"InvalidMethodName",
"",
"invalidName",
},
{
"NonAlhanumericMethodName",
stefanprisca marked this conversation as resolved.
Show resolved Hide resolved
"serviceName_123",
"/github.com.foo.%s/method",
},
}

for _, tc := range testCases {
t.Run(tc.testName, tc.testUCISetsExpectedNameAttribute)
}
}

func (tc nameAttributeTestCase) testUCISetsExpectedNameAttribute(t *testing.T) {
exp := &testExporter{make(map[string][]*export.SpanData)}
tp, _ := sdktrace.NewProvider(sdktrace.WithSyncer(exp),
sdktrace.WithConfig(sdktrace.Config{DefaultSampler: sdktrace.AlwaysSample()}))
global.SetTraceProvider(tp)
stefanprisca marked this conversation as resolved.
Show resolved Hide resolved

tr := tp.Tracer("grpctrace/client")
ctx, span := tr.Start(context.Background(), tc.testName)
defer span.End()

clientConn, err := grpc.Dial("fake:connection", grpc.WithInsecure())

if err != nil {
t.Fatalf("failed to create client connection: %v", err)
}

unaryInt := UnaryClientInterceptor(tr)

req := &mockProtoMessage{}
reply := &mockProtoMessage{}
ccInvoker := &mockCCInvoker{}

err = unaryInt(ctx, tc.fullName(), req, reply, clientConn, ccInvoker.invoke)
if err != nil {
t.Fatalf("failed to run unary interceptor: %v", err)
}

attributes := exp.spanMap[tc.fullName()][0].Attributes
stefanprisca marked this conversation as resolved.
Show resolved Hide resolved

var actualServiceName string
for _, attr := range attributes {
if attr.Key == rpcServiceKey {
actualServiceName = attr.Value.AsString()
stefanprisca marked this conversation as resolved.
Show resolved Hide resolved
}
}

if tc.expectedName != actualServiceName {
t.Fatalf("invalid service name found. expected %s, actual %s",
tc.expectedName, actualServiceName)
}
}