Skip to content

Commit 933c0ea

Browse files
committed
add testable examples and more detailed godoc
1 parent fbbad1d commit 933c0ea

File tree

2 files changed

+55
-8
lines changed

2 files changed

+55
-8
lines changed

consumer/consumererror/xconsumererror/partial.go

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,20 @@ func (pe partialError) Failed() int {
2828
return pe.failed
2929
}
3030

31-
// NewPartial creates a new partial error. This is used by consumers
32-
// to report errors where only a subset of the total items failed
33-
// to be written, but it is not possible to tell which particular items
34-
// failed.
31+
// NewPartial creates a new partial error. This is used to report errors
32+
// where only a subset of the total items failed to be written, but it
33+
// is not possible to tell which particular items failed. An example of this
34+
// would be a backend that can report partial successes, but only communicate
35+
// the number of failed items without specifying which specific items failed.
36+
//
37+
// A partial error wraps a PermanentError; it can be treated as any other permanent
38+
// error with no changes, meaning that consumers can transition to producing this
39+
// error when appropriate without breaking any parts of the pipeline that check for
40+
// permanent errors.
41+
//
42+
// In a scenario where the exact items that failed are known and can be retried,
43+
// it's recommended to use the respective signal error ([consumererror.Logs],
44+
// [consumererror.Metrics], or [consumererror.Traces]).
3545
func NewPartial(err error, failed int) error {
3646
return consumererror.NewPermanent(partialError{
3747
inner: err,
@@ -40,7 +50,9 @@ func NewPartial(err error, failed int) error {
4050
}
4151

4252
// AsPartial checks if an error was wrapped with the NewPartial function,
43-
// or if it contains one such error in its Unwrap() tree.
53+
// or if it contains one such error in its Unwrap() tree. The resulting
54+
// error has a method Failed() that can be used to retrieve the count of
55+
// failed items from the error.
4456
func AsPartial(err error) (partialError, bool) {
4557
var pe partialError
4658
ok := errors.As(err, &pe)
Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,58 @@
11
// Copyright The OpenTelemetry Authors
22
// SPDX-License-Identifier: Apache-2.0
33

4-
package xconsumererror
4+
package xconsumererror_test
55

66
import (
77
"errors"
8+
"fmt"
89
"testing"
910

1011
"github.com/stretchr/testify/assert"
1112
"go.opentelemetry.io/collector/consumer/consumererror"
13+
"go.opentelemetry.io/collector/consumer/consumererror/xconsumererror"
1214
)
1315

1416
func TestPartial(t *testing.T) {
1517
internalErr := errors.New("some points failed")
16-
err := NewPartial(internalErr, 5)
18+
err := xconsumererror.NewPartial(internalErr, 5)
1719
assert.True(t, consumererror.IsPermanent(err))
18-
partialErr, ok := AsPartial(err)
20+
partialErr, ok := xconsumererror.AsPartial(err)
1921
assert.True(t, ok)
2022
assert.Equal(t, 5, partialErr.Failed())
2123
assert.Equal(t, internalErr, partialErr.Unwrap())
2224
assert.Equal(t, internalErr.Error(), partialErr.Error())
2325
}
26+
27+
func ExampleNewPartial() {
28+
// Produce a partial error.
29+
failedItems := 5
30+
consumptionErr := errors.New("some points failed to be written")
31+
err := xconsumererror.NewPartial(consumptionErr, failedItems)
32+
fmt.Println(err)
33+
34+
// Output: "Permanent error: some points failed to be written"
35+
}
36+
37+
func ExampleAsPartial() {
38+
// Produce a partial error.
39+
partialErr := xconsumererror.NewPartial(errors.New("some points failed to be written"), 10)
40+
41+
// The result of `xconsumererror.AsPartial` has a method `Failed`
42+
// which can be used to retrieve the failed item count.
43+
if err, ok := xconsumererror.AsPartial(partialErr); ok {
44+
fmt.Println(err.Failed())
45+
}
46+
47+
// Output: 10
48+
}
49+
50+
func ExampleAsPartial_second() {
51+
// Produce a partial error.
52+
partialErr := xconsumererror.NewPartial(errors.New("some points failed to be written"), 10)
53+
54+
// Partial errors wrap a Permanent error.
55+
fmt.Println(consumererror.IsPermanent(partialErr))
56+
57+
// Output: true
58+
}

0 commit comments

Comments
 (0)