Skip to content

Commit 36c2f1b

Browse files
authored
semconvkit: add invariant test for histogram-exclusion rule (#8370)
Fixes #8353
1 parent d0b6cbd commit 36c2f1b

1 file changed

Lines changed: 41 additions & 0 deletions

File tree

‎internal/tools/semconvkit/main_test.go‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44
package main
55

66
import (
7+
"bytes"
78
"os"
89
"path/filepath"
10+
"strings"
911
"testing"
1012
)
1113

@@ -42,6 +44,45 @@ func errorType(err error) {}
4244
}
4345
}
4446

47+
// TestNoInvalidObservableHistogramTypes asserts that the semconv code
48+
// generator never emits types containing "HistogramObservable" or
49+
// "ObservableObservable", which would indicate the histogram-exclusion
50+
// rule in metric.go.j2 was accidentally removed or bypassed.
51+
func TestNoInvalidObservableHistogramTypes(t *testing.T) {
52+
t.Parallel()
53+
54+
semconvDir := filepath.Join("..", "..", "..", "semconv")
55+
56+
forbidden := [][]byte{
57+
[]byte("HistogramObservable"),
58+
[]byte("ObservableObservable"),
59+
}
60+
61+
err := filepath.WalkDir(semconvDir, func(path string, d os.DirEntry, err error) error {
62+
if err != nil {
63+
return err
64+
}
65+
if d.IsDir() || filepath.Base(path) != "metric.go" || !strings.HasSuffix(filepath.Base(filepath.Dir(path)), "conv") {
66+
return nil
67+
}
68+
69+
data, err := os.ReadFile(path)
70+
if err != nil {
71+
return err
72+
}
73+
74+
for _, term := range forbidden {
75+
if bytes.Contains(data, term) {
76+
t.Errorf("%s: contains forbidden substring %q", path, term)
77+
}
78+
}
79+
return nil
80+
})
81+
if err != nil {
82+
t.Fatalf("WalkDir(%q) error = %v", semconvDir, err)
83+
}
84+
}
85+
4586
func writeGoFile(t *testing.T, dir, pkg, src string) {
4687
t.Helper()
4788

0 commit comments

Comments
 (0)