Skip to content

Commit dac5aec

Browse files
committed
change metric name only if withUnit opted in
Signed-off-by: Arianna Vespri <[email protected]>
1 parent 6e23ea6 commit dac5aec

File tree

2 files changed

+71
-4
lines changed

2 files changed

+71
-4
lines changed

expfmt/openmetrics_create.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,9 @@ func MetricFamilyToOpenMetrics(out io.Writer, in *dto.MetricFamily, options ...T
111111
if metricType == dto.MetricType_COUNTER && strings.HasSuffix(shortName, "_total") {
112112
shortName = name[:len(name)-6]
113113
}
114+
if toOM.withUnit && !strings.HasSuffix(shortName, fmt.Sprintf("_%s", *in.Unit)) {
115+
shortName = shortName + fmt.Sprintf("_%s", *in.Unit)
116+
}
114117

115118
// Comments, first HELP, then TYPE.
116119
if in.Help != nil {
@@ -183,6 +186,7 @@ func MetricFamilyToOpenMetrics(out io.Writer, in *dto.MetricFamily, options ...T
183186
if err != nil {
184187
return
185188
}
189+
186190
err = w.WriteByte(' ')
187191
written++
188192
if err != nil {

expfmt/openmetrics_create_test.go

Lines changed: 67 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,9 @@ func TestCreateOpenMetrics(t *testing.T) {
3333
}
3434

3535
var scenarios = []struct {
36-
in *dto.MetricFamily
37-
out string
36+
in *dto.MetricFamily
37+
withUnit ToOpenMetricsOption
38+
out string
3839
}{
3940
// 0: Counter, timestamp given, no _total suffix.
4041
{
@@ -430,7 +431,6 @@ foos_total 42.0
430431
},
431432
out: `# HELP name_seconds doc string
432433
# TYPE name_seconds counter
433-
# UNIT name_seconds seconds
434434
`,
435435
},
436436
// 10: Histogram plus unit.
@@ -473,7 +473,6 @@ foos_total 42.0
473473
},
474474
out: `# HELP request_duration_microseconds The response latency.
475475
# TYPE request_duration_microseconds histogram
476-
# UNIT request_duration_microseconds microseconds
477476
request_duration_microseconds_bucket{le="100.0"} 123
478477
request_duration_microseconds_bucket{le="120.0"} 412
479478
request_duration_microseconds_bucket{le="144.0"} 592
@@ -487,6 +486,7 @@ request_duration_microseconds_count 2693
487486

488487
for i, scenario := range scenarios {
489488
out := bytes.NewBuffer(make([]byte, 0, len(scenario.out)))
489+
490490
n, err := MetricFamilyToOpenMetrics(out, scenario.in)
491491
if err != nil {
492492
t.Errorf("%d. error: %s", i, err)
@@ -508,6 +508,69 @@ request_duration_microseconds_count 2693
508508

509509
}
510510

511+
func TestCreateOpenMatricsWithUnit(t *testing.T) {
512+
openMetricsTimestamp := timestamppb.New(time.Unix(12345, 600000000))
513+
if err := openMetricsTimestamp.CheckValid(); err != nil {
514+
t.Error(err)
515+
}
516+
517+
var scenarios = []struct {
518+
in *dto.MetricFamily
519+
withUnit ToOpenMetricsOption
520+
out string
521+
}{
522+
// 1: No metric plus unit.
523+
{
524+
in: &dto.MetricFamily{
525+
Name: proto.String("name_seconds_total"),
526+
Help: proto.String("doc string"),
527+
Type: dto.MetricType_COUNTER.Enum(),
528+
Unit: proto.String("seconds"),
529+
Metric: []*dto.Metric{},
530+
},
531+
out: `# HELP name_seconds doc string
532+
# TYPE name_seconds counter
533+
# UNIT name_seconds seconds
534+
`,
535+
},
536+
// 2: No metric plus unit no unit in name.
537+
{
538+
in: &dto.MetricFamily{
539+
Name: proto.String("name_total"),
540+
Help: proto.String("doc string"),
541+
Type: dto.MetricType_COUNTER.Enum(),
542+
Unit: proto.String("seconds"),
543+
Metric: []*dto.Metric{},
544+
},
545+
out: `# HELP name_seconds doc string
546+
# TYPE name_seconds counter
547+
# UNIT name_seconds seconds
548+
`,
549+
},
550+
}
551+
for i, scenario := range scenarios {
552+
out := bytes.NewBuffer(make([]byte, 0, len(scenario.out)))
553+
554+
n, err := MetricFamilyToOpenMetrics(out, scenario.in, ToOpenMetricsWithUnit())
555+
if err != nil {
556+
t.Errorf("%d. error: %s", i, err)
557+
continue
558+
}
559+
if expected, got := len(scenario.out), n; expected != got {
560+
t.Errorf(
561+
"%d. expected %d bytes written, got %d",
562+
i, expected, got,
563+
)
564+
}
565+
if expected, got := scenario.out, out.String(); expected != got {
566+
t.Errorf(
567+
"%d. expected out=%q, got %q",
568+
i, expected, got,
569+
)
570+
}
571+
}
572+
}
573+
511574
func BenchmarkOpenMetricsCreate(b *testing.B) {
512575
mf := &dto.MetricFamily{
513576
Name: proto.String("request_duration_microseconds"),

0 commit comments

Comments
 (0)