-
Notifications
You must be signed in to change notification settings - Fork 292
/
DDSpanContext.java
380 lines (325 loc) · 11 KB
/
DDSpanContext.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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
package datadog.opentracing;
import com.fasterxml.jackson.annotation.JsonIgnore;
import datadog.opentracing.decorators.AbstractDecorator;
import datadog.trace.api.DDTags;
import datadog.trace.api.sampling.PrioritySampling;
import java.math.BigInteger;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicReference;
import lombok.extern.slf4j.Slf4j;
/**
* SpanContext represents Span state that must propagate to descendant Spans and across process
* boundaries.
*
* <p>SpanContext is logically divided into two pieces: (1) the user-level "Baggage" that propagates
* across Span boundaries and (2) any Datadog fields that are needed to identify or contextualize
* the associated Span instance
*/
@Slf4j
public class DDSpanContext implements io.opentracing.SpanContext {
public static final String PRIORITY_SAMPLING_KEY = "_sampling_priority_v1";
public static final String SAMPLE_RATE_KEY = "_sample_rate";
public static final String ORIGIN_KEY = "_dd.origin";
private static final Map<String, Number> EMPTY_METRICS = Collections.emptyMap();
// Shared with other span contexts
/** For technical reasons, the ref to the original tracer */
private final DDTracer tracer;
/** The collection of all span related to this one */
private final PendingTrace trace;
/** Baggage is associated with the whole trace and shared with other spans */
private final Map<String, String> baggageItems;
// Not Shared with other span contexts
private final BigInteger traceId;
private final BigInteger spanId;
private final BigInteger parentId;
/** Tags are associated to the current span, they will not propagate to the children span */
private final Map<String, Object> tags = new ConcurrentHashMap<>();
/** The service name is required, otherwise the span are dropped by the agent */
private volatile String serviceName;
/** The resource associated to the service (server_web, database, etc.) */
private volatile String resourceName;
/** Each span have an operation name describing the current span */
private volatile String operationName;
/** The type of the span. If null, the Datadog Agent will report as a custom */
private volatile String spanType;
/** True indicates that the span reports an error */
private volatile boolean errorFlag;
/**
* When true, the samplingPriority cannot be changed. This prevents the sampling flag from
* changing after the context has propagated.
*
* <p>For thread safety, this boolean is only modified or accessed under instance lock.
*/
private boolean samplingPriorityLocked = false;
/** The origin of the trace. (eg. Synthetics) */
private final String origin;
/** Metrics on the span */
private final AtomicReference<Map<String, Number>> metrics = new AtomicReference<>();
// Additional Metadata
private final String threadName = Thread.currentThread().getName();
private final long threadId = Thread.currentThread().getId();
public DDSpanContext(
final BigInteger traceId,
final BigInteger spanId,
final BigInteger parentId,
final String serviceName,
final String operationName,
final String resourceName,
final int samplingPriority,
final String origin,
final Map<String, String> baggageItems,
final boolean errorFlag,
final String spanType,
final Map<String, Object> tags,
final PendingTrace trace,
final DDTracer tracer) {
assert tracer != null;
assert trace != null;
this.tracer = tracer;
this.trace = trace;
assert traceId != null;
assert spanId != null;
assert parentId != null;
this.traceId = traceId;
this.spanId = spanId;
this.parentId = parentId;
if (baggageItems == null) {
this.baggageItems = new ConcurrentHashMap<>(0);
} else {
this.baggageItems = new ConcurrentHashMap<>(baggageItems);
}
if (tags != null) {
this.tags.putAll(tags);
}
this.serviceName = serviceName;
this.operationName = operationName;
this.resourceName = resourceName;
this.errorFlag = errorFlag;
this.spanType = spanType;
this.origin = origin;
if (samplingPriority != PrioritySampling.UNSET) {
setSamplingPriority(samplingPriority);
}
if (origin != null) {
this.tags.put(ORIGIN_KEY, origin);
}
this.tags.put(DDTags.THREAD_NAME, threadName);
this.tags.put(DDTags.THREAD_ID, threadId);
}
public BigInteger getTraceId() {
return traceId;
}
@Override
public String toTraceId() {
return traceId.toString();
}
public BigInteger getParentId() {
return parentId;
}
public BigInteger getSpanId() {
return spanId;
}
@Override
public String toSpanId() {
return spanId.toString();
}
public String getServiceName() {
return serviceName;
}
public void setServiceName(final String serviceName) {
this.serviceName = serviceName;
}
public String getResourceName() {
return resourceName == null || resourceName.isEmpty() ? operationName : resourceName;
}
public void setResourceName(final String resourceName) {
this.resourceName = resourceName;
}
public String getOperationName() {
return operationName;
}
public void setOperationName(final String operationName) {
this.operationName = operationName;
}
public boolean getErrorFlag() {
return errorFlag;
}
public void setErrorFlag(final boolean errorFlag) {
this.errorFlag = errorFlag;
}
public String getSpanType() {
return spanType;
}
public void setSpanType(final String spanType) {
this.spanType = spanType;
}
/** @return if sampling priority was set by this method invocation */
public boolean setSamplingPriority(final int newPriority) {
if (newPriority == PrioritySampling.UNSET) {
log.debug("{}: Refusing to set samplingPriority to UNSET", this);
return false;
}
if (trace != null) {
final DDSpan rootSpan = trace.getRootSpan();
if (null != rootSpan && rootSpan.context() != this) {
return rootSpan.context().setSamplingPriority(newPriority);
}
}
// sync with lockSamplingPriority
synchronized (this) {
if (samplingPriorityLocked) {
log.debug(
"samplingPriority locked at {}. Refusing to set to {}",
getMetrics().get(PRIORITY_SAMPLING_KEY),
newPriority);
return false;
} else {
setMetric(PRIORITY_SAMPLING_KEY, newPriority);
log.debug("Set sampling priority to {}", getMetrics().get(PRIORITY_SAMPLING_KEY));
return true;
}
}
}
/** @return the sampling priority of this span's trace, or null if no priority has been set */
public int getSamplingPriority() {
final DDSpan rootSpan = trace.getRootSpan();
if (null != rootSpan && rootSpan.context() != this) {
return rootSpan.context().getSamplingPriority();
}
final Number val = getMetrics().get(PRIORITY_SAMPLING_KEY);
return null == val ? PrioritySampling.UNSET : val.intValue();
}
/**
* Prevent future changes to the context's sampling priority.
*
* <p>Used when a span is extracted or injected for propagation.
*
* <p>Has no effect if the sampling priority is unset.
*
* @return true if the sampling priority was locked.
*/
public boolean lockSamplingPriority() {
final DDSpan rootSpan = trace.getRootSpan();
if (null != rootSpan && rootSpan.context() != this) {
return rootSpan.context().lockSamplingPriority();
}
// sync with setSamplingPriority
synchronized (this) {
if (getMetrics().get(PRIORITY_SAMPLING_KEY) == null) {
log.debug("{} : refusing to lock unset samplingPriority", this);
} else if (samplingPriorityLocked == false) {
samplingPriorityLocked = true;
log.debug(
"{} : locked samplingPriority to {}", this, getMetrics().get(PRIORITY_SAMPLING_KEY));
}
return samplingPriorityLocked;
}
}
public String getOrigin() {
final DDSpan rootSpan = trace.getRootSpan();
if (null != rootSpan) {
return rootSpan.context().origin;
} else {
return origin;
}
}
public void setBaggageItem(final String key, final String value) {
baggageItems.put(key, value);
}
public String getBaggageItem(final String key) {
return baggageItems.get(key);
}
public Map<String, String> getBaggageItems() {
return baggageItems;
}
/* (non-Javadoc)
* @see io.opentracing.SpanContext#baggageItems()
*/
@Override
public Iterable<Map.Entry<String, String>> baggageItems() {
return baggageItems.entrySet();
}
@JsonIgnore
public PendingTrace getTrace() {
return trace;
}
@JsonIgnore
public DDTracer getTracer() {
return tracer;
}
public Map<String, Number> getMetrics() {
final Map<String, Number> metrics = this.metrics.get();
return metrics == null ? EMPTY_METRICS : metrics;
}
public void setMetric(final String key, final Number value) {
if (metrics.get() == null) {
metrics.compareAndSet(null, new ConcurrentHashMap<String, Number>());
}
if (value instanceof Float) {
metrics.get().put(key, value.doubleValue());
} else {
metrics.get().put(key, value);
}
}
/**
* Add a tag to the span. Tags are not propagated to the children
*
* @param tag the tag-name
* @param value the value of the tag. tags with null values are ignored.
*/
public synchronized void setTag(final String tag, final Object value) {
if (value == null || (value instanceof String && ((String) value).isEmpty())) {
tags.remove(tag);
return;
}
boolean addTag = true;
// Call decorators
final List<AbstractDecorator> decorators = tracer.getSpanContextDecorators(tag);
if (decorators != null) {
for (final AbstractDecorator decorator : decorators) {
try {
addTag &= decorator.shouldSetTag(this, tag, value);
} catch (final Throwable ex) {
log.debug(
"Could not decorate the span decorator={}: {}",
decorator.getClass().getSimpleName(),
ex.getMessage());
}
}
}
if (addTag) {
tags.put(tag, value);
}
}
public synchronized Map<String, Object> getTags() {
return Collections.unmodifiableMap(tags);
}
@Override
public String toString() {
final StringBuilder s =
new StringBuilder()
.append("DDSpan [ t_id=")
.append(traceId)
.append(", s_id=")
.append(spanId)
.append(", p_id=")
.append(parentId)
.append("] trace=")
.append(getServiceName())
.append("/")
.append(getOperationName())
.append("/")
.append(getResourceName())
.append(" metrics=")
.append(new TreeMap<>(getMetrics()));
if (errorFlag) {
s.append(" *errored*");
}
s.append(" tags=").append(new TreeMap<>(tags));
return s.toString();
}
}