Skip to content

Commit

Permalink
Strip labels when writing metric metadata (#8149)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
mh0lt authored and yperbasis committed Sep 7, 2023
1 parent 29f348e commit 034ef63
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions metrics/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"bytes"
"fmt"
"strconv"
"strings"

"github.com/VictoriaMetrics/metrics"
)
Expand Down Expand Up @@ -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))
Expand Down

0 comments on commit 034ef63

Please sign in to comment.