11package io .prometheus .metrics .exporter .httpserver ;
22
3- import com .sun .net .httpserver .Authenticator ;
4- import com .sun .net .httpserver .HttpContext ;
5- import com .sun .net .httpserver .HttpExchange ;
6- import com .sun .net .httpserver .HttpHandler ;
7- import com .sun .net .httpserver .HttpServer ;
8- import com .sun .net .httpserver .HttpsConfigurator ;
9- import com .sun .net .httpserver .HttpsServer ;
3+ import com .sun .net .httpserver .*;
104import io .prometheus .metrics .config .PrometheusProperties ;
115import io .prometheus .metrics .model .registry .PrometheusRegistry ;
126import java .io .Closeable ;
1610import java .net .InetSocketAddress ;
1711import java .security .PrivilegedActionException ;
1812import java .security .PrivilegedExceptionAction ;
19- import java .util .concurrent .ExecutionException ;
20- import java .util .concurrent .ExecutorService ;
21- import java .util .concurrent .SynchronousQueue ;
22- import java .util .concurrent .ThreadPoolExecutor ;
23- import java .util .concurrent .TimeUnit ;
13+ import java .util .concurrent .*;
2414import javax .annotation .Nullable ;
2515import javax .security .auth .Subject ;
2616
@@ -57,24 +47,29 @@ private HTTPServer(
5747 PrometheusRegistry registry ,
5848 @ Nullable Authenticator authenticator ,
5949 @ Nullable String authenticatedSubjectAttributeName ,
60- @ Nullable HttpHandler defaultHandler ) {
50+ @ Nullable HttpHandler defaultHandler ,
51+ @ Nullable String metricsHandlerPath ,
52+ @ Nullable Boolean registerHealthHandler ) {
6153 if (httpServer .getAddress () == null ) {
6254 throw new IllegalArgumentException ("HttpServer hasn't been bound to an address" );
6355 }
6456 this .server = httpServer ;
6557 this .executorService = executorService ;
58+ String metricsPath = getMetricsPath (metricsHandlerPath );
6659 registerHandler (
6760 "/" ,
68- defaultHandler == null ? new DefaultHandler () : defaultHandler ,
61+ defaultHandler == null ? new DefaultHandler (metricsPath ) : defaultHandler ,
6962 authenticator ,
7063 authenticatedSubjectAttributeName );
7164 registerHandler (
72- "/metrics" ,
65+ metricsPath ,
7366 new MetricsHandler (config , registry ),
7467 authenticator ,
7568 authenticatedSubjectAttributeName );
76- registerHandler (
77- "/-/healthy" , new HealthyHandler (), authenticator , authenticatedSubjectAttributeName );
69+ if (registerHealthHandler == null || registerHealthHandler ) {
70+ registerHandler (
71+ "/-/healthy" , new HealthyHandler (), authenticator , authenticatedSubjectAttributeName );
72+ }
7873 try {
7974 // HttpServer.start() starts the HttpServer in a new background thread.
8075 // If we call HttpServer.start() from a thread of the executorService,
@@ -88,6 +83,16 @@ private HTTPServer(
8883 }
8984 }
9085
86+ private String getMetricsPath (@ Nullable String metricsHandlerPath ) {
87+ if (metricsHandlerPath == null ) {
88+ return "/metrics" ;
89+ }
90+ if (!metricsHandlerPath .startsWith ("/" )) {
91+ return "/" + metricsHandlerPath ;
92+ }
93+ return metricsHandlerPath ;
94+ }
95+
9196 private void registerHandler (
9297 String path ,
9398 HttpHandler handler ,
@@ -179,9 +184,11 @@ public static class Builder {
179184 @ Nullable private ExecutorService executorService = null ;
180185 @ Nullable private PrometheusRegistry registry = null ;
181186 @ Nullable private Authenticator authenticator = null ;
187+ @ Nullable private String authenticatedSubjectAttributeName = null ;
182188 @ Nullable private HttpsConfigurator httpsConfigurator = null ;
183189 @ Nullable private HttpHandler defaultHandler = null ;
184- @ Nullable private String authenticatedSubjectAttributeName = null ;
190+ @ Nullable private String metricsHandlerPath = null ;
191+ @ Nullable private Boolean registerHealthHandler = null ;
185192
186193 private Builder (PrometheusProperties config ) {
187194 this .config = config ;
@@ -254,6 +261,18 @@ public Builder defaultHandler(HttpHandler defaultHandler) {
254261 return this ;
255262 }
256263
264+ /** Optional: Override default path for the metrics endpoint. Default is {@code /metrics}. */
265+ public Builder metricsHandlerPath (String metricsHandlerPath ) {
266+ this .metricsHandlerPath = metricsHandlerPath ;
267+ return this ;
268+ }
269+
270+ /** Optional: Override if the health handler should be registered. Default is {@code true}. */
271+ public Builder registerHealthHandler (boolean registerHealthHandler ) {
272+ this .registerHealthHandler = registerHealthHandler ;
273+ return this ;
274+ }
275+
257276 /** Build and start the HTTPServer. */
258277 public HTTPServer buildAndStart () throws IOException {
259278 if (registry == null ) {
@@ -275,7 +294,9 @@ public HTTPServer buildAndStart() throws IOException {
275294 registry ,
276295 authenticator ,
277296 authenticatedSubjectAttributeName ,
278- defaultHandler );
297+ defaultHandler ,
298+ metricsHandlerPath ,
299+ registerHealthHandler );
279300 }
280301
281302 private InetSocketAddress makeInetSocketAddress () {
0 commit comments