-
Notifications
You must be signed in to change notification settings - Fork 775
/
SimpleSpanProcessor.java
156 lines (137 loc) · 5 KB
/
SimpleSpanProcessor.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/
package io.opentelemetry.sdk.trace.export;
import static java.util.Objects.requireNonNull;
import io.opentelemetry.context.Context;
import io.opentelemetry.sdk.common.CompletableResultCode;
import io.opentelemetry.sdk.trace.ReadWriteSpan;
import io.opentelemetry.sdk.trace.ReadableSpan;
import io.opentelemetry.sdk.trace.SpanProcessor;
import io.opentelemetry.sdk.trace.data.SpanData;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* An implementation of the {@link SpanProcessor} that converts the {@link ReadableSpan} to {@link
* SpanData} and passes it directly to the configured exporter.
*
* <p>This processor will cause all spans to be exported directly as they finish, meaning each
* export request will have a single span. Most backends will not perform well with a single span
* per request so unless you know what you're doing, strongly consider using {@link
* BatchSpanProcessor} instead, including in special environments such as serverless runtimes.
* {@link SimpleSpanProcessor} is generally meant to for logging exporters only.
*/
public final class SimpleSpanProcessor implements SpanProcessor {
private static final Logger logger = Logger.getLogger(SimpleSpanProcessor.class.getName());
private final SpanExporter spanExporter;
private final boolean exportUnsampledSpans;
private final Set<CompletableResultCode> pendingExports =
Collections.newSetFromMap(new ConcurrentHashMap<>());
private final AtomicBoolean isShutdown = new AtomicBoolean(false);
/**
* Returns a new {@link SimpleSpanProcessor} which exports spans to the {@link SpanExporter}
* synchronously.
*
* <p>This processor will cause all spans to be exported directly as they finish, meaning each
* export request will have a single span. Most backends will not perform well with a single span
* per request so unless you know what you're doing, strongly consider using {@link
* BatchSpanProcessor} instead, including in special environments such as serverless runtimes.
* {@link SimpleSpanProcessor} is generally meant to for logging exporters only.
*/
public static SpanProcessor create(SpanExporter exporter) {
requireNonNull(exporter, "exporter");
return builder(exporter).build();
}
/**
* Returns a new Builder for {@link SimpleSpanProcessor}.
*
* @since 1.34.0
*/
public static SimpleSpanProcessorBuilder builder(SpanExporter exporter) {
requireNonNull(exporter, "exporter");
return new SimpleSpanProcessorBuilder(exporter);
}
SimpleSpanProcessor(SpanExporter spanExporter, boolean exportUnsampledSpans) {
this.spanExporter = requireNonNull(spanExporter, "spanExporter");
this.exportUnsampledSpans = exportUnsampledSpans;
}
@Override
public void onStart(Context parentContext, ReadWriteSpan span) {
// Do nothing.
}
@Override
public boolean isStartRequired() {
return false;
}
@Override
public void onEnd(ReadableSpan span) {
if (span != null && (exportUnsampledSpans || span.getSpanContext().isSampled())) {
try {
List<SpanData> spans = Collections.singletonList(span.toSpanData());
CompletableResultCode result = spanExporter.export(spans);
pendingExports.add(result);
result.whenComplete(
() -> {
pendingExports.remove(result);
if (!result.isSuccess()) {
logger.log(Level.FINE, "Exporter failed");
}
});
} catch (RuntimeException e) {
logger.log(Level.WARNING, "Exporter threw an Exception", e);
}
}
}
@Override
public boolean isEndRequired() {
return true;
}
@Override
public CompletableResultCode shutdown() {
if (isShutdown.getAndSet(true)) {
return CompletableResultCode.ofSuccess();
}
CompletableResultCode result = new CompletableResultCode();
CompletableResultCode flushResult = forceFlush();
flushResult.whenComplete(
() -> {
CompletableResultCode shutdownResult = spanExporter.shutdown();
shutdownResult.whenComplete(
() -> {
if (!flushResult.isSuccess() || !shutdownResult.isSuccess()) {
result.fail();
} else {
result.succeed();
}
});
});
return result;
}
@Override
public CompletableResultCode forceFlush() {
return CompletableResultCode.ofAll(pendingExports);
}
/**
* Return the processor's configured {@link SpanExporter}.
*
* @since 1.37.0
*/
public SpanExporter getSpanExporter() {
return spanExporter;
}
@Override
public String toString() {
return "SimpleSpanProcessor{"
+ "spanExporter="
+ spanExporter
+ ", exportUnsampledSpans="
+ exportUnsampledSpans
+ '}';
}
}