Skip to content

Commit 4e0e752

Browse files
thecoopbrian-brazil
authored andcommitted
Allow a specific HttpServer to be specified, so HttpsServer could be used (#470) (#471)
Signed-off-by: Simon Cooper <thecoop@runbox.com>
1 parent b64cfc1 commit 4e0e752

File tree

2 files changed

+27
-5
lines changed

2 files changed

+27
-5
lines changed

simpleclient_httpserver/src/main/java/io/prometheus/client/exporter/HTTPServer.java

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -142,13 +142,15 @@ static ThreadFactory defaultThreadFactory(boolean daemon) {
142142
protected final HttpServer server;
143143
protected final ExecutorService executorService;
144144

145-
146145
/**
147-
* Start a HTTP server serving Prometheus metrics from the given registry.
146+
* Start a HTTP server serving Prometheus metrics from the given registry using the given {@link HttpServer}.
147+
* The {@code httpServer} is expected to already be bound to an address
148148
*/
149-
public HTTPServer(InetSocketAddress addr, CollectorRegistry registry, boolean daemon) throws IOException {
150-
server = HttpServer.create();
151-
server.bind(addr, 3);
149+
public HTTPServer(HttpServer httpServer, CollectorRegistry registry, boolean daemon) throws IOException {
150+
if (httpServer.getAddress() == null)
151+
throw new IllegalArgumentException("HttpServer hasn't been bound to an address");
152+
153+
server = httpServer;
152154
HttpHandler mHandler = new HTTPMetricHandler(registry);
153155
server.createContext("/", mHandler);
154156
server.createContext("/metrics", mHandler);
@@ -157,6 +159,13 @@ public HTTPServer(InetSocketAddress addr, CollectorRegistry registry, boolean da
157159
start(daemon);
158160
}
159161

162+
/**
163+
* Start a HTTP server serving Prometheus metrics from the given registry.
164+
*/
165+
public HTTPServer(InetSocketAddress addr, CollectorRegistry registry, boolean daemon) throws IOException {
166+
this(HttpServer.create(addr, 3), registry, daemon);
167+
}
168+
160169
/**
161170
* Start a HTTP server serving Prometheus metrics from the given registry using non-daemon threads.
162171
*/

simpleclient_httpserver/src/test/java/io/prometheus/client/exporter/TestHTTPServer.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package io.prometheus.client.exporter;
22

3+
import com.sun.net.httpserver.HttpServer;
34
import io.prometheus.client.Gauge;
45
import io.prometheus.client.CollectorRegistry;
56
import java.io.IOException;
@@ -14,6 +15,7 @@
1415
import org.junit.Test;
1516

1617
import static org.assertj.core.api.Assertions.assertThat;
18+
import static org.assertj.core.api.Assertions.fail;
1719

1820
public class TestHTTPServer {
1921

@@ -54,6 +56,17 @@ String requestWithCompression(String suffix) throws IOException {
5456
return s.hasNext() ? s.next() : "";
5557
}
5658

59+
@Test
60+
public void testUnbound() throws IOException {
61+
CollectorRegistry registry = new CollectorRegistry();
62+
try {
63+
HTTPServer s = new HTTPServer(HttpServer.create(), registry, true);
64+
s.stop();
65+
fail("Should refuse to use an unbound HttpServer");
66+
}
67+
catch (IllegalArgumentException expected) {}
68+
}
69+
5770
@Test
5871
public void testSimpleRequest() throws IOException {
5972
String response = request("");

0 commit comments

Comments
 (0)