@@ -344,8 +344,21 @@ func TestEscapedEncode(t *testing.T) {
344
344
t .Errorf ("expected the output bytes buffer to be non-empty" )
345
345
}
346
346
347
- buff .Reset ()
347
+ dec , err := NewDecoder (bytes .NewReader (out ), FmtProtoDelim )
348
+ if err != nil {
349
+ t .Errorf ("unexpected error calling NewDecoder: %s" , err .Error ())
350
+ }
351
+ var gotFamily dto.MetricFamily
352
+ err = dec .Decode (& gotFamily )
353
+ if err != nil {
354
+ t .Errorf ("unexpected error during proto decode: %s" , err .Error ())
355
+ }
356
+ expectEscapedName := "foo_metric"
357
+ if gotFamily .GetName () != expectEscapedName {
358
+ t .Errorf ("incorrect encoded metric name, want %v, got %v" , expectEscapedName , gotFamily .GetName ())
359
+ }
348
360
361
+ buff .Reset ()
349
362
compactEncoder := NewEncoder (& buff , FmtProtoCompact )
350
363
err = compactEncoder .Encode (metric )
351
364
if err != nil {
@@ -356,9 +369,19 @@ func TestEscapedEncode(t *testing.T) {
356
369
if len (out ) == 0 {
357
370
t .Errorf ("expected the output bytes buffer to be non-empty" )
358
371
}
372
+ dec , err = NewDecoder (bytes .NewReader (out ), FmtProtoCompact )
373
+ if err != nil {
374
+ t .Errorf ("unexpected error calling NewDecoder: %s" , err .Error ())
375
+ }
376
+ err = dec .Decode (& gotFamily )
377
+ if err != nil {
378
+ t .Errorf ("unexpected error during proto decode: %s" , err .Error ())
379
+ }
380
+ if gotFamily .GetName () != "foo_metric" {
381
+ t .Errorf ("incorrect encoded metricname , want foo_metric, got %v" , gotFamily .GetName ())
382
+ }
359
383
360
384
buff .Reset ()
361
-
362
385
protoTextEncoder := NewEncoder (& buff , FmtProtoText )
363
386
err = protoTextEncoder .Encode (metric )
364
387
if err != nil {
@@ -369,9 +392,19 @@ func TestEscapedEncode(t *testing.T) {
369
392
if len (out ) == 0 {
370
393
t .Errorf ("expected the output bytes buffer to be non-empty" )
371
394
}
395
+ dec , err = NewDecoder (bytes .NewReader (out ), FmtProtoText )
396
+ if err != nil {
397
+ t .Errorf ("unexpected error calling NewDecoder: %s" , err .Error ())
398
+ }
399
+ err = dec .Decode (& gotFamily )
400
+ if err != nil {
401
+ t .Errorf ("unexpected error during proto decode: %s" , err .Error ())
402
+ }
403
+ if gotFamily .GetName () != "foo_metric" {
404
+ t .Errorf ("incorrect encoded metric name, want foo_metic, got %v" , gotFamily .GetName ())
405
+ }
372
406
373
407
buff .Reset ()
374
-
375
408
textEncoder := NewEncoder (& buff , FmtText )
376
409
err = textEncoder .Encode (metric )
377
410
if err != nil {
@@ -392,3 +425,116 @@ foo_metric{dotted_label_name="my.label.value"} 8
392
425
t .Errorf ("expected TextEncoder to return %s, but got %s instead" , expected , string (out ))
393
426
}
394
427
}
428
+
429
+ func TestDottedEncode (t * testing.T ) {
430
+ //nolint:staticcheck
431
+ model .NameValidationScheme = model .UTF8Validation
432
+ metric := & dto.MetricFamily {
433
+ Name : proto .String ("foo.metric" ),
434
+ Type : dto .MetricType_COUNTER .Enum (),
435
+ Metric : []* dto.Metric {
436
+ {
437
+ Counter : & dto.Counter {
438
+ Value : proto .Float64 (1.234 ),
439
+ },
440
+ },
441
+ {
442
+ Label : []* dto.LabelPair {
443
+ {
444
+ Name : proto .String ("dotted.label.name" ),
445
+ Value : proto .String ("my.label.value" ),
446
+ },
447
+ },
448
+ Counter : & dto.Counter {
449
+ Value : proto .Float64 (8 ),
450
+ },
451
+ },
452
+ },
453
+ }
454
+
455
+ scenarios := []struct {
456
+ format Format
457
+ expectMetricName string
458
+ expectLabelName string
459
+ }{
460
+ {
461
+ format : FmtProtoDelim ,
462
+ expectMetricName : "foo_metric" ,
463
+ expectLabelName : "dotted_label_name" ,
464
+ },
465
+ {
466
+ format : FmtProtoDelim .WithEscapingScheme (model .NoEscaping ),
467
+ expectMetricName : "foo.metric" ,
468
+ expectLabelName : "dotted.label.name" ,
469
+ },
470
+ {
471
+ format : FmtProtoDelim .WithEscapingScheme (model .DotsEscaping ),
472
+ expectMetricName : "foo_dot_metric" ,
473
+ expectLabelName : "dotted_dot_label_dot_name" ,
474
+ },
475
+ {
476
+ format : FmtProtoCompact ,
477
+ expectMetricName : "foo_metric" ,
478
+ expectLabelName : "dotted_label_name" ,
479
+ },
480
+ {
481
+ format : FmtProtoCompact .WithEscapingScheme (model .NoEscaping ),
482
+ expectMetricName : "foo.metric" ,
483
+ expectLabelName : "dotted.label.name" ,
484
+ },
485
+ {
486
+ format : FmtProtoCompact .WithEscapingScheme (model .ValueEncodingEscaping ),
487
+ expectMetricName : "U__foo_2e_metric" ,
488
+ expectLabelName : "U__dotted_2e_label_2e_name" ,
489
+ },
490
+ {
491
+ format : FmtProtoText ,
492
+ expectMetricName : "foo_metric" ,
493
+ expectLabelName : "dotted_label_name" ,
494
+ },
495
+ {
496
+ format : FmtProtoText .WithEscapingScheme (model .NoEscaping ),
497
+ expectMetricName : "foo.metric" ,
498
+ expectLabelName : "dotted.label.name" ,
499
+ },
500
+ {
501
+ format : FmtText ,
502
+ expectMetricName : "foo_metric" ,
503
+ expectLabelName : "dotted_label_name" ,
504
+ },
505
+ {
506
+ format : FmtText .WithEscapingScheme (model .NoEscaping ),
507
+ expectMetricName : "foo.metric" ,
508
+ expectLabelName : "dotted.label.name" ,
509
+ },
510
+ // common library does not support open metrics parsing so we do not test
511
+ // that here.
512
+ }
513
+
514
+ for i , scenario := range scenarios {
515
+ out := bytes .NewBuffer (make ([]byte , 0 ))
516
+ enc := NewEncoder (out , scenario .format )
517
+ err := enc .Encode (metric )
518
+ if err != nil {
519
+ t .Errorf ("%d. error: %s" , i , err )
520
+ continue
521
+ }
522
+
523
+ dec , err := NewDecoder (bytes .NewReader (out .Bytes ()), scenario .format )
524
+ if err != nil {
525
+ t .Errorf ("unexpected error calling NewDecoder: %s" , err .Error ())
526
+ }
527
+ var gotFamily dto.MetricFamily
528
+ err = dec .Decode (& gotFamily )
529
+ if err != nil {
530
+ t .Errorf ("%v: unexpected error during decode: %s" , scenario .format , err .Error ())
531
+ }
532
+ if gotFamily .GetName () != scenario .expectMetricName {
533
+ t .Errorf ("%v: incorrect encoded metric name, want %v, got %v" , scenario .format , scenario .expectMetricName , gotFamily .GetName ())
534
+ }
535
+ lName := gotFamily .GetMetric ()[1 ].Label [0 ].GetName ()
536
+ if lName != scenario .expectLabelName {
537
+ t .Errorf ("%v: incorrect encoded label name, want %v, got %v" , scenario .format , scenario .expectLabelName , lName )
538
+ }
539
+ }
540
+ }
0 commit comments