mirror of
https://github.com/VictoriaMetrics/VictoriaMetrics.git
synced 2024-11-21 14:44:00 +00:00
lib/promrelabel: properly replace :
char with _
in metric names when -usePromCompatibleNaming command-line flag is set
This addresses https://github.com/VictoriaMetrics/VictoriaMetrics/issues/3113#issuecomment-1275077071 comment from @johnseekins
This commit is contained in:
parent
63e3571e8c
commit
fdae53a75b
7 changed files with 68 additions and 19 deletions
|
@ -114,9 +114,9 @@ func (rctx *relabelCtx) applyRelabeling(tss []prompbmarshal.TimeSeries, extraLab
|
||||||
for j := range tmpLabels {
|
for j := range tmpLabels {
|
||||||
label := &tmpLabels[j]
|
label := &tmpLabels[j]
|
||||||
if label.Name == "__name__" {
|
if label.Name == "__name__" {
|
||||||
label.Value = promrelabel.SanitizeName(label.Value)
|
label.Value = promrelabel.SanitizeMetricName(label.Value)
|
||||||
} else {
|
} else {
|
||||||
label.Name = promrelabel.SanitizeName(label.Name)
|
label.Name = promrelabel.SanitizeLabelName(label.Name)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -180,7 +180,7 @@ func modifyData(msg Metric, normalize bool) (Metric, error) {
|
||||||
/*
|
/*
|
||||||
replace bad characters in metric name with _ per the data model
|
replace bad characters in metric name with _ per the data model
|
||||||
*/
|
*/
|
||||||
finalMsg.Metric = promrelabel.SanitizeName(name)
|
finalMsg.Metric = promrelabel.SanitizeMetricName(name)
|
||||||
// replace bad characters in tag keys with _ per the data model
|
// replace bad characters in tag keys with _ per the data model
|
||||||
for key, value := range msg.Tags {
|
for key, value := range msg.Tags {
|
||||||
// if normalization requested, lowercase the key and value
|
// if normalization requested, lowercase the key and value
|
||||||
|
@ -191,7 +191,7 @@ func modifyData(msg Metric, normalize bool) (Metric, error) {
|
||||||
/*
|
/*
|
||||||
replace all explicitly bad characters with _
|
replace all explicitly bad characters with _
|
||||||
*/
|
*/
|
||||||
key = promrelabel.SanitizeName(key)
|
key = promrelabel.SanitizeLabelName(key)
|
||||||
// tags that start with __ are considered custom stats for internal prometheus stuff, we should drop them
|
// tags that start with __ are considered custom stats for internal prometheus stuff, we should drop them
|
||||||
if !strings.HasPrefix(key, "__") {
|
if !strings.HasPrefix(key, "__") {
|
||||||
finalMsg.Tags[key] = value
|
finalMsg.Tags[key] = value
|
||||||
|
|
|
@ -134,9 +134,9 @@ func (ctx *Ctx) ApplyRelabeling(labels []prompb.Label) []prompb.Label {
|
||||||
for i := range tmpLabels {
|
for i := range tmpLabels {
|
||||||
label := &tmpLabels[i]
|
label := &tmpLabels[i]
|
||||||
if label.Name == "__name__" {
|
if label.Name == "__name__" {
|
||||||
label.Value = promrelabel.SanitizeName(label.Value)
|
label.Value = promrelabel.SanitizeMetricName(label.Value)
|
||||||
} else {
|
} else {
|
||||||
label.Name = promrelabel.SanitizeName(label.Name)
|
label.Name = promrelabel.SanitizeLabelName(label.Name)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -27,6 +27,7 @@ The following `tip` changes can be tested by building VictoriaMetrics components
|
||||||
* FEATURE: [vmbackup](https://docs.victoriametrics.com/vmbackup.html): add support for server-side copy of existing backups. See [these docs](https://docs.victoriametrics.com/vmbackup.html#server-side-copy-of-the-existing-backup) for details.
|
* FEATURE: [vmbackup](https://docs.victoriametrics.com/vmbackup.html): add support for server-side copy of existing backups. See [these docs](https://docs.victoriametrics.com/vmbackup.html#server-side-copy-of-the-existing-backup) for details.
|
||||||
|
|
||||||
* BUGFIX: remove `DEBUG` logging when parsing `if` filters inside [relabeling rules](https://docs.victoriametrics.com/vmagent.html#relabeling-enhancements) and when parsing `match` filters inside [stream aggregation rules](https://docs.victoriametrics.com/stream-aggregation.html).
|
* BUGFIX: remove `DEBUG` logging when parsing `if` filters inside [relabeling rules](https://docs.victoriametrics.com/vmagent.html#relabeling-enhancements) and when parsing `match` filters inside [stream aggregation rules](https://docs.victoriametrics.com/stream-aggregation.html).
|
||||||
|
* BUGFIX: properly replace `:` chars in label names with `_` when `-usePromCompatibleNaming` command-line flag is passed to `vmagent`, `vminsert` or single-node VictoriaMetrics. This addresses [this comment](https://github.com/VictoriaMetrics/VictoriaMetrics/issues/3113#issuecomment-1275077071).
|
||||||
|
|
||||||
## [v1.93.0](https://github.com/VictoriaMetrics/VictoriaMetrics/releases/tag/v1.93.0)
|
## [v1.93.0](https://github.com/VictoriaMetrics/VictoriaMetrics/releases/tag/v1.93.0)
|
||||||
|
|
||||||
|
|
|
@ -619,15 +619,28 @@ func fillLabelReferences(dst []byte, replacement string, labels []prompbmarshal.
|
||||||
return dst
|
return dst
|
||||||
}
|
}
|
||||||
|
|
||||||
// SanitizeName replaces unsupported by Prometheus chars in metric names and label names with _.
|
// SanitizeLabelName replaces unsupported by Prometheus chars in label names with _.
|
||||||
//
|
//
|
||||||
// See https://prometheus.io/docs/concepts/data_model/#metric-names-and-labels
|
// See https://prometheus.io/docs/concepts/data_model/#metric-names-and-labels
|
||||||
func SanitizeName(name string) string {
|
func SanitizeLabelName(name string) string {
|
||||||
return promSanitizer.Transform(name)
|
return labelNameSanitizer.Transform(name)
|
||||||
}
|
}
|
||||||
|
|
||||||
var promSanitizer = bytesutil.NewFastStringTransformer(func(s string) string {
|
var labelNameSanitizer = bytesutil.NewFastStringTransformer(func(s string) string {
|
||||||
return unsupportedPromChars.ReplaceAllString(s, "_")
|
return unsupportedLabelNameChars.ReplaceAllString(s, "_")
|
||||||
})
|
})
|
||||||
|
|
||||||
var unsupportedPromChars = regexp.MustCompile(`[^a-zA-Z0-9_:]`)
|
var unsupportedLabelNameChars = regexp.MustCompile(`[^a-zA-Z0-9_]`)
|
||||||
|
|
||||||
|
// SanitizeMetricName replaces unsupported by Prometheus chars in metric names with _.
|
||||||
|
//
|
||||||
|
// See https://prometheus.io/docs/concepts/data_model/#metric-names-and-labels
|
||||||
|
func SanitizeMetricName(value string) string {
|
||||||
|
return metricNameSanitizer.Transform(value)
|
||||||
|
}
|
||||||
|
|
||||||
|
var metricNameSanitizer = bytesutil.NewFastStringTransformer(func(s string) string {
|
||||||
|
return unsupportedMetricNameChars.ReplaceAllString(s, "_")
|
||||||
|
})
|
||||||
|
|
||||||
|
var unsupportedMetricNameChars = regexp.MustCompile(`[^a-zA-Z0-9_:]`)
|
||||||
|
|
|
@ -9,13 +9,13 @@ import (
|
||||||
"github.com/VictoriaMetrics/VictoriaMetrics/lib/promutils"
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/promutils"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestSanitizeName(t *testing.T) {
|
func TestSanitizeMetricName(t *testing.T) {
|
||||||
f := func(s, resultExpected string) {
|
f := func(s, resultExpected string) {
|
||||||
t.Helper()
|
t.Helper()
|
||||||
for i := 0; i < 5; i++ {
|
for i := 0; i < 5; i++ {
|
||||||
result := SanitizeName(s)
|
result := SanitizeMetricName(s)
|
||||||
if result != resultExpected {
|
if result != resultExpected {
|
||||||
t.Fatalf("unexpected result for SanitizeName(%q) at iteration %d; got %q; want %q", s, i, result, resultExpected)
|
t.Fatalf("unexpected result for SanitizeMetricName(%q) at iteration %d; got %q; want %q", s, i, result, resultExpected)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -25,6 +25,22 @@ func TestSanitizeName(t *testing.T) {
|
||||||
f("foo...bar", "foo___bar")
|
f("foo...bar", "foo___bar")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestSanitizeLabelName(t *testing.T) {
|
||||||
|
f := func(s, resultExpected string) {
|
||||||
|
t.Helper()
|
||||||
|
for i := 0; i < 5; i++ {
|
||||||
|
result := SanitizeLabelName(s)
|
||||||
|
if result != resultExpected {
|
||||||
|
t.Fatalf("unexpected result for SanitizeLabelName(%q) at iteration %d; got %q; want %q", s, i, result, resultExpected)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
f("", "")
|
||||||
|
f("a", "a")
|
||||||
|
f("foo.bar/baz:a", "foo_bar_baz_a")
|
||||||
|
f("foo...bar", "foo___bar")
|
||||||
|
}
|
||||||
|
|
||||||
func TestLabelsToString(t *testing.T) {
|
func TestLabelsToString(t *testing.T) {
|
||||||
f := func(labels []prompbmarshal.Label, sExpected string) {
|
f := func(labels []prompbmarshal.Label, sExpected string) {
|
||||||
t.Helper()
|
t.Helper()
|
||||||
|
|
|
@ -8,20 +8,39 @@ import (
|
||||||
"github.com/VictoriaMetrics/VictoriaMetrics/lib/prompbmarshal"
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/prompbmarshal"
|
||||||
)
|
)
|
||||||
|
|
||||||
func BenchmarkSanitizeName(b *testing.B) {
|
func BenchmarkSanitizeMetricName(b *testing.B) {
|
||||||
for _, name := range []string{"", "foo", "foo-bar-baz", "http_requests_total"} {
|
for _, name := range []string{"", "foo", "foo-bar-baz", "http_requests_total"} {
|
||||||
b.Run(name, func(b *testing.B) {
|
b.Run(name, func(b *testing.B) {
|
||||||
benchmarkSanitizeName(b, name)
|
benchmarkSanitizeMetricName(b, name)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func benchmarkSanitizeName(b *testing.B, name string) {
|
func benchmarkSanitizeMetricName(b *testing.B, name string) {
|
||||||
b.ReportAllocs()
|
b.ReportAllocs()
|
||||||
b.SetBytes(1)
|
b.SetBytes(1)
|
||||||
b.RunParallel(func(pb *testing.PB) {
|
b.RunParallel(func(pb *testing.PB) {
|
||||||
for pb.Next() {
|
for pb.Next() {
|
||||||
sanitizedName := SanitizeName(name)
|
sanitizedName := SanitizeMetricName(name)
|
||||||
|
GlobalSink += len(sanitizedName)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkSanitizeLabelName(b *testing.B) {
|
||||||
|
for _, name := range []string{"", "foo", "foo-bar-baz", "http_requests_total"} {
|
||||||
|
b.Run(name, func(b *testing.B) {
|
||||||
|
benchmarkSanitizeLabelName(b, name)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func benchmarkSanitizeLabelName(b *testing.B, name string) {
|
||||||
|
b.ReportAllocs()
|
||||||
|
b.SetBytes(1)
|
||||||
|
b.RunParallel(func(pb *testing.PB) {
|
||||||
|
for pb.Next() {
|
||||||
|
sanitizedName := SanitizeLabelName(name)
|
||||||
GlobalSink += len(sanitizedName)
|
GlobalSink += len(sanitizedName)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
Loading…
Reference in a new issue