Skip to content

Commit

Permalink
Refactor error handling into help
Browse files Browse the repository at this point in the history
  • Loading branch information
TylerHelmuth committed Jan 17, 2024
1 parent 4371e14 commit 5a83d85
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 36 deletions.
14 changes: 2 additions & 12 deletions receiver/otlpreceiver/internal/logs/otlp.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,9 @@ package logs // import "go.opentelemetry.io/collector/receiver/otlpreceiver/inte
import (
"context"

"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"

"go.opentelemetry.io/collector/consumer"
"go.opentelemetry.io/collector/consumer/consumererror"
"go.opentelemetry.io/collector/pdata/plog/plogotlp"
"go.opentelemetry.io/collector/receiver/otlpreceiver/internal/util"
"go.opentelemetry.io/collector/receiver/receiverhelper"
)

Expand Down Expand Up @@ -51,14 +48,7 @@ func (r *Receiver) Export(ctx context.Context, req plogotlp.ExportRequest) (plog
// NonPermanent errors will be converted to codes.Unavailable (equivalent to HTTP 503)
// Permanent errors will be converted to codes.InvalidArgument (equivalent to HTTP 400)
if err != nil {
s, ok := status.FromError(err)
if !ok {
code := codes.Unavailable
if consumererror.IsPermanent(err) {
code = codes.InvalidArgument
}
s = status.New(code, err.Error())
}
s := util.GetStatusFromError(err)
return plogotlp.NewExportResponse(), s.Err()
}

Expand Down
14 changes: 2 additions & 12 deletions receiver/otlpreceiver/internal/metrics/otlp.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,9 @@ package metrics // import "go.opentelemetry.io/collector/receiver/otlpreceiver/i
import (
"context"

"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"

"go.opentelemetry.io/collector/consumer"
"go.opentelemetry.io/collector/consumer/consumererror"
"go.opentelemetry.io/collector/pdata/pmetric/pmetricotlp"
"go.opentelemetry.io/collector/receiver/otlpreceiver/internal/util"
"go.opentelemetry.io/collector/receiver/receiverhelper"
)

Expand Down Expand Up @@ -51,14 +48,7 @@ func (r *Receiver) Export(ctx context.Context, req pmetricotlp.ExportRequest) (p
// NonPermanent errors will be converted to codes.Unavailable (equivalent to HTTP 503)
// Permanent errors will be converted to codes.InvalidArgument (equivalent to HTTP 400)
if err != nil {
s, ok := status.FromError(err)
if !ok {
code := codes.Unavailable
if consumererror.IsPermanent(err) {
code = codes.InvalidArgument
}
s = status.New(code, err.Error())
}
s := util.GetStatusFromError(err)
return pmetricotlp.NewExportResponse(), s.Err()
}

Expand Down
14 changes: 2 additions & 12 deletions receiver/otlpreceiver/internal/trace/otlp.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,9 @@ package trace // import "go.opentelemetry.io/collector/receiver/otlpreceiver/int
import (
"context"

"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"

"go.opentelemetry.io/collector/consumer"
"go.opentelemetry.io/collector/consumer/consumererror"
"go.opentelemetry.io/collector/pdata/ptrace/ptraceotlp"
"go.opentelemetry.io/collector/receiver/otlpreceiver/internal/util"
"go.opentelemetry.io/collector/receiver/receiverhelper"
)

Expand Down Expand Up @@ -52,14 +49,7 @@ func (r *Receiver) Export(ctx context.Context, req ptraceotlp.ExportRequest) (pt
// NonPermanent errors will be converted to codes.Unavailable (equivalent to HTTP 503)
// Permanent errors will be converted to codes.InvalidArgument (equivalent to HTTP 400)
if err != nil {
s, ok := status.FromError(err)
if !ok {
code := codes.Unavailable
if consumererror.IsPermanent(err) {
code = codes.InvalidArgument
}
s = status.New(code, err.Error())
}
s := util.GetStatusFromError(err)
return ptraceotlp.NewExportResponse(), s.Err()
}

Expand Down
22 changes: 22 additions & 0 deletions receiver/otlpreceiver/internal/util/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

package util // import "go.opentelemetry.io/collector/receiver/otlpreceiver/internal/util"

import (
"go.opentelemetry.io/collector/consumer/consumererror"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)

func GetStatusFromError(err error) *status.Status {
s, ok := status.FromError(err)
if !ok {
code := codes.Unavailable
if consumererror.IsPermanent(err) {
code = codes.InvalidArgument
}
s = status.New(code, err.Error())
}
return s
}
44 changes: 44 additions & 0 deletions receiver/otlpreceiver/internal/util/errors_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

package util // import "go.opentelemetry.io/collector/receiver/otlpreceiver/internal/util"

import (
"fmt"
"testing"

"github.com/stretchr/testify/assert"
"go.opentelemetry.io/collector/consumer/consumererror"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)

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)
})
}
}

0 comments on commit 5a83d85

Please sign in to comment.