@@ -33,8 +33,9 @@ func TestCreateOpenMetrics(t *testing.T) {
33
33
}
34
34
35
35
var scenarios = []struct {
36
- in * dto.MetricFamily
37
- out string
36
+ in * dto.MetricFamily
37
+ withUnit ToOpenMetricsOption
38
+ out string
38
39
}{
39
40
// 0: Counter, timestamp given, no _total suffix.
40
41
{
@@ -430,7 +431,6 @@ foos_total 42.0
430
431
},
431
432
out : `# HELP name_seconds doc string
432
433
# TYPE name_seconds counter
433
- # UNIT name_seconds seconds
434
434
` ,
435
435
},
436
436
// 10: Histogram plus unit.
@@ -473,7 +473,6 @@ foos_total 42.0
473
473
},
474
474
out : `# HELP request_duration_microseconds The response latency.
475
475
# TYPE request_duration_microseconds histogram
476
- # UNIT request_duration_microseconds microseconds
477
476
request_duration_microseconds_bucket{le="100.0"} 123
478
477
request_duration_microseconds_bucket{le="120.0"} 412
479
478
request_duration_microseconds_bucket{le="144.0"} 592
@@ -487,6 +486,7 @@ request_duration_microseconds_count 2693
487
486
488
487
for i , scenario := range scenarios {
489
488
out := bytes .NewBuffer (make ([]byte , 0 , len (scenario .out )))
489
+
490
490
n , err := MetricFamilyToOpenMetrics (out , scenario .in )
491
491
if err != nil {
492
492
t .Errorf ("%d. error: %s" , i , err )
@@ -508,6 +508,69 @@ request_duration_microseconds_count 2693
508
508
509
509
}
510
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.
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
+
511
574
func BenchmarkOpenMetricsCreate (b * testing.B ) {
512
575
mf := & dto.MetricFamily {
513
576
Name : proto .String ("request_duration_microseconds" ),
0 commit comments