| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package statusconversion // import "go.opentelemetry.io/collector/consumer/consumererror/internal/statusconversion" |
| 5 |
|
| 6 |
import ( |
| 7 |
"net/http" |
| 8 |
|
| 9 |
"google.golang.org/grpc/codes" |
| 10 |
"google.golang.org/grpc/status" |
| 11 |
) |
| 12 |
|
| 13 |
func GetHTTPStatusCodeFromStatus(s *status.Status) int { |
| 14 |
// See https://github.com/open-telemetry/opentelemetry-proto/blob/main/docs/specification.md#failures |
| 15 |
// to see if a code is retryable. |
| 16 |
// See https://github.com/open-telemetry/opentelemetry-proto/blob/main/docs/specification.md#failures-1 |
| 17 |
// to see a list of retryable http status codes. |
| 18 |
switch s.Code() { |
| 19 |
// Retryable |
| 20 |
case codes.Canceled, codes.DeadlineExceeded, codes.Aborted, codes.OutOfRange, codes.Unavailable, codes.DataLoss: |
| 21 |
return http.StatusServiceUnavailable |
| 22 |
// Retryable |
| 23 |
case codes.ResourceExhausted: |
| 24 |
return http.StatusTooManyRequests |
| 25 |
// Not Retryable |
| 26 |
case codes.InvalidArgument: |
| 27 |
return http.StatusBadRequest |
| 28 |
// Not Retryable |
| 29 |
case codes.Unauthenticated: |
| 30 |
return http.StatusUnauthorized |
| 31 |
// Not Retryable |
| 32 |
case codes.PermissionDenied: |
| 33 |
return http.StatusForbidden |
| 34 |
// Not Retryable |
| 35 |
case codes.Unimplemented: |
| 36 |
return http.StatusNotFound |
| 37 |
// Not Retryable |
| 38 |
default: |
| 39 |
return http.StatusInternalServerError |
| 40 |
} |
| 41 |
} |
| 42 |
|
| 43 |
func NewStatusFromMsgAndHTTPCode(errMsg string, statusCode int) *status.Status { |
| 44 |
var c codes.Code |
| 45 |
// Mapping based on https://github.com/grpc/grpc/blob/master/doc/http-grpc-status-mapping.md |
| 46 |
// 429 mapping to ResourceExhausted and 400 mapping to StatusBadRequest are exceptions. |
| 47 |
switch statusCode { |
| 48 |
case http.StatusBadRequest: |
| 49 |
c = codes.InvalidArgument |
| 50 |
case http.StatusUnauthorized: |
| 51 |
c = codes.Unauthenticated |
| 52 |
case http.StatusForbidden: |
| 53 |
c = codes.PermissionDenied |
| 54 |
case http.StatusNotFound: |
| 55 |
c = codes.Unimplemented |
| 56 |
case http.StatusTooManyRequests: |
| 57 |
c = codes.ResourceExhausted |
| 58 |
case http.StatusBadGateway, http.StatusServiceUnavailable, http.StatusGatewayTimeout: |
| 59 |
c = codes.Unavailable |
| 60 |
default: |
| 61 |
c = codes.Unknown |
| 62 |
} |
| 63 |
return status.New(c, errMsg) |
| 64 |
} |