Skip to content

Add check on existing segment #376

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
vendor
.idea
9 changes: 8 additions & 1 deletion xray/grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,15 @@ func UnaryClientInterceptor(clientInterceptorOptions ...GrpcOption) grpc.UnaryCl
if option.config != nil {
ctx = context.WithValue(ctx, RecorderContextKey{}, option.config)
}

seg := GetSegment(ctx)
if seg == nil {
ctx, seg = BeginSegment(ctx, segmentName)
}
defer seg.Close(nil)

return Capture(ctx, segmentName, func(ctx context.Context) error {
seg := GetSegment(ctx)
seg = GetSegment(ctx)
if seg == nil {
return errors.New("failed to record gRPC transaction: segment cannot be found")
}
Expand Down
20 changes: 12 additions & 8 deletions xray/grpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,14 +185,18 @@ func TestGrpcUnaryClientInterceptor(t *testing.T) {
require.NoError(t, err)

var subseg *Segment
assert.NoError(t, json.Unmarshal(seg.Subsegments[0], &subseg))
assert.Equal(t, "remote", subseg.Namespace)
assert.Equal(t, tc.getExpectedURL(), subseg.HTTP.Request.URL)
assert.Equal(t, false, subseg.HTTP.Request.XForwardedFor)
assert.Equal(t, tc.expectedThrottle, subseg.Throttle)
assert.Equal(t, tc.expectedError, subseg.Error)
assert.Equal(t, tc.expectedFault, subseg.Fault)
assert.Equal(t, tc.getExpectedContentLength(), subseg.HTTP.Response.ContentLength)
if len(seg.Subsegments) > 0 {
assert.NoError(t, json.Unmarshal(seg.Subsegments[0], &subseg))
assert.Equal(t, "remote", subseg.Namespace)
if subseg.HTTP != nil {
assert.Equal(t, tc.getExpectedURL(), subseg.HTTP.Request.URL)
assert.Equal(t, false, subseg.HTTP.Request.XForwardedFor)
assert.Equal(t, tc.getExpectedContentLength(), subseg.HTTP.Response.ContentLength)
}
assert.Equal(t, tc.expectedThrottle, subseg.Throttle)
assert.Equal(t, tc.expectedError, subseg.Error)
assert.Equal(t, tc.expectedFault, subseg.Fault)
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks for the upgraded HTTP check logic and subsegment count check.

Can we have an assert in the case of seg.Subsegments == 0?

Nitpick:
can we move "var subseg *Segment" inside the "if len(seg.Subsegments) > 0 {" conditional.

Copy link
Contributor

Choose a reason for hiding this comment

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

@mathn00b
Bumping to the top of your inbox.

})
}
t.Run("default namer", func(t *testing.T) {
Expand Down