Skip to content

Commit 741c8db

Browse files
utafralijack-berg
andauthored
Change default Prometheus host to localhost (#8298)
Co-authored-by: Jack Berg <34418638+jack-berg@users.noreply.github.com>
1 parent ca41b47 commit 741c8db

File tree

2 files changed

+24
-5
lines changed

2 files changed

+24
-5
lines changed

exporters/prometheus/src/main/java/io/opentelemetry/exporter/prometheus/PrometheusHttpServerBuilder.java

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,21 @@
1818
import java.util.concurrent.ExecutorService;
1919
import java.util.concurrent.Executors;
2020
import java.util.function.Predicate;
21+
import java.util.logging.Logger;
2122
import javax.annotation.Nullable;
2223

2324
/** A builder for {@link PrometheusHttpServer}. */
2425
public 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,

exporters/prometheus/src/test/java/io/opentelemetry/exporter/prometheus/internal/PrometheusMetricReaderProviderTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,8 @@ void createMetricReader_Default() throws IOException {
5959
.extracting("server", as(InstanceOfAssertFactories.type(HttpServer.class)))
6060
.satisfies(
6161
server -> {
62-
assertThat(server.getAddress().getAddress().isAnyLocalAddress()).isTrue();
62+
assertThat(server.getAddress().getHostName())
63+
.isIn("localhost", "127.0.0.1", "kubernetes.docker.internal");
6364
assertThat(server.getAddress().getPort()).isEqualTo(9464);
6465
});
6566
assertThat(metricReader.getMemoryMode()).isEqualTo(MemoryMode.REUSABLE_DATA);

0 commit comments

Comments
 (0)