Skip to content

Commit c61d2eb

Browse files
dhoardfstab
authored andcommitted
Added code to HTTPServer.Builder to use ExecutorService
Signed-off-by: Doug Hoard <doug.hoard@gmail.com>
1 parent 1f859ef commit c61d2eb

File tree

2 files changed

+75
-7
lines changed

2 files changed

+75
-7
lines changed

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

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,7 @@ public static class Builder {
199199
private InetAddress inetAddress = null;
200200
private InetSocketAddress inetSocketAddress = null;
201201
private HttpServer httpServer = null;
202+
private ExecutorService executorService = null;
202203
private CollectorRegistry registry = CollectorRegistry.defaultRegistry;
203204
private boolean daemon = false;
204205
private Predicate<String> sampleNameFilter;
@@ -248,13 +249,26 @@ public Builder withInetSocketAddress(InetSocketAddress address) {
248249
/**
249250
* Use this httpServer. The {@code httpServer} is expected to already be bound to an address.
250251
* Must not be called together with {@link #withPort(int)}, or {@link #withHostname(String)},
251-
* or {@link #withInetAddress(InetAddress)}, or {@link #withInetSocketAddress(InetSocketAddress)}.
252+
* or {@link #withInetAddress(InetAddress)}, or {@link #withInetSocketAddress(InetSocketAddress)},
253+
* or {@link #withExecutorService(ExecutorService)}.
252254
*/
253255
public Builder withHttpServer(HttpServer httpServer) {
254256
this.httpServer = httpServer;
255257
return this;
256258
}
257259

260+
/**
261+
* Optional: ExecutorService used by the {@code httpServer}.
262+
* Must not be called together with the {@link #withHttpServer(HttpServer)}.
263+
*
264+
* @param executorService
265+
* @return
266+
*/
267+
public Builder withExecutorService(ExecutorService executorService) {
268+
this.executorService = executorService;
269+
return this;
270+
}
271+
258272
/**
259273
* By default, the {@link HTTPServer} uses non-daemon threads. Set this to {@code true} to
260274
* run the {@link HTTPServer} with daemon threads.
@@ -323,12 +337,13 @@ public HTTPServer build() throws IOException {
323337
}
324338

325339
if (httpServer != null) {
340+
assertNull(executorService, "cannot configure 'httpServer' and `executorService'");
326341
assertZero(port, "cannot configure 'httpServer' and 'port' at the same time");
327342
assertNull(hostname, "cannot configure 'httpServer' and 'hostname' at the same time");
328343
assertNull(inetAddress, "cannot configure 'httpServer' and 'inetAddress' at the same time");
329344
assertNull(inetSocketAddress, "cannot configure 'httpServer' and 'inetSocketAddress' at the same time");
330345
assertNull(httpsConfigurator, "cannot configure 'httpServer' and 'httpsConfigurator' at the same time");
331-
return new HTTPServer(httpServer, registry, daemon, sampleNameFilterSupplier, authenticator);
346+
return new HTTPServer(executorService, httpServer, registry, daemon, sampleNameFilterSupplier, authenticator);
332347
} else if (inetSocketAddress != null) {
333348
assertZero(port, "cannot configure 'inetSocketAddress' and 'port' at the same time");
334349
assertNull(hostname, "cannot configure 'inetSocketAddress' and 'hostname' at the same time");
@@ -350,7 +365,7 @@ public HTTPServer build() throws IOException {
350365
httpServer = HttpServer.create(inetSocketAddress, 3);
351366
}
352367

353-
return new HTTPServer(httpServer, registry, daemon, sampleNameFilterSupplier, authenticator);
368+
return new HTTPServer(executorService, httpServer, registry, daemon, sampleNameFilterSupplier, authenticator);
354369
}
355370

356371
private void assertNull(Object o, String msg) {
@@ -371,7 +386,7 @@ private void assertZero(int i, String msg) {
371386
* The {@code httpServer} is expected to already be bound to an address
372387
*/
373388
public HTTPServer(HttpServer httpServer, CollectorRegistry registry, boolean daemon) throws IOException {
374-
this(httpServer, registry, daemon, null, null);
389+
this(null, httpServer, registry, daemon, null, null);
375390
}
376391

377392
/**
@@ -416,7 +431,7 @@ public HTTPServer(String host, int port) throws IOException {
416431
this(new InetSocketAddress(host, port), CollectorRegistry.defaultRegistry, false);
417432
}
418433

419-
private HTTPServer(HttpServer httpServer, CollectorRegistry registry, boolean daemon, Supplier<Predicate<String>> sampleNameFilterSupplier, Authenticator authenticator) {
434+
private HTTPServer(ExecutorService executorService, HttpServer httpServer, CollectorRegistry registry, boolean daemon, Supplier<Predicate<String>> sampleNameFilterSupplier, Authenticator authenticator) {
420435
if (httpServer.getAddress() == null)
421436
throw new IllegalArgumentException("HttpServer hasn't been bound to an address");
422437

@@ -434,8 +449,12 @@ private HTTPServer(HttpServer httpServer, CollectorRegistry registry, boolean da
434449
if (authenticator != null) {
435450
mContext.setAuthenticator(authenticator);
436451
}
437-
executorService = Executors.newFixedThreadPool(5, NamedDaemonThreadFactory.defaultThreadFactory(daemon));
438-
server.setExecutor(executorService);
452+
if (executorService != null) {
453+
this.executorService = executorService;
454+
} else {
455+
this.executorService = Executors.newFixedThreadPool(5, NamedDaemonThreadFactory.defaultThreadFactory(daemon));
456+
}
457+
server.setExecutor(this.executorService);
439458
start(daemon);
440459
}
441460

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

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
import java.security.GeneralSecurityException;
3131
import java.security.KeyStore;
3232
import java.security.cert.X509Certificate;
33+
import java.util.concurrent.ExecutorService;
34+
import java.util.concurrent.Executors;
3335

3436
import static org.assertj.core.api.Java6Assertions.assertThat;
3537

@@ -451,6 +453,53 @@ public void testHEADRequestWithSSLAndBasicAuthWrongCredentials() throws GeneralS
451453
}
452454
}
453455

456+
@Test
457+
public void testExecutorService() throws IOException {
458+
ExecutorService executorService = Executors.newFixedThreadPool(20);
459+
460+
HTTPServer httpServer = new HTTPServer.Builder()
461+
.withExecutorService(executorService)
462+
.withRegistry(registry)
463+
.build();
464+
465+
Assert.assertEquals(httpServer.executorService, executorService);
466+
467+
try {
468+
String body = createHttpRequestBuilder(httpServer, "/metrics").build().execute().getBody();
469+
assertThat(body).contains("a 0.0");
470+
assertThat(body).contains("b 0.0");
471+
assertThat(body).contains("c 0.0");
472+
} finally {
473+
httpServer.close();
474+
}
475+
}
476+
477+
@Test(expected = IllegalStateException.class)
478+
public void testExecutorServiceWithHttpServer() throws IOException {
479+
InetSocketAddress inetSocketAddress = new InetSocketAddress("localhost", 0);
480+
481+
HttpServer externalHttpServer = HttpServer.create(inetSocketAddress, 0);
482+
externalHttpServer.createContext("/metrics", new HTTPServer.HTTPMetricHandler(registry));
483+
externalHttpServer.start();
484+
485+
ExecutorService executorService = Executors.newFixedThreadPool(20);
486+
487+
HTTPServer httpServer = new HTTPServer.Builder()
488+
.withExecutorService(executorService)
489+
.withHttpServer(externalHttpServer)
490+
.withRegistry(registry)
491+
.build();
492+
493+
try {
494+
String body = createHttpRequestBuilder(httpServer, "/metrics").build().execute().getBody();
495+
assertThat(body).contains("a 0.0");
496+
assertThat(body).contains("b 0.0");
497+
assertThat(body).contains("c 0.0");
498+
} finally {
499+
httpServer.close();
500+
}
501+
}
502+
454503
/**
455504
* Encodes authorization credentials
456505
*

0 commit comments

Comments
 (0)