From 077266606c9625f5b6ff5905fbcc1b62057cdf91 Mon Sep 17 00:00:00 2001 From: Mark Holt <135143369+mh0lt@users.noreply.github.com> Date: Thu, 7 Sep 2023 15:23:13 +0100 Subject: [PATCH] Strip labels when writing metric metadata (#8149) This is a fix for ```cache_total{target="acc_read"} counter it's invalid syntax of metric type. prometheus parsing error``` which does not remove the whole metadata tag --- metrics/collector.go | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/metrics/collector.go b/metrics/collector.go index 17664d07c..1df9d0fb2 100644 --- a/metrics/collector.go +++ b/metrics/collector.go @@ -20,6 +20,7 @@ import ( "bytes" "fmt" "strconv" + "strings" "github.com/VictoriaMetrics/metrics" ) @@ -84,15 +85,23 @@ func (c *collector) addTimer(name string, m *metrics.Summary) { } func (c *collector) writeGauge(name string, value interface{}) { - //c.buff.WriteString(fmt.Sprintf(typeGaugeTpl, name)) + c.buff.WriteString(fmt.Sprintf(typeGaugeTpl, stripLabels(name))) c.buff.WriteString(fmt.Sprintf(keyValueTpl, name, value)) } func (c *collector) writeCounter(name string, value interface{}) { - //c.buff.WriteString(fmt.Sprintf(typeCounterTpl, name)) + c.buff.WriteString(fmt.Sprintf(typeCounterTpl, stripLabels(name))) c.buff.WriteString(fmt.Sprintf(keyValueTpl, name, value)) } +func stripLabels(name string) string { + if labelsIndex := strings.IndexByte(name, '{'); labelsIndex >= 0 { + return name[0:labelsIndex] + } + + return name +} + func (c *collector) writeSummaryCounter(name string, value interface{}) { name = name + "_count" c.buff.WriteString(fmt.Sprintf(keyCounterTpl, name, value))