Skip to content

Commit

Permalink
Add test to Validate if duplicate metrics are intentional
Browse files Browse the repository at this point in the history
Issue: open-telemetry#26499

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.
  • Loading branch information
mackjmr committed Sep 14, 2023
1 parent ec3f9bf commit ee23533
Showing 1 changed file with 49 additions and 0 deletions.
49 changes: 49 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,48 @@ 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)
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 {
val, exists := seen[metricName]
if receivers, allowed := allowedMetrics[metricName]; allowed {
if contains(receiver, receivers) && contains(val, receivers) {
continue
}
}
assert.False(t, exists, 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 ee23533

Please sign in to comment.