Skip to content

Commit ca30161

Browse files
committed
move EscapingScheme
Signed-off-by: Gregor Zeitlinger <[email protected]>
1 parent 97cb296 commit ca30161

File tree

42 files changed

+89
-76
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+89
-76
lines changed

benchmarks/src/main/java/io/prometheus/metrics/benchmarks/TextFormatUtilBenchmark.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package io.prometheus.metrics.benchmarks;
22

3+
import io.prometheus.metrics.config.EscapingScheme;
34
import io.prometheus.metrics.expositionformats.ExpositionFormatWriter;
45
import io.prometheus.metrics.expositionformats.OpenMetricsTextFormatWriter;
56
import io.prometheus.metrics.expositionformats.PrometheusTextFormatWriter;
6-
import io.prometheus.metrics.model.snapshots.EscapingScheme;
77
import io.prometheus.metrics.model.snapshots.GaugeSnapshot;
88
import io.prometheus.metrics.model.snapshots.GaugeSnapshot.GaugeDataPointSnapshot;
99
import io.prometheus.metrics.model.snapshots.Labels;

prometheus-metrics-model/src/main/java/io/prometheus/metrics/model/snapshots/EscapingScheme.java renamed to prometheus-metrics-config/src/main/java/io/prometheus/metrics/config/EscapingScheme.java

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
package io.prometheus.metrics.model.snapshots;
2-
3-
import static io.prometheus.metrics.model.snapshots.PrometheusNaming.DEFAULT_ESCAPING_SCHEME;
4-
import static io.prometheus.metrics.model.snapshots.PrometheusNaming.ESCAPING_KEY;
1+
package io.prometheus.metrics.config;
52

63
public enum EscapingScheme {
74
/** NO_ESCAPING indicates that a name will not be escaped. */
@@ -21,8 +18,12 @@ public enum EscapingScheme {
2118
* the Unicode value, surrounded by underscores. Single underscores are replaced with double
2219
* underscores.
2320
*/
24-
VALUE_ENCODING_ESCAPING("values"),
25-
;
21+
VALUE_ENCODING_ESCAPING("values");
22+
23+
private static final String ESCAPING_KEY = "escaping";
24+
25+
/** Default escaping scheme for names when not specified. */
26+
public static final EscapingScheme DEFAULT = UNDERSCORE_ESCAPING;
2627

2728
public final String getValue() {
2829
return value;
@@ -53,12 +54,12 @@ public static EscapingScheme fromAcceptHeader(String acceptHeader) {
5354
return EscapingScheme.forString(value);
5455
} catch (IllegalArgumentException e) {
5556
// If the escaping parameter is unknown, ignore it.
56-
return DEFAULT_ESCAPING_SCHEME;
57+
return DEFAULT;
5758
}
5859
}
5960
}
6061
}
61-
return DEFAULT_ESCAPING_SCHEME;
62+
return DEFAULT;
6263
}
6364

6465
static EscapingScheme forString(String value) {
Lines changed: 34 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package io.prometheus.metrics.config;
22

33
import java.util.Map;
4+
import javax.annotation.Nullable;
45

56
public class ExporterPushgatewayProperties {
67

@@ -9,20 +10,24 @@ public class ExporterPushgatewayProperties {
910
private static final String SCHEME = "scheme";
1011
private static final String ESCAPING_SCHEME = "escapingScheme";
1112
private static final String PREFIX = "io.prometheus.exporter.pushgateway";
12-
private final String scheme;
13-
private final String address;
14-
private final String job;
15-
private final String escapingScheme;
13+
@Nullable private final String scheme;
14+
@Nullable private final String address;
15+
@Nullable private final String job;
16+
@Nullable private final EscapingScheme escapingScheme;
1617

1718
private ExporterPushgatewayProperties(
18-
String address, String job, String scheme, String escapingScheme) {
19+
@Nullable String address,
20+
@Nullable String job,
21+
@Nullable String scheme,
22+
@Nullable EscapingScheme escapingScheme) {
1923
this.address = address;
2024
this.job = job;
2125
this.scheme = scheme;
2226
this.escapingScheme = escapingScheme;
2327
}
2428

2529
/** Address of the Pushgateway in the form {@code host:port}. Default is {@code localhost:9091} */
30+
@Nullable
2631
public String getAddress() {
2732
return address;
2833
}
@@ -31,6 +36,7 @@ public String getAddress() {
3136
* {@code job} label for metrics being pushed. Default is the name of the JAR file that is
3237
* running.
3338
*/
39+
@Nullable
3440
public String getJob() {
3541
return job;
3642
}
@@ -39,15 +45,14 @@ public String getJob() {
3945
* Scheme to be used when pushing metrics to the pushgateway. Must be "http" or "https". Default
4046
* is "http".
4147
*/
48+
@Nullable
4249
public String getScheme() {
4350
return scheme;
4451
}
4552

46-
/**
47-
* Escaping scheme to be used when pushing metric data to the pushgateway. Valid values:
48-
* "no-escaping", "values", "underscores", "dots". Default is "no-escaping".
49-
*/
50-
public String getEscapingScheme() {
53+
/** Escaping scheme to be used when pushing metric data to the pushgateway. */
54+
@Nullable
55+
public EscapingScheme getEscapingScheme() {
5156
return escapingScheme;
5257
}
5358

@@ -71,19 +76,29 @@ static ExporterPushgatewayProperties load(Map<Object, Object> properties)
7176
}
7277
}
7378

74-
if (escapingScheme != null) {
75-
if (!escapingScheme.equals("no-escaping")
76-
&& !escapingScheme.equals("values")
77-
&& !escapingScheme.equals("underscores")
78-
&& !escapingScheme.equals("dots")) {
79+
return new ExporterPushgatewayProperties(
80+
address, job, scheme, parseEscapingScheme(escapingScheme));
81+
}
82+
83+
private static @Nullable EscapingScheme parseEscapingScheme(@Nullable String scheme) {
84+
if (scheme == null) {
85+
return null;
86+
}
87+
switch (scheme) {
88+
case "no-escaping":
89+
return EscapingScheme.NO_ESCAPING;
90+
case "values":
91+
return EscapingScheme.VALUE_ENCODING_ESCAPING;
92+
case "underscores":
93+
return EscapingScheme.UNDERSCORE_ESCAPING;
94+
case "dots":
95+
return EscapingScheme.DOTS_ESCAPING;
96+
default:
7997
throw new PrometheusPropertiesException(
8098
String.format(
8199
"%s.%s: Illegal value. Expecting 'no-escaping', 'values', 'underscores', "
82100
+ "or 'dots'. Found: %s",
83-
PREFIX, ESCAPING_SCHEME, escapingScheme));
84-
}
101+
PREFIX, ESCAPING_SCHEME, scheme));
85102
}
86-
87-
return new ExporterPushgatewayProperties(address, job, scheme, escapingScheme);
88103
}
89104
}

prometheus-metrics-core/src/test/java/io/prometheus/metrics/core/metrics/CounterTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@
55
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
66
import static org.assertj.core.data.Offset.offset;
77

8+
import io.prometheus.metrics.config.EscapingScheme;
89
import io.prometheus.metrics.config.MetricsProperties;
910
import io.prometheus.metrics.config.PrometheusProperties;
1011
import io.prometheus.metrics.core.exemplars.ExemplarSamplerConfigTestUtil;
1112
import io.prometheus.metrics.expositionformats.generated.com_google_protobuf_4_31_1.Metrics;
1213
import io.prometheus.metrics.expositionformats.internal.PrometheusProtobufWriterImpl;
1314
import io.prometheus.metrics.expositionformats.internal.ProtobufUtil;
1415
import io.prometheus.metrics.model.snapshots.CounterSnapshot;
15-
import io.prometheus.metrics.model.snapshots.EscapingScheme;
1616
import io.prometheus.metrics.model.snapshots.Exemplar;
1717
import io.prometheus.metrics.model.snapshots.Label;
1818
import io.prometheus.metrics.model.snapshots.Labels;

prometheus-metrics-core/src/test/java/io/prometheus/metrics/core/metrics/HistogramTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
66
import static org.assertj.core.data.Offset.offset;
77

8+
import io.prometheus.metrics.config.EscapingScheme;
89
import io.prometheus.metrics.config.MetricsProperties;
910
import io.prometheus.metrics.config.PrometheusProperties;
1011
import io.prometheus.metrics.core.datapoints.DistributionDataPoint;

prometheus-metrics-core/src/test/java/io/prometheus/metrics/core/metrics/InfoTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
import static org.assertj.core.api.Assertions.assertThat;
44
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
55

6+
import io.prometheus.metrics.config.EscapingScheme;
67
import io.prometheus.metrics.expositionformats.OpenMetricsTextFormatWriter;
78
import io.prometheus.metrics.expositionformats.generated.com_google_protobuf_4_31_1.Metrics;
89
import io.prometheus.metrics.expositionformats.internal.PrometheusProtobufWriterImpl;
910
import io.prometheus.metrics.expositionformats.internal.ProtobufUtil;
10-
import io.prometheus.metrics.model.snapshots.EscapingScheme;
1111
import io.prometheus.metrics.model.snapshots.Labels;
1212
import io.prometheus.metrics.model.snapshots.MetricSnapshots;
1313
import io.prometheus.metrics.model.snapshots.Unit;

prometheus-metrics-exporter-common/src/main/java/io/prometheus/metrics/exporter/common/PrometheusScrapeHandler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
package io.prometheus.metrics.exporter.common;
22

3+
import io.prometheus.metrics.config.EscapingScheme;
34
import io.prometheus.metrics.config.ExporterFilterProperties;
45
import io.prometheus.metrics.config.PrometheusProperties;
56
import io.prometheus.metrics.expositionformats.ExpositionFormatWriter;
67
import io.prometheus.metrics.expositionformats.ExpositionFormats;
78
import io.prometheus.metrics.model.registry.MetricNameFilter;
89
import io.prometheus.metrics.model.registry.PrometheusRegistry;
9-
import io.prometheus.metrics.model.snapshots.EscapingScheme;
1010
import io.prometheus.metrics.model.snapshots.MetricSnapshots;
1111
import java.io.ByteArrayOutputStream;
1212
import java.io.IOException;

prometheus-metrics-exporter-pushgateway/src/main/java/io/prometheus/metrics/exporter/pushgateway/PushGateway.java

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import static io.prometheus.metrics.exporter.pushgateway.Scheme.HTTP;
44
import static io.prometheus.metrics.model.snapshots.PrometheusNaming.escapeName;
55

6+
import io.prometheus.metrics.config.EscapingScheme;
67
import io.prometheus.metrics.config.ExporterPushgatewayProperties;
78
import io.prometheus.metrics.config.PrometheusProperties;
89
import io.prometheus.metrics.config.PrometheusPropertiesException;
@@ -12,7 +13,6 @@
1213
import io.prometheus.metrics.model.registry.Collector;
1314
import io.prometheus.metrics.model.registry.MultiCollector;
1415
import io.prometheus.metrics.model.registry.PrometheusRegistry;
15-
import io.prometheus.metrics.model.snapshots.EscapingScheme;
1616
import java.io.ByteArrayOutputStream;
1717
import java.io.IOException;
1818
import java.io.InputStream;
@@ -439,19 +439,7 @@ private String getJob(ExporterPushgatewayProperties properties) {
439439

440440
private EscapingScheme getEscapingScheme(ExporterPushgatewayProperties properties) {
441441
if (properties != null && properties.getEscapingScheme() != null) {
442-
String scheme = properties.getEscapingScheme();
443-
switch (scheme) {
444-
case "no-escaping":
445-
return EscapingScheme.NO_ESCAPING;
446-
case "values":
447-
return EscapingScheme.VALUE_ENCODING_ESCAPING;
448-
case "underscores":
449-
return EscapingScheme.UNDERSCORE_ESCAPING;
450-
case "dots":
451-
return EscapingScheme.DOTS_ESCAPING;
452-
default:
453-
return EscapingScheme.NO_ESCAPING;
454-
}
442+
return properties.getEscapingScheme();
455443
}
456444
return EscapingScheme.NO_ESCAPING;
457445
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
import static org.mockserver.model.HttpRequest.request;
66
import static org.mockserver.model.HttpResponse.response;
77

8+
import io.prometheus.metrics.config.EscapingScheme;
89
import io.prometheus.metrics.core.metrics.Gauge;
910
import io.prometheus.metrics.model.registry.PrometheusRegistry;
10-
import io.prometheus.metrics.model.snapshots.EscapingScheme;
1111
import java.io.IOException;
1212
import java.lang.reflect.Field;
1313
import java.net.InetAddress;

prometheus-metrics-exposition-formats/src/main/java/io/prometheus/metrics/expositionformats/internal/PrometheusProtobufWriterImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@
44
import static io.prometheus.metrics.model.snapshots.SnapshotEscaper.getSnapshotLabelName;
55

66
import com.google.protobuf.TextFormat;
7+
import io.prometheus.metrics.config.EscapingScheme;
78
import io.prometheus.metrics.expositionformats.ExpositionFormatWriter;
89
import io.prometheus.metrics.expositionformats.generated.com_google_protobuf_4_31_1.Metrics;
910
import io.prometheus.metrics.model.snapshots.ClassicHistogramBuckets;
1011
import io.prometheus.metrics.model.snapshots.CounterSnapshot;
1112
import io.prometheus.metrics.model.snapshots.CounterSnapshot.CounterDataPointSnapshot;
1213
import io.prometheus.metrics.model.snapshots.DataPointSnapshot;
13-
import io.prometheus.metrics.model.snapshots.EscapingScheme;
1414
import io.prometheus.metrics.model.snapshots.Exemplar;
1515
import io.prometheus.metrics.model.snapshots.GaugeSnapshot;
1616
import io.prometheus.metrics.model.snapshots.HistogramSnapshot;

0 commit comments

Comments
 (0)