You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Prevent exception in HttpServer exporter when using custom root metrics path (#1772)
The behavior of `HttpServer.createContext()` was changed between Java 11
and 21. In the current JDKs the server doesn't allow to register
multiple contexts with the same path. So if you configure
```java
HTTPServer.builder()
.port(0)
.registry(registry)
.metricsHandlerPath("/")
.buildAndStart()
```
you get the following exception:
```
java.lang.IllegalArgumentException: cannot add context to list
at jdk.httpserver/sun.net.httpserver.ContextList.add(ContextList.java:37)
at jdk.httpserver/sun.net.httpserver.ServerImpl.createContext(ServerImpl.java:306)
at jdk.httpserver/sun.net.httpserver.HttpServerImpl.createContext(HttpServerImpl.java:69)
at jdk.httpserver/sun.net.httpserver.HttpServerImpl.createContext(HttpServerImpl.java:34)
at io.prometheus.metrics.exporter.httpserver.HTTPServer.registerHandler(HTTPServer.java:111)
```
This PR fixes the issue by skipping the default handler registration in
case the metrics are configured for the root path.
I also improved the unit test so we can properly assert
`expectedStatusCode` and `expectedBody`. With this improvement we can be
sure that an endpoint actually returns the expected handler content, not
just a matching status code.
Signed-off-by: David Sondermann <david.sondermann@hivemq.com>
Copy file name to clipboardExpand all lines: prometheus-metrics-exporter-httpserver/src/main/java/io/prometheus/metrics/exporter/httpserver/HTTPServer.java
Copy file name to clipboardExpand all lines: prometheus-metrics-exporter-httpserver/src/test/java/io/prometheus/metrics/exporter/httpserver/HTTPServerTest.java
0 commit comments