@@ -34,7 +34,7 @@ func TestCreateOpenMetrics(t *testing.T) {
34
34
35
35
var scenarios = []struct {
36
36
in * dto.MetricFamily
37
- withUnit ToOpenMetricsOption
37
+ withUnit bool
38
38
out string
39
39
}{
40
40
// 0: Counter, timestamp given, no _total suffix.
@@ -236,6 +236,7 @@ summary_name_count{name_1="value 1",name_2="value 2"} 4711
236
236
Name : proto .String ("request_duration_microseconds" ),
237
237
Help : proto .String ("The response latency." ),
238
238
Type : dto .MetricType_HISTOGRAM .Enum (),
239
+ Unit : proto .String ("microseconds" ),
239
240
Metric : []* dto.Metric {
240
241
& dto.Metric {
241
242
Histogram : & dto.Histogram {
@@ -267,8 +268,10 @@ summary_name_count{name_1="value 1",name_2="value 2"} 4711
267
268
},
268
269
},
269
270
},
271
+ withUnit : true ,
270
272
out : `# HELP request_duration_microseconds The response latency.
271
273
# TYPE request_duration_microseconds histogram
274
+ # UNIT request_duration_microseconds microseconds
272
275
request_duration_microseconds_bucket{le="100.0"} 123
273
276
request_duration_microseconds_bucket{le="120.0"} 412
274
277
request_duration_microseconds_bucket{le="144.0"} 592
@@ -284,6 +287,7 @@ request_duration_microseconds_count 2693
284
287
Name : proto .String ("request_duration_microseconds" ),
285
288
Help : proto .String ("The response latency." ),
286
289
Type : dto .MetricType_HISTOGRAM .Enum (),
290
+ Unit : proto .String ("microseconds" ),
287
291
Metric : []* dto.Metric {
288
292
& dto.Metric {
289
293
Histogram : & dto.Histogram {
@@ -429,8 +433,10 @@ foos_total 42.0
429
433
Unit : proto .String ("seconds" ),
430
434
Metric : []* dto.Metric {},
431
435
},
436
+ withUnit : true ,
432
437
out : `# HELP name_seconds doc string
433
438
# TYPE name_seconds counter
439
+ # UNIT name_seconds seconds
434
440
` ,
435
441
},
436
442
// 10: Histogram plus unit.
@@ -482,58 +488,7 @@ request_duration_microseconds_sum 1.7560473e+06
482
488
request_duration_microseconds_count 2693
483
489
` ,
484
490
},
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
537
492
{
538
493
in : & dto.MetricFamily {
539
494
Name : proto .String ("name_total" ),
@@ -542,44 +497,21 @@ func TestCreateOpenMatricsWithUnit(t *testing.T) {
542
497
Unit : proto .String ("seconds" ),
543
498
Metric : []* dto.Metric {},
544
499
},
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 ,
573
501
out : `# HELP name_seconds doc string
574
502
# TYPE name_seconds counter
575
503
# UNIT name_seconds seconds
576
504
` ,
577
505
},
578
506
}
507
+
579
508
for i , scenario := range scenarios {
580
509
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 ... )
583
515
if err != nil {
584
516
t .Errorf ("%d. error: %s" , i , err )
585
517
continue
0 commit comments