@@ -183,6 +183,30 @@ http_requests_total 1027 1234567891 st@1234567890 # {trace_id="1234"} 1 12345678
183183 },
184184 out : `# TYPE http_requests_total counter
185185http_requests_total 1027
186+ ` ,
187+ },
188+ {
189+ name : "CounterWithNaNExemplar" ,
190+ in : & dto.MetricFamily {
191+ Name : proto .String ("http_requests_total" ),
192+ Type : dto .MetricType_COUNTER .Enum (),
193+ Metric : []* dto.Metric {
194+ {
195+ Counter : & dto.Counter {
196+ Value : proto .Float64 (1027 ),
197+ Exemplar : & dto.Exemplar {
198+ Label : []* dto.LabelPair {
199+ {Name : proto .String ("trace_id" ), Value : proto .String ("1234" )},
200+ },
201+ Value : proto .Float64 (math .NaN ()),
202+ Timestamp : & timestamppb.Timestamp {Seconds : 1234567890 },
203+ },
204+ },
205+ },
206+ },
207+ },
208+ out : `# TYPE http_requests_total counter
209+ http_requests_total 1027 # {trace_id="1234"} NaN 1234567890
186210` ,
187211 },
188212 {
@@ -379,6 +403,98 @@ func TestCreateOpenMetrics20_Errors(t *testing.T) {
379403 },
380404 },
381405 },
406+ {
407+ name : "CounterValueNaN" ,
408+ in : & dto.MetricFamily {
409+ Name : proto .String ("test_counter_total" ),
410+ Type : dto .MetricType_COUNTER .Enum (),
411+ Metric : []* dto.Metric {
412+ {Counter : & dto.Counter {Value : proto .Float64 (math .NaN ())}},
413+ },
414+ },
415+ },
416+ {
417+ name : "CounterValueNegative" ,
418+ in : & dto.MetricFamily {
419+ Name : proto .String ("test_counter_total" ),
420+ Type : dto .MetricType_COUNTER .Enum (),
421+ Metric : []* dto.Metric {
422+ {Counter : & dto.Counter {Value : proto .Float64 (- 5.0 )}},
423+ },
424+ },
425+ },
426+ {
427+ name : "DuplicateLabelName" ,
428+ in : & dto.MetricFamily {
429+ Name : proto .String ("test_counter_total" ),
430+ Type : dto .MetricType_COUNTER .Enum (),
431+ Metric : []* dto.Metric {
432+ {
433+ Label : []* dto.LabelPair {
434+ {Name : proto .String ("foo" ), Value : proto .String ("bar" )},
435+ {Name : proto .String ("foo" ), Value : proto .String ("baz" )},
436+ },
437+ Counter : & dto.Counter {Value : proto .Float64 (1.0 )},
438+ },
439+ },
440+ },
441+ },
442+ {
443+ name : "EmptyLabelName" ,
444+ in : & dto.MetricFamily {
445+ Name : proto .String ("test_counter_total" ),
446+ Type : dto .MetricType_COUNTER .Enum (),
447+ Metric : []* dto.Metric {
448+ {
449+ Label : []* dto.LabelPair {
450+ {Name : proto .String ("" ), Value : proto .String ("bar" )},
451+ },
452+ Counter : & dto.Counter {Value : proto .Float64 (1.0 )},
453+ },
454+ },
455+ },
456+ },
457+ {
458+ name : "NewlineInMetricName" ,
459+ in : & dto.MetricFamily {
460+ Name : proto .String ("test_counter\n total" ),
461+ Type : dto .MetricType_COUNTER .Enum (),
462+ Metric : []* dto.Metric {
463+ {Counter : & dto.Counter {Value : proto .Float64 (1.0 )}},
464+ },
465+ },
466+ },
467+ {
468+ name : "InvalidUTF8MetricName" ,
469+ in : & dto.MetricFamily {
470+ Name : proto .String ("test_\xff _total" ),
471+ Type : dto .MetricType_COUNTER .Enum (),
472+ Metric : []* dto.Metric {
473+ {Counter : & dto.Counter {Value : proto .Float64 (1.0 )}},
474+ },
475+ },
476+ },
477+ {
478+ name : "NilMetric" ,
479+ in : & dto.MetricFamily {
480+ Name : proto .String ("test_counter_total" ),
481+ Type : dto .MetricType_COUNTER .Enum (),
482+ Metric : []* dto.Metric {nil },
483+ },
484+ },
485+ {
486+ name : "NilLabelPair" ,
487+ in : & dto.MetricFamily {
488+ Name : proto .String ("test_counter_total" ),
489+ Type : dto .MetricType_COUNTER .Enum (),
490+ Metric : []* dto.Metric {
491+ {
492+ Label : []* dto.LabelPair {nil },
493+ Counter : & dto.Counter {Value : proto .Float64 (1.0 )},
494+ },
495+ },
496+ },
497+ },
382498 }
383499
384500 for _ , tc := range tests {
@@ -437,3 +553,71 @@ http_requests_total 1027
437553 t .Errorf ("expected %q, got %q" , expected , buf .String ())
438554 }
439555}
556+
557+ func benchmarkCounterMetricFamily () * dto.MetricFamily {
558+ return & dto.MetricFamily {
559+ Name : proto .String ("http_requests_total" ),
560+ Help : proto .String ("Total number of HTTP requests." ),
561+ Type : dto .MetricType_COUNTER .Enum (),
562+ Unit : proto .String ("requests" ),
563+ Metric : []* dto.Metric {
564+ {
565+ Label : []* dto.LabelPair {
566+ {Name : proto .String ("method" ), Value : proto .String ("GET" )},
567+ {Name : proto .String ("handler" ), Value : proto .String ("/api/v1/query" )},
568+ {Name : proto .String ("status" ), Value : proto .String ("200" )},
569+ },
570+ Counter : & dto.Counter {
571+ Value : proto .Float64 (1027 ),
572+ CreatedTimestamp : & timestamppb.Timestamp {Seconds : 1234567890 },
573+ Exemplar : & dto.Exemplar {
574+ Label : []* dto.LabelPair {
575+ {Name : proto .String ("trace_id" ), Value : proto .String ("1234567890abcdef" )},
576+ },
577+ Value : proto .Float64 (1 ),
578+ Timestamp : & timestamppb.Timestamp {Seconds : 1234567890 , Nanos : 500000000 },
579+ },
580+ },
581+ TimestampMs : proto .Int64 (1234567891000 ),
582+ },
583+ {
584+ Label : []* dto.LabelPair {
585+ {Name : proto .String ("method" ), Value : proto .String ("POST" )},
586+ {Name : proto .String ("handler" ), Value : proto .String ("/api/v1/query" )},
587+ {Name : proto .String ("status" ), Value : proto .String ("500" )},
588+ },
589+ Counter : & dto.Counter {
590+ Value : proto .Float64 (3 ),
591+ CreatedTimestamp : & timestamppb.Timestamp {Seconds : 1234567890 },
592+ },
593+ TimestampMs : proto .Int64 (1234567891000 ),
594+ },
595+ },
596+ }
597+ }
598+
599+ func BenchmarkOpenMetrics20Create (b * testing.B ) {
600+ mf := benchmarkCounterMetricFamily ()
601+ out := bytes .NewBuffer (make ([]byte , 0 , 1024 ))
602+ b .ResetTimer ()
603+ for i := 0 ; i < b .N ; i ++ {
604+ _ , err := MetricFamilyToOpenMetrics20 (out , mf )
605+ if err != nil {
606+ b .Fatal (err )
607+ }
608+ out .Reset ()
609+ }
610+ }
611+
612+ func BenchmarkOpenMetricsCreate_Counter (b * testing.B ) {
613+ mf := benchmarkCounterMetricFamily ()
614+ out := bytes .NewBuffer (make ([]byte , 0 , 1024 ))
615+ b .ResetTimer ()
616+ for i := 0 ; i < b .N ; i ++ {
617+ _ , err := MetricFamilyToOpenMetrics (out , mf )
618+ if err != nil {
619+ b .Fatal (err )
620+ }
621+ out .Reset ()
622+ }
623+ }
0 commit comments