Skip to content

Commit d95bc83

Browse files
abhiseshMateusz Rzeszutek
and
Mateusz Rzeszutek
authored
Make the Executor for PrometheusHttpServer configurable (open-telemetry#5296)
* Add executor to builder * Add tests * Fix static imports * Add javadoc comment * Fix formatting * Fix formatting * Use random port * Update exporters/prometheus/src/test/java/io/opentelemetry/exporter/prometheus/PrometheusHttpServerTest.java Co-authored-by: Mateusz Rzeszutek <[email protected]> * Remove executor assignment from constructor * Fix import statements * Use try-with-resources --------- Co-authored-by: Mateusz Rzeszutek <[email protected]>
1 parent 38bf4d8 commit d95bc83

File tree

3 files changed

+59
-5
lines changed

3 files changed

+59
-5
lines changed

exporters/prometheus/src/main/java/io/opentelemetry/exporter/prometheus/PrometheusHttpServer.java

+2-4
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@
3737
import java.util.Set;
3838
import java.util.concurrent.ConcurrentHashMap;
3939
import java.util.concurrent.ExecutorService;
40-
import java.util.concurrent.Executors;
4140
import java.util.concurrent.TimeUnit;
4241
import java.util.function.Predicate;
4342
import java.util.function.Supplier;
@@ -76,7 +75,7 @@ public static PrometheusHttpServerBuilder builder() {
7675
return new PrometheusHttpServerBuilder();
7776
}
7877

79-
PrometheusHttpServer(String host, int port) {
78+
PrometheusHttpServer(String host, int port, ExecutorService executor) {
8079
try {
8180
server = HttpServer.create(new InetSocketAddress(host, port), 3);
8281
} catch (IOException e) {
@@ -87,8 +86,7 @@ public static PrometheusHttpServerBuilder builder() {
8786
server.createContext("/", metricsHandler);
8887
server.createContext("/metrics", metricsHandler);
8988
server.createContext("/-/healthy", HealthHandler.INSTANCE);
90-
91-
executor = Executors.newFixedThreadPool(5, THREAD_FACTORY);
89+
this.executor = executor;
9290
server.setExecutor(executor);
9391

9492
start();

exporters/prometheus/src/main/java/io/opentelemetry/exporter/prometheus/PrometheusHttpServerBuilder.java

+23-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@
88
import static io.opentelemetry.api.internal.Utils.checkArgument;
99
import static java.util.Objects.requireNonNull;
1010

11+
import io.opentelemetry.sdk.internal.DaemonThreadFactory;
12+
import java.util.concurrent.ExecutorService;
13+
import java.util.concurrent.Executors;
14+
import javax.annotation.Nullable;
15+
1116
/** A builder for {@link PrometheusHttpServer}. */
1217
public final class PrometheusHttpServerBuilder {
1318

@@ -17,6 +22,8 @@ public final class PrometheusHttpServerBuilder {
1722
private String host = DEFAULT_HOST;
1823
private int port = DEFAULT_PORT;
1924

25+
@Nullable private ExecutorService executor;
26+
2027
/** Sets the host to bind to. If unset, defaults to {@value #DEFAULT_HOST}. */
2128
public PrometheusHttpServerBuilder setHost(String host) {
2229
requireNonNull(host, "host");
@@ -32,13 +39,28 @@ public PrometheusHttpServerBuilder setPort(int port) {
3239
return this;
3340
}
3441

42+
/** Sets the {@link ExecutorService} to be used for {@link PrometheusHttpServer}. */
43+
public PrometheusHttpServerBuilder setExecutor(ExecutorService executor) {
44+
requireNonNull(executor, "executor");
45+
this.executor = executor;
46+
return this;
47+
}
48+
3549
/**
3650
* Returns a new {@link PrometheusHttpServer} with the configuration of this builder which can be
3751
* registered with a {@link io.opentelemetry.sdk.metrics.SdkMeterProvider}.
3852
*/
3953
public PrometheusHttpServer build() {
40-
return new PrometheusHttpServer(host, port);
54+
ExecutorService executorService = this.executor;
55+
if (executorService == null) {
56+
executorService = getDefaultExecutor();
57+
}
58+
return new PrometheusHttpServer(host, port, executorService);
4159
}
4260

4361
PrometheusHttpServerBuilder() {}
62+
63+
private static ExecutorService getDefaultExecutor() {
64+
return Executors.newFixedThreadPool(5, new DaemonThreadFactory("prometheus-http"));
65+
}
4466
}

exporters/prometheus/src/test/java/io/opentelemetry/exporter/prometheus/PrometheusHttpServerTest.java

+34
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
package io.opentelemetry.exporter.prometheus;
77

88
import static io.opentelemetry.api.common.AttributeKey.stringKey;
9+
import static org.assertj.core.api.Assertions.as;
910
import static org.assertj.core.api.Assertions.assertThat;
1011
import static org.assertj.core.api.Assertions.assertThatThrownBy;
1112

@@ -32,11 +33,17 @@
3233
import io.opentelemetry.sdk.resources.Resource;
3334
import java.io.ByteArrayInputStream;
3435
import java.io.IOException;
36+
import java.net.ServerSocket;
3537
import java.nio.charset.StandardCharsets;
3638
import java.util.Collections;
3739
import java.util.List;
40+
import java.util.concurrent.Executors;
41+
import java.util.concurrent.ScheduledExecutorService;
42+
import java.util.concurrent.ScheduledThreadPoolExecutor;
43+
import java.util.concurrent.ThreadPoolExecutor;
3844
import java.util.concurrent.atomic.AtomicReference;
3945
import java.util.zip.GZIPInputStream;
46+
import org.assertj.core.api.InstanceOfAssertFactories;
4047
import org.junit.jupiter.api.AfterAll;
4148
import org.junit.jupiter.api.BeforeAll;
4249
import org.junit.jupiter.api.BeforeEach;
@@ -295,6 +302,33 @@ void stringRepresentation() {
295302
.isEqualTo("PrometheusHttpServer{address=" + prometheusServer.getAddress() + "}");
296303
}
297304

305+
@Test
306+
void defaultExecutor() {
307+
assertThat(prometheusServer)
308+
.extracting("executor", as(InstanceOfAssertFactories.type(ThreadPoolExecutor.class)))
309+
.satisfies(executor -> assertThat(executor.getCorePoolSize()).isEqualTo(5));
310+
}
311+
312+
@Test
313+
void customExecutor() throws IOException {
314+
ScheduledExecutorService scheduledExecutor = Executors.newScheduledThreadPool(10);
315+
int port;
316+
try (ServerSocket socket = new ServerSocket(0)) {
317+
port = socket.getLocalPort();
318+
}
319+
try (PrometheusHttpServer server =
320+
PrometheusHttpServer.builder()
321+
.setHost("localhost")
322+
.setPort(port)
323+
.setExecutor(scheduledExecutor)
324+
.build()) {
325+
assertThat(server)
326+
.extracting(
327+
"executor", as(InstanceOfAssertFactories.type(ScheduledThreadPoolExecutor.class)))
328+
.satisfies(executor -> assertThat(executor).isSameAs(scheduledExecutor));
329+
}
330+
}
331+
298332
private static List<MetricData> generateTestData() {
299333
return ImmutableList.of(
300334
ImmutableMetricData.createLongSum(

0 commit comments

Comments
 (0)