mirror of
https://github.com/prometheus/prometheus.git
synced 2025-03-05 20:59:13 -08:00
Merge pull request #11936 from kelnage/sanitize-labels-edge-cases
Fix SanitizeLabelName for certain edge case invalid labels
This commit is contained in:
commit
e1679a80f3
|
@ -16,6 +16,7 @@ package strutil
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/url"
|
"net/url"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/grafana/regexp"
|
"github.com/grafana/regexp"
|
||||||
)
|
)
|
||||||
|
@ -38,6 +39,26 @@ func GraphLinkForExpression(expr string) string {
|
||||||
|
|
||||||
// SanitizeLabelName replaces anything that doesn't match
|
// SanitizeLabelName replaces anything that doesn't match
|
||||||
// client_label.LabelNameRE with an underscore.
|
// client_label.LabelNameRE with an underscore.
|
||||||
|
// Note: this does not handle all Prometheus label name restrictions (such as
|
||||||
|
// not starting with a digit 0-9), and hence should only be used if the label
|
||||||
|
// name is prefixed with a known valid string.
|
||||||
func SanitizeLabelName(name string) string {
|
func SanitizeLabelName(name string) string {
|
||||||
return invalidLabelCharRE.ReplaceAllString(name, "_")
|
return invalidLabelCharRE.ReplaceAllString(name, "_")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SanitizeFullLabelName replaces any invalid character with an underscore, and
|
||||||
|
// if given an empty string, returns a string containing a single underscore.
|
||||||
|
func SanitizeFullLabelName(name string) string {
|
||||||
|
if len(name) == 0 {
|
||||||
|
return "_"
|
||||||
|
}
|
||||||
|
var validSb strings.Builder
|
||||||
|
for i, b := range name {
|
||||||
|
if !((b >= 'a' && b <= 'z') || (b >= 'A' && b <= 'Z') || b == '_' || (b >= '0' && b <= '9' && i > 0)) {
|
||||||
|
validSb.WriteRune('_')
|
||||||
|
} else {
|
||||||
|
validSb.WriteRune(b)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return validSb.String()
|
||||||
|
}
|
||||||
|
|
|
@ -59,3 +59,21 @@ func TestSanitizeLabelName(t *testing.T) {
|
||||||
expected = "barClient_LABEL____"
|
expected = "barClient_LABEL____"
|
||||||
require.Equal(t, expected, actual, "SanitizeLabelName failed for label (%s)", expected)
|
require.Equal(t, expected, actual, "SanitizeLabelName failed for label (%s)", expected)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestSanitizeFullLabelName(t *testing.T) {
|
||||||
|
actual := SanitizeFullLabelName("fooClientLABEL")
|
||||||
|
expected := "fooClientLABEL"
|
||||||
|
require.Equal(t, expected, actual, "SanitizeFullLabelName failed for label (%s)", expected)
|
||||||
|
|
||||||
|
actual = SanitizeFullLabelName("barClient.LABEL$$##")
|
||||||
|
expected = "barClient_LABEL____"
|
||||||
|
require.Equal(t, expected, actual, "SanitizeFullLabelName failed for label (%s)", expected)
|
||||||
|
|
||||||
|
actual = SanitizeFullLabelName("0zerothClient1LABEL")
|
||||||
|
expected = "_zerothClient1LABEL"
|
||||||
|
require.Equal(t, expected, actual, "SanitizeFullLabelName failed for label (%s)", expected)
|
||||||
|
|
||||||
|
actual = SanitizeFullLabelName("")
|
||||||
|
expected = "_"
|
||||||
|
require.Equal(t, expected, actual, "SanitizeFullLabelName failed for the empty label")
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue