Skip to content

Commit 15a1f3f

Browse files
committed
Dont shutdown executor on shutdown when setExecutorService is called
1 parent 00c8d28 commit 15a1f3f

File tree

11 files changed

+83
-15
lines changed

11 files changed

+83
-15
lines changed

exporters/otlp/all/src/main/java/io/opentelemetry/exporter/otlp/http/logs/OtlpHttpLogRecordExporterBuilder.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,13 @@ public OtlpHttpLogRecordExporterBuilder setServiceClassLoader(ClassLoader servic
233233
return this;
234234
}
235235

236-
/** Set the {@link ExecutorService} used to execute requests. */
236+
/**
237+
* Set the {@link ExecutorService} used to execute requests.
238+
*
239+
* <p>NOTE: By calling this method, you are opting into managing the lifecycle of the {@code
240+
* executorService}. {@link ExecutorService#shutdown()} will NOT be called when this exporter is
241+
* shutdown.
242+
*/
237243
public OtlpHttpLogRecordExporterBuilder setExecutorService(ExecutorService executorService) {
238244
requireNonNull(executorService, "executorService");
239245
delegate.setExecutorService(executorService);

exporters/otlp/all/src/main/java/io/opentelemetry/exporter/otlp/http/metrics/OtlpHttpMetricExporterBuilder.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,13 @@ public OtlpHttpMetricExporterBuilder setServiceClassLoader(ClassLoader serviceCl
261261
return this;
262262
}
263263

264-
/** Set the {@link ExecutorService} used to execute requests. */
264+
/**
265+
* Set the {@link ExecutorService} used to execute requests.
266+
*
267+
* <p>NOTE: By calling this method, you are opting into managing the lifecycle of the {@code
268+
* executorService}. {@link ExecutorService#shutdown()} will NOT be called when this exporter is
269+
* shutdown.
270+
*/
265271
public OtlpHttpMetricExporterBuilder setExecutorService(ExecutorService executorService) {
266272
requireNonNull(executorService, "executorService");
267273
delegate.setExecutorService(executorService);

exporters/otlp/all/src/main/java/io/opentelemetry/exporter/otlp/http/trace/OtlpHttpSpanExporterBuilder.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,13 @@ public OtlpHttpSpanExporterBuilder setServiceClassLoader(ClassLoader serviceClas
234234
return this;
235235
}
236236

237-
/** Set the {@link ExecutorService} used to execute requests. */
237+
/**
238+
* Set the {@link ExecutorService} used to execute requests.
239+
*
240+
* <p>NOTE: By calling this method, you are opting into managing the lifecycle of the {@code
241+
* executorService}. {@link ExecutorService#shutdown()} will NOT be called when this exporter is
242+
* shutdown.
243+
*/
238244
public OtlpHttpSpanExporterBuilder setExecutorService(ExecutorService executorService) {
239245
requireNonNull(executorService, "executorService");
240246
delegate.setExecutorService(executorService);

exporters/otlp/all/src/main/java/io/opentelemetry/exporter/otlp/logs/OtlpGrpcLogRecordExporterBuilder.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,13 @@ public OtlpGrpcLogRecordExporterBuilder setServiceClassLoader(ClassLoader servic
266266
return this;
267267
}
268268

269-
/** Set the {@link ExecutorService} used to execute requests. */
269+
/**
270+
* Set the {@link ExecutorService} used to execute requests.
271+
*
272+
* <p>NOTE: By calling this method, you are opting into managing the lifecycle of the {@code
273+
* executorService}. {@link ExecutorService#shutdown()} will NOT be called when this exporter is
274+
* shutdown.
275+
*/
270276
public OtlpGrpcLogRecordExporterBuilder setExecutorService(ExecutorService executorService) {
271277
requireNonNull(executorService, "executorService");
272278
delegate.setExecutorService(executorService);

exporters/otlp/all/src/main/java/io/opentelemetry/exporter/otlp/metrics/OtlpGrpcMetricExporterBuilder.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,13 @@ public OtlpGrpcMetricExporterBuilder setServiceClassLoader(ClassLoader serviceCl
294294
return this;
295295
}
296296

297-
/** Set the {@link ExecutorService} used to execute requests. */
297+
/**
298+
* Set the {@link ExecutorService} used to execute requests.
299+
*
300+
* <p>NOTE: By calling this method, you are opting into managing the lifecycle of the {@code
301+
* executorService}. {@link ExecutorService#shutdown()} will NOT be called when this exporter is
302+
* shutdown.
303+
*/
298304
public OtlpGrpcMetricExporterBuilder setExecutorService(ExecutorService executorService) {
299305
requireNonNull(executorService, "executorService");
300306
delegate.setExecutorService(executorService);

exporters/otlp/all/src/main/java/io/opentelemetry/exporter/otlp/trace/OtlpGrpcSpanExporterBuilder.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,13 @@ public OtlpGrpcSpanExporterBuilder setServiceClassLoader(ClassLoader serviceClas
263263
return this;
264264
}
265265

266-
/** Set the {@link ExecutorService} used to execute requests. */
266+
/**
267+
* Set the {@link ExecutorService} used to execute requests.
268+
*
269+
* <p>NOTE: By calling this method, you are opting into managing the lifecycle of the {@code
270+
* executorService}. {@link ExecutorService#shutdown()} will NOT be called when this exporter is
271+
* shutdown.
272+
*/
267273
public OtlpGrpcSpanExporterBuilder setExecutorService(ExecutorService executorService) {
268274
requireNonNull(executorService, "executorService");
269275
delegate.setExecutorService(executorService);

exporters/otlp/testing-internal/src/main/java/io/opentelemetry/exporter/otlp/testing/internal/AbstractGrpcTelemetryExporterTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -834,6 +834,8 @@ void executorService() {
834834
assertThat(executorService.getTaskCount()).isPositive();
835835
} finally {
836836
exporter.shutdown();
837+
// If setting executor, the user is responsible for calling shutdown
838+
assertThat(executorService.isShutdown()).isFalse();
837839
executorService.shutdown();
838840
}
839841
}

exporters/otlp/testing-internal/src/main/java/io/opentelemetry/exporter/otlp/testing/internal/AbstractHttpTelemetryExporterTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -717,6 +717,8 @@ void executorService() {
717717
assertThat(executorService.getTaskCount()).isPositive();
718718
} finally {
719719
exporter.shutdown();
720+
// If setting executor, the user is responsible for calling shutdown
721+
assertThat(executorService.isShutdown()).isFalse();
720722
executorService.shutdown();
721723
}
722724
}

exporters/sender/jdk/src/main/java/io/opentelemetry/exporter/sender/jdk/internal/JdkHttpSender.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ public final class JdkHttpSender implements HttpSender {
6161

6262
private static final Logger logger = Logger.getLogger(JdkHttpSender.class.getName());
6363

64+
private final boolean managedExecutor;
6465
private final ExecutorService executorService;
6566
private final HttpClient client;
6667
private final URI uri;
@@ -99,8 +100,13 @@ public final class JdkHttpSender implements HttpSender {
99100
Optional.ofNullable(retryPolicy)
100101
.map(RetryPolicy::getRetryExceptionPredicate)
101102
.orElse(JdkHttpSender::isRetryableException);
102-
this.executorService =
103-
executorService == null ? Executors.newFixedThreadPool(5) : executorService;
103+
if (executorService == null) {
104+
this.executorService = Executors.newFixedThreadPool(5);
105+
this.managedExecutor = true;
106+
} else {
107+
this.executorService = executorService;
108+
this.managedExecutor = false;
109+
}
104110
}
105111

106112
JdkHttpSender(
@@ -368,7 +374,9 @@ private void resetPool() {
368374

369375
@Override
370376
public CompletableResultCode shutdown() {
371-
executorService.shutdown();
377+
if (managedExecutor) {
378+
executorService.shutdown();
379+
}
372380
return CompletableResultCode.ofSuccess();
373381
}
374382
}

exporters/sender/okhttp/src/main/java/io/opentelemetry/exporter/sender/okhttp/internal/OkHttpGrpcSender.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ public final class OkHttpGrpcSender<T extends Marshaler> implements GrpcSender<T
6868
private static final String GRPC_STATUS = "grpc-status";
6969
private static final String GRPC_MESSAGE = "grpc-message";
7070

71+
private final boolean managedExecutor;
7172
private final OkHttpClient client;
7273
private final HttpUrl url;
7374
private final Supplier<Map<String, List<String>>> headersSupplier;
@@ -89,8 +90,15 @@ public OkHttpGrpcSender(
8990
int connectTimeoutMillis =
9091
(int) Math.min(Duration.ofNanos(connectTimeoutNanos).toMillis(), Integer.MAX_VALUE);
9192

92-
Dispatcher dispatcher =
93-
executorService == null ? OkHttpUtil.newDispatcher() : new Dispatcher(executorService);
93+
Dispatcher dispatcher;
94+
if (executorService == null) {
95+
dispatcher = OkHttpUtil.newDispatcher();
96+
this.managedExecutor = true;
97+
} else {
98+
dispatcher = new Dispatcher(executorService);
99+
;
100+
this.managedExecutor = false;
101+
}
94102

95103
OkHttpClient.Builder clientBuilder =
96104
new OkHttpClient.Builder()
@@ -205,7 +213,9 @@ private static String grpcMessage(Response response) {
205213
@Override
206214
public CompletableResultCode shutdown() {
207215
client.dispatcher().cancelAll();
208-
client.dispatcher().executorService().shutdownNow();
216+
if (managedExecutor) {
217+
client.dispatcher().executorService().shutdownNow();
218+
}
209219
client.connectionPool().evictAll();
210220
return CompletableResultCode.ofSuccess();
211221
}

0 commit comments

Comments
 (0)