|
1 | 1 | package io.prometheus.metrics.expositionformats; |
2 | 2 |
|
3 | | -import static java.nio.charset.StandardCharsets.UTF_8; |
4 | | -import static org.assertj.core.api.Assertions.assertThat; |
5 | | -import static org.mockito.Mockito.CALLS_REAL_METHODS; |
6 | | -import static org.mockito.Mockito.mockStatic; |
7 | | - |
8 | | -import io.prometheus.metrics.model.snapshots.*; |
9 | 3 | import io.prometheus.metrics.model.snapshots.ClassicHistogramBuckets; |
10 | 4 | import io.prometheus.metrics.model.snapshots.CounterSnapshot; |
11 | 5 | import io.prometheus.metrics.model.snapshots.CounterSnapshot.CounterDataPointSnapshot; |
| 6 | +import io.prometheus.metrics.model.snapshots.EscapingScheme; |
| 7 | +import io.prometheus.metrics.model.snapshots.Exemplar; |
| 8 | +import io.prometheus.metrics.model.snapshots.Exemplars; |
| 9 | +import io.prometheus.metrics.model.snapshots.GaugeSnapshot; |
12 | 10 | import io.prometheus.metrics.model.snapshots.GaugeSnapshot.GaugeDataPointSnapshot; |
| 11 | +import io.prometheus.metrics.model.snapshots.HistogramSnapshot; |
| 12 | +import io.prometheus.metrics.model.snapshots.InfoSnapshot; |
| 13 | +import io.prometheus.metrics.model.snapshots.Labels; |
| 14 | +import io.prometheus.metrics.model.snapshots.MetricSnapshot; |
| 15 | +import io.prometheus.metrics.model.snapshots.MetricSnapshots; |
| 16 | +import io.prometheus.metrics.model.snapshots.NativeHistogramBuckets; |
| 17 | +import io.prometheus.metrics.model.snapshots.PrometheusNaming; |
| 18 | +import io.prometheus.metrics.model.snapshots.Quantiles; |
| 19 | +import io.prometheus.metrics.model.snapshots.StateSetSnapshot; |
| 20 | +import io.prometheus.metrics.model.snapshots.SummarySnapshot; |
13 | 21 | import io.prometheus.metrics.model.snapshots.SummarySnapshot.SummaryDataPointSnapshot; |
| 22 | +import io.prometheus.metrics.model.snapshots.Unit; |
| 23 | +import io.prometheus.metrics.model.snapshots.UnknownSnapshot; |
14 | 24 | import io.prometheus.metrics.model.snapshots.UnknownSnapshot.UnknownDataPointSnapshot; |
15 | | -import java.io.ByteArrayOutputStream; |
16 | | -import java.io.IOException; |
17 | | -import java.util.concurrent.atomic.AtomicInteger; |
| 25 | +import org.junit.jupiter.api.AfterEach; |
18 | 26 | import org.junit.jupiter.api.Test; |
19 | 27 | import org.junit.jupiter.params.ParameterizedTest; |
20 | 28 | import org.junit.jupiter.params.provider.CsvSource; |
21 | | -import org.mockito.MockedStatic; |
| 29 | +import org.junitpioneer.jupiter.SetSystemProperty; |
| 30 | + |
| 31 | +import java.io.ByteArrayOutputStream; |
| 32 | +import java.io.IOException; |
| 33 | +import java.util.concurrent.atomic.AtomicInteger; |
| 34 | + |
| 35 | +import static java.nio.charset.StandardCharsets.UTF_8; |
| 36 | +import static org.assertj.core.api.Assertions.assertThat; |
22 | 37 |
|
23 | 38 | class ExpositionFormatsTest { |
24 | 39 |
|
@@ -85,6 +100,11 @@ class ExpositionFormatsTest { |
85 | 100 | .timestampMillis(1690298864383L) |
86 | 101 | .build(); |
87 | 102 |
|
| 103 | + @AfterEach |
| 104 | + void tearDown() { |
| 105 | + PrometheusNaming.resetForTest(); |
| 106 | + } |
| 107 | + |
88 | 108 | @Test |
89 | 109 | void init() { |
90 | 110 | ExpositionFormats formats = ExpositionFormats.init(); |
@@ -457,42 +477,37 @@ public void testGaugeWithDots() throws IOException { |
457 | 477 | assertPrometheusProtobuf(prometheusProtobuf, gauge); |
458 | 478 | } |
459 | 479 |
|
| 480 | + @SetSystemProperty(key = "io.prometheus.naming.validationScheme", value = "utf-8") |
460 | 481 | @Test |
461 | 482 | public void testGaugeUTF8() throws IOException { |
462 | | - try (MockedStatic<PrometheusNaming> mock = |
463 | | - mockStatic(PrometheusNaming.class, CALLS_REAL_METHODS)) { |
464 | | - mock.when(PrometheusNaming::getValidationScheme) |
465 | | - .thenReturn(ValidationScheme.UTF_8_VALIDATION); |
466 | | - |
467 | | - String prometheusText = |
468 | | - """ |
| 483 | + PrometheusNaming.resetForTest(); |
| 484 | + String prometheusText = |
| 485 | + """ |
469 | 486 | # HELP \"gauge.name\" gauge\\ndoc\\nstr\"ing |
470 | 487 | # TYPE \"gauge.name\" gauge |
471 | 488 | {\"gauge.name\",\"name*2\"=\"val with \\\\backslash and \\\"quotes\\\"\",\"name.1\"=\"val with\\nnew line\"} +Inf |
472 | 489 | {\"gauge.name\",\"name*2\"=\"佖佥\",\"name.1\"=\"Björn\"} 3.14E42 |
473 | 490 | """; |
474 | | - GaugeSnapshot gauge = |
475 | | - GaugeSnapshot.builder() |
476 | | - .name("gauge.name") |
477 | | - .help("gauge\ndoc\nstr\"ing") |
478 | | - .dataPoint( |
479 | | - GaugeDataPointSnapshot.builder() |
480 | | - .value(Double.POSITIVE_INFINITY) |
481 | | - .labels( |
482 | | - Labels.builder() |
483 | | - .label("name.1", "val with\nnew line") |
484 | | - .label("name*2", "val with \\backslash and \"quotes\"") |
485 | | - .build()) |
486 | | - .build()) |
487 | | - .dataPoint( |
488 | | - GaugeDataPointSnapshot.builder() |
489 | | - .value(3.14e42) |
490 | | - .labels( |
491 | | - Labels.builder().label("name.1", "Björn").label("name*2", "佖佥").build()) |
492 | | - .build()) |
493 | | - .build(); |
494 | | - assertPrometheusText(prometheusText, gauge); |
495 | | - } |
| 491 | + GaugeSnapshot gauge = |
| 492 | + GaugeSnapshot.builder() |
| 493 | + .name("gauge.name") |
| 494 | + .help("gauge\ndoc\nstr\"ing") |
| 495 | + .dataPoint( |
| 496 | + GaugeDataPointSnapshot.builder() |
| 497 | + .value(Double.POSITIVE_INFINITY) |
| 498 | + .labels( |
| 499 | + Labels.builder() |
| 500 | + .label("name.1", "val with\nnew line") |
| 501 | + .label("name*2", "val with \\backslash and \"quotes\"") |
| 502 | + .build()) |
| 503 | + .build()) |
| 504 | + .dataPoint( |
| 505 | + GaugeDataPointSnapshot.builder() |
| 506 | + .value(3.14e42) |
| 507 | + .labels(Labels.builder().label("name.1", "Björn").label("name*2", "佖佥").build()) |
| 508 | + .build()) |
| 509 | + .build(); |
| 510 | + assertPrometheusText(prometheusText, gauge); |
496 | 511 | } |
497 | 512 |
|
498 | 513 | @Test |
|
0 commit comments