Skip to content

Commit

Permalink
Allow a specific HttpServer to be specified, so HttpsServer could be …
Browse files Browse the repository at this point in the history
…used (#470) (#471)

Signed-off-by: Simon Cooper <thecoop@runbox.com>
  • Loading branch information
thecoop authored and brian-brazil committed Apr 24, 2019
1 parent b64cfc1 commit 4e0e752
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -142,13 +142,15 @@ static ThreadFactory defaultThreadFactory(boolean daemon) {
protected final HttpServer server;
protected final ExecutorService executorService;


/**
* Start a HTTP server serving Prometheus metrics from the given registry.
* Start a HTTP server serving Prometheus metrics from the given registry using the given {@link HttpServer}.
* The {@code httpServer} is expected to already be bound to an address
*/
public HTTPServer(InetSocketAddress addr, CollectorRegistry registry, boolean daemon) throws IOException {
server = HttpServer.create();
server.bind(addr, 3);
public HTTPServer(HttpServer httpServer, CollectorRegistry registry, boolean daemon) throws IOException {
if (httpServer.getAddress() == null)
throw new IllegalArgumentException("HttpServer hasn't been bound to an address");

server = httpServer;
HttpHandler mHandler = new HTTPMetricHandler(registry);
server.createContext("/", mHandler);
server.createContext("/metrics", mHandler);
Expand All @@ -157,6 +159,13 @@ public HTTPServer(InetSocketAddress addr, CollectorRegistry registry, boolean da
start(daemon);
}

/**
* Start a HTTP server serving Prometheus metrics from the given registry.
*/
public HTTPServer(InetSocketAddress addr, CollectorRegistry registry, boolean daemon) throws IOException {
this(HttpServer.create(addr, 3), registry, daemon);
}

/**
* Start a HTTP server serving Prometheus metrics from the given registry using non-daemon threads.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.prometheus.client.exporter;

import com.sun.net.httpserver.HttpServer;
import io.prometheus.client.Gauge;
import io.prometheus.client.CollectorRegistry;
import java.io.IOException;
Expand All @@ -14,6 +15,7 @@
import org.junit.Test;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.fail;

public class TestHTTPServer {

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

@Test
public void testUnbound() throws IOException {
CollectorRegistry registry = new CollectorRegistry();
try {
HTTPServer s = new HTTPServer(HttpServer.create(), registry, true);
s.stop();
fail("Should refuse to use an unbound HttpServer");
}
catch (IllegalArgumentException expected) {}
}

@Test
public void testSimpleRequest() throws IOException {
String response = request("");
Expand Down

0 comments on commit 4e0e752

Please sign in to comment.