| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package errors // import "go.opentelemetry.io/collector/receiver/otlpreceiver/internal/errors" |
| 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" |
| 13 |
) |
| 14 |
|
| 15 |
func GetStatusFromError(err error) error { |
| 16 |
s, ok := status.FromError(err) |
| 17 |
if !ok { |
| 18 |
// Default to a retryable error |
| 19 |
// https://github.com/open-telemetry/opentelemetry-proto/blob/main/docs/specification.md#failures |
| 20 |
code := codes.Unavailable |
| 21 |
if consumererror.IsPermanent(err) { |
| 22 |
// If an error is permanent but doesn't have an attached gRPC status, assume it is server-side. |
| 23 |
code = codes.Internal |
| 24 |
} |
| 25 |
s = status.New(code, err.Error()) |
| 26 |
} |
| 27 |
return s.Err() |
| 28 |
} |
| 29 |
|
| 30 |
func GetHTTPStatusCodeFromStatus(s *status.Status) int { |
| 31 |
// See https://github.com/open-telemetry/opentelemetry-proto/blob/main/docs/specification.md#failures |
| 32 |
// to see if a code is retryable. |
| 33 |
// See https://github.com/open-telemetry/opentelemetry-proto/blob/main/docs/specification.md#failures-1 |
| 34 |
// to see a list of retryable http status codes. |
| 35 |
switch s.Code() { |
| 36 |
// Retryable |
| 37 |
case codes.Canceled, codes.DeadlineExceeded, codes.Aborted, codes.OutOfRange, codes.Unavailable, codes.DataLoss: |
| 38 |
return http.StatusServiceUnavailable |
| 39 |
// Retryable |
| 40 |
case codes.ResourceExhausted: |
| 41 |
return http.StatusTooManyRequests |
| 42 |
// Not Retryable |
| 43 |
case codes.InvalidArgument: |
| 44 |
return http.StatusBadRequest |
| 45 |
// Not Retryable |
| 46 |
case codes.Unauthenticated: |
| 47 |
return http.StatusUnauthorized |
| 48 |
// Not Retryable |
| 49 |
case codes.PermissionDenied: |
| 50 |
return http.StatusForbidden |
| 51 |
// Not Retryable |
| 52 |
case codes.Unimplemented: |
| 53 |
return http.StatusNotFound |
| 54 |
// Not Retryable |
| 55 |
default: |
| 56 |
return http.StatusInternalServerError |
| 57 |
} |
| 58 |
} |