-
Notifications
You must be signed in to change notification settings - Fork 2.1k
[consumererror] Add OTLP-centric error type #13042
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
Merged
bogdandrutu
merged 25 commits into
open-telemetry:main
from
evan-bradley:consumererror-otlp-type
Jul 9, 2025
Merged
Changes from all commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
b5c90ce
Add new otlp-centric error type
TylerHelmuth 4e85ad6
Remove options
TylerHelmuth 8fa1cda
merge in upstream/main
TylerHelmuth 5acc22d
Fix lint
TylerHelmuth 7ae4662
Merge remote-tracking branch 'upstream/main' into consumererror-otlp-…
evan-bradley 947b601
Fix bad merge
evan-bradley 5d51161
Adjust feedback from previous PR
evan-bradley c9ec634
go mod tidy
evan-bradley 062590a
Add changelog
evan-bradley 42b5cc5
Improve test coverage
evan-bradley a3021d5
Remove experimental notice
evan-bradley e58b8ca
Fix Godoc comment
evan-bradley 32bd5ef
Address PR feedback
evan-bradley 05e21df
Merge remote-tracking branch 'upstream/main' into consumererror-otlp-…
evan-bradley 957abf5
Fix tests
evan-bradley 0897af5
Fix lint
evan-bradley f4e31ab
Address PR feedback
evan-bradley 15e3d3b
Merge remote-tracking branch 'upstream/main' into consumererror-otlp-…
evan-bradley 2e2f7ae
Reserve behavior for success codes
evan-bradley 5b78443
Merge remote-tracking branch 'upstream/main' into consumererror-otlp-…
evan-bradley 8783f62
Fix lint
evan-bradley 7e52b96
Fix lint
evan-bradley 5d34f3b
Improve wrapped errors
evan-bradley c44f8a3
Merge remote-tracking branch 'upstream/main' into consumererror-otlp-…
evan-bradley 20af93c
Merge branch 'main' into consumererror-otlp-type
evan-bradley File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,27 @@ | ||
| # Use this changelog template to create an entry for release notes. | ||
|
|
||
| # One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix' | ||
| change_type: enhancement | ||
|
|
||
| # The name of the component, or a single word describing the area of concern, (e.g. otlpreceiver) | ||
| component: consumererror | ||
|
|
||
| # A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). | ||
| note: Add `Error` type | ||
|
|
||
| # One or more tracking issues or pull requests related to the change | ||
| issues: [7047] | ||
|
|
||
| # (Optional) One or more lines of additional information to render under the primary note. | ||
| # These lines will be padded with 2 spaces and then inserted directly into the document. | ||
| # Use pipe (|) for multiline entries. | ||
| subtext: | | ||
| This type can contain information about errors that allow components (e.g. exporters) | ||
| to communicate error information back up the pipeline. | ||
|
|
||
| # Optional: The change log or logs in which this entry should be included. | ||
| # e.g. '[user]' or '[user, api]' | ||
| # Include 'user' if the change is relevant to end users. | ||
| # Include 'api' if there is a change to a library API. | ||
| # Default: '[user]' | ||
| change_logs: [api] |
This file contains hidden or 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,176 @@ | ||
| // Copyright The OpenTelemetry Authors | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| package consumererror // import "go.opentelemetry.io/collector/consumer/consumererror" | ||
|
|
||
| import ( | ||
| "errors" | ||
| "fmt" | ||
| "net/http" | ||
|
|
||
| "google.golang.org/grpc/codes" | ||
| "google.golang.org/grpc/status" | ||
|
|
||
| "go.opentelemetry.io/collector/consumer/consumererror/internal/statusconversion" | ||
| ) | ||
|
|
||
| // Error is intended to be used to encapsulate various information that can add | ||
| // context to an error that occurred within a pipeline component. Error objects | ||
| // are constructed through calling `New` with the relevant options to capture | ||
| // data around the error that occurred. | ||
| // | ||
| // Error should be obtained from a given `error` object using `errors.As`. | ||
| type Error struct { | ||
| error | ||
| httpStatus int | ||
| grpcStatus *status.Status | ||
| isRetryable bool | ||
| } | ||
|
|
||
| var _ error = (*Error)(nil) | ||
|
|
||
| // NewOTLPHTTPError records an HTTP status code that was received from a server | ||
| // during data submission. | ||
| // | ||
| // NOTE: This function will panic if passed an HTTP status between 200 and 299 inclusive. | ||
| // This is to reserve the behavior for handling these codes for the future. | ||
| func NewOTLPHTTPError(origErr error, httpStatus int) error { | ||
|
evan-bradley marked this conversation as resolved.
|
||
| // Matches what is considered a successful response in the OTLP/HTTP Exporter. | ||
| if httpStatus >= 200 && httpStatus <= 299 { | ||
| panic("NewOTLPHTTPError should not be called with a success code") | ||
| } | ||
| var retryable bool | ||
| switch httpStatus { | ||
| case http.StatusTooManyRequests, http.StatusBadGateway, http.StatusServiceUnavailable, http.StatusGatewayTimeout: | ||
| retryable = true | ||
| } | ||
|
|
||
| return &Error{error: origErr, httpStatus: httpStatus, isRetryable: retryable} | ||
| } | ||
|
|
||
| // NewOTLPGRPCError records a gRPC status code that was received from a server | ||
| // during data submission. | ||
| // | ||
| // NOTE: This function will panic if passed a *status.Status with an underlying | ||
| // code of codes.OK. This is to reserve the behavior for handling this code for | ||
| // the future. | ||
| func NewOTLPGRPCError(origErr error, status *status.Status) error { | ||
|
evan-bradley marked this conversation as resolved.
|
||
| var retryable bool | ||
| if status != nil { | ||
| switch status.Code() { | ||
| case codes.Canceled, codes.DeadlineExceeded, codes.Aborted, codes.OutOfRange, codes.Unavailable, codes.DataLoss: | ||
| retryable = true | ||
| // Matches what is considered a successful response in the OTLP Exporter. | ||
| case codes.OK: | ||
| panic("NewOTLPGRPCError should not be called with an OK code") | ||
| } | ||
| } | ||
|
|
||
| return &Error{error: origErr, grpcStatus: status, isRetryable: retryable} | ||
| } | ||
|
|
||
| // NewRetryableError records that this error is retryable according to OTLP specification. | ||
| func NewRetryableError(origErr error) error { | ||
| return &Error{error: origErr, isRetryable: true} | ||
| } | ||
|
|
||
| // Error implements the error interface. | ||
| // | ||
| // If an error object was given, that is used. | ||
| // Otherwise, the gRPC error from the status.Status is used, | ||
| // or an error message containing the HTTP status code is given. | ||
| func (e *Error) Error() string { | ||
| if e.error != nil { | ||
| return e.error.Error() | ||
| } | ||
|
|
||
| if e.grpcStatus != nil { | ||
| return e.grpcStatus.Err().Error() | ||
| } else if e.httpStatus > 0 { | ||
| return fmt.Sprintf("network error: received HTTP status %d", e.httpStatus) | ||
| } | ||
|
|
||
| return "uninitialized consumererror.Error" | ||
| } | ||
|
|
||
| // Unwrap returns the wrapped error for use by `errors.Is` and `errors.As`. | ||
| // | ||
| // If an error object was not passed but a gRPC `status.Status` was passed, | ||
| // the underlying error from the status is returned. | ||
| func (e *Error) Unwrap() error { | ||
| if e.error != nil { | ||
| return e.error | ||
| } | ||
|
|
||
| if e.grpcStatus != nil { | ||
| return e.grpcStatus.Err() | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| // IsRetryable returns true if the error was created with NewRetryableError, if | ||
| // the HTTP status code is retryable according to OTLP, or if the gRPC status is | ||
| // retryable according to OTLP. Otherwise, returns false. | ||
| // | ||
| // See https://github.com/open-telemetry/opentelemetry-proto/blob/main/docs/specification.md for retryable | ||
| // HTTP and gRPC codes. | ||
| func (e *Error) IsRetryable() bool { | ||
| return e.isRetryable | ||
| } | ||
|
|
||
| // ToHTTPStatus returns an HTTP status code either directly set by the source on | ||
| // an [Error] object, derived from a gRPC status code set by the source, or | ||
| // derived from Retryable. When deriving the value, the OTLP specification is | ||
| // used to map to HTTP. See | ||
| // https://github.com/open-telemetry/opentelemetry-proto/blob/main/docs/specification.md | ||
| // for more details. | ||
| // | ||
| // If a http status code cannot be derived from these three sources then 500 is | ||
| // returned. | ||
| func ToHTTPStatus(err error) int { | ||
| var e *Error | ||
| if errors.As(err, &e) { | ||
| if e.httpStatus != 0 { | ||
| return e.httpStatus | ||
| } | ||
| if e.grpcStatus != nil { | ||
| return statusconversion.GetHTTPStatusCodeFromStatus(e.grpcStatus) | ||
| } | ||
| if e.isRetryable { | ||
| return http.StatusServiceUnavailable | ||
| } | ||
| } | ||
| return http.StatusInternalServerError | ||
| } | ||
|
|
||
| // ToGRPCStatus returns a gRPC status code either directly set by the source on | ||
| // an [Error] object, derived from an HTTP status code set by the | ||
| // source, or derived from Retryable. When deriving the value, the OTLP | ||
| // specification is used to map to gRPC. See | ||
| // https://github.com/open-telemetry/opentelemetry-proto/blob/main/docs/specification.md | ||
| // for more details. | ||
| // | ||
| // If an [Error] object is not present, then we attempt to get a status.Status from the | ||
| // error tree. | ||
| // | ||
| // If a status.Status cannot be derived from these sources then INTERNAL is | ||
| // returned. | ||
| func ToGRPCStatus(err error) *status.Status { | ||
| var e *Error | ||
| if errors.As(err, &e) { | ||
| if e.grpcStatus != nil { | ||
| return e.grpcStatus | ||
| } | ||
| if e.httpStatus != 0 { | ||
| return statusconversion.NewStatusFromMsgAndHTTPCode(e.Error(), e.httpStatus) | ||
| } | ||
| if e.isRetryable { | ||
| return status.New(codes.Unavailable, e.Error()) | ||
| } | ||
| } | ||
| if st, ok := status.FromError(err); ok { | ||
| return st | ||
| } | ||
| return status.New(codes.Unknown, e.Error()) | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.