Skip to content

Commit a1b19ef

Browse files
committed
Refactor name validation scheme config
Signed-off-by: Federico Torres <[email protected]>
1 parent 2f888f4 commit a1b19ef

File tree

6 files changed

+73
-5
lines changed

6 files changed

+73
-5
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package io.prometheus.metrics.config;
2+
3+
import java.util.Map;
4+
5+
public class NamingProperties {
6+
7+
private static final String VALIDATION_SCHEME = "validationScheme";
8+
private final String validationScheme;
9+
10+
private NamingProperties(String validation) {
11+
this.validationScheme = validation;
12+
}
13+
14+
public String getValidationScheme() {
15+
return validationScheme;
16+
}
17+
18+
static NamingProperties load(String prefix, Map<Object, Object> properties) throws PrometheusPropertiesException {
19+
String validationScheme = Util.loadString(prefix + "." + VALIDATION_SCHEME, properties);
20+
return new NamingProperties(validationScheme);
21+
}
22+
23+
public static Builder builder() {
24+
return new Builder();
25+
}
26+
27+
public static class Builder {
28+
29+
private String validationScheme;
30+
31+
private Builder() {}
32+
33+
public Builder validation(String validationScheme) {
34+
this.validationScheme = validationScheme;
35+
return this;
36+
}
37+
38+
public NamingProperties build() {
39+
return new NamingProperties(validationScheme);
40+
}
41+
}
42+
43+
}

prometheus-metrics-config/src/main/java/io/prometheus/metrics/config/PrometheusProperties.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ public class PrometheusProperties {
1919
private final ExporterFilterProperties exporterFilterProperties;
2020
private final ExporterHttpServerProperties exporterHttpServerProperties;
2121
private final ExporterOpenTelemetryProperties exporterOpenTelemetryProperties;
22+
private final NamingProperties namingProperties;
2223

2324
/**
2425
* Get the properties instance. When called for the first time, {@code get()} loads the properties from the following locations:
@@ -39,14 +40,16 @@ public PrometheusProperties(
3940
ExporterProperties exporterProperties,
4041
ExporterFilterProperties exporterFilterProperties,
4142
ExporterHttpServerProperties httpServerConfig,
42-
ExporterOpenTelemetryProperties otelConfig) {
43+
ExporterOpenTelemetryProperties otelConfig,
44+
NamingProperties namingProperties) {
4345
this.defaultMetricsProperties = defaultMetricsProperties;
4446
this.metricProperties.putAll(metricProperties);
4547
this.exemplarProperties = exemplarProperties;
4648
this.exporterProperties = exporterProperties;
4749
this.exporterFilterProperties = exporterFilterProperties;
4850
this.exporterHttpServerProperties = httpServerConfig;
4951
this.exporterOpenTelemetryProperties = otelConfig;
52+
this.namingProperties = namingProperties;
5053
}
5154

5255
/**
@@ -83,4 +86,8 @@ public ExporterHttpServerProperties getExporterHttpServerProperties() {
8386
public ExporterOpenTelemetryProperties getExporterOpenTelemetryProperties() {
8487
return exporterOpenTelemetryProperties;
8588
}
89+
90+
public NamingProperties getNamingProperties() {
91+
return namingProperties;
92+
}
8693
}

prometheus-metrics-config/src/main/java/io/prometheus/metrics/config/PrometheusPropertiesLoader.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,9 @@ public static PrometheusProperties load() throws PrometheusPropertiesException {
3333
ExporterFilterProperties exporterFilterProperties = ExporterFilterProperties.load("io.prometheus.exporter.filter", properties);
3434
ExporterHttpServerProperties exporterHttpServerProperties = ExporterHttpServerProperties.load("io.prometheus.exporter.httpServer", properties);
3535
ExporterOpenTelemetryProperties exporterOpenTelemetryProperties = ExporterOpenTelemetryProperties.load("io.prometheus.exporter.opentelemetry", properties);
36+
NamingProperties namingProperties = NamingProperties.load("io.prometheus.naming", properties);
3637
validateAllPropertiesProcessed(properties);
37-
return new PrometheusProperties(defaultMetricsProperties, metricsConfigs, exemplarConfig, exporterProperties, exporterFilterProperties, exporterHttpServerProperties, exporterOpenTelemetryProperties);
38+
return new PrometheusProperties(defaultMetricsProperties, metricsConfigs, exemplarConfig, exporterProperties, exporterFilterProperties, exporterHttpServerProperties, exporterOpenTelemetryProperties, namingProperties);
3839
}
3940

4041
// This will remove entries from properties when they are processed.

prometheus-metrics-model/pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,12 @@
3737
</developers>
3838

3939
<dependencies>
40+
<dependency>
41+
<groupId>io.prometheus</groupId>
42+
<artifactId>prometheus-metrics-config</artifactId>
43+
<version>${project.version}</version>
44+
</dependency>
45+
4046
<!-- test dependencies -->
4147
<dependency>
4248
<groupId>junit</groupId>

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

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package io.prometheus.metrics.model.snapshots;
22

3+
import io.prometheus.metrics.config.PrometheusProperties;
4+
35
import java.nio.charset.StandardCharsets;
46
import java.util.ArrayList;
57
import java.util.List;
@@ -16,7 +18,7 @@
1618
* in Prometheus exposition formats. However, if metrics are exposed in OpenTelemetry format the dots are retained.
1719
*/
1820
public class PrometheusNaming {
19-
public static ValidationScheme nameValidationScheme = ValidationScheme.LEGACY_VALIDATION;
21+
public static ValidationScheme nameValidationScheme = initValidationScheme();
2022

2123
public static EscapingScheme nameEscapingScheme = EscapingScheme.VALUE_ENCODING_ESCAPING;
2224

@@ -81,6 +83,16 @@ public static boolean isValidMetricName(String name) {
8183
return validateMetricName(name) == null;
8284
}
8385

86+
static ValidationScheme initValidationScheme() {
87+
if (PrometheusProperties.get() != null && PrometheusProperties.get().getNamingProperties() != null) {
88+
String validationScheme = PrometheusProperties.get().getNamingProperties().getValidationScheme();
89+
if (validationScheme != null && validationScheme.equals("utf-8")) {
90+
return ValidationScheme.UTF_8_VALIDATION;
91+
}
92+
}
93+
return ValidationScheme.LEGACY_VALIDATION;
94+
}
95+
8496
static String validateMetricName(String name) {
8597
switch (nameValidationScheme) {
8698
case LEGACY_VALIDATION:
@@ -488,7 +500,6 @@ static String escapeName(String name, EscapingScheme scheme) {
488500
} else if (!isValidUTF8Char(c)) {
489501
escaped.append("_FFFD_");
490502
} else if (c < 0x100) {
491-
// TODO Check if this is ok
492503
escaped.append('_');
493504
for (int s = 4; s >= 0; s -= 4) {
494505
escaped.append(LOWERHEX.charAt((c >> s) & 0xF));
@@ -523,7 +534,6 @@ static String unescapeName(String name, EscapingScheme scheme) {
523534
name = name.replaceAll("__", "_");
524535
return name;
525536
case VALUE_ENCODING_ESCAPING:
526-
// TODO Check if this is ok
527537
Matcher matcher = Pattern.compile("U__").matcher(name);
528538
if (matcher.find()) {
529539
String escapedName = name.substring(matcher.end());
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
io.prometheus.naming.validationScheme=legacy

0 commit comments

Comments
 (0)