Skip to content

Commit

Permalink
Avoid off-spec openmetrics exposition when exemplars have empty labels
Browse files Browse the repository at this point in the history
  • Loading branch information
orls committed Jan 13, 2024
1 parent 7e44242 commit a472b6c
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
2 changes: 1 addition & 1 deletion expfmt/openmetrics_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ func writeOpenMetricsSample(
return written, err
}
}
if exemplar != nil {
if exemplar != nil && len(exemplar.Label) > 0 {
n, err = writeExemplar(w, exemplar)
written += n
if err != nil {
Expand Down
25 changes: 25 additions & 0 deletions expfmt/openmetrics_create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,31 @@ foos_total 42.0
},
out: `# HELP name doc string
# TYPE name counter
`,
},
// 9: Simple Counter with exemplar that has empty label set:
// ignore the exemplar, since OpenMetrics spec requires labels.
{
in: &dto.MetricFamily{
Name: proto.String("foos_total"),
Help: proto.String("Number of foos."),
Type: dto.MetricType_COUNTER.Enum(),
Metric: []*dto.Metric{
{
Counter: &dto.Counter{
Value: proto.Float64(42),
Exemplar: &dto.Exemplar{
Label: []*dto.LabelPair{},
Value: proto.Float64(1),
Timestamp: openMetricsTimestamp,
},
},
},
},
},
out: `# HELP foos Number of foos.
# TYPE foos counter
foos_total 42.0
`,
},
}
Expand Down

0 comments on commit a472b6c

Please sign in to comment.