-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Return proper http response code based on retryable error
- Loading branch information
1 parent
f7c380d
commit bc6c78b
Showing
7 changed files
with
219 additions
and
77 deletions.
There are no files selected for viewing
This file contains 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,46 @@ | ||
// 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) *status.Status { | ||
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) { | ||
code = codes.InvalidArgument | ||
} | ||
s = status.New(code, err.Error()) | ||
} | ||
return s | ||
} | ||
|
||
func GetHTTPStatusCodeFromStatus(err error) int { | ||
s, ok := status.FromError(err) | ||
if !ok { | ||
return http.StatusInternalServerError | ||
} | ||
// 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 | ||
// Not Retryable | ||
default: | ||
return http.StatusInternalServerError | ||
} | ||
} |
This file contains 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,76 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package errors // import "go.opentelemetry.io/collector/receiver/otlpreceiver/internal/util" | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"google.golang.org/grpc/codes" | ||
"google.golang.org/grpc/status" | ||
|
||
"go.opentelemetry.io/collector/consumer/consumererror" | ||
) | ||
|
||
func Test_GetStatusFromError(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
input error | ||
expected *status.Status | ||
}{ | ||
{ | ||
name: "Status", | ||
input: status.Error(codes.Aborted, "test"), | ||
expected: status.New(codes.Aborted, "test"), | ||
}, | ||
{ | ||
name: "Permanent Error", | ||
input: consumererror.NewPermanent(fmt.Errorf("test")), | ||
expected: status.New(codes.InvalidArgument, "Permanent error: test"), | ||
}, | ||
{ | ||
name: "Non-Permanent Error", | ||
input: fmt.Errorf("test"), | ||
expected: status.New(codes.Unavailable, "test"), | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
result := GetStatusFromError(tt.input) | ||
assert.Equal(t, tt.expected, result) | ||
}) | ||
} | ||
} | ||
|
||
func Test_GetHTTPStatusCodeFromStatus(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
input error | ||
expected int | ||
}{ | ||
{ | ||
name: "Not a Status", | ||
input: fmt.Errorf("not a status error"), | ||
expected: http.StatusInternalServerError, | ||
}, | ||
{ | ||
name: "Retryable Status", | ||
input: status.New(codes.Unavailable, "test").Err(), | ||
expected: http.StatusServiceUnavailable, | ||
}, | ||
{ | ||
name: "Non-retryable Status", | ||
input: status.New(codes.InvalidArgument, "test").Err(), | ||
expected: http.StatusInternalServerError, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
result := GetHTTPStatusCodeFromStatus(tt.input) | ||
assert.Equal(t, tt.expected, result) | ||
}) | ||
} | ||
} |
This file contains 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
This file contains 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
This file contains 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
Oops, something went wrong.