|
16 | 16 | package com.google.cloud.opentelemetry.trace;
|
17 | 17 |
|
18 | 18 | import com.google.cloud.ServiceOptions;
|
| 19 | +import com.google.common.base.Suppliers; |
19 | 20 | import io.opentelemetry.sdk.common.CompletableResultCode;
|
20 | 21 | import io.opentelemetry.sdk.trace.data.SpanData;
|
21 | 22 | import io.opentelemetry.sdk.trace.export.SpanExporter;
|
22 |
| -import java.io.IOException; |
23 |
| -import java.util.Collection; |
24 |
| -import java.util.concurrent.atomic.AtomicReference; |
25 |
| -import javax.annotation.Nonnull; |
26 | 23 | import org.slf4j.Logger;
|
27 | 24 | import org.slf4j.LoggerFactory;
|
28 | 25 |
|
| 26 | +import javax.annotation.Nonnull; |
| 27 | +import java.io.IOException; |
| 28 | +import java.util.Collection; |
| 29 | +import java.util.function.Supplier; |
| 30 | + |
29 | 31 | public class TraceExporter implements SpanExporter {
|
30 | 32 |
|
31 | 33 | private static final Logger logger = LoggerFactory.getLogger(TraceExporter.class);
|
32 | 34 |
|
33 |
| - private final TraceConfiguration customTraceConfiguration; |
34 |
| - private final AtomicReference<SpanExporter> internalTraceExporter; |
| 35 | + private final Supplier<SpanExporter> internalTraceExporterSupplier; |
35 | 36 |
|
36 | 37 | private TraceExporter(TraceConfiguration configuration) {
|
37 |
| - this.customTraceConfiguration = configuration; |
38 |
| - this.internalTraceExporter = new AtomicReference<>(null); |
| 38 | + this.internalTraceExporterSupplier = |
| 39 | + Suppliers.memoize( |
| 40 | + () -> { |
| 41 | + try { |
| 42 | + return InternalTraceExporter.createWithConfiguration(configuration); |
| 43 | + } catch (IOException e) { |
| 44 | + logger.warn("Unable to initialize TraceExporter. Export operation failed.", e); |
| 45 | + return new NoopSpanExporter(); |
| 46 | + } |
| 47 | + }); |
39 | 48 | }
|
40 | 49 |
|
41 | 50 | /**
|
@@ -75,33 +84,16 @@ public static SpanExporter createWithConfiguration(TraceConfiguration configurat
|
75 | 84 |
|
76 | 85 | @Override
|
77 | 86 | public CompletableResultCode flush() {
|
78 |
| - // We do no exporter buffering of spans, so we're always flushed. |
79 |
| - return CompletableResultCode.ofSuccess(); |
| 87 | + return internalTraceExporterSupplier.get().flush(); |
80 | 88 | }
|
81 | 89 |
|
82 | 90 | @Override
|
83 | 91 | public CompletableResultCode export(@Nonnull Collection<SpanData> spanDataList) {
|
84 |
| - SpanExporter currentExporter = internalTraceExporter.get(); |
85 |
| - if (currentExporter == null) { |
86 |
| - try { |
87 |
| - internalTraceExporter.compareAndSet( |
88 |
| - null, InternalTraceExporter.createWithConfiguration(this.customTraceConfiguration)); |
89 |
| - currentExporter = internalTraceExporter.get(); |
90 |
| - } catch (IOException e) { |
91 |
| - logger.warn("Unable to initialize TraceExporter. Export operation failed.", e); |
92 |
| - return CompletableResultCode.ofFailure(); |
93 |
| - } |
94 |
| - } |
95 |
| - return currentExporter.export(spanDataList); |
| 92 | + return internalTraceExporterSupplier.get().export(spanDataList); |
96 | 93 | }
|
97 | 94 |
|
98 | 95 | @Override
|
99 | 96 | public CompletableResultCode shutdown() {
|
100 |
| - SpanExporter currentExporter = internalTraceExporter.get(); |
101 |
| - if (currentExporter != null) { |
102 |
| - return currentExporter.shutdown(); |
103 |
| - } else { |
104 |
| - return CompletableResultCode.ofFailure(); |
105 |
| - } |
| 97 | + return internalTraceExporterSupplier.get().shutdown(); |
106 | 98 | }
|
107 | 99 | }
|
0 commit comments