Skip to content
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

[receiver/discovery] Remove redundant resource attributes #5409

Merged
merged 1 commit into from
Sep 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion internal/receiver/discoveryreceiver/metric_evaluator.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func (m *metricsConsumer) Capabilities() consumer.Capabilities {
func (m *metricsConsumer) ConsumeMetrics(ctx context.Context, md pmetric.Metrics) error {
m.evaluateMetrics(md)
if m.nextConsumer != nil {
return m.nextConsumer.ConsumeMetrics(ctx, md)
return m.nextConsumer.ConsumeMetrics(ctx, cleanupMetrics(md))
}
return nil
}
Expand Down Expand Up @@ -154,3 +154,13 @@ func (m *metricsConsumer) findMatchedMetric(md pmetric.Metrics, match Match, rec
}
return pcommon.NewResource(), false
}

// cleanupMetrics removes resource attributes used for status correlation.
func cleanupMetrics(md pmetric.Metrics) pmetric.Metrics {
for i := 0; i < md.ResourceMetrics().Len(); i++ {
attrs := md.ResourceMetrics().At(i).Resource().Attributes()
attrs.Remove(discovery.ReceiverTypeAttr)
attrs.Remove(discovery.ReceiverNameAttr)
}
return md
}
20 changes: 17 additions & 3 deletions internal/receiver/discoveryreceiver/metric_evaluator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,11 @@ import (
"time"

"github.com/open-telemetry/opentelemetry-collector-contrib/extension/observer"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/consumer"
"go.opentelemetry.io/collector/consumer/consumertest"
"go.opentelemetry.io/collector/pdata/pcommon"
"go.opentelemetry.io/collector/pdata/pmetric"
"go.uber.org/zap"
Expand All @@ -43,7 +45,7 @@ func TestMetricEvaluatorBaseMetricConsumer(t *testing.T) {
require.NoError(t, me.ConsumeMetrics(context.Background(), md))
}

func TestMetricEvaluation(t *testing.T) {
func TestConsumeMetrics(t *testing.T) {
// If debugging tests, replace the Nop Logger with a test instance to see
// all statements. Not in regular use to avoid spamming output.
// logger := zaptest.NewLogger(t)
Expand Down Expand Up @@ -88,7 +90,8 @@ func TestMetricEvaluation(t *testing.T) {
endpointID := observer.EndpointID("endpoint.id")
cStore.UpdateEndpoint(observer.Endpoint{ID: endpointID}, receiverID, observerID)

me := newMetricsConsumer(logger, cfg, cStore, nil)
ms := &consumertest.MetricsSink{}
me := newMetricsConsumer(logger, cfg, cStore, ms)

expectedRes := pcommon.NewResource()
expectedRes.Attributes().PutStr("discovery.receiver.type", "a_receiver")
Expand Down Expand Up @@ -116,7 +119,7 @@ func TestMetricEvaluation(t *testing.T) {
sms.AppendEmpty().SetName("desired.name")
sms.AppendEmpty().SetName("desired.name")

me.evaluateMetrics(md)
require.NoError(t, me.ConsumeMetrics(context.Background(), md))

// wait for the emit channel to be processed
emitWG.Wait()
Expand All @@ -129,6 +132,17 @@ func TestMetricEvaluation(t *testing.T) {
"discovery.message": "desired body content",
"extra_attr": "target_resource",
}, cStore.Attrs(endpointID))

assert.Equal(t, 1, len(ms.AllMetrics()))
assert.Equal(t, 2, ms.AllMetrics()[0].ResourceMetrics().Len())
// Ensure redundant attributes are not added
for i := 0; i < ms.AllMetrics()[0].ResourceMetrics().Len(); i++ {
attrs := ms.AllMetrics()[0].ResourceMetrics().At(i).Resource().Attributes()
_, ok := attrs.Get(discovery.ReceiverTypeAttr)
assert.False(t, ok)
_, ok = attrs.Get(discovery.ReceiverNameAttr)
assert.False(t, ok)
}
})
}
})
Expand Down
Loading