Skip to content

Commit 034ef63

Browse files
mh0ltyperbasis
authored andcommitted
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
1 parent 29f348e commit 034ef63

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

metrics/collector.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"bytes"
2121
"fmt"
2222
"strconv"
23+
"strings"
2324

2425
"github.com/VictoriaMetrics/metrics"
2526
)
@@ -84,15 +85,23 @@ func (c *collector) addTimer(name string, m *metrics.Summary) {
8485
}
8586

8687
func (c *collector) writeGauge(name string, value interface{}) {
87-
//c.buff.WriteString(fmt.Sprintf(typeGaugeTpl, name))
88+
c.buff.WriteString(fmt.Sprintf(typeGaugeTpl, stripLabels(name)))
8889
c.buff.WriteString(fmt.Sprintf(keyValueTpl, name, value))
8990
}
9091

9192
func (c *collector) writeCounter(name string, value interface{}) {
92-
//c.buff.WriteString(fmt.Sprintf(typeCounterTpl, name))
93+
c.buff.WriteString(fmt.Sprintf(typeCounterTpl, stripLabels(name)))
9394
c.buff.WriteString(fmt.Sprintf(keyValueTpl, name, value))
9495
}
9596

97+
func stripLabels(name string) string {
98+
if labelsIndex := strings.IndexByte(name, '{'); labelsIndex >= 0 {
99+
return name[0:labelsIndex]
100+
}
101+
102+
return name
103+
}
104+
96105
func (c *collector) writeSummaryCounter(name string, value interface{}) {
97106
name = name + "_count"
98107
c.buff.WriteString(fmt.Sprintf(keyCounterTpl, name, value))

0 commit comments

Comments
 (0)