Skip to content

Commit e052cca

Browse files
committed
Add JVM shutdown hook to flush telemetry
1 parent a9f2b68 commit e052cca

23 files changed

+90
-123
lines changed

agent/agent-tooling/src/main/java/com/microsoft/applicationinsights/agent/internal/BeforeAgentInstaller.java

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@
2828
import java.util.Map;
2929
import java.util.Properties;
3030
import java.util.concurrent.CountDownLatch;
31+
import java.util.concurrent.ExecutionException;
32+
import java.util.concurrent.Future;
33+
import java.util.concurrent.TimeUnit;
34+
import java.util.concurrent.TimeoutException;
3135

3236
import com.google.common.base.Strings;
3337
import com.microsoft.applicationinsights.TelemetryClient;
@@ -60,6 +64,7 @@
6064
import io.opentelemetry.auto.bootstrap.instrumentation.aiappid.AiAppId;
6165
import io.opentelemetry.auto.config.Config;
6266
import io.opentelemetry.auto.config.ConfigOverride;
67+
import io.opentelemetry.sdk.OpenTelemetrySdk;
6368
import org.apache.http.HttpHost;
6469
import org.checkerframework.checker.nullness.qual.Nullable;
6570
import org.slf4j.Logger;
@@ -143,11 +148,26 @@ private static void start(Instrumentation instrumentation, File agentJarFile) th
143148
if (fixedRateSampling != null && fixedRateSampling.percentage != null) {
144149
Global.setFixedRateSamplingPercentage(fixedRateSampling.percentage);
145150
}
146-
TelemetryClient telemetryClient = new TelemetryClient();
151+
final TelemetryClient telemetryClient = new TelemetryClient();
147152
Global.setTelemetryClient(telemetryClient);
148153
AiAppId.setSupplier(new AppIdSupplier());
149154
// this is currently used by Micrometer instrumentation in addition to 2.x SDK
150155
BytecodeUtil.setDelegate(new BytecodeUtilImpl());
156+
Runtime.getRuntime().addShutdownHook(new Thread() {
157+
@Override
158+
public void run() {
159+
startupLogger.debug("running shutdown hook");
160+
try {
161+
telemetryClient.flush();
162+
telemetryClient.shutdown(5, TimeUnit.SECONDS);
163+
startupLogger.debug("completed shutdown hook");
164+
} catch (InterruptedException e) {
165+
startupLogger.debug("interrupted while flushing telemetry during shutdown");
166+
} catch (Throwable t) {
167+
startupLogger.debug(t.getMessage(), t);
168+
}
169+
}
170+
});
151171
}
152172

153173
@Nullable

agent/agent-tooling/src/main/java/io/opentelemetry/auto/tooling/TracerInstaller.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ public static void installAgentTracer() {
3030
.setSampler(new FixedRateSampler(fixedRateSamplingPercentage))
3131
.build());
3232
}
33+
// if changing the span processor to something async, flush it in the shutdown hook before flushing TelemetryClient
3334
OpenTelemetrySdk.getTracerProvider()
3435
.addSpanProcessor(SimpleSpansProcessor.newBuilder(new Exporter(telemetryClient)).build());
3536
}

core/src/main/java/com/microsoft/applicationinsights/TelemetryClient.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
import java.util.Date;
2525
import java.util.Map;
26+
import java.util.concurrent.TimeUnit;
2627
import java.util.concurrent.atomic.AtomicLong;
2728

2829
import com.google.common.base.Strings;
@@ -455,6 +456,10 @@ public void flush() {
455456
getChannel().flush();
456457
}
457458

459+
public void shutdown(long timeout, TimeUnit timeUnit) throws InterruptedException {
460+
getChannel().shutdown(timeout, timeUnit);
461+
}
462+
458463
/**
459464
* Gets the channel used by the client.
460465
*/

core/src/main/java/com/microsoft/applicationinsights/channel/TelemetryChannel.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public interface TelemetryChannel {
5353
* @param timeout Time to try and stop
5454
* @param timeUnit The units of the 'timeout' parameter
5555
*/
56-
void stop(long timeout, TimeUnit timeUnit);
56+
void shutdown(long timeout, TimeUnit timeUnit) throws InterruptedException;
5757

5858
/**
5959
* Flushes the data that the channel might have internally.

core/src/main/java/com/microsoft/applicationinsights/channel/concrete/TelemetryChannelBase.java

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,6 @@ public abstract class TelemetryChannelBase<T> implements TelemetryChannel {
7070
private TransmitterFactory transmitterFactory;
7171
private AtomicLong itemsSent = new AtomicLong(0);
7272

73-
protected boolean stopped = false;
7473
protected boolean isInitailized = false;
7574

7675
protected TelemetriesTransmitter<T> telemetriesTransmitter;
@@ -260,30 +259,9 @@ public void setDeveloperMode(boolean developerMode) {
260259
}
261260
}
262261

263-
/**
264-
* Stops on going work
265-
*/
266262
@Override
267-
public synchronized void stop(long timeout, TimeUnit timeUnit) {
268-
try {
269-
if (stopped) {
270-
return;
271-
}
272-
273-
telemetriesTransmitter.stop(timeout, timeUnit);
274-
stopped = true;
275-
} catch (ThreadDeath td) {
276-
throw td;
277-
} catch (Throwable t) {
278-
try {
279-
logger.error("Exception generated while stopping telemetry transmitter");
280-
logger.trace("Exception generated while stopping telemetry transmitter", t);
281-
} catch (ThreadDeath td) {
282-
throw td;
283-
} catch (Throwable t2) {
284-
// chomp
285-
}
286-
}
263+
public synchronized void shutdown(long timeout, TimeUnit timeUnit) throws InterruptedException {
264+
telemetriesTransmitter.shutdown(timeout, timeUnit);
287265
}
288266

289267
/**

core/src/main/java/com/microsoft/applicationinsights/internal/channel/TelemetriesTransmitter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,5 +49,5 @@ public interface TelemetriesFetcher<T> {
4949

5050
boolean sendNow(Collection<T> telemetries);
5151

52-
void stop(long timeout, TimeUnit timeUnit);
52+
void shutdown(long timeout, TimeUnit timeUnit) throws InterruptedException;
5353
}

core/src/main/java/com/microsoft/applicationinsights/internal/channel/TransmissionDispatcher.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,6 @@
3333
public interface TransmissionDispatcher {
3434
void dispatch(Transmission transmission);
3535

36-
void stop(long timeout, TimeUnit timeUnit);
36+
void shutdown(long timeout, TimeUnit timeUnit) throws InterruptedException;
3737
}
3838

core/src/main/java/com/microsoft/applicationinsights/internal/channel/TransmissionOutputAsync.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,6 @@
3636
public interface TransmissionOutputAsync {
3737
boolean sendAsync(Transmission transmission);
3838

39-
void stop(long timeout, TimeUnit timeUnit);
39+
void shutdown(long timeout, TimeUnit timeUnit) throws InterruptedException;
4040
}
4141

core/src/main/java/com/microsoft/applicationinsights/internal/channel/TransmissionOutputSync.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@
2121

2222
package com.microsoft.applicationinsights.internal.channel;
2323

24-
import java.util.concurrent.TimeUnit;
25-
2624
import com.microsoft.applicationinsights.internal.channel.common.Transmission;
2725

2826
/**
@@ -35,7 +33,5 @@
3533
*/
3634
public interface TransmissionOutputSync {
3735
boolean sendSync(Transmission transmission);
38-
39-
void stop(long timeout, TimeUnit timeUnit);
4036
}
4137

core/src/main/java/com/microsoft/applicationinsights/internal/channel/TransmissionsLoader.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,11 @@
2121

2222
package com.microsoft.applicationinsights.internal.channel;
2323

24-
import com.microsoft.applicationinsights.internal.shutdown.Stoppable;
25-
26-
import java.util.concurrent.TimeUnit;
27-
2824
/**
2925
* Created by gupele on 12/22/2014.
3026
*/
31-
public interface TransmissionsLoader extends Stoppable {
27+
public interface TransmissionsLoader {
3228
boolean load(boolean waitForThreadsToStart);
29+
30+
void shutdown();
3331
}

0 commit comments

Comments
 (0)