Skip to content

Commit 1708f6d

Browse files
authored
Fix sporadic failures for smoke test NonDaemonThreads (#2289)
* Fix NonDaemonThreads sporadic failure * Better implementation
1 parent 2a0f469 commit 1708f6d

File tree

4 files changed

+51
-274
lines changed

4 files changed

+51
-274
lines changed

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

+51-13
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
import com.microsoft.applicationinsights.agent.internal.telemetry.TelemetryClient;
4141
import io.opentelemetry.api.common.Attributes;
4242
import io.opentelemetry.api.common.AttributesBuilder;
43+
import io.opentelemetry.sdk.autoconfigure.AutoConfiguredOpenTelemetrySdkBuilder;
4344
import io.opentelemetry.sdk.autoconfigure.spi.AutoConfigurationCustomizer;
4445
import io.opentelemetry.sdk.autoconfigure.spi.AutoConfigurationCustomizerProvider;
4546
import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties;
@@ -48,6 +49,7 @@
4849
import io.opentelemetry.sdk.logs.export.BatchLogProcessor;
4950
import io.opentelemetry.sdk.logs.export.LogExporter;
5051
import io.opentelemetry.sdk.metrics.SdkMeterProviderBuilder;
52+
import io.opentelemetry.sdk.metrics.export.MetricReader;
5153
import io.opentelemetry.sdk.metrics.export.PeriodicMetricReader;
5254
import io.opentelemetry.sdk.trace.SdkTracerProviderBuilder;
5355
import io.opentelemetry.sdk.trace.data.SpanData;
@@ -59,6 +61,7 @@
5961
import java.util.Collection;
6062
import java.util.Collections;
6163
import java.util.List;
64+
import java.util.concurrent.TimeUnit;
6265
import java.util.stream.Collectors;
6366
import javax.annotation.Nullable;
6467

@@ -67,6 +70,10 @@ public class OpenTelemetryConfigurer implements AutoConfigurationCustomizerProvi
6770

6871
@Nullable public static LoggerExporter loggerExporter;
6972

73+
@Nullable private static BatchLogProcessor batchLogProcessor;
74+
@Nullable private static BatchSpanProcessor batchSpanProcessor;
75+
@Nullable private static MetricReader metricReader;
76+
7077
@Override
7178
public void customize(AutoConfigurationCustomizer autoConfiguration) {
7279
TelemetryClient telemetryClient = TelemetryClient.getActive();
@@ -75,6 +82,9 @@ public void customize(AutoConfigurationCustomizer autoConfiguration) {
7582
return;
7683
}
7784

85+
// TODO (trask) add this method to AutoConfigurationCustomizer upstream?
86+
((AutoConfiguredOpenTelemetrySdkBuilder) autoConfiguration).registerShutdownHook(false);
87+
7888
Configuration configuration = MainEntryPoint.getConfiguration();
7989

8090
autoConfiguration
@@ -84,6 +94,41 @@ public void customize(AutoConfigurationCustomizer autoConfiguration) {
8494
(builder, config) -> configureLogging(builder, telemetryClient, configuration))
8595
.addMeterProviderCustomizer(
8696
(builder, config) -> configureMetrics(builder, telemetryClient, configuration));
97+
98+
Runtime.getRuntime()
99+
.addShutdownHook(new Thread(() -> flushAll(telemetryClient).join(10, TimeUnit.SECONDS)));
100+
}
101+
102+
private static CompletableResultCode flushAll(TelemetryClient telemetryClient) {
103+
List<CompletableResultCode> results = new ArrayList<>();
104+
if (batchSpanProcessor != null) {
105+
results.add(batchSpanProcessor.forceFlush());
106+
}
107+
if (metricReader != null) {
108+
results.add(metricReader.forceFlush());
109+
}
110+
if (batchLogProcessor != null) {
111+
results.add(batchLogProcessor.forceFlush());
112+
}
113+
CompletableResultCode overallResult = new CompletableResultCode();
114+
CompletableResultCode initialResult = CompletableResultCode.ofAll(results);
115+
initialResult.whenComplete(
116+
() -> {
117+
if (initialResult.isSuccess()) {
118+
CompletableResultCode telemetryClientResult = telemetryClient.forceFlush();
119+
telemetryClientResult.whenComplete(
120+
() -> {
121+
if (telemetryClientResult.isSuccess()) {
122+
overallResult.succeed();
123+
} else {
124+
overallResult.fail();
125+
}
126+
});
127+
} else {
128+
overallResult.fail();
129+
}
130+
});
131+
return overallResult;
87132
}
88133

89134
private static SdkTracerProviderBuilder configureTracing(
@@ -147,13 +192,12 @@ private static SdkTracerProviderBuilder configureTracing(
147192
telemetryClient, configuration, configuration.preview.captureHttpServer4xxAsError);
148193

149194
// using BatchSpanProcessor in order to get off of the application thread as soon as possible
150-
BatchSpanProcessor batchSpanProcessor =
195+
batchSpanProcessor =
151196
BatchSpanProcessor.builder(spanExporter)
152197
.setScheduleDelay(getBatchProcessorDelay())
153198
.build();
154199

155-
tracerProvider.addSpanProcessor(
156-
new TelemetryClientFlushingSpanProcessor(batchSpanProcessor, telemetryClient));
200+
tracerProvider.addSpanProcessor(batchSpanProcessor);
157201
}
158202

159203
return tracerProvider;
@@ -210,18 +254,15 @@ private static SdkLogEmitterProviderBuilder configureLogging(
210254
LogExporter logExporter = createLogExporter(telemetryClient, configuration);
211255

212256
// using BatchLogProcessor in order to get off of the application thread as soon as possible
213-
BatchLogProcessor batchLogProcessor =
257+
batchLogProcessor =
214258
BatchLogProcessor.builder(logExporter).setScheduleDelay(getBatchProcessorDelay()).build();
215259

216-
TelemetryClientFlushingLogProcessor telemetryClientFlushingLogProcessor =
217-
new TelemetryClientFlushingLogProcessor(batchLogProcessor, telemetryClient);
218-
219260
// inherited attributes log processor also handles operation name, ikey and role name attributes
220261
// and these all need access to Span.current(), so must be run before passing off to the
221262
// BatchLogProcessor
222263
return builder.addLogProcessor(
223264
new InheritedAttributesLogProcessor(
224-
configuration.preview.inheritedAttributes, telemetryClientFlushingLogProcessor));
265+
configuration.preview.inheritedAttributes, batchLogProcessor));
225266
}
226267

227268
private static LogExporter createLogExporter(
@@ -282,15 +323,12 @@ private static SdkMeterProviderBuilder configureMetrics(
282323
TelemetryClient telemetryClient,
283324
Configuration configuration) {
284325

285-
PeriodicMetricReader metricReader =
326+
metricReader =
286327
PeriodicMetricReader.builder(new AzureMonitorMetricExporter(telemetryClient))
287328
.setInterval(Duration.ofSeconds(configuration.preview.metricIntervalSeconds))
288329
.build();
289330

290-
TelemetryClientFlushingMetricReader telemetryClientFlushingMetricReader =
291-
new TelemetryClientFlushingMetricReader(metricReader, telemetryClient);
292-
293-
return builder.registerMetricReader(telemetryClientFlushingMetricReader);
331+
return builder.registerMetricReader(metricReader);
294332
}
295333

296334
private static class BackCompatHttpUrlProcessor implements SpanExporter {

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

-81
This file was deleted.

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

-82
This file was deleted.

0 commit comments

Comments
 (0)