-
Notifications
You must be signed in to change notification settings - Fork 443
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
contrib/google.golang.org/grpc: add WithErrorCheck
option
#2035
Open
BIwashi
wants to merge
19
commits into
DataDog:main
Choose a base branch
from
BIwashi:add-rpc-non-error-func-option
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 10 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
08a7e75
Add non error func option for grpc
BIwashi 252e275
Add FullMethod string for `nonErrorFunc`
BIwashi 06ce818
Fix condition
BIwashi 6a8cac1
fix conditon
BIwashi 057eaab
Fix comment
BIwashi c6b7f79
Merge branch 'main' into add-rpc-non-error-func-option
katiehockman fb4336f
Fix deprecated InterceptorOption
BIwashi 50c90b2
Add unit tests
BIwashi 0fd73e7
Merge branch 'main' into add-rpc-non-error-func-option
BIwashi b925a3a
Merge branch 'main' into add-rpc-non-error-func-option
dianashevchenko 100083f
Renam function and config field name
BIwashi 6a3227e
Refactor error check in grpc_test.go
BIwashi 5b83467
Refactor stats_client and stats_server to use a
BIwashi 4fca224
Move mocktrace start for stream test
BIwashi cff830c
Fix error handling in TestWithError
BIwashi ca200c3
Merge branch 'main' into add-rpc-non-error-func-option
BIwashi 7f0c9b5
Merge branch 'main' into add-rpc-non-error-func-option
BIwashi fab8dab
Merge branch 'main' into add-rpc-non-error-func-option
rarguelloF 94a4961
Merge branch 'main' into add-rpc-non-error-func-option
darccio 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
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 |
---|---|---|
|
@@ -663,6 +663,221 @@ func waitForSpans(mt mocktracer.Tracer, sz int) { | |
} | ||
} | ||
|
||
func TestNonErrorFunc(t *testing.T) { | ||
t.Run("unary", func(t *testing.T) { | ||
for name, tt := range map[string]struct { | ||
nonErrorFunc func(method string, err error) bool | ||
message string | ||
withError bool | ||
wantCode string | ||
wantMessage string | ||
}{ | ||
"Invalid_with_no_error": { | ||
message: "invalid", | ||
nonErrorFunc: func(method string, err error) bool { | ||
if err == nil { | ||
return true | ||
} | ||
|
||
errCode := status.Code(err) | ||
if errCode == codes.InvalidArgument && method == "/grpc.Fixture/Ping" { | ||
return true | ||
} | ||
|
||
return false | ||
}, | ||
withError: false, | ||
wantCode: codes.InvalidArgument.String(), | ||
wantMessage: "invalid", | ||
}, | ||
"Invalid_with_error": { | ||
message: "invalid", | ||
nonErrorFunc: func(method string, err error) bool { | ||
if err == nil { | ||
return true | ||
} | ||
|
||
errCode := status.Code(err) | ||
if errCode == codes.InvalidArgument && method == "/some/endpoint" { | ||
return true | ||
} | ||
|
||
return false | ||
}, | ||
withError: true, | ||
wantCode: codes.InvalidArgument.String(), | ||
wantMessage: "invalid", | ||
}, | ||
"Invalid_with_error_without_nonErrorFunc": { | ||
message: "invalid", | ||
nonErrorFunc: nil, | ||
withError: true, | ||
wantCode: codes.InvalidArgument.String(), | ||
wantMessage: "invalid", | ||
}, | ||
} { | ||
t.Run(name, func(t *testing.T) { | ||
mt := mocktracer.Start() | ||
defer mt.Stop() | ||
|
||
var ( | ||
rig *rig | ||
err error | ||
) | ||
if tt.nonErrorFunc == nil { | ||
rig, err = newRig(true) | ||
} else { | ||
rig, err = newRig(true, NonErrorFunc(tt.nonErrorFunc)) | ||
} | ||
if err != nil { | ||
t.Fatalf("error setting up rig: %s", err) | ||
} | ||
|
||
client := rig.client | ||
_, err = client.Ping(context.Background(), &FixtureRequest{Name: tt.message}) | ||
assert.Error(t, err) | ||
assert.Equal(t, tt.wantCode, status.Code(err).String()) | ||
assert.Equal(t, tt.wantMessage, status.Convert(err).Message()) | ||
|
||
spans := mt.FinishedSpans() | ||
assert.Len(t, spans, 2) | ||
|
||
var serverSpan, clientSpan mocktracer.Span | ||
|
||
for _, s := range spans { | ||
// order of traces in buffer is not garanteed | ||
switch s.OperationName() { | ||
case "grpc.server": | ||
serverSpan = s | ||
case "grpc.client": | ||
clientSpan = s | ||
} | ||
} | ||
|
||
if tt.withError { | ||
assert.NotNil(t, clientSpan.Tag(ext.Error)) | ||
assert.NotNil(t, serverSpan.Tag(ext.Error)) | ||
} else { | ||
assert.Nil(t, clientSpan.Tag(ext.Error)) | ||
assert.Nil(t, serverSpan.Tag(ext.Error)) | ||
} | ||
|
||
rig.Close() | ||
mt.Reset() | ||
}) | ||
} | ||
}) | ||
|
||
t.Run("stream", func(t *testing.T) { | ||
mt := mocktracer.Start() | ||
defer mt.Stop() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please move the start of the mocktracer this into the test case run There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this the correct revision of your point? |
||
for name, tt := range map[string]struct { | ||
nonErrorFunc func(method string, err error) bool | ||
message string | ||
withError bool | ||
wantCode string | ||
wantMessage string | ||
}{ | ||
"Invalid_with_no_error": { | ||
message: "invalid", | ||
nonErrorFunc: func(method string, err error) bool { | ||
if err == nil { | ||
return true | ||
} | ||
|
||
errCode := status.Code(err) | ||
if errCode == codes.InvalidArgument && method == "/grpc.Fixture/StreamPing" { | ||
return true | ||
} | ||
|
||
return false | ||
}, | ||
withError: false, | ||
wantCode: codes.InvalidArgument.String(), | ||
wantMessage: "invalid", | ||
}, | ||
"Invalid_with_error": { | ||
message: "invalid", | ||
nonErrorFunc: func(method string, err error) bool { | ||
if err == nil { | ||
return true | ||
} | ||
|
||
errCode := status.Code(err) | ||
if errCode == codes.InvalidArgument && method == "/some/endpoint" { | ||
return true | ||
} | ||
|
||
return false | ||
}, | ||
withError: true, | ||
wantCode: codes.InvalidArgument.String(), | ||
wantMessage: "invalid", | ||
}, | ||
"Invalid_with_error_without_nonErrorFunc": { | ||
message: "invalid", | ||
nonErrorFunc: nil, | ||
withError: true, | ||
wantCode: codes.InvalidArgument.String(), | ||
wantMessage: "invalid", | ||
}, | ||
} { | ||
t.Run(name, func(t *testing.T) { | ||
var ( | ||
rig *rig | ||
err error | ||
) | ||
if tt.nonErrorFunc == nil { | ||
rig, err = newRig(true) | ||
} else { | ||
rig, err = newRig(true, NonErrorFunc(tt.nonErrorFunc)) | ||
} | ||
darccio marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if err != nil { | ||
t.Fatalf("error setting up rig: %s", err) | ||
} | ||
|
||
ctx, done := context.WithCancel(context.Background()) | ||
client := rig.client | ||
stream, err := client.StreamPing(ctx) | ||
assert.NoError(t, err) | ||
|
||
err = stream.Send(&FixtureRequest{Name: tt.message}) | ||
assert.NoError(t, err) | ||
|
||
_, err = stream.Recv() | ||
assert.Error(t, err) | ||
assert.Equal(t, tt.wantCode, status.Code(err).String()) | ||
assert.Equal(t, tt.wantMessage, status.Convert(err).Message()) | ||
|
||
assert.NoError(t, stream.CloseSend()) | ||
done() // close stream from client side | ||
rig.Close() | ||
|
||
waitForSpans(mt, 5) | ||
|
||
spans := mt.FinishedSpans() | ||
assert.Len(t, spans, 5) | ||
|
||
noError := true | ||
for _, s := range spans { | ||
if s.Tag(ext.Error) != nil { | ||
noError = false | ||
break | ||
} | ||
} | ||
|
||
if tt.withError { | ||
assert.False(t, noError) | ||
} else { | ||
assert.True(t, noError) | ||
} | ||
darccio marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
mt.Reset() | ||
}) | ||
} | ||
}) | ||
} | ||
|
||
func TestAnalyticsSettings(t *testing.T) { | ||
assertRate := func(t *testing.T, mt mocktracer.Tracer, rate interface{}, opts ...InterceptorOption) { | ||
rig, err := newRig(true, opts...) | ||
|
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
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
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.