VictoriaMetrics/lib/protoparser/opentelemetry/stream/sanitize_test.go
Aliaksandr Valialkin bb9bb600b3
lib/protoparser/opentelemetry: follow-up after 47892b4a4c
- Rename -opentelemetry.sanitizeMetrics command-line flag to more clear -opentelemetry.usePrometheusNaming
- Clarify the description of the change at docs/CHANGELOG.md
- Rename promrelabel.SanitizeLabelNameParts to more clear promrelabel.SplitMetricNameToTokens
- Properly split metric names at '_' char in promerlabel.SplitMetricNameToTokens.
- Add tests for various edge cases for Prometheus metric names' normalization
  according to the code at b865505850/pkg/translator/prometheus/normalize_name.go
- Extract the code responsible for Prometheus metric names' normalization into a separate file (santize.go)

Updates https://github.com/VictoriaMetrics/VictoriaMetrics/issues/6037
Updates https://github.com/VictoriaMetrics/VictoriaMetrics/pull/6035
2024-04-03 03:09:52 +03:00

127 lines
2.1 KiB
Go

package stream
import (
"testing"
"github.com/VictoriaMetrics/VictoriaMetrics/lib/protoparser/opentelemetry/pb"
)
func TestSanitizePrometheusLabelName(t *testing.T) {
f := func(labelName, expectedResult string) {
t.Helper()
result := sanitizePrometheusLabelName(labelName)
if result != expectedResult {
t.Fatalf("unexpected result; got %q; want %q", result, expectedResult)
}
}
f("", "")
f("foo", "foo")
f("foo_bar/baz:abc", "foo_bar_baz_abc")
f("1foo", "key_1foo")
f("_foo", "key_foo")
f("__bar", "__bar")
}
func TestSanitizePrometheusMetricName(t *testing.T) {
f := func(m *pb.Metric, expectedResult string) {
t.Helper()
result := sanitizePrometheusMetricName(m)
if result != expectedResult {
t.Fatalf("unexpected result; got %q; want %q", result, expectedResult)
}
}
f(&pb.Metric{}, "")
f(&pb.Metric{
Name: "foo",
}, "foo")
f(&pb.Metric{
Name: "foo",
Unit: "s",
}, "foo_seconds")
f(&pb.Metric{
Name: "foo_seconds",
Unit: "s",
}, "foo_seconds")
f(&pb.Metric{
Name: "foo",
Sum: &pb.Sum{
IsMonotonic: true,
},
}, "foo_total")
f(&pb.Metric{
Name: "foo_total",
Sum: &pb.Sum{
IsMonotonic: true,
},
}, "foo_total")
f(&pb.Metric{
Name: "foo",
Sum: &pb.Sum{
IsMonotonic: true,
},
Unit: "s",
}, "foo_seconds_total")
f(&pb.Metric{
Name: "foo_seconds",
Sum: &pb.Sum{
IsMonotonic: true,
},
Unit: "s",
}, "foo_seconds_total")
f(&pb.Metric{
Name: "foo_total",
Sum: &pb.Sum{
IsMonotonic: true,
},
Unit: "s",
}, "foo_seconds_total")
f(&pb.Metric{
Name: "foo_seconds_total",
Sum: &pb.Sum{
IsMonotonic: true,
},
Unit: "s",
}, "foo_seconds_total")
f(&pb.Metric{
Name: "foo_total_seconds",
Sum: &pb.Sum{
IsMonotonic: true,
},
Unit: "s",
}, "foo_seconds_total")
f(&pb.Metric{
Name: "foo",
Gauge: &pb.Gauge{},
Unit: "1",
}, "foo_ratio")
f(&pb.Metric{
Name: "foo",
Unit: "m/s",
}, "foo_meters_per_second")
f(&pb.Metric{
Name: "foo_second",
Unit: "m/s",
}, "foo_second_meters")
f(&pb.Metric{
Name: "foo_meters",
Unit: "m/s",
}, "foo_meters_per_second")
}