Skip to content

Commit e20fe3c

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 e20fe3c

File tree

3 files changed

+88
-0
lines changed

3 files changed

+88
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Use this changelog template to create an entry for release notes.
2+
3+
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
4+
change_type: enhancement
5+
6+
# The name of the component, or a single word describing the area of concern, (e.g. otlpreceiver)
7+
component: consumererror
8+
9+
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
10+
note: Add new partial error type to consumererror to allow consumers to report partial successes.
11+
12+
# One or more tracking issues or pull requests related to the change
13+
issues: [13423]
14+
15+
# (Optional) One or more lines of additional information to render under the primary note.
16+
# These lines will be padded with 2 spaces and then inserted directly into the document.
17+
# Use pipe (|) for multiline entries.
18+
subtext:
19+
20+
# Optional: The change log or logs in which this entry should be included.
21+
# e.g. '[user]' or '[user, api]'
22+
# Include 'user' if the change is relevant to end users.
23+
# Include 'api' if there is a change to a library API.
24+
# Default: '[user]'
25+
change_logs: []

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)