Skip to content

Commit 2511f67

Browse files
committed
Correct the tests
Signed-off-by: Arianna Vespri <[email protected]>
1 parent 7de7f54 commit 2511f67

File tree

1 file changed

+15
-83
lines changed

1 file changed

+15
-83
lines changed

expfmt/openmetrics_create_test.go

Lines changed: 15 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ func TestCreateOpenMetrics(t *testing.T) {
3434

3535
var scenarios = []struct {
3636
in *dto.MetricFamily
37-
withUnit ToOpenMetricsOption
37+
withUnit bool
3838
out string
3939
}{
4040
// 0: Counter, timestamp given, no _total suffix.
@@ -236,6 +236,7 @@ summary_name_count{name_1="value 1",name_2="value 2"} 4711
236236
Name: proto.String("request_duration_microseconds"),
237237
Help: proto.String("The response latency."),
238238
Type: dto.MetricType_HISTOGRAM.Enum(),
239+
Unit: proto.String("microseconds"),
239240
Metric: []*dto.Metric{
240241
&dto.Metric{
241242
Histogram: &dto.Histogram{
@@ -267,8 +268,10 @@ summary_name_count{name_1="value 1",name_2="value 2"} 4711
267268
},
268269
},
269270
},
271+
withUnit: true,
270272
out: `# HELP request_duration_microseconds The response latency.
271273
# TYPE request_duration_microseconds histogram
274+
# UNIT request_duration_microseconds microseconds
272275
request_duration_microseconds_bucket{le="100.0"} 123
273276
request_duration_microseconds_bucket{le="120.0"} 412
274277
request_duration_microseconds_bucket{le="144.0"} 592
@@ -284,6 +287,7 @@ request_duration_microseconds_count 2693
284287
Name: proto.String("request_duration_microseconds"),
285288
Help: proto.String("The response latency."),
286289
Type: dto.MetricType_HISTOGRAM.Enum(),
290+
Unit: proto.String("microseconds"),
287291
Metric: []*dto.Metric{
288292
&dto.Metric{
289293
Histogram: &dto.Histogram{
@@ -429,8 +433,10 @@ foos_total 42.0
429433
Unit: proto.String("seconds"),
430434
Metric: []*dto.Metric{},
431435
},
436+
withUnit: true,
432437
out: `# HELP name_seconds doc string
433438
# TYPE name_seconds counter
439+
# UNIT name_seconds seconds
434440
`,
435441
},
436442
// 10: Histogram plus unit.
@@ -482,58 +488,7 @@ request_duration_microseconds_sum 1.7560473e+06
482488
request_duration_microseconds_count 2693
483489
`,
484490
},
485-
}
486-
487-
for i, scenario := range scenarios {
488-
out := bytes.NewBuffer(make([]byte, 0, len(scenario.out)))
489-
490-
n, err := MetricFamilyToOpenMetrics(out, scenario.in)
491-
if err != nil {
492-
t.Errorf("%d. error: %s", i, err)
493-
continue
494-
}
495-
if expected, got := len(scenario.out), n; expected != got {
496-
t.Errorf(
497-
"%d. expected %d bytes written, got %d",
498-
i, expected, got,
499-
)
500-
}
501-
if expected, got := scenario.out, out.String(); expected != got {
502-
t.Errorf(
503-
"%d. expected out=%q, got %q",
504-
i, expected, got,
505-
)
506-
}
507-
}
508-
509-
}
510-
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.
491+
// 11: No metric unit opted in no unit in name
537492
{
538493
in: &dto.MetricFamily{
539494
Name: proto.String("name_total"),
@@ -542,44 +497,21 @@ func TestCreateOpenMatricsWithUnit(t *testing.T) {
542497
Unit: proto.String("seconds"),
543498
Metric: []*dto.Metric{},
544499
},
545-
out: `# HELP name_seconds doc string
546-
# TYPE name_seconds counter
547-
# UNIT name_seconds seconds
548-
`,
549-
},
550-
// 3: No metric plus unit wrong unit in name. // Can this happen at all?
551-
{
552-
in: &dto.MetricFamily{
553-
Name: proto.String("name_milliseconds_total"),
554-
Help: proto.String("doc string"),
555-
Type: dto.MetricType_COUNTER.Enum(),
556-
Unit: proto.String("seconds"),
557-
Metric: []*dto.Metric{},
558-
},
559-
out: `# HELP name_milliseconds_seconds doc string
560-
# TYPE name_milliseconds_seconds counter
561-
# UNIT name_milliseconds_seconds seconds
562-
`,
563-
},
564-
// 3: No metric plus unit already in name.
565-
{
566-
in: &dto.MetricFamily{
567-
Name: proto.String("name_seconds_total"),
568-
Help: proto.String("doc string"),
569-
Type: dto.MetricType_COUNTER.Enum(),
570-
Unit: proto.String("seconds"),
571-
Metric: []*dto.Metric{},
572-
},
500+
withUnit: true,
573501
out: `# HELP name_seconds doc string
574502
# TYPE name_seconds counter
575503
# UNIT name_seconds seconds
576504
`,
577505
},
578506
}
507+
579508
for i, scenario := range scenarios {
580509
out := bytes.NewBuffer(make([]byte, 0, len(scenario.out)))
581-
582-
n, err := MetricFamilyToOpenMetrics(out, scenario.in, ToOpenMetricsWithUnit())
510+
opts := []ToOpenMetricsOption{}
511+
if scenario.withUnit {
512+
opts = append(opts, ToOpenMetricsWithUnit())
513+
}
514+
n, err := MetricFamilyToOpenMetrics(out, scenario.in, opts...)
583515
if err != nil {
584516
t.Errorf("%d. error: %s", i, err)
585517
continue

0 commit comments

Comments
 (0)