-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Changes from 9 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
c3e9bdb
fix interceptor regexp
stefanprisca 1176d9c
Merge branch 'master' into master
MrAlias 6ade533
fix interceptor regexp
stefanprisca 1bd88dc
Add tests for interceptor service attributes
stefanprisca 54f5c35
rename to make the tests more relevant
stefanprisca 7c6554f
Merge branch 'master' of github.com:stefanprisca/opentelemetry-go int…
stefanprisca c8578e1
address feedback
stefanprisca 4ad6e3b
refactor tests to have single test method
stefanprisca 75b4d68
use golang table test setup
stefanprisca 6904c9e
Update plugin/grpctrace/interceptor_test.go
stefanprisca 13c5db4
Update plugin/grpctrace/interceptor_test.go
stefanprisca 3e4da5a
Update plugin/grpctrace/interceptor_test.go
stefanprisca e1c90dc
add explicit error handling for span data
stefanprisca de6c80d
Merge suggested changes
stefanprisca 8be299b
Merge upstream changes from otel
stefanprisca 5414d1a
forgot to precomit...
stefanprisca eccf131
Merge branch 'master' into master
lizthegrey 0858728
Merge branch 'master' into master
MrAlias File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
// 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) { | ||
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.conef.uk.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.conef.uk.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) | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.