Skip to content

Commit 9c61db0

Browse files
authored
Avoid off-spec openmetrics exposition when exemplars have empty labels (#569)
Will fix prometheus/client_golang#1333 Signed-off-by: Owen Smith <[email protected]>
1 parent dd8b1ec commit 9c61db0

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

expfmt/openmetrics_create.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,7 @@ func writeOpenMetricsSample(
350350
return written, err
351351
}
352352
}
353-
if exemplar != nil {
353+
if exemplar != nil && len(exemplar.Label) > 0 {
354354
n, err = writeExemplar(w, exemplar)
355355
written += n
356356
if err != nil {

expfmt/openmetrics_create_test.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -542,6 +542,31 @@ foos_total 42.0
542542
},
543543
out: `# HELP name doc string
544544
# TYPE name counter
545+
`,
546+
},
547+
// 9: Simple Counter with exemplar that has empty label set:
548+
// ignore the exemplar, since OpenMetrics spec requires labels.
549+
{
550+
in: &dto.MetricFamily{
551+
Name: proto.String("foos_total"),
552+
Help: proto.String("Number of foos."),
553+
Type: dto.MetricType_COUNTER.Enum(),
554+
Metric: []*dto.Metric{
555+
{
556+
Counter: &dto.Counter{
557+
Value: proto.Float64(42),
558+
Exemplar: &dto.Exemplar{
559+
Label: []*dto.LabelPair{},
560+
Value: proto.Float64(1),
561+
Timestamp: openMetricsTimestamp,
562+
},
563+
},
564+
},
565+
},
566+
},
567+
out: `# HELP foos Number of foos.
568+
# TYPE foos counter
569+
foos_total 42.0
545570
`,
546571
},
547572
}

0 commit comments

Comments
 (0)