Skip to content

Commit

Permalink
Provide conversion methods to consumererror signal error types.
Browse files Browse the repository at this point in the history
This moves the accessors for signal data to methods on the individual error types
and provides As<Signal>() package functions that behave as targeted versions of
the errors.As() function.
  • Loading branch information
Aneurysm9 committed Mar 23, 2021
1 parent b6392d9 commit 76ccbe6
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 119 deletions.
57 changes: 21 additions & 36 deletions consumer/consumererror/signalerrors.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,21 +35,18 @@ func NewTraces(err error, failed pdata.Traces) error {
}
}

// IsTraces checks if an error includes a Traces.
func IsTraces(err error) bool {
// AsTraces finds the first error in err's chain that can be assigned to target. If such an error is found
// it is assigned to target and true is returned, otherwise false is returned.
func AsTraces(err error, target *Traces) bool {
if err == nil {
return false
}
return errors.As(err, &Traces{})
return errors.As(err, target)
}

// GetTraces returns failed traces from the provided error.
func GetTraces(err error) pdata.Traces {
var res pdata.Traces
if traceError, ok := err.(Traces); ok {
res = traceError.failed
}
return res
// GetTraces returns failed traces from the associated error.
func (err Traces) GetTraces() pdata.Traces {
return err.failed
}

// Logs is an error that may carry associated Log data for a subset of received data
Expand All @@ -67,21 +64,18 @@ func NewLogs(err error, failed pdata.Logs) error {
}
}

// IsLogs checks if an error includes a Logs.
func IsLogs(err error) bool {
// AsLogs finds the first error in err's chain that can be assigned to target. If such an error is found
// it is assigned to target and true is returned, otherwise false is returned.
func AsLogs(err error, target *Logs) bool {
if err == nil {
return false
}
return errors.As(err, &Logs{})
return errors.As(err, target)
}

// GetLogs returns failed logs from the provided error.
func GetLogs(err error) pdata.Logs {
var res pdata.Logs
if logError, ok := err.(Logs); ok {
res = logError.failed
}
return res
// GetLogs returns failed logs from the associated error.
func (err Logs) GetLogs() pdata.Logs {
return err.failed
}

// Metrics is an error that may carry associated Metrics data for a subset of received data
Expand All @@ -99,25 +93,16 @@ func NewMetrics(err error, failed pdata.Metrics) error {
}
}

// IsMetrics checks if an error includes a Metrics.
func IsMetrics(err error) bool {
// AsMetrics finds the first error in err's chain that can be assigned to target. If such an error is found
// it is assigned to target and true is returned, otherwise false is returned.
func AsMetrics(err error, target *Metrics) bool {
if err == nil {
return false
}
return errors.As(err, &Metrics{})
}

// GetMetrics returns failed metrics from the provided error.
func GetMetrics(err error) pdata.Metrics {
var res pdata.Metrics
if metricError, ok := err.(Metrics); ok {
res = metricError.failed
}
return res
return errors.As(err, target)
}

// IsPartial is a convenience for testing whether an error is any of the consumererror types
// that may convey information about partial processing failures.
func IsPartial(err error) bool {
return IsTraces(err) || IsMetrics(err) || IsLogs(err)
// GetMetrics returns failed metrics from the associated error.
func (err Metrics) GetMetrics() pdata.Metrics {
return err.failed
}
95 changes: 15 additions & 80 deletions consumer/consumererror/signalerrors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,99 +27,34 @@ func TestTraces(t *testing.T) {
td := testdata.GenerateTraceDataOneSpan()
err := fmt.Errorf("some error")
traceErr := NewTraces(err, td)
assert.True(t, IsTraces(traceErr))
assert.Equal(t, err.Error(), traceErr.Error())
assert.Equal(t, td, GetTraces(traceErr))
target := &Traces{}
assert.False(t, AsTraces(nil, target))
assert.False(t, AsTraces(err, target))
assert.True(t, AsTraces(traceErr, target))
assert.Equal(t, td, target.GetTraces())
}

func TestLogs(t *testing.T) {
td := testdata.GenerateLogDataOneLog()
err := fmt.Errorf("some error")
logsErr := NewLogs(err, td)
assert.True(t, IsLogs(logsErr))
assert.Equal(t, err.Error(), logsErr.Error())
assert.Equal(t, td, GetLogs(logsErr))
target := &Logs{}
assert.False(t, AsLogs(nil, target))
assert.False(t, AsLogs(err, target))
assert.True(t, AsLogs(logsErr, target))
assert.Equal(t, td, target.GetLogs())
}

func TestMetrics(t *testing.T) {
td := testdata.GenerateMetricsOneMetric()
err := fmt.Errorf("some error")
metricErr := NewMetrics(err, td)
assert.True(t, IsMetrics(metricErr))
assert.Equal(t, err.Error(), metricErr.Error())
assert.Equal(t, td, GetMetrics(metricErr))
}

func TestPredicates(t *testing.T) {
for _, tc := range []struct {
name string
expected bool
err error
predicate func(error) bool
}{
{
name: "IsNotTraces(nil)",
expected: false,
err: nil,
predicate: IsTraces,
},
{
name: "IsNotTraces",
expected: false,
err: fmt.Errorf("some error"),
predicate: IsTraces,
},
{
name: "IsTraces",
expected: true,
err: NewTraces(fmt.Errorf("some error"), testdata.GenerateTraceDataOneSpan()),
predicate: IsTraces,
},
{
name: "IsNotMetrics(nil)",
expected: false,
err: nil,
predicate: IsMetrics,
},
{
name: "IsNotMetrics",
expected: false,
err: fmt.Errorf("some error"),
predicate: IsMetrics,
},
{
name: "IsMetrics",
expected: true,
err: NewMetrics(fmt.Errorf("some error"), testdata.GenerateMetricsOneMetric()),
predicate: IsMetrics,
},
{
name: "IsNotLogs(nil)",
expected: false,
err: nil,
predicate: IsLogs,
},
{
name: "IsNotLogs",
expected: false,
err: fmt.Errorf("some error"),
predicate: IsLogs,
},
{
name: "IsLogs",
expected: true,
err: NewLogs(fmt.Errorf("some error"), testdata.GenerateLogDataOneLog()),
predicate: IsLogs,
},
{
name: "IsPartial",
expected: true,
err: NewLogs(fmt.Errorf("some error"), testdata.GenerateLogDataOneLog()),
predicate: IsPartial,
},
} {
t.Run(tc.name, func(t *testing.T) {
assert.Equal(t, tc.expected, tc.predicate(tc.err))
})
}
target := &Metrics{}
assert.False(t, AsMetrics(nil, target))
assert.False(t, AsMetrics(err, target))
assert.True(t, AsMetrics(metricErr, target))
assert.Equal(t, td, target.GetMetrics())
}
6 changes: 5 additions & 1 deletion exporter/exporterhelper/logshelper.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,11 @@ func newLogsRequest(ctx context.Context, ld pdata.Logs, pusher PushLogs) request
}

func (req *logsRequest) onError(err error) request {
return newLogsRequest(req.ctx, consumererror.GetLogs(err), req.pusher)
logError := &consumererror.Logs{}
if consumererror.AsLogs(err, logError) {
return newLogsRequest(req.ctx, logError.GetLogs(), req.pusher)
}
return req
}

func (req *logsRequest) export(ctx context.Context) error {
Expand Down
6 changes: 5 additions & 1 deletion exporter/exporterhelper/metricshelper.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,11 @@ func newMetricsRequest(ctx context.Context, md pdata.Metrics, pusher PushMetrics
}

func (req *metricsRequest) onError(err error) request {
return newMetricsRequest(req.ctx, consumererror.GetMetrics(err), req.pusher)
metricsError := &consumererror.Metrics{}
if consumererror.AsMetrics(err, metricsError) {
return newMetricsRequest(req.ctx, metricsError.GetMetrics(), req.pusher)
}
return req
}

func (req *metricsRequest) export(ctx context.Context) error {
Expand Down
6 changes: 5 additions & 1 deletion exporter/exporterhelper/tracehelper.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,11 @@ func newTracesRequest(ctx context.Context, td pdata.Traces, pusher PushTraces) r
}

func (req *tracesRequest) onError(err error) request {
return newTracesRequest(req.ctx, consumererror.GetTraces(err), req.pusher)
traceError := &consumererror.Traces{}
if consumererror.AsTraces(err, traceError) {
return newTracesRequest(req.ctx, traceError.GetTraces(), req.pusher)
}
return req
}

func (req *tracesRequest) export(ctx context.Context) error {
Expand Down

0 comments on commit 76ccbe6

Please sign in to comment.