Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 18 additions & 18 deletions exporter/prometheusremotewriteexporter/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,16 @@ import (
)

const (
nameStr = "__name__"
sumStr = "_sum"
countStr = "_count"
bucketStr = "_bucket"
leStr = "le"
quantileStr = "quantile"
pInfStr = "+Inf"
totalStr = "total"
delimeter = "_"
keyStr = "key"
nameStr = "__name__"
sumStr = "_sum"
countStr = "_count"
bucketStr = "_bucket"
leStr = "le"
quantileStr = "quantile"
pInfStr = "+Inf"
counterSuffix = "_total"
delimiter = "_"
keyStr = "key"
)

// ByLabelName enables the usage of sort.Sort() with a slice of labels
Expand Down Expand Up @@ -188,14 +188,11 @@ func getPromMetricName(metric *otlp.Metric, ns string) string {
b.WriteString(ns)

if b.Len() > 0 {
b.WriteString(delimeter)
b.WriteString(delimiter)
}
name := metric.GetName()
b.WriteString(name)

// do not add the total suffix if the metric name already ends in "total"
isCounter = isCounter && name[len(name)-len(totalStr):] != totalStr

// Including units makes two metrics with the same name and label set belong to two different TimeSeries if the
// units are different.
/*
Expand All @@ -205,9 +202,12 @@ func getPromMetricName(metric *otlp.Metric, ns string) string {
}
*/

if b.Len() > 0 && isCounter {
b.WriteString(delimeter)
b.WriteString(totalStr)
counterSuffixLength := len(counterSuffix)
nameLength := len(name)
hasCounterSuffix := nameLength > counterSuffixLength && name[nameLength-counterSuffixLength:] == counterSuffix

if b.Len() > 0 && isCounter && !hasCounterSuffix {
b.WriteString(counterSuffix)
}
return sanitize(b.String())
}
Expand Down Expand Up @@ -262,7 +262,7 @@ func sanitize(s string) string {
// See https://github.com/orijtech/prometheus-go-metrics-exporter/issues/4.
s = strings.Map(sanitizeRune, s)
if unicode.IsDigit(rune(s[0])) {
s = keyStr + delimeter + s
s = keyStr + delimiter + s
}
if s[0] == '_' {
s = keyStr + s
Expand Down
2 changes: 1 addition & 1 deletion exporter/prometheusremotewriteexporter/helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ func Test_getPromMetricName(t *testing.T) {
"total_suffix",
validMetrics1[validIntSum],
ns1,
"test_ns_" + validIntSum + delimeter + totalStr,
"test_ns_" + validIntSum + counterSuffix,
},
{
"dirty_string",
Expand Down