@@ -282,17 +282,15 @@ public static class Builder {
282
282
private PrometheusRegistry registry = PrometheusRegistry .defaultRegistry ;
283
283
private HttpConnectionFactory connectionFactory = new DefaultHttpConnectionFactory ();
284
284
private final Map <String , String > groupingKey = new TreeMap <>();
285
+ @ Nullable private EscapingScheme escapingScheme ;
285
286
286
287
private Builder (PrometheusProperties config ) {
287
288
this .config = config ;
288
289
}
289
290
290
291
/** Default is {@link Format#PROMETHEUS_PROTOBUF}. */
291
292
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" );
296
294
return this ;
297
295
}
298
296
@@ -302,30 +300,27 @@ public Builder format(Format format) {
302
300
* property.
303
301
*/
304
302
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" );
309
304
return this ;
310
305
}
311
306
312
307
/** Username and password for HTTP basic auth when pushing to the Pushgateway. */
313
308
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 );
318
314
String encoded = Base64 .getEncoder ().encodeToString (credentialsBytes );
319
315
requestHeaders .put ("Authorization" , String .format ("Basic %s" , encoded ));
320
316
return this ;
321
317
}
322
318
323
319
/** Bearer token authorization when pushing to the Pushgateway. */
324
320
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" )));
329
324
return this ;
330
325
}
331
326
@@ -334,10 +329,7 @@ public Builder bearerToken(String token) {
334
329
* at runtime with the {@code io.prometheus.exporter.pushgateway.scheme} property.
335
330
*/
336
331
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" );
341
333
return this ;
342
334
}
343
335
@@ -348,10 +340,8 @@ public Builder scheme(Scheme scheme) {
348
340
* of a custom connection factory that skips SSL certificate validation for HTTPS connections.
349
341
*/
350
342
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" );
355
345
return this ;
356
346
}
357
347
@@ -361,10 +351,7 @@ public Builder connectionFactory(HttpConnectionFactory connectionFactory) {
361
351
* io.prometheus.exporter.pushgateway.job} property.
362
352
*/
363
353
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" );
368
355
return this ;
369
356
}
370
357
@@ -373,10 +360,9 @@ public Builder job(String job) {
373
360
* adding multiple grouping keys.
374
361
*/
375
362
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" ));
380
366
return this ;
381
367
}
382
368
@@ -387,10 +373,16 @@ public Builder instanceIpGroupingKey() throws UnknownHostException {
387
373
388
374
/** Push metrics from this registry instead of {@link PrometheusRegistry#defaultRegistry}. */
389
375
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" );
394
386
return this ;
395
387
}
396
388
@@ -442,6 +434,8 @@ private String getJob(@Nullable ExporterPushgatewayProperties properties) {
442
434
private EscapingScheme getEscapingScheme (@ Nullable ExporterPushgatewayProperties properties ) {
443
435
if (properties != null && properties .getEscapingScheme () != null ) {
444
436
return properties .getEscapingScheme ();
437
+ } else if (this .escapingScheme != null ) {
438
+ return this .escapingScheme ;
445
439
}
446
440
return EscapingScheme .UNDERSCORE_ESCAPING ;
447
441
}
0 commit comments