mirror of
https://github.com/prometheus/prometheus.git
synced 2024-12-25 21:54:10 -08:00
Merge pull request #15537 from aknuds1/arve/clarify-utf8-labels
OTLP receiver: Don't call NormalizeLabel in `NoUTF8EscapingWithSuffixes` mode
This commit is contained in:
commit
a3a88c793d
|
@ -29,15 +29,15 @@ import (
|
||||||
//
|
//
|
||||||
// Labels that start with non-letter rune will be prefixed with "key_".
|
// Labels that start with non-letter rune will be prefixed with "key_".
|
||||||
// An exception is made for double-underscores which are allowed.
|
// An exception is made for double-underscores which are allowed.
|
||||||
func NormalizeLabel(label string, allowUTF8 bool) string {
|
func NormalizeLabel(label string) string {
|
||||||
// Trivial case
|
// Trivial case.
|
||||||
if len(label) == 0 || allowUTF8 {
|
if len(label) == 0 {
|
||||||
return label
|
return label
|
||||||
}
|
}
|
||||||
|
|
||||||
label = strutil.SanitizeLabelName(label)
|
label = strutil.SanitizeLabelName(label)
|
||||||
|
|
||||||
// If label starts with a number, prepend with "key_"
|
// If label starts with a number, prepend with "key_".
|
||||||
if unicode.IsDigit(rune(label[0])) {
|
if unicode.IsDigit(rune(label[0])) {
|
||||||
label = "key_" + label
|
label = "key_" + label
|
||||||
} else if strings.HasPrefix(label, "_") && !strings.HasPrefix(label, "__") {
|
} else if strings.HasPrefix(label, "_") && !strings.HasPrefix(label, "__") {
|
||||||
|
|
|
@ -22,27 +22,24 @@ import (
|
||||||
|
|
||||||
func TestNormalizeLabel(t *testing.T) {
|
func TestNormalizeLabel(t *testing.T) {
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
label string
|
label string
|
||||||
expected string
|
expected string
|
||||||
expectedUTF8 string
|
|
||||||
}{
|
}{
|
||||||
{"", "", ""},
|
{"", ""},
|
||||||
{"label:with:colons", "label_with_colons", "label:with:colons"}, // Without UTF-8 support, colons are only allowed in metric names
|
{"label:with:colons", "label_with_colons"},
|
||||||
{"LabelWithCapitalLetters", "LabelWithCapitalLetters", "LabelWithCapitalLetters"},
|
{"LabelWithCapitalLetters", "LabelWithCapitalLetters"},
|
||||||
{"label!with&special$chars)", "label_with_special_chars_", "label!with&special$chars)"},
|
{"label!with&special$chars)", "label_with_special_chars_"},
|
||||||
{"label_with_foreign_characters_字符", "label_with_foreign_characters___", "label_with_foreign_characters_字符"},
|
{"label_with_foreign_characters_字符", "label_with_foreign_characters___"},
|
||||||
{"label.with.dots", "label_with_dots", "label.with.dots"},
|
{"label.with.dots", "label_with_dots"},
|
||||||
{"123label", "key_123label", "123label"},
|
{"123label", "key_123label"},
|
||||||
{"_label_starting_with_underscore", "key_label_starting_with_underscore", "_label_starting_with_underscore"},
|
{"_label_starting_with_underscore", "key_label_starting_with_underscore"},
|
||||||
{"__label_starting_with_2underscores", "__label_starting_with_2underscores", "__label_starting_with_2underscores"},
|
{"__label_starting_with_2underscores", "__label_starting_with_2underscores"},
|
||||||
}
|
}
|
||||||
|
|
||||||
for i, test := range tests {
|
for i, test := range tests {
|
||||||
t.Run(fmt.Sprintf("test_%d", i), func(t *testing.T) {
|
t.Run(fmt.Sprintf("test_%d", i), func(t *testing.T) {
|
||||||
result := NormalizeLabel(test.label, false)
|
result := NormalizeLabel(test.label)
|
||||||
require.Equal(t, test.expected, result)
|
require.Equal(t, test.expected, result)
|
||||||
uTF8result := NormalizeLabel(test.label, true)
|
|
||||||
require.Equal(t, test.expectedUTF8, uTF8result)
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -157,7 +157,10 @@ func createAttributes(resource pcommon.Resource, attributes pcommon.Map, setting
|
||||||
// map ensures no duplicate label names.
|
// map ensures no duplicate label names.
|
||||||
l := make(map[string]string, maxLabelCount)
|
l := make(map[string]string, maxLabelCount)
|
||||||
for _, label := range labels {
|
for _, label := range labels {
|
||||||
var finalKey = prometheustranslator.NormalizeLabel(label.Name, settings.AllowUTF8)
|
finalKey := label.Name
|
||||||
|
if !settings.AllowUTF8 {
|
||||||
|
finalKey = prometheustranslator.NormalizeLabel(finalKey)
|
||||||
|
}
|
||||||
if existingValue, alreadyExists := l[finalKey]; alreadyExists {
|
if existingValue, alreadyExists := l[finalKey]; alreadyExists {
|
||||||
l[finalKey] = existingValue + ";" + label.Value
|
l[finalKey] = existingValue + ";" + label.Value
|
||||||
} else {
|
} else {
|
||||||
|
@ -166,7 +169,10 @@ func createAttributes(resource pcommon.Resource, attributes pcommon.Map, setting
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, lbl := range promotedAttrs {
|
for _, lbl := range promotedAttrs {
|
||||||
normalized := prometheustranslator.NormalizeLabel(lbl.Name, settings.AllowUTF8)
|
normalized := lbl.Name
|
||||||
|
if !settings.AllowUTF8 {
|
||||||
|
normalized = prometheustranslator.NormalizeLabel(normalized)
|
||||||
|
}
|
||||||
if _, exists := l[normalized]; !exists {
|
if _, exists := l[normalized]; !exists {
|
||||||
l[normalized] = lbl.Value
|
l[normalized] = lbl.Value
|
||||||
}
|
}
|
||||||
|
@ -204,8 +210,8 @@ func createAttributes(resource pcommon.Resource, attributes pcommon.Map, setting
|
||||||
log.Println("label " + name + " is overwritten. Check if Prometheus reserved labels are used.")
|
log.Println("label " + name + " is overwritten. Check if Prometheus reserved labels are used.")
|
||||||
}
|
}
|
||||||
// internal labels should be maintained
|
// internal labels should be maintained
|
||||||
if !(len(name) > 4 && name[:2] == "__" && name[len(name)-2:] == "__") {
|
if !settings.AllowUTF8 && !(len(name) > 4 && name[:2] == "__" && name[len(name)-2:] == "__") {
|
||||||
name = prometheustranslator.NormalizeLabel(name, settings.AllowUTF8)
|
name = prometheustranslator.NormalizeLabel(name)
|
||||||
}
|
}
|
||||||
l[name] = extras[i+1]
|
l[name] = extras[i+1]
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue