| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package consumererror // import "go.opentelemetry.io/collector/consumer/consumererror" |
| 5 |
|
| 6 |
import ( |
| 7 |
"errors" |
| 8 |
"fmt" |
| 9 |
"net/http" |
| 10 |
|
| 11 |
"google.golang.org/grpc/codes" |
| 12 |
"google.golang.org/grpc/status" |
| 13 |
|
| 14 |
"go.opentelemetry.io/collector/consumer/consumererror/internal/statusconversion" |
| 15 |
) |
| 16 |
|
| 17 |
// Error is intended to be used to encapsulate various information that can add |
| 18 |
// context to an error that occurred within a pipeline component. Error objects |
| 19 |
// are constructed through calling `New` with the relevant options to capture |
| 20 |
// data around the error that occurred. |
| 21 |
// |
| 22 |
// Error should be obtained from a given `error` object using `errors.As`. |
| 23 |
type Error struct { |
| 24 |
error |
| 25 |
httpStatus int |
| 26 |
grpcStatus *status.Status |
| 27 |
isRetryable bool |
| 28 |
} |
| 29 |
|
| 30 |
var _ error = (*Error)(nil) |
| 31 |
|
| 32 |
// NewOTLPHTTPError records an HTTP status code that was received from a server |
| 33 |
// during data submission. |
| 34 |
// |
| 35 |
// NOTE: This function will panic if passed an HTTP status between 200 and 299 inclusive. |
| 36 |
// This is to reserve the behavior for handling these codes for the future. |
| 37 |
func NewOTLPHTTPError(origErr error, httpStatus int) error { |
| 38 |
// Matches what is considered a successful response in the OTLP/HTTP Exporter. |
| 39 |
if httpStatus >= 200 && httpStatus <= 299 { |
| 40 |
panic("NewOTLPHTTPError should not be called with a success code") |
| 41 |
} |
| 42 |
var retryable bool |
| 43 |
switch httpStatus { |
| 44 |
case http.StatusTooManyRequests, http.StatusBadGateway, http.StatusServiceUnavailable, http.StatusGatewayTimeout: |
| 45 |
retryable = true |
| 46 |
} |
| 47 |
|
| 48 |
return &Error{error: origErr, httpStatus: httpStatus, isRetryable: retryable} |
| 49 |
} |
| 50 |
|
| 51 |
// NewOTLPGRPCError records a gRPC status code that was received from a server |
| 52 |
// during data submission. |
| 53 |
// |
| 54 |
// NOTE: This function will panic if passed a *status.Status with an underlying |
| 55 |
// code of codes.OK. This is to reserve the behavior for handling this code for |
| 56 |
// the future. |
| 57 |
func NewOTLPGRPCError(origErr error, status *status.Status) error { |
| 58 |
var retryable bool |
| 59 |
if status != nil { |
| 60 |
switch status.Code() { |
| 61 |
case codes.Canceled, codes.DeadlineExceeded, codes.Aborted, codes.OutOfRange, codes.Unavailable, codes.DataLoss: |
| 62 |
retryable = true |
| 63 |
// Matches what is considered a successful response in the OTLP Exporter. |
| 64 |
case codes.OK: |
| 65 |
panic("NewOTLPGRPCError should not be called with an OK code") |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
return &Error{error: origErr, grpcStatus: status, isRetryable: retryable} |
| 70 |
} |
| 71 |
|
| 72 |
// NewRetryableError records that this error is retryable according to OTLP specification. |
| 73 |
func NewRetryableError(origErr error) error { |
| 74 |
return &Error{error: origErr, isRetryable: true} |
| 75 |
} |
| 76 |
|
| 77 |
// Error implements the error interface. |
| 78 |
// |
| 79 |
// If an error object was given, that is used. |
| 80 |
// Otherwise, the gRPC error from the status.Status is used, |
| 81 |
// or an error message containing the HTTP status code is given. |
| 82 |
func (e *Error) Error() string { |
| 83 |
if e.error != nil { |
| 84 |
return e.error.Error() |
| 85 |
} |
| 86 |
|
| 87 |
if e.grpcStatus != nil { |
| 88 |
return e.grpcStatus.Err().Error() |
| 89 |
} else if e.httpStatus > 0 { |
| 90 |
return fmt.Sprintf("network error: received HTTP status %d", e.httpStatus) |
| 91 |
} |
| 92 |
|
| 93 |
return "uninitialized consumererror.Error" |
| 94 |
} |
| 95 |
|
| 96 |
// Unwrap returns the wrapped error for use by `errors.Is` and `errors.As`. |
| 97 |
// |
| 98 |
// If an error object was not passed but a gRPC `status.Status` was passed, |
| 99 |
// the underlying error from the status is returned. |
| 100 |
func (e *Error) Unwrap() error { |
| 101 |
if e.error != nil { |
| 102 |
return e.error |
| 103 |
} |
| 104 |
|
| 105 |
if e.grpcStatus != nil { |
| 106 |
return e.grpcStatus.Err() |
| 107 |
} |
| 108 |
|
| 109 |
return nil |
| 110 |
} |
| 111 |
|
| 112 |
// IsRetryable returns true if the error was created with NewRetryableError, if |
| 113 |
// the HTTP status code is retryable according to OTLP, or if the gRPC status is |
| 114 |
// retryable according to OTLP. Otherwise, returns false. |
| 115 |
// |
| 116 |
// See https://github.com/open-telemetry/opentelemetry-proto/blob/main/docs/specification.md for retryable |
| 117 |
// HTTP and gRPC codes. |
| 118 |
func (e *Error) IsRetryable() bool { |
| 119 |
return e.isRetryable |
| 120 |
} |
| 121 |
|
| 122 |
// ToHTTPStatus returns an HTTP status code either directly set by the source on |
| 123 |
// an [Error] object, derived from a gRPC status code set by the source, or |
| 124 |
// derived from Retryable. When deriving the value, the OTLP specification is |
| 125 |
// used to map to HTTP. See |
| 126 |
// https://github.com/open-telemetry/opentelemetry-proto/blob/main/docs/specification.md |
| 127 |
// for more details. |
| 128 |
// |
| 129 |
// If a http status code cannot be derived from these three sources then 500 is |
| 130 |
// returned. |
| 131 |
func ToHTTPStatus(err error) int { |
| 132 |
var e *Error |
| 133 |
if errors.As(err, &e) { |
| 134 |
if e.httpStatus != 0 { |
| 135 |
return e.httpStatus |
| 136 |
} |
| 137 |
if e.grpcStatus != nil { |
| 138 |
return statusconversion.GetHTTPStatusCodeFromStatus(e.grpcStatus) |
| 139 |
} |
| 140 |
if e.isRetryable { |
| 141 |
return http.StatusServiceUnavailable |
| 142 |
} |
| 143 |
} |
| 144 |
return http.StatusInternalServerError |
| 145 |
} |
| 146 |
|
| 147 |
// ToGRPCStatus returns a gRPC status code either directly set by the source on |
| 148 |
// an [Error] object, derived from an HTTP status code set by the |
| 149 |
// source, or derived from Retryable. When deriving the value, the OTLP |
| 150 |
// specification is used to map to gRPC. See |
| 151 |
// https://github.com/open-telemetry/opentelemetry-proto/blob/main/docs/specification.md |
| 152 |
// for more details. |
| 153 |
// |
| 154 |
// If an [Error] object is not present, then we attempt to get a status.Status from the |
| 155 |
// error tree. |
| 156 |
// |
| 157 |
// If a status.Status cannot be derived from these sources then INTERNAL is |
| 158 |
// returned. |
| 159 |
func ToGRPCStatus(err error) *status.Status { |
| 160 |
var e *Error |
| 161 |
if errors.As(err, &e) { |
| 162 |
if e.grpcStatus != nil { |
| 163 |
return e.grpcStatus |
| 164 |
} |
| 165 |
if e.httpStatus != 0 { |
| 166 |
return statusconversion.NewStatusFromMsgAndHTTPCode(e.Error(), e.httpStatus) |
| 167 |
} |
| 168 |
if e.isRetryable { |
| 169 |
return status.New(codes.Unavailable, e.Error()) |
| 170 |
} |
| 171 |
} |
| 172 |
if st, ok := status.FromError(err); ok { |
| 173 |
return st |
| 174 |
} |
| 175 |
return status.New(codes.Unknown, e.Error()) |
| 176 |
} |