Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[prometheusremotewrite] fix: counter name check #2613

Merged
Show file tree
Hide file tree
Changes from all commits
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
32 changes: 14 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,8 @@ func getPromMetricName(metric *otlp.Metric, ns string) string {
}
*/

if b.Len() > 0 && isCounter {
b.WriteString(delimeter)
b.WriteString(totalStr)
if b.Len() > 0 && isCounter && !strings.HasSuffix(name, counterSuffix) {
b.WriteString(counterSuffix)
}
return sanitize(b.String())
}
Expand Down Expand Up @@ -262,7 +258,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
8 changes: 7 additions & 1 deletion exporter/prometheusremotewriteexporter/helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,13 @@ func Test_getPromMetricName(t *testing.T) {
"total_suffix",
validMetrics1[validIntSum],
ns1,
"test_ns_" + validIntSum + delimeter + totalStr,
"test_ns_" + validIntSum + counterSuffix,
},
{
"already_has_total_suffix",
validMetrics1[suffixedCounter],
ns1,
"test_ns_" + suffixedCounter,
},
{
"dirty_string",
Expand Down
13 changes: 13 additions & 0 deletions exporter/prometheusremotewriteexporter/testutil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ var (
validIntHistogram = "valid_IntHistogram"
validDoubleHistogram = "valid_DoubleHistogram"
validDoubleSummary = "valid_DoubleSummary"
suffixedCounter = "valid_IntSum_total"

validIntGaugeDirty = "*valid_IntGauge$"

Expand Down Expand Up @@ -136,6 +137,18 @@ var (
},
},
},
suffixedCounter: {
Name: suffixedCounter,
Data: &otlp.Metric_IntSum{
IntSum: &otlp.IntSum{
DataPoints: []*otlp.IntDataPoint{
getIntDataPoint(lbs1, intVal1, time1),
nil,
},
AggregationTemporality: otlp.AggregationTemporality_AGGREGATION_TEMPORALITY_CUMULATIVE,
},
},
},
validDoubleSum: {
Name: validDoubleSum,
Data: &otlp.Metric_DoubleSum{
Expand Down