| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package statusutil // import "go.opentelemetry.io/collector/internal/statusutil" |
| 5 |
|
| 6 |
import ( |
| 7 |
"net/http" |
| 8 |
|
| 9 |
"google.golang.org/genproto/googleapis/rpc/errdetails" |
| 10 |
"google.golang.org/grpc/codes" |
| 11 |
"google.golang.org/grpc/status" |
| 12 |
) |
| 13 |
|
| 14 |
// NewStatusFromMsgAndHTTPCode returns a gRPC status based on an error message string and a http status code. |
| 15 |
// This function is shared between the http receiver and http exporter for error propagation. |
| 16 |
func NewStatusFromMsgAndHTTPCode(errMsg string, statusCode int) *status.Status { |
| 17 |
var c codes.Code |
| 18 |
// Mapping based on https://github.com/grpc/grpc/blob/master/doc/http-grpc-status-mapping.md |
| 19 |
// 429 mapping to ResourceExhausted and 400 mapping to StatusBadRequest are exceptions. |
| 20 |
switch statusCode { |
| 21 |
case http.StatusBadRequest: |
| 22 |
c = codes.InvalidArgument |
| 23 |
case http.StatusUnauthorized: |
| 24 |
c = codes.Unauthenticated |
| 25 |
case http.StatusForbidden: |
| 26 |
c = codes.PermissionDenied |
| 27 |
case http.StatusNotFound: |
| 28 |
c = codes.Unimplemented |
| 29 |
case http.StatusTooManyRequests: |
| 30 |
c = codes.ResourceExhausted |
| 31 |
case http.StatusBadGateway, http.StatusServiceUnavailable, http.StatusGatewayTimeout: |
| 32 |
c = codes.Unavailable |
| 33 |
default: |
| 34 |
c = codes.Unknown |
| 35 |
} |
| 36 |
return status.New(c, errMsg) |
| 37 |
} |
| 38 |
|
| 39 |
func GetRetryInfo(status *status.Status) *errdetails.RetryInfo { |
| 40 |
for _, detail := range status.Details() { |
| 41 |
if t, ok := detail.(*errdetails.RetryInfo); ok { |
| 42 |
return t |
| 43 |
} |
| 44 |
} |
| 45 |
return nil |
| 46 |
} |