Skip to content

Commit b68c124

Browse files
committed
Make the DefaultHandler in HTTPServer configurable
Signed-off-by: Fabian Stäber <[email protected]>
1 parent 4fde831 commit b68c124

File tree

1 file changed

+12
-3
lines changed
  • prometheus-metrics-exporter-httpserver/src/main/java/io/prometheus/metrics/exporter/httpserver

1 file changed

+12
-3
lines changed

prometheus-metrics-exporter-httpserver/src/main/java/io/prometheus/metrics/exporter/httpserver/HTTPServer.java

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,13 @@ public class HTTPServer implements Closeable {
4545
protected final HttpServer server;
4646
protected final ExecutorService executorService;
4747

48-
private HTTPServer(PrometheusProperties config, ExecutorService executorService, HttpServer httpServer, PrometheusRegistry registry, Authenticator authenticator) {
48+
private HTTPServer(PrometheusProperties config, ExecutorService executorService, HttpServer httpServer, PrometheusRegistry registry, Authenticator authenticator, HttpHandler defaultHandler) {
4949
if (httpServer.getAddress() == null) {
5050
throw new IllegalArgumentException("HttpServer hasn't been bound to an address");
5151
}
5252
this.server = httpServer;
5353
this.executorService = executorService;
54-
registerHandler("/", new DefaultHandler(), authenticator);
54+
registerHandler("/", defaultHandler == null ? new DefaultHandler() : defaultHandler, authenticator);
5555
registerHandler("/metrics", new MetricsHandler(config, registry), authenticator);
5656
registerHandler("/-/healthy", new HealthyHandler(), authenticator);
5757
this.server.start();
@@ -108,6 +108,7 @@ public static class Builder {
108108
private ExporterHttpServerProperties properties = null;
109109
private Authenticator authenticator = null;
110110
private HttpsConfigurator httpsConfigurator = null;
111+
private HttpHandler defaultHandler = null;
111112

112113
private Builder(PrometheusProperties config) {
113114
this.config = config;
@@ -174,6 +175,14 @@ public Builder withHttpsConfigurator(HttpsConfigurator configurator) {
174175
return this;
175176
}
176177

178+
/**
179+
* Optional: Override default handler, i.e. the handler that will be registered for the / endpoint.
180+
*/
181+
public Builder withDefaultHandler(HttpHandler defaultHandler) {
182+
this.defaultHandler = defaultHandler;
183+
return this;
184+
}
185+
177186
/**
178187
* Build and start the HTTPServer.
179188
*/
@@ -190,7 +199,7 @@ public HTTPServer buildAndStart() throws IOException {
190199
}
191200
ExecutorService executorService = makeExecutorService();
192201
httpServer.setExecutor(executorService);
193-
return new HTTPServer(config, executorService, httpServer, registry, authenticator);
202+
return new HTTPServer(config, executorService, httpServer, registry, authenticator, defaultHandler);
194203
}
195204

196205
private InetSocketAddress makeInetSocketAddress() {

0 commit comments

Comments
 (0)