-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Emit outcome: failure in obsconsumer
#13234
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
mx-psi
merged 16 commits into
open-telemetry:main
from
jade-guiton-dd:obsconsumer-downstream-refused
Jul 25, 2025
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
0fa4e93
Add "downstream" wrapper in obsconsumer, emit "outcome: failure" base…
jade-guiton-dd 09899ba
Move wrapper to consumererror, add comments and changelog
jade-guiton-dd 421cac4
make gotidy
jade-guiton-dd 9961a78
make goporto
jade-guiton-dd 05cd135
Merge branch 'main' into obsconsumer-downstream-refused
jade-guiton-dd c965e04
Add test for "outcome: refused" behavior
jade-guiton-dd debe328
Merge branch 'main' into obsconsumer-downstream-refused
jade-guiton-dd 75e9b5f
make gogenerate
jade-guiton-dd 16496d7
lint
jade-guiton-dd 7d98a32
Add test for downstream wrapper
jade-guiton-dd 6bb8302
lint
jade-guiton-dd b1e986c
Merge branch 'main' into obsconsumer-downstream-refused
codeboten 330009c
Merge branch 'main' into obsconsumer-downstream-refused
mx-psi a64e707
Switch to errors.As logic for IsDownstream + refactor test
jade-guiton-dd c7eedf6
Add API release note for consumererror
jade-guiton-dd 61ba663
Merge branch 'main' into obsconsumer-downstream-refused
jade-guiton-dd File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| # Use this changelog template to create an entry for release notes. | ||
|
|
||
| # One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix' | ||
| change_type: 'enhancement' | ||
|
|
||
| # The name of the component, or a single word describing the area of concern, (e.g. otlpreceiver) | ||
| component: consumererror | ||
|
|
||
| # A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). | ||
| note: Add new "Downstream" error marker | ||
|
|
||
| # One or more tracking issues or pull requests related to the change | ||
| issues: [13234] | ||
|
|
||
| # (Optional) One or more lines of additional information to render under the primary note. | ||
| # These lines will be padded with 2 spaces and then inserted directly into the document. | ||
| # Use pipe (|) for multiline entries. | ||
| subtext: | | ||
| This new error wrapper type indicates that the error returned by a component's | ||
| `Consume` method is not an internal failure of the component, but instead | ||
| was passed through from another component further downstream. | ||
| This is used internally by the new pipeline instrumentation feature to | ||
| determine the `outcome` of a component call. This wrapper is not intended to | ||
| be used by components directly. | ||
|
|
||
| # Optional: The change log or logs in which this entry should be included. | ||
| # e.g. '[user]' or '[user, api]' | ||
| # Include 'user' if the change is relevant to end users. | ||
| # Include 'api' if there is a change to a library API. | ||
| # Default: '[user]' | ||
| change_logs: [api] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| # Use this changelog template to create an entry for release notes. | ||
|
|
||
| # One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix' | ||
| change_type: 'enhancement' | ||
|
|
||
| # The name of the component, or a single word describing the area of concern, (e.g. otlpreceiver) | ||
| component: service | ||
|
|
||
| # A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). | ||
| note: New pipeline instrumentation now differentiates internal failures from downstream errors | ||
|
|
||
| # One or more tracking issues or pull requests related to the change | ||
| issues: [13234] | ||
|
|
||
| # (Optional) One or more lines of additional information to render under the primary note. | ||
| # These lines will be padded with 2 spaces and then inserted directly into the document. | ||
| # Use pipe (|) for multiline entries. | ||
| subtext: | | ||
| With the telemetry.newPipelineTelemetry feature gate enabled, the "received" and "produced" | ||
| metrics related to a component now distinguish between two types of errors: | ||
| - "outcome = failure" indicates that the component returned an internal error; | ||
| - "outcome = refused" indicates that the component successfully emitted data, but returned an | ||
| error coming from a downstream component processing that data. | ||
|
|
||
| # Optional: The change log or logs in which this entry should be included. | ||
| # e.g. '[user]' or '[user, api]' | ||
| # Include 'user' if the change is relevant to end users. | ||
| # Include 'api' if there is a change to a library API. | ||
| # Default: '[user]' | ||
| change_logs: [] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| // Copyright The OpenTelemetry Authors | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| package consumererror // import "go.opentelemetry.io/collector/consumer/consumererror" | ||
|
|
||
| import "errors" | ||
|
|
||
| type downstreamError struct { | ||
| inner error | ||
| } | ||
|
|
||
| var _ error = downstreamError{} | ||
|
|
||
| func (de downstreamError) Error() string { | ||
| return de.inner.Error() | ||
| } | ||
|
|
||
| func (de downstreamError) Unwrap() error { | ||
| return de.inner | ||
| } | ||
|
|
||
| // NewDownstream wraps an error to indicate that it is a downstream error, i.e. an | ||
| // error that does not come from the current component, but from one further downstream. | ||
| // This is used by pipeline instrumentation to determine whether an operation's outcome | ||
| // was an internal failure, or if it successfully produced data that was later refused. | ||
| // This wrapper is not intended to be used manually inside components. | ||
| func NewDownstream(err error) error { | ||
evan-bradley marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return downstreamError{ | ||
| inner: err, | ||
| } | ||
| } | ||
|
|
||
| // IsDownstream checks if an error was wrapped with the NewDownstream function, | ||
evan-bradley marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| // or if it contains one such error in its Unwrap() tree. | ||
| func IsDownstream(err error) bool { | ||
| var de downstreamError | ||
| return errors.As(err, &de) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 consumererror | ||
|
|
||
| import ( | ||
| "errors" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| ) | ||
|
|
||
| //nolint:testifylint // Testing properties of errors, no reason to use require | ||
| func TestDownstream(t *testing.T) { | ||
evan-bradley marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| err1 := errors.New("test error") | ||
| assert.False(t, IsDownstream(err1)) | ||
| err2 := errors.New("test error 2") | ||
| assert.False(t, IsDownstream(err2)) | ||
|
|
||
| errDownstream1 := NewDownstream(err1) | ||
| assert.True(t, IsDownstream(errDownstream1)) | ||
| assert.Equal(t, err1.Error(), errDownstream1.Error()) | ||
| assert.ErrorIs(t, errDownstream1, err1) | ||
| assert.NotErrorIs(t, errDownstream1, err2) | ||
|
|
||
| // we can access downstream wrappers through other wrappers | ||
| errWrapDownstream := NewRetryableError(errDownstream1) | ||
| assert.True(t, IsDownstream(errWrapDownstream)) | ||
| errorStruct := new(Error) | ||
| assert.ErrorAs(t, errWrapDownstream, &errorStruct) | ||
|
|
||
| // we can access other wrappers through downstream wrappers | ||
| errDownstreamWrap := NewDownstream(NewRetryableError(err1)) | ||
| assert.True(t, IsDownstream(errDownstreamWrap)) | ||
| assert.ErrorAs(t, errDownstreamWrap, &errorStruct) | ||
|
|
||
| // downstream + downstream = downstream | ||
| errJoin2 := errors.Join(errDownstream1, NewDownstream(err2)) | ||
| assert.True(t, IsDownstream(errJoin2)) | ||
|
|
||
| // downstream + not downstream = downstream | ||
| errJoin1 := errors.Join(errDownstream1, err2) | ||
| assert.True(t, IsDownstream(errJoin1)) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,198 @@ | ||
| // Copyright The OpenTelemetry Authors | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| package obsconsumer_test | ||
|
|
||
| import ( | ||
| "context" | ||
| "errors" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| "go.opentelemetry.io/otel/attribute" | ||
| sdkmetric "go.opentelemetry.io/otel/sdk/metric" | ||
| "go.opentelemetry.io/otel/sdk/metric/metricdata" | ||
|
|
||
| "go.opentelemetry.io/collector/consumer" | ||
| "go.opentelemetry.io/collector/consumer/consumererror" | ||
| "go.opentelemetry.io/collector/consumer/xconsumer" | ||
| "go.opentelemetry.io/collector/pdata/plog" | ||
| "go.opentelemetry.io/collector/pdata/pmetric" | ||
| "go.opentelemetry.io/collector/pdata/pprofile" | ||
| "go.opentelemetry.io/collector/pdata/ptrace" | ||
| "go.opentelemetry.io/collector/service/internal/obsconsumer" | ||
| ) | ||
|
|
||
| type failingConsumer struct { | ||
| err error | ||
| } | ||
|
|
||
| var ( | ||
| _ consumer.Metrics = (*failingConsumer)(nil) | ||
| _ consumer.Logs = (*failingConsumer)(nil) | ||
| _ consumer.Traces = (*failingConsumer)(nil) | ||
| _ xconsumer.Profiles = (*failingConsumer)(nil) | ||
| ) | ||
|
|
||
| func (*failingConsumer) Capabilities() consumer.Capabilities { | ||
| return consumer.Capabilities{} | ||
| } | ||
|
|
||
| func (fc *failingConsumer) ConsumeMetrics(_ context.Context, _ pmetric.Metrics) error { | ||
| return fc.err | ||
| } | ||
|
|
||
| func (fc *failingConsumer) ConsumeLogs(_ context.Context, _ plog.Logs) error { | ||
| return fc.err | ||
| } | ||
|
|
||
| func (fc *failingConsumer) ConsumeTraces(_ context.Context, _ ptrace.Traces) error { | ||
| return fc.err | ||
| } | ||
|
|
||
| func (fc *failingConsumer) ConsumeProfiles(_ context.Context, _ pprofile.Profiles) error { | ||
| return fc.err | ||
| } | ||
|
|
||
| func TestConsumeRefused(t *testing.T) { | ||
| setGateForTest(t, true) | ||
|
|
||
| ctx := context.Background() | ||
| originalErr := errors.New("test error") | ||
| expectedErr := consumererror.NewDownstream(originalErr) | ||
| mockConsumer := &failingConsumer{err: originalErr} | ||
|
|
||
| // Use delta temporality so sums don't accumulate across tests | ||
| reader := sdkmetric.NewManualReader(sdkmetric.WithTemporalitySelector(func(_ sdkmetric.InstrumentKind) metricdata.Temporality { | ||
| return metricdata.DeltaTemporality | ||
| })) | ||
| mp := sdkmetric.NewMeterProvider(sdkmetric.WithReader(reader)) | ||
| meter := mp.Meter("test") | ||
|
|
||
| receivedItemsCounter, err := meter.Int64Counter("received.items") | ||
| require.NoError(t, err) | ||
| receivedSizeCounter, err := meter.Int64Counter("received.size") | ||
| require.NoError(t, err) | ||
|
|
||
| producedItemsCounter, err := meter.Int64Counter("produced.items") | ||
| require.NoError(t, err) | ||
| producedSizeConter, err := meter.Int64Counter("produced.size") | ||
| require.NoError(t, err) | ||
|
|
||
| type testCase struct { | ||
| name string | ||
| testConsumer func() error | ||
| } | ||
|
|
||
| testCases := []testCase{ | ||
| { | ||
| name: "metrics", | ||
| testConsumer: func() error { | ||
| consumer1 := obsconsumer.NewMetrics(mockConsumer, receivedItemsCounter, receivedSizeCounter) | ||
| consumer2 := obsconsumer.NewMetrics(consumer1, producedItemsCounter, producedSizeConter) | ||
| md := pmetric.NewMetrics() | ||
| md.ResourceMetrics().AppendEmpty().ScopeMetrics().AppendEmpty().Metrics().AppendEmpty().SetEmptyGauge().DataPoints().AppendEmpty() | ||
| return consumer2.ConsumeMetrics(ctx, md) | ||
| }, | ||
| }, | ||
| { | ||
| name: "logs", | ||
| testConsumer: func() error { | ||
| consumer1 := obsconsumer.NewLogs(mockConsumer, receivedItemsCounter, receivedSizeCounter) | ||
| consumer2 := obsconsumer.NewLogs(consumer1, producedItemsCounter, producedSizeConter) | ||
| ld := plog.NewLogs() | ||
| ld.ResourceLogs().AppendEmpty().ScopeLogs().AppendEmpty().LogRecords().AppendEmpty() | ||
| return consumer2.ConsumeLogs(ctx, ld) | ||
| }, | ||
| }, | ||
| { | ||
| name: "traces", | ||
| testConsumer: func() error { | ||
| consumer1 := obsconsumer.NewTraces(mockConsumer, receivedItemsCounter, receivedSizeCounter) | ||
| consumer2 := obsconsumer.NewTraces(consumer1, producedItemsCounter, producedSizeConter) | ||
| td := ptrace.NewTraces() | ||
| td.ResourceSpans().AppendEmpty().ScopeSpans().AppendEmpty().Spans().AppendEmpty() | ||
| return consumer2.ConsumeTraces(ctx, td) | ||
| }, | ||
| }, | ||
| { | ||
| name: "profiles", | ||
| testConsumer: func() error { | ||
| consumer1 := obsconsumer.NewProfiles(mockConsumer, receivedItemsCounter, receivedSizeCounter) | ||
| consumer2 := obsconsumer.NewProfiles(consumer1, producedItemsCounter, producedSizeConter) | ||
| pd := pprofile.NewProfiles() | ||
| pd.ResourceProfiles().AppendEmpty().ScopeProfiles().AppendEmpty().Profiles().AppendEmpty().Sample().AppendEmpty() | ||
| return consumer2.ConsumeProfiles(ctx, pd) | ||
| }, | ||
| }, | ||
| } | ||
|
|
||
| for _, tc := range testCases { | ||
| t.Run(tc.name, func(t *testing.T) { | ||
| err := tc.testConsumer() | ||
| assert.Equal(t, expectedErr, err) | ||
|
|
||
| var rm metricdata.ResourceMetrics | ||
| err = reader.Collect(ctx, &rm) | ||
| require.NoError(t, err) | ||
| require.Len(t, rm.ScopeMetrics, 1) | ||
| require.Len(t, rm.ScopeMetrics[0].Metrics, 4) | ||
|
|
||
| var receivedItemMetric, receivedSizeMetric metricdata.Metrics | ||
| var producedItemMetric, producedSizeMetric metricdata.Metrics | ||
| for _, m := range rm.ScopeMetrics[0].Metrics { | ||
| switch m.Name { | ||
| case "received.items": | ||
| receivedItemMetric = m | ||
| case "received.size": | ||
| receivedSizeMetric = m | ||
| case "produced.items": | ||
| producedItemMetric = m | ||
| case "produced.size": | ||
| producedSizeMetric = m | ||
| } | ||
| } | ||
| require.NotNil(t, receivedItemMetric) | ||
| require.NotNil(t, receivedSizeMetric) | ||
| require.NotNil(t, producedItemMetric) | ||
| require.NotNil(t, producedSizeMetric) | ||
|
|
||
| data := receivedItemMetric.Data.(metricdata.Sum[int64]) | ||
| require.Len(t, data.DataPoints, 1) | ||
| require.Equal(t, int64(1), data.DataPoints[0].Value) | ||
| attrs := data.DataPoints[0].Attributes | ||
| require.Equal(t, 1, attrs.Len()) | ||
| val, ok := attrs.Value(attribute.Key(obsconsumer.ComponentOutcome)) | ||
| require.True(t, ok) | ||
| require.Equal(t, "failure", val.Emit()) | ||
|
|
||
| data = receivedSizeMetric.Data.(metricdata.Sum[int64]) | ||
| require.Len(t, data.DataPoints, 1) | ||
| require.Positive(t, data.DataPoints[0].Value) | ||
| attrs = data.DataPoints[0].Attributes | ||
| require.Equal(t, 1, attrs.Len()) | ||
| val, ok = attrs.Value(attribute.Key(obsconsumer.ComponentOutcome)) | ||
| require.True(t, ok) | ||
| require.Equal(t, "failure", val.Emit()) | ||
|
|
||
| data = producedItemMetric.Data.(metricdata.Sum[int64]) | ||
| require.Len(t, data.DataPoints, 1) | ||
| require.Equal(t, int64(1), data.DataPoints[0].Value) | ||
| attrs = data.DataPoints[0].Attributes | ||
| require.Equal(t, 1, attrs.Len()) | ||
| val, ok = attrs.Value(attribute.Key(obsconsumer.ComponentOutcome)) | ||
| require.True(t, ok) | ||
| require.Equal(t, "refused", val.Emit()) | ||
|
|
||
| data = producedSizeMetric.Data.(metricdata.Sum[int64]) | ||
| require.Len(t, data.DataPoints, 1) | ||
| require.Positive(t, data.DataPoints[0].Value) | ||
| attrs = data.DataPoints[0].Attributes | ||
| require.Equal(t, 1, attrs.Len()) | ||
| val, ok = attrs.Value(attribute.Key(obsconsumer.ComponentOutcome)) | ||
| require.True(t, ok) | ||
| require.Equal(t, "refused", val.Emit()) | ||
| }) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.