Skip to content

Commit

Permalink
Correct logic in sample naming for counters, add new test
Browse files Browse the repository at this point in the history
Signed-off-by: Arianna Vespri <[email protected]>
  • Loading branch information
vesari committed Mar 21, 2024
1 parent b0624a8 commit da75ecd
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 3 deletions.
6 changes: 3 additions & 3 deletions expfmt/openmetrics_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,12 +248,12 @@ func MetricFamilyToOpenMetrics(out io.Writer, in *dto.MetricFamily, options ...E
var createdTsBytesWritten int

// Finally the samples, one line for each.
if metricType == dto.MetricType_COUNTER && strings.HasSuffix(name, "_total") {
compliantName = compliantName + "_total"
}
for _, metric := range in.Metric {
switch metricType {
case dto.MetricType_COUNTER:
if strings.HasSuffix(name, "_total") {
compliantName = compliantName + "_total"
}
if metric.Counter == nil {
return written, fmt.Errorf(
"expected counter in metric %s %s", compliantName, metric,
Expand Down
49 changes: 49 additions & 0 deletions expfmt/openmetrics_create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -694,6 +694,55 @@ request_duration_microseconds_count 2693
options: []EncoderOption{WithUnit()},
out: `# HELP name doc string
# TYPE name counter
`,
},
// 18: Counter, timestamp given, unit opted in, _total suffix.
{
in: &dto.MetricFamily{
Name: proto.String("some_measure_total"),
Help: proto.String("some testing measurement"),
Type: dto.MetricType_COUNTER.Enum(),
Unit: proto.String("seconds"),
Metric: []*dto.Metric{
{
Label: []*dto.LabelPair{
{
Name: proto.String("labelname"),
Value: proto.String("val1"),
},
{
Name: proto.String("basename"),
Value: proto.String("basevalue"),
},
},
Counter: &dto.Counter{
Value: proto.Float64(42),
},
},
{
Label: []*dto.LabelPair{
{
Name: proto.String("labelname"),
Value: proto.String("val2"),
},
{
Name: proto.String("basename"),
Value: proto.String("basevalue"),
},
},
Counter: &dto.Counter{
Value: proto.Float64(.23),
},
TimestampMs: proto.Int64(1234567890),
},
},
},
options: []EncoderOption{WithUnit()},
out: `# HELP some_measure_seconds some testing measurement
# TYPE some_measure_seconds counter
# UNIT some_measure_seconds seconds
some_measure_seconds_total{labelname="val1",basename="basevalue"} 42.0
some_measure_seconds_total{labelname="val2",basename="basevalue"} 0.23 1.23456789e+06
`,
},
}
Expand Down

0 comments on commit da75ecd

Please sign in to comment.