forked from grpc-ecosystem/grpc-opentracing
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
standardize error class and add span error tags
- Loading branch information
Daniela Miao
committed
Apr 27, 2017
1 parent
ef7d6c8
commit d78177f
Showing
3 changed files
with
68 additions
and
2 deletions.
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,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) | ||
} | ||
} |
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