@@ -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 }
0 commit comments