Skip to content

Commit

Permalink
Merge pull request #179 from paypay/master
Browse files Browse the repository at this point in the history
use daemon threads in telemetryclient
  • Loading branch information
breedx-nr authored Jun 17, 2020
2 parents b48aaa1 + 80c6574 commit 0c9c20a
Showing 1 changed file with 56 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,16 @@
public class TelemetryClient {

private static final Logger LOG = LoggerFactory.getLogger(TelemetryClient.class);
private static final int DEFAULT_SHUTDOWN_SECONDS = 3;
private static final boolean DEFAULT_IS_DAEMON = true;

private final EventBatchSender eventBatchSender;
private final MetricBatchSender metricBatchSender;
private final SpanBatchSender spanBatchSender;
private final ScheduledExecutorService executor;
private final int shutdownSeconds;
private final LogBatchSender logBatchSender;

private final ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();

/**
* Create a new TelemetryClient instance, with four senders. Note that if you don't intend to send
* one of the telemetry types, you can pass in a null value for that sender.
Expand All @@ -58,10 +60,38 @@ public TelemetryClient(
SpanBatchSender spanBatchSender,
EventBatchSender eventBatchSender,
LogBatchSender logBatchSender) {
this(
metricBatchSender,
spanBatchSender,
eventBatchSender,
logBatchSender,
DEFAULT_SHUTDOWN_SECONDS,
DEFAULT_IS_DAEMON);
}

/**
* Create a new TelemetryClient instance, with four senders and seconds to wait for shutdown.
*
* @param metricBatchSender The sender for dimensional metrics.
* @param spanBatchSender The sender for distributed tracing spans.
* @param eventBatchSender The sender for custom events
* @param logBatchSender The sender for log entries.
* @param shutdownSeconds num of seconds to wait for graceful shutdown of its executor
* @param useDaemonThread A flag to decide user-threads or daemon-threads
*/
public TelemetryClient(
MetricBatchSender metricBatchSender,
SpanBatchSender spanBatchSender,
EventBatchSender eventBatchSender,
LogBatchSender logBatchSender,
int shutdownSeconds,
boolean useDaemonThread) {
this.metricBatchSender = metricBatchSender;
this.spanBatchSender = spanBatchSender;
this.eventBatchSender = eventBatchSender;
this.logBatchSender = logBatchSender;
this.shutdownSeconds = shutdownSeconds;
this.executor = buildExecutorService(useDaemonThread);
}

/**
Expand Down Expand Up @@ -191,6 +221,15 @@ private void backoff(
public void shutdown() {
LOG.info("Shutting down the TelemetryClient background Executor");
executor.shutdown();
try {
if (!executor.awaitTermination(shutdownSeconds, TimeUnit.SECONDS)) {
LOG.warn("couldn't shutdown within timeout");
executor.shutdownNow();
}
} catch (InterruptedException e) {
LOG.error("interrupted graceful shutdown", e);
Thread.currentThread().interrupt();
}
}

/**
Expand Down Expand Up @@ -228,4 +267,19 @@ public static TelemetryClient create(
return new TelemetryClient(
metricBatchSender, spanBatchSender, eventBatchSender, logBatchSender);
}

/**
* Create ScheduledExecutorService from a parameter given by constructor
*
* @param useDaemonThread A flag to decide user-threads or daemon-threads
* @return ScheduledExecutorService
*/
private static ScheduledExecutorService buildExecutorService(boolean useDaemonThread) {
return Executors.newSingleThreadScheduledExecutor(
r -> {
Thread thread = new Thread(r);
thread.setDaemon(useDaemonThread);
return thread;
});
}
}

0 comments on commit 0c9c20a

Please sign in to comment.