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

Correct logic in sample naming for counters, add new test #608

Merged
merged 1 commit into from Mar 21, 2024
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 3 additions & 3 deletions expfmt/openmetrics_create.go
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
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