Skip to content

Commit 8da6c1e

Browse files
authored
add missing builder method for escaping scheme (#1518)
Signed-off-by: Gregor Zeitlinger <gregor.zeitlinger@grafana.com>
1 parent f613ab4 commit 8da6c1e

File tree

3 files changed

+35
-40
lines changed

3 files changed

+35
-40
lines changed

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

Lines changed: 30 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -282,17 +282,15 @@ public static class Builder {
282282
private PrometheusRegistry registry = PrometheusRegistry.defaultRegistry;
283283
private HttpConnectionFactory connectionFactory = new DefaultHttpConnectionFactory();
284284
private final Map<String, String> groupingKey = new TreeMap<>();
285+
@Nullable private EscapingScheme escapingScheme;
285286

286287
private Builder(PrometheusProperties config) {
287288
this.config = config;
288289
}
289290

290291
/** Default is {@link Format#PROMETHEUS_PROTOBUF}. */
291292
public Builder format(Format format) {
292-
if (format == null) {
293-
throw new NullPointerException();
294-
}
295-
this.format = format;
293+
this.format = requireNonNull(format, "format must not be null");
296294
return this;
297295
}
298296

@@ -302,30 +300,27 @@ public Builder format(Format format) {
302300
* property.
303301
*/
304302
public Builder address(String address) {
305-
if (address == null) {
306-
throw new NullPointerException();
307-
}
308-
this.address = address;
303+
this.address = requireNonNull(address, "address must not be null");
309304
return this;
310305
}
311306

312307
/** Username and password for HTTP basic auth when pushing to the Pushgateway. */
313308
public Builder basicAuth(String user, String password) {
314-
if (user == null || password == null) {
315-
throw new NullPointerException();
316-
}
317-
byte[] credentialsBytes = (user + ":" + password).getBytes(StandardCharsets.UTF_8);
309+
byte[] credentialsBytes =
310+
(requireNonNull(user, "user must not be null")
311+
+ ":"
312+
+ requireNonNull(password, "password must not be null"))
313+
.getBytes(StandardCharsets.UTF_8);
318314
String encoded = Base64.getEncoder().encodeToString(credentialsBytes);
319315
requestHeaders.put("Authorization", String.format("Basic %s", encoded));
320316
return this;
321317
}
322318

323319
/** Bearer token authorization when pushing to the Pushgateway. */
324320
public Builder bearerToken(String token) {
325-
if (token == null) {
326-
throw new NullPointerException();
327-
}
328-
requestHeaders.put("Authorization", String.format("Bearer %s", token));
321+
requestHeaders.put(
322+
"Authorization",
323+
String.format("Bearer %s", requireNonNull(token, "token must not be null")));
329324
return this;
330325
}
331326

@@ -334,10 +329,7 @@ public Builder bearerToken(String token) {
334329
* at runtime with the {@code io.prometheus.exporter.pushgateway.scheme} property.
335330
*/
336331
public Builder scheme(Scheme scheme) {
337-
if (scheme == null) {
338-
throw new NullPointerException();
339-
}
340-
this.scheme = scheme;
332+
this.scheme = requireNonNull(scheme, "scheme must not be null");
341333
return this;
342334
}
343335

@@ -348,10 +340,8 @@ public Builder scheme(Scheme scheme) {
348340
* of a custom connection factory that skips SSL certificate validation for HTTPS connections.
349341
*/
350342
public Builder connectionFactory(HttpConnectionFactory connectionFactory) {
351-
if (connectionFactory == null) {
352-
throw new NullPointerException();
353-
}
354-
this.connectionFactory = connectionFactory;
343+
this.connectionFactory =
344+
requireNonNull(connectionFactory, "connectionFactory must not be null");
355345
return this;
356346
}
357347

@@ -361,10 +351,7 @@ public Builder connectionFactory(HttpConnectionFactory connectionFactory) {
361351
* io.prometheus.exporter.pushgateway.job} property.
362352
*/
363353
public Builder job(String job) {
364-
if (job == null) {
365-
throw new NullPointerException();
366-
}
367-
this.job = job;
354+
this.job = requireNonNull(job, "job must not be null");
368355
return this;
369356
}
370357

@@ -373,10 +360,9 @@ public Builder job(String job) {
373360
* adding multiple grouping keys.
374361
*/
375362
public Builder groupingKey(String name, String value) {
376-
if (name == null || value == null) {
377-
throw new NullPointerException();
378-
}
379-
groupingKey.put(name, value);
363+
groupingKey.put(
364+
requireNonNull(name, "name must not be null"),
365+
requireNonNull(value, "value must not be null"));
380366
return this;
381367
}
382368

@@ -387,10 +373,16 @@ public Builder instanceIpGroupingKey() throws UnknownHostException {
387373

388374
/** Push metrics from this registry instead of {@link PrometheusRegistry#defaultRegistry}. */
389375
public Builder registry(PrometheusRegistry registry) {
390-
if (registry == null) {
391-
throw new NullPointerException();
392-
}
393-
this.registry = registry;
376+
this.registry = requireNonNull(registry, "registry must not be null");
377+
return this;
378+
}
379+
380+
/**
381+
* Specify the escaping scheme to be used when pushing metrics. Default is {@link
382+
* EscapingScheme#UNDERSCORE_ESCAPING}.
383+
*/
384+
public Builder escapingScheme(EscapingScheme escapingScheme) {
385+
this.escapingScheme = requireNonNull(escapingScheme, "escapingScheme must not be null");
394386
return this;
395387
}
396388

@@ -442,6 +434,8 @@ private String getJob(@Nullable ExporterPushgatewayProperties properties) {
442434
private EscapingScheme getEscapingScheme(@Nullable ExporterPushgatewayProperties properties) {
443435
if (properties != null && properties.getEscapingScheme() != null) {
444436
return properties.getEscapingScheme();
437+
} else if (this.escapingScheme != null) {
438+
return this.escapingScheme;
445439
}
446440
return EscapingScheme.UNDERSCORE_ESCAPING;
447441
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import static org.mockserver.model.HttpRequest.request;
44
import static org.mockserver.model.HttpResponse.response;
55

6+
import io.prometheus.metrics.config.EscapingScheme;
67
import io.prometheus.metrics.core.metrics.Gauge;
78
import io.prometheus.metrics.model.registry.PrometheusRegistry;
89
import java.io.IOException;
@@ -30,6 +31,7 @@ public void setUp() {
3031
.basicAuth("testUser", "testPwd")
3132
.registry(registry)
3233
.prometheusTimestampsInMs(true)
34+
.escapingScheme(EscapingScheme.ALLOW_UTF8)
3335
.job("j")
3436
.build();
3537
}

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

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

3+
import static java.util.Objects.requireNonNull;
4+
35
import javax.annotation.Nullable;
46

57
/** Immutable representation of an Exemplar. */
@@ -85,10 +87,7 @@ public Builder spanId(String spanId) {
8587
}
8688

8789
public Builder labels(Labels labels) {
88-
if (labels == null) {
89-
throw new NullPointerException();
90-
}
91-
this.labels = labels;
90+
this.labels = requireNonNull(labels, "Labels must not be null.");
9291
return this;
9392
}
9493

0 commit comments

Comments
 (0)