Skip to content

Commit

Permalink
standardize error class and add span error tags
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniela Miao committed Apr 27, 2017
1 parent ef7d6c8 commit d78177f
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 2 deletions.
2 changes: 1 addition & 1 deletion go/otgrpc/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ func OpenTracingClientInterceptor(tracer opentracing.Tracer, optFuncs ...Option)
clientSpan.LogFields(log.Object("gRPC response", resp))
}
} else {
SetSpanTags(clientSpan, err, true)
clientSpan.LogFields(log.String("event", "gRPC error"), log.Error(err))
ext.Error.Set(clientSpan, true)
}
if otgrpcOpts.decorator != nil {
otgrpcOpts.decorator(clientSpan, method, req, resp, err)
Expand Down
66 changes: 66 additions & 0 deletions go/otgrpc/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package otgrpc

import (
"github.com/opentracing/opentracing-go"
"github.com/opentracing/opentracing-go/ext"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)

// A Class is a set of types of outcomes (including errors) that will often
// be handled in the same way.
type Class string

const (
Unknown Class = "0xx"
// Success represents outcomes that achieved the desired results.
Success Class = "2xx"
// ClientError represents errors that were the client's fault.
ClientError Class = "4xx"
// ServerError represents errors that were the server's fault.
ServerError Class = "5xx"
)

// ErrorClass returns the class of the given error
func ErrorClass(err error) Class {
if s, ok := status.FromError(err); ok {
switch s.Code() {
// Success or "success"
case codes.OK, codes.Canceled:
return Success

// Client errors
case codes.InvalidArgument, codes.NotFound, codes.AlreadyExists,
codes.PermissionDenied, codes.Unauthenticated, codes.FailedPrecondition,
codes.OutOfRange:
return ClientError

// Server errors
case codes.DeadlineExceeded, codes.ResourceExhausted, codes.Aborted,
codes.Unimplemented, codes.Internal, codes.Unavailable, codes.DataLoss:
return ServerError

// Not sure
case codes.Unknown:
fallthrough
default:
return Unknown
}
}
return Unknown
}

// SetSpanTags sets one or more tags on the given span according to the
// error.
func SetSpanTags(span opentracing.Span, err error, client bool) {
c := ErrorClass(err)
code := codes.Unknown
if s, ok := status.FromError(err); ok {
code = s.Code()
}
span.SetTag("response_code", code)
span.SetTag("response_class", c)
if client || c == ServerError {
ext.Error.Set(span, true)
}
}
2 changes: 1 addition & 1 deletion go/otgrpc/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func OpenTracingServerInterceptor(tracer opentracing.Tracer, optFuncs ...Option)
serverSpan.LogFields(log.Object("gRPC response", resp))
}
} else {
ext.Error.Set(serverSpan, true)
SetSpanTags(serverSpan, err, false)
serverSpan.LogFields(log.String("event", "gRPC error"), log.Error(err))
}
if otgrpcOpts.decorator != nil {
Expand Down

0 comments on commit d78177f

Please sign in to comment.