Files
VictoriaMetrics/lib/protoparser/opentelemetry/stream/sanitize_test.go
andriibeee 85e0253569 lib/protoparser: add flag to allow OpenTelemetry underscore labels to pass through without being prefixed (#10475)
Add `-opentelemetry.labelNameUnderscoreSanitization` command-line flag to control whether to enable prepending of `key` to labels starting with `_` when `-opentelemetry.usePrometheusNaming` is enabled. The labels starting with `__` are not modified.

Fixes https://github.com/VictoriaMetrics/VictoriaMetrics/issues/9663

Signed-off-by: andriibeee <154226341+andriibeee@users.noreply.github.com>
Co-authored-by: Max Kotliar <mkotlyar@victoriametrics.com>
2026-05-12 15:17:36 +03:00

74 lines
2.2 KiB
Go

package stream
import (
"testing"
"github.com/VictoriaMetrics/VictoriaMetrics/lib/prompb"
"github.com/VictoriaMetrics/VictoriaMetrics/lib/protoparser/opentelemetry/pb"
)
func TestSanitizePrometheusLabelName(t *testing.T) {
f := func(labelName, expectedResult string) {
t.Helper()
var sctx sanitizerContext
result := sctx.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")
prev := *labelNameUnderscoreSanitization
*labelNameUnderscoreSanitization = false
defer func() {
*labelNameUnderscoreSanitization = prev
}()
f("", "")
f("foo", "foo")
f("foo_bar", "foo_bar")
f("1foo", "key_1foo")
f("_foo", "_foo")
f("__bar", "__bar")
}
func TestSanitizePrometheusMetricName(t *testing.T) {
f := func(metricName, unit string, metricType prompb.MetricType, expectedResult string) {
t.Helper()
var sctx sanitizerContext
mm := pb.MetricMetadata{
Name: metricName,
Unit: unit,
Type: metricType,
}
result := sctx.sanitizePrometheusMetricName(&mm)
if result != expectedResult {
t.Fatalf("unexpected result; got %q; want %q", result, expectedResult)
}
}
f("", "", prompb.MetricTypeUnknown, "")
f("foo", "", prompb.MetricTypeUnknown, "foo")
f("foo", "s", prompb.MetricTypeUnknown, "foo_seconds")
f("foo_seconds", "s", prompb.MetricTypeUnknown, "foo_seconds")
f("foo", "", prompb.MetricTypeCounter, "foo_total")
f("foo_total", "", prompb.MetricTypeCounter, "foo_total")
f("foo", "s", prompb.MetricTypeCounter, "foo_seconds_total")
f("foo_seconds", "s", prompb.MetricTypeCounter, "foo_seconds_total")
f("foo_total", "s", prompb.MetricTypeCounter, "foo_seconds_total")
f("foo_seconds_total", "s", prompb.MetricTypeCounter, "foo_seconds_total")
f("foo_total_seconds", "s", prompb.MetricTypeCounter, "foo_seconds_total")
f("foo", "1", prompb.MetricTypeGauge, "foo_ratio")
f("foo", "m/s", prompb.MetricTypeUnknown, "foo_meters_per_second")
f("foo_second", "m/s", prompb.MetricTypeUnknown, "foo_second_meters")
f("foo_meters", "m/s", prompb.MetricTypeUnknown, "foo_meters_per_second")
}