Skip to content

Commit 8faf1e8

Browse files
committed
consumererror: Add partial error type
This PR adds functionality for consumers to create a partial error type. This will allow consumers to properly report partial success/failure with failed item counts, which can subsequently be used when reporting sent/failed metrics.
1 parent 14a7832 commit 8faf1e8

File tree

2 files changed

+63
-0
lines changed

2 files changed

+63
-0
lines changed

consumer/consumererror/partial.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// Copyright The OpenTelemetry Authors
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package consumererror // import "go.opentelemetry.io/collector/consumer/consumererror"
5+
6+
import "errors"
7+
8+
type partialError struct {
9+
inner error
10+
failed int
11+
}
12+
13+
var _ error = partialError{}
14+
15+
func (pe partialError) Error() string {
16+
return pe.inner.Error()
17+
}
18+
19+
func (pe partialError) Unwrap() error {
20+
return pe.inner
21+
}
22+
23+
func (pe partialError) Failed() int {
24+
return pe.failed
25+
}
26+
27+
// NewPartial creates a new partial error. This is used by consumers
28+
// to report errors where only a subset of the total items failed
29+
// to be written, but it is not possible to tell which particular items
30+
// failed.
31+
func NewPartial(err error, failed int) error {
32+
return NewPermanent(partialError{
33+
inner: err,
34+
failed: failed,
35+
})
36+
}
37+
38+
// AsPartial checks if an error was wrapped with the NewPartial function,
39+
// or if it contains one such error in its Unwrap() tree.
40+
func AsPartial(err error) (partialError, bool) {
41+
var pe partialError
42+
ok := errors.As(err, &pe)
43+
return pe, ok
44+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package consumererror
2+
3+
import (
4+
"errors"
5+
"testing"
6+
7+
"github.com/stretchr/testify/assert"
8+
)
9+
10+
func TestPartial(t *testing.T) {
11+
internalErr := errors.New("some points failed")
12+
err := NewPartial(internalErr, 5)
13+
assert.True(t, IsPermanent(err))
14+
partialErr, ok := AsPartial(err)
15+
assert.True(t, ok)
16+
assert.Equal(t, 5, partialErr.Failed())
17+
assert.Equal(t, internalErr, partialErr.Unwrap())
18+
assert.Equal(t, internalErr.Error(), partialErr.Error())
19+
}

0 commit comments

Comments
 (0)