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
This commit is contained in:
Mark Holt 2023-09-07 15:23:13 +01:00 committed by GitHub
parent 22bd173ab3
commit 077266606c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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))