-
Notifications
You must be signed in to change notification settings - Fork 293
/
Copy pathCustomScopeManagerWrapper.java
268 lines (231 loc) · 8.02 KB
/
CustomScopeManagerWrapper.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
package datadog.opentracing;
import static java.util.concurrent.TimeUnit.NANOSECONDS;
import static java.util.concurrent.TimeUnit.SECONDS;
import datadog.trace.api.Config;
import datadog.trace.bootstrap.instrumentation.api.AgentScope;
import datadog.trace.bootstrap.instrumentation.api.AgentScopeManager;
import datadog.trace.bootstrap.instrumentation.api.AgentSpan;
import datadog.trace.bootstrap.instrumentation.api.ScopeSource;
import datadog.trace.bootstrap.instrumentation.api.ScopeState;
import datadog.trace.util.AgentTaskScheduler;
import io.opentracing.Scope;
import io.opentracing.ScopeManager;
import io.opentracing.Span;
import java.util.ArrayDeque;
import java.util.Deque;
import java.util.Iterator;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.TimeUnit;
/**
* Allows custom OpenTracing ScopeManagers used by CoreTracer
*
* <p>Normal case:
*
* <p>CoreTracer.scopeManager = ContextualScopeManager
*
* <p>DDTracer.scopeManager = OTScopeManager wrapping CoreTracer.scopeManager
*
* <p>Custom case:
*
* <p>CoreTracer.scopeManager = CustomScopeManagerWrapper wrapping passed in scopemanager
*
* <p>DDTracer.scopeManager = passed in scopemanager
*/
class CustomScopeManagerWrapper implements AgentScopeManager {
private static final String DD_ITERATION = "_dd.iteration";
private static final boolean CAN_GET_ACTIVE_SPAN;
private static final boolean CAN_GET_ACTIVE_SCOPE;
static {
boolean canGetActiveScope = true;
boolean canGetActiveSpan = true;
try {
ScopeManager.class.getMethod("active");
} catch (Throwable e) {
canGetActiveScope = false;
}
try {
ScopeManager.class.getMethod("activeSpan");
} catch (Throwable e) {
canGetActiveSpan = false;
}
CAN_GET_ACTIVE_SCOPE = canGetActiveScope;
CAN_GET_ACTIVE_SPAN = canGetActiveSpan;
}
private final ScopeManager delegate;
private final TypeConverter converter;
static final long iterationKeepAlive =
SECONDS.toMillis(Config.get().getScopeIterationKeepAlive());
volatile ConcurrentMap<Thread, IterationSpanStack> iterationSpans;
CustomScopeManagerWrapper(final ScopeManager scopeManager, final TypeConverter converter) {
delegate = scopeManager;
this.converter = converter;
}
@Override
public AgentScope activate(final AgentSpan agentSpan, final ScopeSource source) {
final Span span = converter.toSpan(agentSpan);
final Scope scope = delegate.activate(span);
return converter.toAgentScope(span, scope);
}
@Override
public AgentScope activate(
final AgentSpan agentSpan, final ScopeSource source, boolean isAsyncPropagating) {
final Span span = converter.toSpan(agentSpan);
final Scope scope = delegate.activate(span);
final AgentScope agentScope = converter.toAgentScope(span, scope);
agentScope.setAsyncPropagation(isAsyncPropagating);
return agentScope;
}
private Span delegateActiveSpan() {
if (CAN_GET_ACTIVE_SPAN) {
return delegate.activeSpan();
} else {
Scope scope = delegate.active();
return scope == null ? null : scope.span();
}
}
@Override
public AgentScope active() {
return converter.toAgentScope(
delegateActiveSpan(), CAN_GET_ACTIVE_SCOPE ? delegate.active() : null);
}
@Override
public AgentSpan activeSpan() {
return converter.toAgentSpan(delegateActiveSpan());
}
@Override
public AgentScope.Continuation captureSpan(final AgentSpan span) {
// I can't see a better way to do this, and I don't know if this even makes sense.
try (AgentScope scope = this.activate(span, ScopeSource.INSTRUMENTATION)) {
return scope.capture();
}
}
@Override
public void closePrevious(final boolean finishSpan) {
Span span = delegateActiveSpan();
if (span != null) {
AgentSpan agentSpan = converter.toAgentSpan(span);
if (agentSpan != null && agentSpan.getTag(DD_ITERATION) != null) {
if (iterationKeepAlive > 0) {
cancelIterationSpanCleanup(agentSpan);
}
if (CAN_GET_ACTIVE_SCOPE) {
delegate.active().close();
}
if (finishSpan) {
agentSpan.finishWithEndToEnd();
}
}
}
}
@Override
public AgentScope activateNext(final AgentSpan agentSpan) {
agentSpan.setTag(DD_ITERATION, "true");
Span span = converter.toSpan(agentSpan);
Scope scope = delegate.activate(span);
if (iterationKeepAlive > 0) {
scheduleIterationSpanCleanup(agentSpan);
}
return converter.toAgentScope(span, scope);
}
@Override
public ScopeState newScopeState() {
return new CustomScopeState();
}
private class CustomScopeState implements ScopeState {
private AgentSpan span = activeSpan();
@Override
public void activate() {
CustomScopeManagerWrapper.this.activate(span, ScopeSource.INSTRUMENTATION);
}
@Override
public void fetchFromActive() {
span = activeSpan();
}
}
private void scheduleIterationSpanCleanup(final AgentSpan span) {
if (iterationSpans == null) {
synchronized (this) {
if (iterationSpans == null) {
iterationSpans = new ConcurrentHashMap<Thread, IterationSpanStack>();
CustomScopeManagerWrapper.IterationCleaner.scheduleFor(iterationSpans);
}
}
}
IterationSpanStack spanStack = iterationSpans.get(Thread.currentThread());
if (spanStack == null) {
iterationSpans.put(Thread.currentThread(), spanStack = new IterationSpanStack());
}
spanStack.trackSpan(span);
}
private void cancelIterationSpanCleanup(AgentSpan span) {
if (iterationSpans != null) {
IterationSpanStack spanStack = iterationSpans.get(Thread.currentThread());
if (spanStack != null) {
spanStack.untrackSpan(span);
}
}
}
/** Background task to clean-up spans from overdue iterations. */
private static final class IterationCleaner
implements AgentTaskScheduler.Task<Map<Thread, IterationSpanStack>> {
private static final CustomScopeManagerWrapper.IterationCleaner CLEANER =
new CustomScopeManagerWrapper.IterationCleaner();
public static void scheduleFor(Map<Thread, IterationSpanStack> iterationSpans) {
long period = Math.min(iterationKeepAlive, 10_000);
AgentTaskScheduler.INSTANCE.scheduleAtFixedRate(
CLEANER, iterationSpans, iterationKeepAlive, period, TimeUnit.MILLISECONDS);
}
@Override
public void run(Map<Thread, IterationSpanStack> iterationSpans) {
Iterator<Map.Entry<Thread, IterationSpanStack>> itr = iterationSpans.entrySet().iterator();
long cutOff = System.currentTimeMillis() - iterationKeepAlive;
while (itr.hasNext()) {
Map.Entry<Thread, IterationSpanStack> entry = itr.next();
Thread thread = entry.getKey();
IterationSpanStack spanStack = entry.getValue();
if (thread.isAlive()) {
spanStack.finishOverdueSpans(cutOff);
} else { // thread has stopped
spanStack.finishAllSpans();
itr.remove();
}
}
}
}
private static final class IterationSpanStack {
private final Deque<AgentSpan> spans = new ArrayDeque<>();
public void trackSpan(AgentSpan span) {
synchronized (spans) {
spans.push(span);
}
}
public void untrackSpan(AgentSpan span) {
synchronized (spans) {
spans.remove(span);
}
}
public void finishOverdueSpans(long cutOff) {
while (true) {
AgentSpan s;
synchronized (spans) {
s = spans.peek();
if (s == null || cutOff <= NANOSECONDS.toMillis(s.getStartTime())) {
break; // no more spans, or span started after the cut-off (keeps previous spans alive)
}
spans.poll();
}
s.finishWithEndToEnd();
}
}
public void finishAllSpans() {
synchronized (spans) {
for (AgentSpan s : spans) {
s.finishWithEndToEnd();
}
// no need to clear as this is only called when the owning thread is no longer alive
}
}
}
}