From e254c4f326ea07c95ab169c88740861e15aef4f3 Mon Sep 17 00:00:00 2001 From: mackjmr Date: Wed, 12 Jul 2023 11:07:20 +0200 Subject: [PATCH 1/3] [cmd/mdatagen] Add unit test for metric collisions. This PR adds a unit test to check for metric collisions in receivers which have metrics defined. Issue: #23375 --- cmd/mdatagen/validate_test.go | 39 +++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/cmd/mdatagen/validate_test.go b/cmd/mdatagen/validate_test.go index 888e910ccaf4..eab44b08dfab 100644 --- a/cmd/mdatagen/validate_test.go +++ b/cmd/mdatagen/validate_test.go @@ -4,8 +4,12 @@ package main import ( + "fmt" + "io/fs" + "path/filepath" "testing" + "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) @@ -101,3 +105,38 @@ func TestValidate(t *testing.T) { }) } } + +func TestMetricCollision(t *testing.T) { + allMetrics := map[string][]string{} + err := filepath.Walk("../../receiver", func(path string, info fs.FileInfo, err error) error { + if info.Name() == "metadata.yaml" { + // TODO: Remove once https://github.com/open-telemetry/opentelemetry-collector-contrib/pull/24011 is merged. + if path == "../../receiver/googlecloudspannerreceiver/internal/metadataconfig/metadata.yaml" { + return nil + } + md, err := loadMetadata(path) + assert.NoError(t, err) + if len(md.Metrics) > 0 { + for metricName := range md.Metrics { + allMetrics[md.Type] = append(allMetrics[md.Type], string(metricName)) + } + } + } + return nil + }) + assert.NoError(t, err) + + seen := make(map[string]string) + for receiver, metrics := range allMetrics { + for _, metricName := range metrics { + // TODO: Remove one https://github.com/open-telemetry/opentelemetry-collector-contrib/issues/24009 and + // https://github.com/open-telemetry/opentelemetry-collector-contrib/issues/24008 have been addressed. + if metricName == "container.cpu.utilization" || metricName == "container.memory.rss" { + continue + } + val, ok := seen[metricName] + assert.False(t, ok, fmt.Sprintf("Collision for metric %v in receivers %v and %v \n", metricName, receiver, val)) + seen[metricName] = receiver + } + } +} From 6c7a7c7eacd3f007a4a7023e381a176f3be3f096 Mon Sep 17 00:00:00 2001 From: mackjmr Date: Thu, 13 Jul 2023 09:33:14 +0200 Subject: [PATCH 2/3] change ok to exists --- cmd/mdatagen/validate_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cmd/mdatagen/validate_test.go b/cmd/mdatagen/validate_test.go index eab44b08dfab..408c3f2835b8 100644 --- a/cmd/mdatagen/validate_test.go +++ b/cmd/mdatagen/validate_test.go @@ -134,8 +134,8 @@ func TestMetricCollision(t *testing.T) { if metricName == "container.cpu.utilization" || metricName == "container.memory.rss" { continue } - val, ok := seen[metricName] - assert.False(t, ok, fmt.Sprintf("Collision for metric %v in receivers %v and %v \n", metricName, receiver, val)) + val, exists := seen[metricName] + assert.False(t, exists, fmt.Sprintf("Collision for metric %v in receivers %v and %v \n", metricName, receiver, val)) seen[metricName] = receiver } } From a7c078987d5e320f232f5892646c9f39a3f63206 Mon Sep 17 00:00:00 2001 From: mackjmr Date: Wed, 19 Jul 2023 09:55:50 +0200 Subject: [PATCH 3/3] remove TODO --- cmd/mdatagen/validate_test.go | 4 ---- 1 file changed, 4 deletions(-) diff --git a/cmd/mdatagen/validate_test.go b/cmd/mdatagen/validate_test.go index 408c3f2835b8..16de5eb154ce 100644 --- a/cmd/mdatagen/validate_test.go +++ b/cmd/mdatagen/validate_test.go @@ -110,10 +110,6 @@ func TestMetricCollision(t *testing.T) { allMetrics := map[string][]string{} err := filepath.Walk("../../receiver", func(path string, info fs.FileInfo, err error) error { if info.Name() == "metadata.yaml" { - // TODO: Remove once https://github.com/open-telemetry/opentelemetry-collector-contrib/pull/24011 is merged. - if path == "../../receiver/googlecloudspannerreceiver/internal/metadataconfig/metadata.yaml" { - return nil - } md, err := loadMetadata(path) assert.NoError(t, err) if len(md.Metrics) > 0 {