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

Report context cancelation as non-error #1

Open
wants to merge 3 commits into
base: fix-cancellation-race
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
8 changes: 8 additions & 0 deletions errors.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package otgrpc

import (
"context"
"errors"

"github.com/opentracing/opentracing-go"
"github.com/opentracing/opentracing-go/ext"
"google.golang.org/grpc/codes"
Expand Down Expand Up @@ -57,6 +60,11 @@ func SetSpanTags(span opentracing.Span, err error, client bool) {
code := codes.Unknown
if s, ok := status.FromError(err); ok {
code = s.Code()
} else if errors.Is(err, context.Canceled) {
Copy link
Owner

Choose a reason for hiding this comment

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

We also need to check for code == codes.Canceled here - the gRPC client libraries translate client-side context cancellations into gRPC errors with code Canceled using either FromContextError or toRPCErr depending on when the error is observed by the client.

TestStreamingContextCancellationOpenTracing doesn't cover this case because it doesn't test cancelling the context while a client is in a Recv() or Send() call - it cancels the context while the stream is open, but not while the client is in Recv() or Send().

Copy link
Author

Choose a reason for hiding this comment

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

Thanks; I have modified the code to check codes.Canceled

Copy link
Owner

Choose a reason for hiding this comment

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

Thanks. Could you please add a test to cover this case as well?

code = codes.Canceled
}
if code == codes.Canceled {
err = nil // Someone else canceled this operation - we should not flag it as an error.
}
span.SetTag("response_code", code)
span.SetTag("response_class", c)
Expand Down
7 changes: 5 additions & 2 deletions test/interceptor_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package interceptor_test

import (
"fmt"
"io"
"net"
"testing"
Expand All @@ -9,8 +10,9 @@ import (
"github.com/stretchr/testify/assert"

"context"

testpb "github.com/grpc-ecosystem/grpc-opentracing/go/otgrpc/test/otgrpc_testing"
"github.com/opentracing-contrib/go-grpc"
otgrpc "github.com/opentracing-contrib/go-grpc"
"github.com/opentracing/opentracing-go/mocktracer"
"google.golang.org/grpc"
)
Expand Down Expand Up @@ -268,5 +270,6 @@ func TestStreamingContextCancellationOpenTracing(t *testing.T) {
parent := spans[0]
child := spans[1]
assert.Equal(t, child.ParentID, parent.Context().(mocktracer.MockSpanContext).SpanID)
assert.True(t, parent.Tag("error").(bool))
assert.Equal(t, fmt.Sprint(parent.Tag("response_code")), "Canceled")
assert.Nil(t, parent.Tag("error"))
}