diff --git a/storage/remote/otlptranslator/prometheus/metric_name_builder.go b/storage/remote/otlptranslator/prometheus/metric_name_builder.go index 490c5ea196..e77ddc0d1a 100644 --- a/storage/remote/otlptranslator/prometheus/metric_name_builder.go +++ b/storage/remote/otlptranslator/prometheus/metric_name_builder.go @@ -164,6 +164,9 @@ func normalizeName(metric pmetric.Metric, namespace string) string { // addUnitTokens will add the suffixes to the nameTokens if they are not already present. // It will also remove trailing underscores from the main suffix to avoid double underscores // when joining the tokens. +// +// If the 'per' unit ends with underscore, the underscore will be removed. If the per unit is just +// 'per_', it will be entirely removed. func addUnitTokens(nameTokens []string, mainUnitSuffix, perUnitSuffix string) []string { if slices.Contains(nameTokens, mainUnitSuffix) { mainUnitSuffix = "" @@ -173,6 +176,11 @@ func addUnitTokens(nameTokens []string, mainUnitSuffix, perUnitSuffix string) [] perUnitSuffix = "" } + if perUnitSuffix == "per_" { + perUnitSuffix = "" + } + perUnitSuffix = strings.TrimSuffix(perUnitSuffix, "_") + if perUnitSuffix != "" { mainUnitSuffix = strings.TrimSuffix(mainUnitSuffix, "_") } diff --git a/storage/remote/otlptranslator/prometheus/metric_name_builder_test.go b/storage/remote/otlptranslator/prometheus/metric_name_builder_test.go index 58cee59cf5..d4e2efe398 100644 --- a/storage/remote/otlptranslator/prometheus/metric_name_builder_test.go +++ b/storage/remote/otlptranslator/prometheus/metric_name_builder_test.go @@ -174,6 +174,9 @@ func TestAddUnitTokens(t *testing.T) { {[]string{"token1", "per"}, "main", "per", []string{"token1", "per", "main"}}, {[]string{"token1", "main"}, "main", "per", []string{"token1", "main", "per"}}, {[]string{"token1"}, "main_", "per", []string{"token1", "main", "per"}}, + + {[]string{"token1"}, "main_unit", "per_seconds_", []string{"token1", "main_unit", "per_seconds"}}, // trailing underscores are removed + {[]string{"token1"}, "main_unit", "per_", []string{"token1", "main_unit"}}, // 'per_' is removed enterily } for _, test := range tests {