Skip to content

Commit

Permalink
[receiver/discovery] Remove redundant resource attributes (#5409)
Browse files Browse the repository at this point in the history
We don't need to send discovery.receiver.type and discovery.receiver.name along with the metrics. They are meant to be used for discovery evaluation only
  • Loading branch information
dmitryax authored Sep 25, 2024
1 parent 1057aa8 commit 081d9fc
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
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

0 comments on commit 081d9fc

Please sign in to comment.