1818import java .util .concurrent .ExecutorService ;
1919import java .util .concurrent .Executors ;
2020import java .util .function .Predicate ;
21+ import java .util .logging .Logger ;
2122import javax .annotation .Nullable ;
2223
2324/** A builder for {@link PrometheusHttpServer}. */
2425public final class PrometheusHttpServerBuilder {
2526
2627 static final int DEFAULT_PORT = 9464 ;
27- private static final String DEFAULT_HOST = "0.0.0.0 " ;
28+ private static final String DEFAULT_HOST = "localhost " ;
2829 private static final MemoryMode DEFAULT_MEMORY_MODE = MemoryMode .REUSABLE_DATA ;
30+ private static final Logger LOGGER =
31+ Logger .getLogger (PrometheusHttpServerBuilder .class .getName ());
2932
30- private String host = DEFAULT_HOST ;
33+ // Temporarily nullable to detect when it's not set and log warning about 0.0.0.0 -> localhost
34+ // change
35+ @ Nullable private String host ;
3136 private int port = DEFAULT_PORT ;
3237 private PrometheusRegistry prometheusRegistry = new PrometheusRegistry ();
3338 private PrometheusMetricReaderBuilder metricReaderBuilder = PrometheusMetricReader .builder ();
@@ -51,7 +56,12 @@ public final class PrometheusHttpServerBuilder {
5156 this .authenticator = builder .authenticator ;
5257 }
5358
54- /** Sets the host to bind to. If unset, defaults to {@value #DEFAULT_HOST}. */
59+ /**
60+ * Sets the host to bind to. If unset, defaults to {@value #DEFAULT_HOST}.
61+ *
62+ * <p>Previously defaulted to {@code 0.0.0.0}. To restore the old behavior, set host to {@code
63+ * 0.0.0.0} explicitly.
64+ */
5565 public PrometheusHttpServerBuilder setHost (String host ) {
5666 requireNonNull (host , "host" );
5767 checkArgument (!host .isEmpty (), "host must not be empty" );
@@ -173,9 +183,17 @@ public PrometheusHttpServer build() {
173183 "MemoryMode REUSEABLE_DATA cannot be used with custom executor, "
174184 + "since data may be corrupted if reading metrics concurrently" );
175185 }
186+ String resolvedHost = host ;
187+ if (resolvedHost == null ) {
188+ // TODO (jack-berg): Remove log after 1.64.0 release
189+ LOGGER .info (
190+ "PrometheusHttpServer host not set, defaulting to localhost. Previously defaulted to 0.0.0.0. "
191+ + "If you depend on the old behavior, set host to 0.0.0.0 explicitly." );
192+ resolvedHost = DEFAULT_HOST ;
193+ }
176194 return new PrometheusHttpServer (
177195 new PrometheusHttpServerBuilder (this ), // copy to prevent modification
178- host ,
196+ resolvedHost ,
179197 port ,
180198 executor ,
181199 prometheusRegistry ,
0 commit comments