1
1
package io .prometheus .metrics .expositionformats ;
2
2
3
+ import io .prometheus .metrics .model .snapshots .*;
3
4
import io .prometheus .metrics .shaded .com_google_protobuf_3_21_7 .TextFormat ;
4
5
import io .prometheus .metrics .expositionformats .generated .com_google_protobuf_3_21_7 .Metrics ;
5
- import io .prometheus .metrics .model .snapshots .CounterSnapshot ;
6
6
import io .prometheus .metrics .model .snapshots .CounterSnapshot .CounterDataPointSnapshot ;
7
- import io .prometheus .metrics .model .snapshots .Exemplar ;
8
- import io .prometheus .metrics .model .snapshots .Exemplars ;
9
- import io .prometheus .metrics .model .snapshots .ClassicHistogramBuckets ;
10
- import io .prometheus .metrics .model .snapshots .GaugeSnapshot ;
11
7
import io .prometheus .metrics .model .snapshots .GaugeSnapshot .GaugeDataPointSnapshot ;
12
- import io .prometheus .metrics .model .snapshots .HistogramSnapshot ;
13
- import io .prometheus .metrics .model .snapshots .InfoSnapshot ;
14
- import io .prometheus .metrics .model .snapshots .Labels ;
15
- import io .prometheus .metrics .model .snapshots .MetricSnapshot ;
16
- import io .prometheus .metrics .model .snapshots .MetricSnapshots ;
17
- import io .prometheus .metrics .model .snapshots .NativeHistogramBuckets ;
18
- import io .prometheus .metrics .model .snapshots .Quantiles ;
19
- import io .prometheus .metrics .model .snapshots .StateSetSnapshot ;
20
- import io .prometheus .metrics .model .snapshots .SummarySnapshot ;
21
8
import io .prometheus .metrics .model .snapshots .SummarySnapshot .SummaryDataPointSnapshot ;
22
- import io .prometheus .metrics .model .snapshots .Unit ;
23
- import io .prometheus .metrics .model .snapshots .UnknownSnapshot ;
24
9
import io .prometheus .metrics .model .snapshots .UnknownSnapshot .UnknownDataPointSnapshot ;
25
10
import org .junit .Assert ;
26
11
import org .junit .Test ;
@@ -140,6 +125,7 @@ public void testCounterComplete() throws IOException {
140
125
"timestamp_ms: 1672850585820 " +
141
126
"}" ;
142
127
//@formatter:on
128
+ PrometheusNaming .nameValidationScheme = ValidationScheme .LegacyValidation ;
143
129
144
130
CounterSnapshot counter = CounterSnapshot .builder ()
145
131
.name ("service_time_seconds" )
@@ -184,6 +170,7 @@ public void testCounterMinimal() throws IOException {
184
170
"my_counter_total 1.1\n " ;
185
171
String prometheusProtobuf = "" +
186
172
"name: \" my_counter_total\" type: COUNTER metric { counter { value: 1.1 } }" ;
173
+ PrometheusNaming .nameValidationScheme = ValidationScheme .LegacyValidation ;
187
174
CounterSnapshot counter = CounterSnapshot .builder ()
188
175
.name ("my_counter" )
189
176
.dataPoint (CounterDataPointSnapshot .builder ().value (1.1 ).build ())
@@ -215,6 +202,7 @@ public void testCounterWithDots() throws IOException {
215
202
"} " +
216
203
"}" ;
217
204
//@formatter:on
205
+ PrometheusNaming .nameValidationScheme = ValidationScheme .LegacyValidation ;
218
206
219
207
CounterSnapshot counter = CounterSnapshot .builder ()
220
208
.name ("my.request.count" )
@@ -260,6 +248,7 @@ public void testGaugeComplete() throws IOException {
260
248
"timestamp_ms: 1672850585820 " +
261
249
"}" ;
262
250
//@formatter:on
251
+ PrometheusNaming .nameValidationScheme = ValidationScheme .LegacyValidation ;
263
252
GaugeSnapshot gauge = GaugeSnapshot .builder ()
264
253
.name ("disk_usage_ratio" )
265
254
.help ("percentage used" )
@@ -299,6 +288,7 @@ public void testGaugeMinimal() throws IOException {
299
288
"temperature_centigrade 22.3\n " ;
300
289
String prometheusProtobuf = "" +
301
290
"name: \" temperature_centigrade\" type: GAUGE metric { gauge { value: 22.3 } }" ;
291
+ PrometheusNaming .nameValidationScheme = ValidationScheme .LegacyValidation ;
302
292
GaugeSnapshot gauge = GaugeSnapshot .builder ()
303
293
.name ("temperature_centigrade" )
304
294
.dataPoint (GaugeDataPointSnapshot .builder ()
@@ -336,6 +326,7 @@ public void testGaugeWithDots() throws IOException {
336
326
"} " +
337
327
"}" ;
338
328
//@formatter:on
329
+ PrometheusNaming .nameValidationScheme = ValidationScheme .LegacyValidation ;
339
330
340
331
GaugeSnapshot gauge = GaugeSnapshot .builder ()
341
332
.name ("my.temperature.celsius" )
@@ -354,6 +345,36 @@ public void testGaugeWithDots() throws IOException {
354
345
assertPrometheusProtobuf (prometheusProtobuf , gauge );
355
346
}
356
347
348
+ @ Test
349
+ public void testGaugeUTF8 () throws IOException {
350
+ String prometheusText =
351
+ "# HELP \" gauge.name\" gauge\\ ndoc\\ nstr\" ing\n " +
352
+ "# TYPE \" gauge.name\" gauge\n " +
353
+ "{\" gauge.name\" ,\" name*2\" =\" val with \\ \\ backslash and \\ \" quotes\\ \" \" ,\" name.1\" =\" val with\\ nnew line\" } +Inf\n " +
354
+ "{\" gauge.name\" ,\" name*2\" =\" 佖佥\" ,\" name.1\" =\" Björn\" } 3.14E42\n " ;
355
+ PrometheusNaming .nameValidationScheme = ValidationScheme .UTF8Validation ;
356
+
357
+ GaugeSnapshot gauge = GaugeSnapshot .builder ()
358
+ .name ("gauge.name" )
359
+ .help ("gauge\n doc\n str\" ing" )
360
+ .dataPoint (GaugeDataPointSnapshot .builder ()
361
+ .value (Double .POSITIVE_INFINITY )
362
+ .labels (Labels .builder ()
363
+ .label ("name.1" , "val with\n new line" )
364
+ .label ("name*2" , "val with \\ backslash and \" quotes\" " )
365
+ .build ())
366
+ .build ())
367
+ .dataPoint (GaugeDataPointSnapshot .builder ()
368
+ .value (3.14e42 )
369
+ .labels (Labels .builder ()
370
+ .label ("name.1" , "Björn" )
371
+ .label ("name*2" , "佖佥" )
372
+ .build ())
373
+ .build ())
374
+ .build ();
375
+ assertPrometheusText (prometheusText , gauge );
376
+ }
377
+
357
378
@ Test
358
379
public void testSummaryComplete () throws IOException {
359
380
String openMetricsText = "" +
@@ -445,6 +466,7 @@ public void testSummaryComplete() throws IOException {
445
466
"timestamp_ms: 1672850585820 " +
446
467
"}" ;
447
468
//@formatter:on
469
+ PrometheusNaming .nameValidationScheme = ValidationScheme .LegacyValidation ;
448
470
SummarySnapshot summary = SummarySnapshot .builder ()
449
471
.name ("http_request_duration_seconds" )
450
472
.help ("request duration" )
@@ -513,6 +535,7 @@ public void testSummaryWithoutQuantiles() throws IOException {
513
535
"} " +
514
536
"}" ;
515
537
//@formatter:on
538
+ PrometheusNaming .nameValidationScheme = ValidationScheme .LegacyValidation ;
516
539
SummarySnapshot summary = SummarySnapshot .builder ()
517
540
.name ("latency_seconds" )
518
541
.help ("latency" )
@@ -548,6 +571,7 @@ public void testSummaryNoCountAndSum() throws IOException {
548
571
"} " +
549
572
"}" ;
550
573
//@formatter:on
574
+ PrometheusNaming .nameValidationScheme = ValidationScheme .LegacyValidation ;
551
575
SummarySnapshot summary = SummarySnapshot .builder ()
552
576
.name ("latency_seconds" )
553
577
.dataPoint (SummaryDataPointSnapshot .builder ()
@@ -580,6 +604,7 @@ public void testSummaryJustCount() throws IOException {
580
604
"} " +
581
605
"}" ;
582
606
//@formatter:on
607
+ PrometheusNaming .nameValidationScheme = ValidationScheme .LegacyValidation ;
583
608
SummarySnapshot summary = SummarySnapshot .builder ()
584
609
.name ("latency_seconds" )
585
610
.dataPoint (SummaryDataPointSnapshot .builder ()
@@ -612,6 +637,7 @@ public void testSummaryJustSum() throws IOException {
612
637
"} " +
613
638
"}" ;
614
639
//@formatter:on
640
+ PrometheusNaming .nameValidationScheme = ValidationScheme .LegacyValidation ;
615
641
SummarySnapshot summary = SummarySnapshot .builder ()
616
642
.name ("latency_seconds" )
617
643
.dataPoint (SummaryDataPointSnapshot .builder ()
@@ -627,6 +653,7 @@ public void testSummaryJustSum() throws IOException {
627
653
628
654
@ Test
629
655
public void testSummaryEmptyData () throws IOException {
656
+ PrometheusNaming .nameValidationScheme = ValidationScheme .LegacyValidation ;
630
657
// SummaryData can be present but empty (no count, no sum, no quantiles).
631
658
// This should be treated like no data is present.
632
659
SummarySnapshot summary = SummarySnapshot .builder ()
@@ -665,6 +692,7 @@ public void testSummaryEmptyAndNonEmpty() throws IOException {
665
692
"} " +
666
693
"}" ;
667
694
//@formatter:on
695
+ PrometheusNaming .nameValidationScheme = ValidationScheme .LegacyValidation ;
668
696
SummarySnapshot summary = SummarySnapshot .builder ()
669
697
.name ("latency_seconds" )
670
698
.dataPoint (SummaryDataPointSnapshot .builder ()
@@ -710,6 +738,7 @@ public void testSummaryWithDots() throws IOException {
710
738
"summary { sample_count: 1 sample_sum: 0.03 } " +
711
739
"}" ;
712
740
//@formatter:on
741
+ PrometheusNaming .nameValidationScheme = ValidationScheme .LegacyValidation ;
713
742
714
743
SummarySnapshot summary = SummarySnapshot .builder ()
715
744
.name ("my.request.duration.seconds" )
@@ -831,6 +860,7 @@ public void testClassicHistogramComplete() throws Exception {
831
860
"} " +
832
861
"}" ;
833
862
//@formatter:on
863
+ PrometheusNaming .nameValidationScheme = ValidationScheme .LegacyValidation ;
834
864
HistogramSnapshot histogram = HistogramSnapshot .builder ()
835
865
.name ("response_size_bytes" )
836
866
.help ("help" )
@@ -892,6 +922,7 @@ public void testClassicHistogramMinimal() throws Exception {
892
922
"} " +
893
923
"}" ;
894
924
//@formatter:on
925
+ PrometheusNaming .nameValidationScheme = ValidationScheme .LegacyValidation ;
895
926
HistogramSnapshot histogram = HistogramSnapshot .builder ()
896
927
.name ("request_latency_seconds" )
897
928
.dataPoint (HistogramSnapshot .HistogramDataPointSnapshot .builder ()
@@ -935,6 +966,7 @@ public void testClassicHistogramCountAndSum() throws Exception {
935
966
"} " +
936
967
"}" ;
937
968
//@formatter:on
969
+ PrometheusNaming .nameValidationScheme = ValidationScheme .LegacyValidation ;
938
970
HistogramSnapshot histogram = HistogramSnapshot .builder ()
939
971
.name ("request_latency_seconds" )
940
972
.dataPoint (HistogramSnapshot .HistogramDataPointSnapshot .builder ()
@@ -1054,6 +1086,7 @@ public void testClassicGaugeHistogramComplete() throws IOException {
1054
1086
"} " +
1055
1087
"}" ;
1056
1088
//@formatter:on
1089
+ PrometheusNaming .nameValidationScheme = ValidationScheme .LegacyValidation ;
1057
1090
HistogramSnapshot gaugeHistogram = HistogramSnapshot .builder ()
1058
1091
.gaugeHistogram (true )
1059
1092
.name ("cache_size_bytes" )
@@ -1117,6 +1150,7 @@ public void testClassicGaugeHistogramMinimal() throws IOException {
1117
1150
"} " +
1118
1151
"}" ;
1119
1152
//@formatter:on
1153
+ PrometheusNaming .nameValidationScheme = ValidationScheme .LegacyValidation ;
1120
1154
HistogramSnapshot gaugeHistogram = HistogramSnapshot .builder ()
1121
1155
.gaugeHistogram (true )
1122
1156
.name ("queue_size_bytes" )
@@ -1163,6 +1197,7 @@ public void testClassicGaugeHistogramCountAndSum() throws IOException {
1163
1197
"} " +
1164
1198
"}" ;
1165
1199
//@formatter:on
1200
+ PrometheusNaming .nameValidationScheme = ValidationScheme .LegacyValidation ;
1166
1201
HistogramSnapshot gaugeHistogram = HistogramSnapshot .builder ()
1167
1202
.gaugeHistogram (true )
1168
1203
.name ("queue_size_bytes" )
@@ -1210,6 +1245,7 @@ public void testClassicHistogramWithDots() throws IOException {
1210
1245
"} " +
1211
1246
"}" ;
1212
1247
//@formatter:on
1248
+ PrometheusNaming .nameValidationScheme = ValidationScheme .LegacyValidation ;
1213
1249
1214
1250
HistogramSnapshot histogram = HistogramSnapshot .builder ()
1215
1251
.name ("my.request.duration.seconds" )
@@ -1337,6 +1373,7 @@ public void testNativeHistogramComplete() throws IOException {
1337
1373
"} " +
1338
1374
"}" ;
1339
1375
//@formatter:on
1376
+ PrometheusNaming .nameValidationScheme = ValidationScheme .LegacyValidation ;
1340
1377
HistogramSnapshot nativeHistogram = HistogramSnapshot .builder ()
1341
1378
.name ("response_size_bytes" )
1342
1379
.help ("help" )
@@ -1417,6 +1454,7 @@ public void testNativeHistogramMinimal() throws IOException {
1417
1454
"} " +
1418
1455
"}" ;
1419
1456
//@formatter:on
1457
+ PrometheusNaming .nameValidationScheme = ValidationScheme .LegacyValidation ;
1420
1458
HistogramSnapshot nativeHistogram = HistogramSnapshot .builder ()
1421
1459
.name ("latency_seconds" )
1422
1460
.dataPoint (HistogramSnapshot .HistogramDataPointSnapshot .builder ()
@@ -1463,6 +1501,7 @@ public void testNativeHistogramWithDots() throws IOException {
1463
1501
"} " +
1464
1502
"}" ;
1465
1503
//@formatter:on
1504
+ PrometheusNaming .nameValidationScheme = ValidationScheme .LegacyValidation ;
1466
1505
1467
1506
HistogramSnapshot histogram = HistogramSnapshot .builder ()
1468
1507
.name ("my.request.duration.seconds" )
@@ -1499,6 +1538,7 @@ public void testInfo() throws IOException {
1499
1538
"# HELP version_info version information\n " +
1500
1539
"# TYPE version_info gauge\n " +
1501
1540
"version_info{version=\" 1.2.3\" } 1\n " ;
1541
+ PrometheusNaming .nameValidationScheme = ValidationScheme .LegacyValidation ;
1502
1542
InfoSnapshot info = InfoSnapshot .builder ()
1503
1543
.name ("version" )
1504
1544
.help ("version information" )
@@ -1533,6 +1573,7 @@ public void testInfoWithDots() throws IOException {
1533
1573
"gauge { value: 1.0 } " +
1534
1574
"}" ;
1535
1575
//@formatter:on
1576
+ PrometheusNaming .nameValidationScheme = ValidationScheme .LegacyValidation ;
1536
1577
InfoSnapshot info = InfoSnapshot .builder ()
1537
1578
.name ("jvm.status" )
1538
1579
.help ("JVM status info" )
@@ -1562,6 +1603,7 @@ public void testStateSetComplete() throws IOException {
1562
1603
"state{env=\" dev\" ,state=\" state2\" } 0 " + scrapeTimestamp1s + "\n " +
1563
1604
"state{env=\" prod\" ,state=\" state1\" } 0 " + scrapeTimestamp2s + "\n " +
1564
1605
"state{env=\" prod\" ,state=\" state2\" } 1 " + scrapeTimestamp2s + "\n " ;
1606
+ PrometheusNaming .nameValidationScheme = ValidationScheme .LegacyValidation ;
1565
1607
StateSetSnapshot stateSet = StateSetSnapshot .builder ()
1566
1608
.name ("state" )
1567
1609
.help ("complete state set example" )
@@ -1595,6 +1637,7 @@ public void testStateSetMinimal() throws IOException {
1595
1637
"# TYPE state gauge\n " +
1596
1638
"state{state=\" a\" } 1\n " +
1597
1639
"state{state=\" bb\" } 0\n " ;
1640
+ PrometheusNaming .nameValidationScheme = ValidationScheme .LegacyValidation ;
1598
1641
StateSetSnapshot stateSet = StateSetSnapshot .builder ()
1599
1642
.name ("state" )
1600
1643
.dataPoint (StateSetSnapshot .StateSetDataPointSnapshot .builder ()
@@ -1636,6 +1679,7 @@ public void testStateSetWithDots() throws IOException {
1636
1679
"gauge { value: 0.0 } " +
1637
1680
"}" ;
1638
1681
//@formatter:on
1682
+ PrometheusNaming .nameValidationScheme = ValidationScheme .LegacyValidation ;
1639
1683
StateSetSnapshot stateSet = StateSetSnapshot .builder ()
1640
1684
.name ("my.application.state" )
1641
1685
.help ("My application state" )
@@ -1664,6 +1708,7 @@ public void testUnknownComplete() throws IOException {
1664
1708
"# TYPE my_special_thing_bytes untyped\n " +
1665
1709
"my_special_thing_bytes{env=\" dev\" } 0.2 " + scrapeTimestamp1s + "\n " +
1666
1710
"my_special_thing_bytes{env=\" prod\" } 0.7 " + scrapeTimestamp2s + "\n " ;
1711
+ PrometheusNaming .nameValidationScheme = ValidationScheme .LegacyValidation ;
1667
1712
UnknownSnapshot unknown = UnknownSnapshot .builder ()
1668
1713
.name ("my_special_thing_bytes" )
1669
1714
.help ("help message" )
@@ -1696,6 +1741,7 @@ public void testUnknownMinimal() throws IOException {
1696
1741
String prometheus = "" +
1697
1742
"# TYPE other untyped\n " +
1698
1743
"other 22.3\n " ;
1744
+ PrometheusNaming .nameValidationScheme = ValidationScheme .LegacyValidation ;
1699
1745
UnknownSnapshot unknown = UnknownSnapshot .builder ()
1700
1746
.name ("other" )
1701
1747
.dataPoint (UnknownDataPointSnapshot .builder ()
@@ -1730,6 +1776,7 @@ public void testUnknownWithDots() throws IOException {
1730
1776
"untyped { value: 0.7 } " +
1731
1777
"}" ;
1732
1778
//@formatter:on
1779
+ PrometheusNaming .nameValidationScheme = ValidationScheme .LegacyValidation ;
1733
1780
UnknownSnapshot unknown = UnknownSnapshot .builder ()
1734
1781
.name ("some.unknown.metric" )
1735
1782
.help ("help message" )
@@ -1756,6 +1803,7 @@ public void testHelpEscape() throws IOException {
1756
1803
"# HELP test_total Some text and \\ n some \" escaping\n " +
1757
1804
"# TYPE test_total counter\n " +
1758
1805
"test_total 1.0\n " ;
1806
+ PrometheusNaming .nameValidationScheme = ValidationScheme .LegacyValidation ;
1759
1807
CounterSnapshot counter = CounterSnapshot .builder ()
1760
1808
.name ("test" )
1761
1809
.help ("Some text and \n some \" escaping" ) // example from https://openMetrics.io
@@ -1776,6 +1824,7 @@ public void testLabelValueEscape() throws IOException {
1776
1824
String prometheus = "" +
1777
1825
"# TYPE test_total counter\n " +
1778
1826
"test_total{a=\" x\" ,b=\" escaping\\ \" example \\ n \" } 1.0\n " ;
1827
+ PrometheusNaming .nameValidationScheme = ValidationScheme .LegacyValidation ;
1779
1828
CounterSnapshot counter = CounterSnapshot .builder ()
1780
1829
.name ("test" )
1781
1830
.dataPoint (CounterDataPointSnapshot .builder ()
0 commit comments