From 8da6c1e2145d79bd38e0e60dc62ec822bf8bb0a1 Mon Sep 17 00:00:00 2001 From: Gregor Zeitlinger Date: Mon, 18 Aug 2025 17:00:00 +0200 Subject: [PATCH] add missing builder method for escaping scheme (#1518) Signed-off-by: Gregor Zeitlinger --- .../exporter/pushgateway/PushGateway.java | 66 +++++++++---------- .../pushgateway/BasicAuthPushGatewayTest.java | 2 + .../metrics/model/snapshots/Exemplar.java | 7 +- 3 files changed, 35 insertions(+), 40 deletions(-) diff --git a/prometheus-metrics-exporter-pushgateway/src/main/java/io/prometheus/metrics/exporter/pushgateway/PushGateway.java b/prometheus-metrics-exporter-pushgateway/src/main/java/io/prometheus/metrics/exporter/pushgateway/PushGateway.java index 747d37379..643e0aeca 100644 --- a/prometheus-metrics-exporter-pushgateway/src/main/java/io/prometheus/metrics/exporter/pushgateway/PushGateway.java +++ b/prometheus-metrics-exporter-pushgateway/src/main/java/io/prometheus/metrics/exporter/pushgateway/PushGateway.java @@ -282,6 +282,7 @@ public static class Builder { private PrometheusRegistry registry = PrometheusRegistry.defaultRegistry; private HttpConnectionFactory connectionFactory = new DefaultHttpConnectionFactory(); private final Map groupingKey = new TreeMap<>(); + @Nullable private EscapingScheme escapingScheme; private Builder(PrometheusProperties config) { this.config = config; @@ -289,10 +290,7 @@ private Builder(PrometheusProperties config) { /** Default is {@link Format#PROMETHEUS_PROTOBUF}. */ public Builder format(Format format) { - if (format == null) { - throw new NullPointerException(); - } - this.format = format; + this.format = requireNonNull(format, "format must not be null"); return this; } @@ -302,19 +300,17 @@ public Builder format(Format format) { * property. */ public Builder address(String address) { - if (address == null) { - throw new NullPointerException(); - } - this.address = address; + this.address = requireNonNull(address, "address must not be null"); return this; } /** Username and password for HTTP basic auth when pushing to the Pushgateway. */ public Builder basicAuth(String user, String password) { - if (user == null || password == null) { - throw new NullPointerException(); - } - byte[] credentialsBytes = (user + ":" + password).getBytes(StandardCharsets.UTF_8); + byte[] credentialsBytes = + (requireNonNull(user, "user must not be null") + + ":" + + requireNonNull(password, "password must not be null")) + .getBytes(StandardCharsets.UTF_8); String encoded = Base64.getEncoder().encodeToString(credentialsBytes); requestHeaders.put("Authorization", String.format("Basic %s", encoded)); return this; @@ -322,10 +318,9 @@ public Builder basicAuth(String user, String password) { /** Bearer token authorization when pushing to the Pushgateway. */ public Builder bearerToken(String token) { - if (token == null) { - throw new NullPointerException(); - } - requestHeaders.put("Authorization", String.format("Bearer %s", token)); + requestHeaders.put( + "Authorization", + String.format("Bearer %s", requireNonNull(token, "token must not be null"))); return this; } @@ -334,10 +329,7 @@ public Builder bearerToken(String token) { * at runtime with the {@code io.prometheus.exporter.pushgateway.scheme} property. */ public Builder scheme(Scheme scheme) { - if (scheme == null) { - throw new NullPointerException(); - } - this.scheme = scheme; + this.scheme = requireNonNull(scheme, "scheme must not be null"); return this; } @@ -348,10 +340,8 @@ public Builder scheme(Scheme scheme) { * of a custom connection factory that skips SSL certificate validation for HTTPS connections. */ public Builder connectionFactory(HttpConnectionFactory connectionFactory) { - if (connectionFactory == null) { - throw new NullPointerException(); - } - this.connectionFactory = connectionFactory; + this.connectionFactory = + requireNonNull(connectionFactory, "connectionFactory must not be null"); return this; } @@ -361,10 +351,7 @@ public Builder connectionFactory(HttpConnectionFactory connectionFactory) { * io.prometheus.exporter.pushgateway.job} property. */ public Builder job(String job) { - if (job == null) { - throw new NullPointerException(); - } - this.job = job; + this.job = requireNonNull(job, "job must not be null"); return this; } @@ -373,10 +360,9 @@ public Builder job(String job) { * adding multiple grouping keys. */ public Builder groupingKey(String name, String value) { - if (name == null || value == null) { - throw new NullPointerException(); - } - groupingKey.put(name, value); + groupingKey.put( + requireNonNull(name, "name must not be null"), + requireNonNull(value, "value must not be null")); return this; } @@ -387,10 +373,16 @@ public Builder instanceIpGroupingKey() throws UnknownHostException { /** Push metrics from this registry instead of {@link PrometheusRegistry#defaultRegistry}. */ public Builder registry(PrometheusRegistry registry) { - if (registry == null) { - throw new NullPointerException(); - } - this.registry = registry; + this.registry = requireNonNull(registry, "registry must not be null"); + return this; + } + + /** + * Specify the escaping scheme to be used when pushing metrics. Default is {@link + * EscapingScheme#UNDERSCORE_ESCAPING}. + */ + public Builder escapingScheme(EscapingScheme escapingScheme) { + this.escapingScheme = requireNonNull(escapingScheme, "escapingScheme must not be null"); return this; } @@ -442,6 +434,8 @@ private String getJob(@Nullable ExporterPushgatewayProperties properties) { private EscapingScheme getEscapingScheme(@Nullable ExporterPushgatewayProperties properties) { if (properties != null && properties.getEscapingScheme() != null) { return properties.getEscapingScheme(); + } else if (this.escapingScheme != null) { + return this.escapingScheme; } return EscapingScheme.UNDERSCORE_ESCAPING; } diff --git a/prometheus-metrics-exporter-pushgateway/src/test/java/io/prometheus/metrics/exporter/pushgateway/BasicAuthPushGatewayTest.java b/prometheus-metrics-exporter-pushgateway/src/test/java/io/prometheus/metrics/exporter/pushgateway/BasicAuthPushGatewayTest.java index ce0fec354..e2f052d6b 100644 --- a/prometheus-metrics-exporter-pushgateway/src/test/java/io/prometheus/metrics/exporter/pushgateway/BasicAuthPushGatewayTest.java +++ b/prometheus-metrics-exporter-pushgateway/src/test/java/io/prometheus/metrics/exporter/pushgateway/BasicAuthPushGatewayTest.java @@ -3,6 +3,7 @@ import static org.mockserver.model.HttpRequest.request; import static org.mockserver.model.HttpResponse.response; +import io.prometheus.metrics.config.EscapingScheme; import io.prometheus.metrics.core.metrics.Gauge; import io.prometheus.metrics.model.registry.PrometheusRegistry; import java.io.IOException; @@ -30,6 +31,7 @@ public void setUp() { .basicAuth("testUser", "testPwd") .registry(registry) .prometheusTimestampsInMs(true) + .escapingScheme(EscapingScheme.ALLOW_UTF8) .job("j") .build(); } diff --git a/prometheus-metrics-model/src/main/java/io/prometheus/metrics/model/snapshots/Exemplar.java b/prometheus-metrics-model/src/main/java/io/prometheus/metrics/model/snapshots/Exemplar.java index 27a69b961..418603580 100644 --- a/prometheus-metrics-model/src/main/java/io/prometheus/metrics/model/snapshots/Exemplar.java +++ b/prometheus-metrics-model/src/main/java/io/prometheus/metrics/model/snapshots/Exemplar.java @@ -1,5 +1,7 @@ package io.prometheus.metrics.model.snapshots; +import static java.util.Objects.requireNonNull; + import javax.annotation.Nullable; /** Immutable representation of an Exemplar. */ @@ -85,10 +87,7 @@ public Builder spanId(String spanId) { } public Builder labels(Labels labels) { - if (labels == null) { - throw new NullPointerException(); - } - this.labels = labels; + this.labels = requireNonNull(labels, "Labels must not be null."); return this; }