mirror of
https://github.com/prometheus/prometheus.git
synced 2024-11-09 15:14:04 -08:00
Implement SanitizeLabelName and Full variant
Rather than removing the previous implementation of SanitizeLabelName, offer another version named SanitizeFullLabelName that achieved the desired requirements, without breaking existing Prometheus code. Update testing to validate correctness of new variant. Signed-off-by: Nick Moore <nicholas.moore@grafana.com>
This commit is contained in:
parent
758914e1fb
commit
c05ebef306
|
@ -17,8 +17,12 @@ import (
|
|||
"fmt"
|
||||
"net/url"
|
||||
"strings"
|
||||
|
||||
"github.com/grafana/regexp"
|
||||
)
|
||||
|
||||
var invalidLabelCharRE = regexp.MustCompile(`[^a-zA-Z0-9_]`)
|
||||
|
||||
// TableLinkForExpression creates an escaped relative link to the table view of
|
||||
// the provided expression.
|
||||
func TableLinkForExpression(expr string) string {
|
||||
|
@ -33,9 +37,18 @@ func GraphLinkForExpression(expr string) string {
|
|||
return fmt.Sprintf("/graph?g0.expr=%s&g0.tab=0", escapedExpression)
|
||||
}
|
||||
|
||||
// SanitizeLabelName replaces any invalid character with an underscore, and if
|
||||
// given an empty string, returns a string containing a single underscore.
|
||||
// SanitizeLabelName replaces anything that doesn't match
|
||||
// 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 {
|
||||
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 "_"
|
||||
}
|
||||
|
|
|
@ -58,12 +58,22 @@ func TestSanitizeLabelName(t *testing.T) {
|
|||
actual = SanitizeLabelName("barClient.LABEL$$##")
|
||||
expected = "barClient_LABEL____"
|
||||
require.Equal(t, expected, actual, "SanitizeLabelName failed for label (%s)", expected)
|
||||
|
||||
actual = SanitizeLabelName("0zerothClient1LABEL")
|
||||
expected = "_zerothClient1LABEL"
|
||||
require.Equal(t, expected, actual, "SanitizeLabelName failed for label (%s)", expected)
|
||||
|
||||
actual = SanitizeLabelName("")
|
||||
expected = "_"
|
||||
require.Equal(t, expected, actual, "SanitizeLabelName failed for the empty label")
|
||||
}
|
||||
|
||||
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