40
40
import com .microsoft .applicationinsights .agent .internal .telemetry .TelemetryClient ;
41
41
import io .opentelemetry .api .common .Attributes ;
42
42
import io .opentelemetry .api .common .AttributesBuilder ;
43
+ import io .opentelemetry .sdk .autoconfigure .AutoConfiguredOpenTelemetrySdkBuilder ;
43
44
import io .opentelemetry .sdk .autoconfigure .spi .AutoConfigurationCustomizer ;
44
45
import io .opentelemetry .sdk .autoconfigure .spi .AutoConfigurationCustomizerProvider ;
45
46
import io .opentelemetry .sdk .autoconfigure .spi .ConfigProperties ;
48
49
import io .opentelemetry .sdk .logs .export .BatchLogProcessor ;
49
50
import io .opentelemetry .sdk .logs .export .LogExporter ;
50
51
import io .opentelemetry .sdk .metrics .SdkMeterProviderBuilder ;
52
+ import io .opentelemetry .sdk .metrics .export .MetricReader ;
51
53
import io .opentelemetry .sdk .metrics .export .PeriodicMetricReader ;
52
54
import io .opentelemetry .sdk .trace .SdkTracerProviderBuilder ;
53
55
import io .opentelemetry .sdk .trace .data .SpanData ;
59
61
import java .util .Collection ;
60
62
import java .util .Collections ;
61
63
import java .util .List ;
64
+ import java .util .concurrent .TimeUnit ;
62
65
import java .util .stream .Collectors ;
63
66
import javax .annotation .Nullable ;
64
67
@@ -67,6 +70,10 @@ public class OpenTelemetryConfigurer implements AutoConfigurationCustomizerProvi
67
70
68
71
@ Nullable public static LoggerExporter loggerExporter ;
69
72
73
+ @ Nullable private static BatchLogProcessor batchLogProcessor ;
74
+ @ Nullable private static BatchSpanProcessor batchSpanProcessor ;
75
+ @ Nullable private static MetricReader metricReader ;
76
+
70
77
@ Override
71
78
public void customize (AutoConfigurationCustomizer autoConfiguration ) {
72
79
TelemetryClient telemetryClient = TelemetryClient .getActive ();
@@ -75,6 +82,9 @@ public void customize(AutoConfigurationCustomizer autoConfiguration) {
75
82
return ;
76
83
}
77
84
85
+ // TODO (trask) add this method to AutoConfigurationCustomizer upstream?
86
+ ((AutoConfiguredOpenTelemetrySdkBuilder ) autoConfiguration ).registerShutdownHook (false );
87
+
78
88
Configuration configuration = MainEntryPoint .getConfiguration ();
79
89
80
90
autoConfiguration
@@ -84,6 +94,41 @@ public void customize(AutoConfigurationCustomizer autoConfiguration) {
84
94
(builder , config ) -> configureLogging (builder , telemetryClient , configuration ))
85
95
.addMeterProviderCustomizer (
86
96
(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 ;
87
132
}
88
133
89
134
private static SdkTracerProviderBuilder configureTracing (
@@ -147,13 +192,12 @@ private static SdkTracerProviderBuilder configureTracing(
147
192
telemetryClient , configuration , configuration .preview .captureHttpServer4xxAsError );
148
193
149
194
// using BatchSpanProcessor in order to get off of the application thread as soon as possible
150
- BatchSpanProcessor batchSpanProcessor =
195
+ batchSpanProcessor =
151
196
BatchSpanProcessor .builder (spanExporter )
152
197
.setScheduleDelay (getBatchProcessorDelay ())
153
198
.build ();
154
199
155
- tracerProvider .addSpanProcessor (
156
- new TelemetryClientFlushingSpanProcessor (batchSpanProcessor , telemetryClient ));
200
+ tracerProvider .addSpanProcessor (batchSpanProcessor );
157
201
}
158
202
159
203
return tracerProvider ;
@@ -210,18 +254,15 @@ private static SdkLogEmitterProviderBuilder configureLogging(
210
254
LogExporter logExporter = createLogExporter (telemetryClient , configuration );
211
255
212
256
// using BatchLogProcessor in order to get off of the application thread as soon as possible
213
- BatchLogProcessor batchLogProcessor =
257
+ batchLogProcessor =
214
258
BatchLogProcessor .builder (logExporter ).setScheduleDelay (getBatchProcessorDelay ()).build ();
215
259
216
- TelemetryClientFlushingLogProcessor telemetryClientFlushingLogProcessor =
217
- new TelemetryClientFlushingLogProcessor (batchLogProcessor , telemetryClient );
218
-
219
260
// inherited attributes log processor also handles operation name, ikey and role name attributes
220
261
// and these all need access to Span.current(), so must be run before passing off to the
221
262
// BatchLogProcessor
222
263
return builder .addLogProcessor (
223
264
new InheritedAttributesLogProcessor (
224
- configuration .preview .inheritedAttributes , telemetryClientFlushingLogProcessor ));
265
+ configuration .preview .inheritedAttributes , batchLogProcessor ));
225
266
}
226
267
227
268
private static LogExporter createLogExporter (
@@ -282,15 +323,12 @@ private static SdkMeterProviderBuilder configureMetrics(
282
323
TelemetryClient telemetryClient ,
283
324
Configuration configuration ) {
284
325
285
- PeriodicMetricReader metricReader =
326
+ metricReader =
286
327
PeriodicMetricReader .builder (new AzureMonitorMetricExporter (telemetryClient ))
287
328
.setInterval (Duration .ofSeconds (configuration .preview .metricIntervalSeconds ))
288
329
.build ();
289
330
290
- TelemetryClientFlushingMetricReader telemetryClientFlushingMetricReader =
291
- new TelemetryClientFlushingMetricReader (metricReader , telemetryClient );
292
-
293
- return builder .registerMetricReader (telemetryClientFlushingMetricReader );
331
+ return builder .registerMetricReader (metricReader );
294
332
}
295
333
296
334
private static class BackCompatHttpUrlProcessor implements SpanExporter {
0 commit comments