Files
Tyler HelmuthandGitHub 41c2de8bd8 Properly handle error code translation (#10574)
#### Description

Fixes a bug where the otlpreceiver was not translating grpc error codes
to http status codes the same way that the otlp exporter was translating
http status codes to grpc error codes.

#### Link to tracking issue
Fixes https://github.com/open-telemetry/opentelemetry-collector/issues/10538

#### Testing
Added unit tests
2024-07-11 11:33:39 -07:00

59 lines
1.8 KiB
Go

// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package errors // import "go.opentelemetry.io/collector/receiver/otlpreceiver/internal/errors"
import (
"net/http"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"go.opentelemetry.io/collector/consumer/consumererror"
)
func GetStatusFromError(err error) error {
s, ok := status.FromError(err)
if !ok {
// Default to a retryable error
// https://github.com/open-telemetry/opentelemetry-proto/blob/main/docs/specification.md#failures
code := codes.Unavailable
if consumererror.IsPermanent(err) {
// If an error is permanent but doesn't have an attached gRPC status, assume it is server-side.
code = codes.Internal
}
s = status.New(code, err.Error())
}
return s.Err()
}
func GetHTTPStatusCodeFromStatus(s *status.Status) int {
// See https://github.com/open-telemetry/opentelemetry-proto/blob/main/docs/specification.md#failures
// to see if a code is retryable.
// See https://github.com/open-telemetry/opentelemetry-proto/blob/main/docs/specification.md#failures-1
// to see a list of retryable http status codes.
switch s.Code() {
// Retryable
case codes.Canceled, codes.DeadlineExceeded, codes.Aborted, codes.OutOfRange, codes.Unavailable, codes.DataLoss:
return http.StatusServiceUnavailable
// Retryable
case codes.ResourceExhausted:
return http.StatusTooManyRequests
// Not Retryable
case codes.InvalidArgument:
return http.StatusBadRequest
// Not Retryable
case codes.Unauthenticated:
return http.StatusUnauthorized
// Not Retryable
case codes.PermissionDenied:
return http.StatusForbidden
// Not Retryable
case codes.Unimplemented:
return http.StatusNotFound
// Not Retryable
default:
return http.StatusInternalServerError
}
}