3232import java .util .HashMap ;
3333import java .util .Map ;
3434import java .util .TreeMap ;
35+ import java .util .Optional ;
3536import javax .annotation .Nullable ;
3637
3738/**
@@ -89,6 +90,8 @@ public class PushGateway {
8990 private final PrometheusRegistry registry ;
9091 private final HttpConnectionFactory connectionFactory ;
9192 private final EscapingScheme escapingScheme ;
93+ private final Integer connectionTimeout ;
94+ private final Integer readTimeout ;
9295
9396 private PushGateway (
9497 PrometheusRegistry registry ,
@@ -97,13 +100,17 @@ private PushGateway(
97100 HttpConnectionFactory connectionFactory ,
98101 Map <String , String > requestHeaders ,
99102 boolean prometheusTimestampsInMs ,
100- EscapingScheme escapingScheme ) {
103+ EscapingScheme escapingScheme ,
104+ @ Nullable Integer connectionTimeout ,
105+ @ Nullable Integer readTimeout ) {
101106 this .registry = registry ;
102107 this .url = url ;
103108 this .requestHeaders = Collections .unmodifiableMap (new HashMap <>(requestHeaders ));
104109 this .connectionFactory = connectionFactory ;
105110 this .prometheusTimestampsInMs = prometheusTimestampsInMs ;
106111 this .escapingScheme = escapingScheme ;
112+ this .connectionTimeout = Optional .ofNullable (connectionTimeout ).orElse (10 * MILLISECONDS_PER_SECOND );
113+ this .readTimeout = Optional .ofNullable (readTimeout ).orElse (10 * MILLISECONDS_PER_SECOND );
107114 writer = getWriter (format );
108115 if (!writer .isAvailable ()) {
109116 throw new RuntimeException (writer .getClass () + " is not available" );
@@ -206,8 +213,8 @@ private void doRequest(@Nullable PrometheusRegistry registry, String method) thr
206213 }
207214 connection .setRequestMethod (method );
208215
209- connection .setConnectTimeout (10 * MILLISECONDS_PER_SECOND );
210- connection .setReadTimeout (10 * MILLISECONDS_PER_SECOND );
216+ connection .setConnectTimeout (this . connectionTimeout );
217+ connection .setReadTimeout (this . readTimeout );
211218 connection .connect ();
212219
213220 try {
@@ -277,6 +284,8 @@ public static class Builder {
277284 @ Nullable private String address ;
278285 @ Nullable private Scheme scheme ;
279286 @ Nullable private String job ;
287+ @ Nullable private Integer connectionTimeout ;
288+ @ Nullable private Integer readTimeout ;
280289 private boolean prometheusTimestampsInMs ;
281290 private final Map <String , String > requestHeaders = new HashMap <>();
282291 private PrometheusRegistry registry = PrometheusRegistry .defaultRegistry ;
@@ -395,6 +404,30 @@ public Builder prometheusTimestampsInMs(boolean prometheusTimestampsInMs) {
395404 return this ;
396405 }
397406
407+ /**
408+ * Specify the connection timeout (in milliseconds) for HTTP connections to the PushGateway.
409+ * Default is {@code 10000} (10 seconds).
410+ *
411+ * @param connectionTimeout timeout value in milliseconds
412+ * @return this {@link Builder} instance
413+ */
414+ public Builder connectionTimeout (Integer connectionTimeout ) {
415+ this .connectionTimeout = connectionTimeout ;
416+ return this ;
417+ }
418+
419+ /**
420+ * Specify the read timeout (in milliseconds) for reading the response from the PushGateway.
421+ * Default is {@code 10000} (10 seconds).
422+ *
423+ * @param readTimeout timeout value in milliseconds
424+ * @return this {@link Builder} instance
425+ */
426+ public Builder readTimeout (Integer readTimeout ) {
427+ this .readTimeout = readTimeout ;
428+ return this ;
429+ }
430+
398431 private boolean getPrometheusTimestampsInMs () {
399432 // accept either to opt in to timestamps in milliseconds
400433 return config .getExporterProperties ().getPrometheusTimestampsInMs ()
@@ -496,7 +529,9 @@ public PushGateway build() {
496529 connectionFactory ,
497530 requestHeaders ,
498531 getPrometheusTimestampsInMs (),
499- getEscapingScheme (properties ));
532+ getEscapingScheme (properties ),
533+ connectionTimeout ,
534+ readTimeout );
500535 } catch (MalformedURLException e ) {
501536 throw new PrometheusPropertiesException (
502537 address + ": Invalid address. Expecting <host>:<port>" );
0 commit comments