|
| 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 | + "net/http" |
| 8 | + |
| 9 | + "google.golang.org/grpc/codes" |
| 10 | + "google.golang.org/grpc/status" |
| 11 | + |
| 12 | + "go.opentelemetry.io/collector/consumer/consumererror/internal/statusconversion" |
| 13 | +) |
| 14 | + |
| 15 | +// Error is intended to be used to encapsulate various information that can add |
| 16 | +// context to an error that occurred within a pipeline component. Error objects |
| 17 | +// are constructed through calling `New` with the relevant options to capture |
| 18 | +// data around the error that occurred. |
| 19 | +// |
| 20 | +// It may hold multiple errors from downstream components, and can be merged |
| 21 | +// with other errors as it travels upstream using `Combine`. The `Error` should |
| 22 | +// be obtained from a given `error` object using `errors.As`. |
| 23 | +// |
| 24 | +// Experimental: This API is at the early stage of development and may change |
| 25 | +// without backward compatibility |
| 26 | +type Error struct { |
| 27 | + error |
| 28 | + httpStatus int |
| 29 | + grpcStatus *status.Status |
| 30 | + retryable bool |
| 31 | +} |
| 32 | + |
| 33 | +var _ error = (*Error)(nil) |
| 34 | + |
| 35 | +// ErrorOption allows annotating an Error with metadata. |
| 36 | +type ErrorOption interface { |
| 37 | + applyOption(*Error) |
| 38 | +} |
| 39 | + |
| 40 | +type errorOptionFunc func(*Error) |
| 41 | + |
| 42 | +func (f errorOptionFunc) applyOption(e *Error) { |
| 43 | + f(e) |
| 44 | +} |
| 45 | + |
| 46 | +// New wraps an error that happened while consuming telemetry and adds metadata |
| 47 | +// onto it to be passed back up the pipeline. |
| 48 | +// At least one option should be provided. |
| 49 | +// |
| 50 | +// Experimental: This API is at the early stage of development and may change |
| 51 | +// without backward compatibility |
| 52 | +func New(origErr error, options ...ErrorOption) error { |
| 53 | + err := &Error{error: origErr} |
| 54 | + |
| 55 | + for _, option := range options { |
| 56 | + option.applyOption(err) |
| 57 | + } |
| 58 | + |
| 59 | + return err |
| 60 | +} |
| 61 | + |
| 62 | +// WithOTLPHTTPStatus records an HTTP status code that was received from a server |
| 63 | +// during data submission. |
| 64 | +// It is not necessary to use WithRetryable with creating an error with WithOTLPHTTPStatus |
| 65 | +// as the retryable property can be inferred from the HTTP status code using OTLP specification. |
| 66 | +// |
| 67 | +// Experimental: This API is at the early stage of development and may change |
| 68 | +// without backward compatibility |
| 69 | +func WithOTLPHTTPStatus(status int) ErrorOption { |
| 70 | + return errorOptionFunc(func(err *Error) { |
| 71 | + err.httpStatus = status |
| 72 | + }) |
| 73 | +} |
| 74 | + |
| 75 | +// WithOTLPGRPCStatus records a gRPC status code that was received from a server |
| 76 | +// during data submission. |
| 77 | +// It is not necessary to use WithRetryable with creating an error with WithOTLPGRPCStatus |
| 78 | +// as the retryable property can be inferred from the grpc status using OTLP specification. |
| 79 | +// |
| 80 | +// Experimental: This API is at the early stage of development and may change |
| 81 | +// without backward compatibility |
| 82 | +func WithOTLPGRPCStatus(status *status.Status) ErrorOption { |
| 83 | + return errorOptionFunc(func(err *Error) { |
| 84 | + err.grpcStatus = status |
| 85 | + }) |
| 86 | +} |
| 87 | + |
| 88 | +// WithRetryable records that this error is retryable according to OTLP specification. |
| 89 | +// WithRetryable is not necessary when creating an error with WithOTLPHTTPStatus or |
| 90 | +// WithOTLPGRPCStatus, as the retryable property can be inferred from OTLP specification. |
| 91 | +// |
| 92 | +// Experimental: This API is at the early stage of development and may change |
| 93 | +// without backward compatibility |
| 94 | +func WithRetryable() ErrorOption { |
| 95 | + return errorOptionFunc(func(err *Error) { |
| 96 | + err.retryable = true |
| 97 | + }) |
| 98 | +} |
| 99 | + |
| 100 | +// Error implements the error interface. |
| 101 | +func (e *Error) Error() string { |
| 102 | + return e.error.Error() |
| 103 | +} |
| 104 | + |
| 105 | +// Unwrap returns the wrapped error for use by `errors.Is` and `errors.As`. |
| 106 | +func (e *Error) Unwrap() error { |
| 107 | + return e.error |
| 108 | +} |
| 109 | + |
| 110 | +// OTLPHTTPStatus returns an HTTP status code either directly set by the source, |
| 111 | +// derived from a gRPC status code set by the source, or derived from Retryable. |
| 112 | +// When deriving the value, the OTLP specification is used to map to HTTP. |
| 113 | +// See https://github.com/open-telemetry/opentelemetry-proto/blob/main/docs/specification.md for more details. |
| 114 | +// |
| 115 | +// If a http status code cannot be derived from these three sources then 500 is returned. |
| 116 | +// |
| 117 | +// Experimental: This API is at the early stage of development and may change |
| 118 | +// without backward compatibility |
| 119 | +func (e *Error) OTLPHTTPStatus() int { |
| 120 | + if e.httpStatus != 0 { |
| 121 | + return e.httpStatus |
| 122 | + } |
| 123 | + if e.grpcStatus != nil { |
| 124 | + return statusconversion.GetHTTPStatusCodeFromStatus(e.grpcStatus) |
| 125 | + } |
| 126 | + if e.retryable { |
| 127 | + return http.StatusServiceUnavailable |
| 128 | + } |
| 129 | + return http.StatusInternalServerError |
| 130 | +} |
| 131 | + |
| 132 | +// OTLPGRPCStatus returns an gRPC status code either directly set by the source, |
| 133 | +// derived from an HTTP status code set by the source, or derived from Retryable. |
| 134 | +// When deriving the value, the OTLP specification is used to map to GRPC. |
| 135 | +// See https://github.com/open-telemetry/opentelemetry-proto/blob/main/docs/specification.md for more details. |
| 136 | +// |
| 137 | +// If a grpc code cannot be derived from these three sources then INTERNAL is returned. |
| 138 | +// |
| 139 | +// Experimental: This API is at the early stage of development and may change |
| 140 | +// without backward compatibility |
| 141 | +func (e *Error) OTLPGRPCStatus() *status.Status { |
| 142 | + if e.grpcStatus != nil { |
| 143 | + return e.grpcStatus |
| 144 | + } |
| 145 | + if e.httpStatus != 0 { |
| 146 | + return statusconversion.NewStatusFromMsgAndHTTPCode(e.Error(), e.httpStatus) |
| 147 | + } |
| 148 | + if e.retryable { |
| 149 | + return status.New(codes.Unavailable, e.Error()) |
| 150 | + } |
| 151 | + return status.New(codes.Internal, e.Error()) |
| 152 | +} |
| 153 | + |
| 154 | +// Retryable returns true if the error was created with the WithRetryable set to true, |
| 155 | +// if the http status code is retryable according to OTLP, |
| 156 | +// or if the grpc status is retryable according to OTLP. |
| 157 | +// Otherwise, returns false. |
| 158 | +// |
| 159 | +// See https://github.com/open-telemetry/opentelemetry-proto/blob/main/docs/specification.md for retryable |
| 160 | +// http and grpc codes. |
| 161 | +// |
| 162 | +// Experimental: This API is at the early stage of development and may change |
| 163 | +// without backward compatibility |
| 164 | +func (e *Error) Retryable() bool { |
| 165 | + if e.retryable { |
| 166 | + return true |
| 167 | + } |
| 168 | + switch e.httpStatus { |
| 169 | + case http.StatusTooManyRequests, http.StatusBadGateway, http.StatusServiceUnavailable, http.StatusGatewayTimeout: |
| 170 | + return true |
| 171 | + } |
| 172 | + if e.grpcStatus != nil { |
| 173 | + switch e.grpcStatus.Code() { |
| 174 | + case codes.Canceled, codes.DeadlineExceeded, codes.Aborted, codes.OutOfRange, codes.Unavailable, codes.DataLoss: |
| 175 | + return true |
| 176 | + } |
| 177 | + } |
| 178 | + return false |
| 179 | +} |
0 commit comments