Skip to content

Commit 057bec8

Browse files
author
Arthur Silva Sens
authored
Merge pull request #608 from vesari/avoid-total-suffix-repetition
Correct logic in sample naming for counters, add new test
2 parents b0624a8 + da75ecd commit 057bec8

File tree

2 files changed

+52
-3
lines changed

2 files changed

+52
-3
lines changed

expfmt/openmetrics_create.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -248,12 +248,12 @@ func MetricFamilyToOpenMetrics(out io.Writer, in *dto.MetricFamily, options ...E
248248
var createdTsBytesWritten int
249249

250250
// Finally the samples, one line for each.
251+
if metricType == dto.MetricType_COUNTER && strings.HasSuffix(name, "_total") {
252+
compliantName = compliantName + "_total"
253+
}
251254
for _, metric := range in.Metric {
252255
switch metricType {
253256
case dto.MetricType_COUNTER:
254-
if strings.HasSuffix(name, "_total") {
255-
compliantName = compliantName + "_total"
256-
}
257257
if metric.Counter == nil {
258258
return written, fmt.Errorf(
259259
"expected counter in metric %s %s", compliantName, metric,

expfmt/openmetrics_create_test.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -694,6 +694,55 @@ request_duration_microseconds_count 2693
694694
options: []EncoderOption{WithUnit()},
695695
out: `# HELP name doc string
696696
# TYPE name counter
697+
`,
698+
},
699+
// 18: Counter, timestamp given, unit opted in, _total suffix.
700+
{
701+
in: &dto.MetricFamily{
702+
Name: proto.String("some_measure_total"),
703+
Help: proto.String("some testing measurement"),
704+
Type: dto.MetricType_COUNTER.Enum(),
705+
Unit: proto.String("seconds"),
706+
Metric: []*dto.Metric{
707+
{
708+
Label: []*dto.LabelPair{
709+
{
710+
Name: proto.String("labelname"),
711+
Value: proto.String("val1"),
712+
},
713+
{
714+
Name: proto.String("basename"),
715+
Value: proto.String("basevalue"),
716+
},
717+
},
718+
Counter: &dto.Counter{
719+
Value: proto.Float64(42),
720+
},
721+
},
722+
{
723+
Label: []*dto.LabelPair{
724+
{
725+
Name: proto.String("labelname"),
726+
Value: proto.String("val2"),
727+
},
728+
{
729+
Name: proto.String("basename"),
730+
Value: proto.String("basevalue"),
731+
},
732+
},
733+
Counter: &dto.Counter{
734+
Value: proto.Float64(.23),
735+
},
736+
TimestampMs: proto.Int64(1234567890),
737+
},
738+
},
739+
},
740+
options: []EncoderOption{WithUnit()},
741+
out: `# HELP some_measure_seconds some testing measurement
742+
# TYPE some_measure_seconds counter
743+
# UNIT some_measure_seconds seconds
744+
some_measure_seconds_total{labelname="val1",basename="basevalue"} 42.0
745+
some_measure_seconds_total{labelname="val2",basename="basevalue"} 0.23 1.23456789e+06
697746
`,
698747
},
699748
}

0 commit comments

Comments
 (0)