Skip to content

Commit a928ea7

Browse files
committed
remove validation scheme
Signed-off-by: Gregor Zeitlinger <[email protected]>
1 parent 8bfcdfe commit a928ea7

File tree

9 files changed

+42
-117
lines changed

9 files changed

+42
-117
lines changed

prometheus-metrics-exporter-pushgateway/src/test/java/io/prometheus/metrics/exporter/pushgateway/PushGatewayTest.java

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,6 @@ public void setUp() {
3737
@AfterEach
3838
void tearDown() {
3939
mockServerClient.stop();
40-
System.clearProperty("io.prometheus.naming.validationScheme");
41-
PrometheusNaming.resetForTest();
4240
}
4341

4442
@Test
@@ -146,10 +144,8 @@ public void testPushWithGroupingKey() throws IOException {
146144
pg.push();
147145
}
148146

149-
@SetSystemProperty(key = "io.prometheus.naming.validationScheme", value = "utf-8")
150147
@Test
151148
public void testPushWithEscapedGroupingKey() throws IOException {
152-
PrometheusNaming.resetForTest();
153149
mockServerClient
154150
.when(request().withMethod("PUT").withPath("/metrics/job/j/U__l_2e_1/v1"))
155151
.respond(response().withStatusCode(202));
@@ -179,10 +175,8 @@ public void testPushWithMultiGroupingKey() throws IOException {
179175
pg.push();
180176
}
181177

182-
@SetSystemProperty(key = "io.prometheus.naming.validationScheme", value = "utf-8")
183178
@Test
184179
public void testPushWithMultiEscapedGroupingKey() throws IOException {
185-
PrometheusNaming.resetForTest();
186180

187181
mockServerClient
188182
.when(request().withMethod("PUT").withPath("/metrics/job/j/U__l_2e_1/v1/U__l_2e_2/v2"))
@@ -246,10 +240,8 @@ public void testPushCollectorWithGroupingKey() throws IOException {
246240
pg.push(gauge);
247241
}
248242

249-
@SetSystemProperty(key = "io.prometheus.naming.validationScheme", value = "utf-8")
250243
@Test
251244
public void testPushCollectorWithEscapedGroupingKey() throws IOException {
252-
PrometheusNaming.resetForTest();
253245
mockServerClient
254246
.when(request().withMethod("PUT").withPath("/metrics/job/j/U__l_2e_1/v1"))
255247
.respond(response().withStatusCode(202));
@@ -302,10 +294,8 @@ public void testPushAddWithGroupingKey() throws IOException {
302294
pg.pushAdd();
303295
}
304296

305-
@SetSystemProperty(key = "io.prometheus.naming.validationScheme", value = "utf-8")
306297
@Test
307298
public void testPushAddWithEscapedGroupingKey() throws IOException {
308-
PrometheusNaming.resetForTest();
309299
mockServerClient
310300
.when(request().withMethod("POST").withPath("/metrics/job/j/U__l_2e_1/v1"))
311301
.respond(response().withStatusCode(202));
@@ -334,10 +324,8 @@ public void testPushAddCollectorWithGroupingKey() throws IOException {
334324
pg.pushAdd(gauge);
335325
}
336326

337-
@SetSystemProperty(key = "io.prometheus.naming.validationScheme", value = "utf-8")
338327
@Test
339328
public void testPushAddCollectorWithEscapedGroupingKey() throws IOException {
340-
PrometheusNaming.resetForTest();
341329

342330
mockServerClient
343331
.when(request().withMethod("POST").withPath("/metrics/job/j/U__l_2e_1/v1"))
@@ -376,10 +364,8 @@ public void testDeleteWithGroupingKey() throws IOException {
376364
pg.delete();
377365
}
378366

379-
@SetSystemProperty(key = "io.prometheus.naming.validationScheme", value = "utf-8")
380367
@Test
381368
public void testDeleteWithEscapedGroupingKey() throws IOException {
382-
PrometheusNaming.resetForTest();
383369

384370
mockServerClient
385371
.when(request().withMethod("DELETE").withPath("/metrics/job/j/U__l_2e_1/v1"))
@@ -410,10 +396,8 @@ public void testInstanceIpGroupingKey() throws IOException {
410396
pg.delete();
411397
}
412398

413-
@SetSystemProperty(key = "io.prometheus.naming.validationScheme", value = "utf-8")
414399
@Test
415400
public void testInstanceIpEscapedGroupingKey() throws IOException {
416-
PrometheusNaming.resetForTest();
417401

418402
String ip = InetAddress.getLocalHost().getHostAddress();
419403
assertThat(ip).isNotEmpty();

prometheus-metrics-exposition-textformats/src/main/java/io/prometheus/metrics/expositionformats/OpenMetricsTextFormatWriter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,7 @@ private void writeNameAndLabels(
362362
boolean metricInsideBraces = false;
363363
// If the name does not pass the legacy validity check, we must put the
364364
// metric name inside the braces.
365-
if (PrometheusNaming.validateLegacyMetricName(name) != null) {
365+
if (!PrometheusNaming.isValidLegacyMetricName(name)) {
366366
metricInsideBraces = true;
367367
writer.write('{');
368368
}

prometheus-metrics-exposition-textformats/src/main/java/io/prometheus/metrics/expositionformats/PrometheusTextFormatWriter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,7 @@ private void writeNameAndLabels(
362362
boolean metricInsideBraces = false;
363363
// If the name does not pass the legacy validity check, we must put the
364364
// metric name inside the braces.
365-
if (PrometheusNaming.validateLegacyMetricName(name) != null) {
365+
if (!PrometheusNaming.isValidLegacyLabelName(name)) {
366366
metricInsideBraces = true;
367367
writer.write('{');
368368
}

prometheus-metrics-exposition-textformats/src/test/java/io/prometheus/metrics/expositionformats/ExpositionFormatsTest.java

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -99,12 +99,6 @@ class ExpositionFormatsTest {
9999
.timestampMillis(1690298864383L)
100100
.build();
101101

102-
@AfterEach
103-
void tearDown() {
104-
System.clearProperty("io.prometheus.naming.validationScheme");
105-
PrometheusNaming.resetForTest();
106-
}
107-
108102
@Test
109103
void init() {
110104
ExpositionFormats formats = ExpositionFormats.init();
@@ -477,10 +471,8 @@ public void testGaugeWithDots() throws IOException {
477471
assertPrometheusProtobuf(prometheusProtobuf, gauge);
478472
}
479473

480-
@SetSystemProperty(key = "io.prometheus.naming.validationScheme", value = "utf-8")
481474
@Test
482475
public void testGaugeUTF8() throws IOException {
483-
PrometheusNaming.resetForTest();
484476
String prometheusText =
485477
"""
486478
# HELP "gauge.name" gauge\\ndoc\\nstr"ing

prometheus-metrics-model/src/main/java/io/prometheus/metrics/model/snapshots/Labels.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,10 +119,13 @@ public static Labels of(String[] names, String[] values) {
119119
static String[] makePrometheusNames(String[] names) {
120120
String[] prometheusNames = names;
121121
for (int i = 0; i < names.length; i++) {
122-
if (prometheusNames == names) {
123-
prometheusNames = Arrays.copyOf(names, names.length);
122+
String name = names[i];
123+
if (!PrometheusNaming.isValidLegacyLabelName(name)) {
124+
if (prometheusNames == names) {
125+
prometheusNames = Arrays.copyOf(names, names.length);
126+
}
127+
prometheusNames[i] = PrometheusNaming.prometheusName(name);
124128
}
125-
prometheusNames[i] = PrometheusNaming.prometheusName(names[i]);
126129
}
127130
return prometheusNames;
128131
}

prometheus-metrics-model/src/main/java/io/prometheus/metrics/model/snapshots/PrometheusNaming.java

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public class PrometheusNaming {
6565
* Test if a metric name is valid. Rules:
6666
*
6767
* <ul>
68-
* <li>The name must match {@link #LEGACY_METRIC_NAME_PATTERN}.
68+
* <li>The name must match {@link #METRIC_NAME_PATTERN}.
6969
* <li>The name MUST NOT end with one of the {@link #RESERVED_METRIC_NAME_SUFFIXES}.
7070
* </ul>
7171
*
@@ -85,54 +85,52 @@ public static boolean isValidMetricName(String name) {
8585
return validateMetricName(name) == null;
8686
}
8787

88-
public static String validateMetricName(String name) {
89-
if (isValidUtf8(name)) {
90-
return null;
91-
}
92-
return "The metric name contains unsupported characters";
93-
}
94-
9588
/**
9689
* Same as {@link #isValidMetricName(String)}, but produces an error message.
9790
*
9891
* <p>The name is valid if the error message is {@code null}.
9992
*/
100-
public static String validateLegacyMetricName(String name) {
93+
public static String validateMetricName(String name) {
10194
for (String reservedSuffix : RESERVED_METRIC_NAME_SUFFIXES) {
10295
if (name.endsWith(reservedSuffix)) {
10396
return "The metric name must not include the '" + reservedSuffix + "' suffix.";
10497
}
10598
}
106-
if (!isValidLegacyMetricName(name)) {
107-
return "The metric name contains unsupported characters";
99+
if (isValidUtf8(name)) {
100+
return null;
108101
}
109-
return null;
102+
return "The metric name contains unsupported characters";
110103
}
111104

112105
public static boolean isValidLegacyMetricName(String name) {
113106
return METRIC_NAME_PATTERN.matcher(name).matches();
114107
}
115108

116109
public static boolean isValidLabelName(String name) {
117-
return isValidUtf8(name);
110+
return isValidUtf8(name)
111+
&& !(name.startsWith("__")
112+
|| name.startsWith("._")
113+
|| name.startsWith("..")
114+
|| name.startsWith("_."));
118115
}
119116

120117
private static boolean isValidUtf8(String name) {
121118
return !name.isEmpty() && StandardCharsets.UTF_8.newEncoder().canEncode(name);
122119
}
123120

124121
public static boolean isValidLegacyLabelName(String name) {
125-
return LEGACY_LABEL_NAME_PATTERN.matcher(name).matches()
126-
&& !(name.startsWith("__")
127-
|| name.startsWith("._")
128-
|| name.startsWith("..")
129-
|| name.startsWith("_."));
122+
return LEGACY_LABEL_NAME_PATTERN.matcher(name).matches();
130123
}
131124

132125
/**
133126
* Units may not have illegal characters, and they may not end with a reserved suffix like
134127
* 'total'.
135128
*/
129+
public static boolean isValidUnitName(String name) {
130+
return validateUnitName(name) == null;
131+
}
132+
133+
/** Same as {@link #isValidUnitName(String)} but returns an error message. */
136134
public static String validateUnitName(String name) {
137135
if (name.isEmpty()) {
138136
return "The unit name must not be empty.";

prometheus-metrics-model/src/test/java/io/prometheus/metrics/model/snapshots/LabelsTest.java

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -76,12 +76,6 @@ public void testCompareEquals() {
7676
assertLabels(labels1).isEqualTo(labels2);
7777
}
7878

79-
@Test
80-
public void testIllegalLabelName() {
81-
assertThatExceptionOfType(IllegalArgumentException.class)
82-
.isThrownBy(() -> Labels.of("my_service/status", "200"));
83-
}
84-
8579
@Test
8680
public void testReservedLabelName() {
8781
assertThatExceptionOfType(IllegalArgumentException.class)

prometheus-metrics-model/src/test/java/io/prometheus/metrics/model/snapshots/MetricMetadataTest.java

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,27 +20,17 @@ public void testNullName() {
2020
.isThrownBy(() -> new MetricMetadata(null));
2121
}
2222

23-
@Test
24-
public void testIllegalName() {
25-
assertThatExceptionOfType(IllegalArgumentException.class)
26-
.isThrownBy(
27-
() ->
28-
new MetricMetadata(
29-
"my_namespace/http_server_duration")); // let's see when we decide to allow
30-
// slashes :)
31-
}
32-
3323
@Test
3424
public void testSanitizationIllegalCharacters() {
3525
MetricMetadata metadata =
3626
new MetricMetadata(
3727
sanitizeMetricName("my_namespace/http.server.duration", Unit.SECONDS),
3828
"help string",
3929
Unit.SECONDS);
40-
assertThat(metadata.getName()).isEqualTo("my_namespace_http.server.duration_seconds");
30+
assertThat(metadata.getName()).isEqualTo("my_namespace/http.server.duration_seconds");
4131
assertThat(metadata.getPrometheusName()).isEqualTo("my_namespace_http_server_duration_seconds");
4232
assertThat(metadata.getHelp()).isEqualTo("help string");
43-
assertThat(metadata.getUnit().toString()).isEqualTo("seconds");
33+
assertThat(metadata.getUnit()).hasToString("seconds");
4434
}
4535

4636
@Test

0 commit comments

Comments
 (0)