@@ -52,7 +52,7 @@ func TestCreateOpenMetrics20(t *testing.T) {
5252 },
5353 out : `# HELP http_requests_total Total number of HTTP requests.
5454# TYPE http_requests_total counter
55- http_requests_total{method="GET",code="200"} 1027 st@1234567890
55+ http_requests_total{method="GET",code="200"} 1027.0 st@1234567890
5656` ,
5757 },
5858 {
@@ -76,7 +76,7 @@ http_requests_total{method="GET",code="200"} 1027 st@1234567890
7676 },
7777 out : `# HELP http_requests_total Total number of HTTP requests.
7878# TYPE http_requests_total counter
79- http_requests_total{method="GET",code="200"} 1027 st@1234567890.987654321
79+ http_requests_total{method="GET",code="200"} 1027.0 st@1234567890.987654321
8080` ,
8181 },
8282 {
@@ -160,7 +160,7 @@ node_memory_active_bytes 1.2345e+09 1234567890
160160 },
161161 },
162162 out : `# TYPE http_requests_total counter
163- http_requests_total 1027 1234567891 st@1234567890 # {trace_id="1234"} 1 1234567890.5
163+ http_requests_total 1027.0 1234567891 st@1234567890 # {trace_id="1234"} 1.0 1234567890.5
164164` ,
165165 },
166166 {
@@ -181,7 +181,7 @@ http_requests_total 1027 1234567891 st@1234567890 # {trace_id="1234"} 1 12345678
181181 },
182182 },
183183 out : `# TYPE http_requests_total counter
184- http_requests_total 1027 # {} 1 1234567890.5
184+ http_requests_total 1027.0 # {} 1.0 1234567890.5
185185` ,
186186 },
187187 {
@@ -204,7 +204,7 @@ http_requests_total 1027 # {} 1 1234567890.5
204204 },
205205 },
206206 out : `# TYPE http_requests_total counter
207- http_requests_total 1027
207+ http_requests_total 1027.0
208208` ,
209209 },
210210 {
@@ -228,7 +228,7 @@ http_requests_total 1027
228228 },
229229 },
230230 out : `# TYPE http_requests_total counter
231- http_requests_total 1027 # {trace_id="1234"} NaN 1234567890
231+ http_requests_total 1027.0 # {trace_id="1234"} NaN 1234567890
232232` ,
233233 },
234234 {
@@ -262,7 +262,7 @@ test_metric 1.23
262262 },
263263 },
264264 out : `# TYPE http_requests_total counter
265- http_requests_total 1027
265+ http_requests_total 1027.0
266266` ,
267267 },
268268 {
@@ -282,7 +282,7 @@ http_requests_total 1027
282282 },
283283 },
284284 out : `# TYPE "你好_total" counter
285- {"你好_total","🌎"="🌍"} 1027
285+ {"你好_total","🌎"="🌍"} 1027.0
286286` ,
287287 },
288288 }
@@ -491,6 +491,30 @@ func TestCreateOpenMetrics20_Errors(t *testing.T) {
491491 },
492492 expectedErr : "contains raw newlines" ,
493493 },
494+ {
495+ name : "NewlineInUnit" ,
496+ in : & dto.MetricFamily {
497+ Name : proto .String ("test_counter_total" ),
498+ Type : dto .MetricType_COUNTER .Enum (),
499+ Unit : proto .String ("seconds\n " ),
500+ Metric : []* dto.Metric {
501+ {Counter : & dto.Counter {Value : proto .Float64 (1.0 )}},
502+ },
503+ },
504+ expectedErr : "contains raw newlines" ,
505+ },
506+ {
507+ name : "CarriageReturnInUnit" ,
508+ in : & dto.MetricFamily {
509+ Name : proto .String ("test_counter_total" ),
510+ Type : dto .MetricType_COUNTER .Enum (),
511+ Unit : proto .String ("seconds\r " ),
512+ Metric : []* dto.Metric {
513+ {Counter : & dto.Counter {Value : proto .Float64 (1.0 )}},
514+ },
515+ },
516+ expectedErr : "contains raw newlines" ,
517+ },
494518 {
495519 name : "NilMetric" ,
496520 in : & dto.MetricFamily {
@@ -614,7 +638,7 @@ func TestCreateOpenMetrics20_SimpleWriter(t *testing.T) {
614638 }
615639
616640 expected := `# TYPE http_requests_total counter
617- http_requests_total 1027
641+ http_requests_total 1027.0
618642`
619643 if buf .String () != expected {
620644 t .Errorf ("expected %q, got %q" , expected , buf .String ())
0 commit comments