Skip to content

Commit

Permalink
Add test to Validate if duplicate metrics are intentional (#26687)
Browse files Browse the repository at this point in the history
**Description:**
This test alerts a user if a metric is duplicated across receivers, and
instructs the user to add said metric to an allow list if this is
intentional.

If semantic conventions on the duplicate metric exist, validating is a
matter of ensuring they both fulfill semantic intent. For validating
duplicate metrics for which there is no semantic conventions, additional
discussion is needed with the semantic conventions WG as mentioned in
the issue. For the time being, it will be best judgment.

**Link to tracking Issue:** #26499
  • Loading branch information
mackjmr committed Nov 14, 2023
1 parent 03b0b7f commit 9de22ef
Showing 1 changed file with 50 additions and 0 deletions.
50 changes: 50 additions & 0 deletions cmd/mdatagen/validate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@
package main

import (
"fmt"
"io/fs"
"path/filepath"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -101,3 +105,49 @@ func TestValidate(t *testing.T) {
})
}
}

func TestValidateMetricDuplicates(t *testing.T) {
allowedMetrics := map[string][]string{
"container.cpu.utilization": {"docker_stats", "kubeletstats"},
"container.memory.rss": {"docker_stats", "kubeletstats"},
"container.uptime": {"docker_stats", "kubeletstats"},
}
allMetrics := map[string][]string{}
err := filepath.Walk("../../receiver", func(path string, info fs.FileInfo, err error) error {
if info.Name() == "metadata.yaml" {
md, err := loadMetadata(path)
require.NoError(t, err)
if len(md.Metrics) > 0 {
for metricName := range md.Metrics {
allMetrics[md.Type] = append(allMetrics[md.Type], string(metricName))
}
}
}
return nil
})
require.NoError(t, err)

seen := make(map[string]string)
for receiver, metrics := range allMetrics {
for _, metricName := range metrics {
if val, exists := seen[metricName]; exists {
receivers, allowed := allowedMetrics[metricName]
assert.True(
t,
allowed && contains(receiver, receivers) && contains(val, receivers),
fmt.Sprintf("Duplicate metric %v in receivers %v and %v. Please validate that this is intentional by adding the metric name and receiver types in the allowedMetrics map in this test\n", metricName, receiver, val),
)
}
seen[metricName] = receiver
}
}
}

func contains(r string, rs []string) bool {
for _, s := range rs {
if s == r {
return true
}
}
return false
}

0 comments on commit 9de22ef

Please sign in to comment.