-
Notifications
You must be signed in to change notification settings - Fork 320
/
Span.java
545 lines (486 loc) · 21.2 KB
/
Span.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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package co.elastic.apm.agent.impl.transaction;
import co.elastic.apm.agent.configuration.CoreConfiguration;
import co.elastic.apm.agent.impl.ElasticApmTracer;
import co.elastic.apm.agent.impl.baggage.Baggage;
import co.elastic.apm.agent.impl.context.Db;
import co.elastic.apm.agent.impl.context.Message;
import co.elastic.apm.agent.impl.context.ServiceTarget;
import co.elastic.apm.agent.impl.context.SpanContext;
import co.elastic.apm.agent.impl.context.Url;
import co.elastic.apm.agent.impl.stacktrace.StacktraceConfiguration;
import co.elastic.apm.agent.tracer.util.ResultUtil;
import co.elastic.apm.agent.sdk.logging.Logger;
import co.elastic.apm.agent.sdk.logging.LoggerFactory;
import co.elastic.apm.agent.tracer.Outcome;
import co.elastic.apm.agent.tracer.pooling.Recyclable;
import co.elastic.apm.agent.util.CharSequenceUtils;
import javax.annotation.Nullable;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.TimeUnit;
public class Span extends AbstractSpan<Span> implements Recyclable, co.elastic.apm.agent.tracer.Span<Span> {
private static final Logger logger = LoggerFactory.getLogger(Span.class);
public static final long MAX_LOG_INTERVAL_MICRO_SECS = TimeUnit.MINUTES.toMicros(5);
private static long lastSpanMaxWarningTimestamp;
private final StacktraceConfiguration stacktraceConfiguration;
/**
* A subtype describing this span (eg 'mysql', 'elasticsearch', 'jsf' etc)
* (Optional)
*/
@Nullable
private String subtype;
/**
* An action describing this span (eg 'query', 'execute', 'render' etc)
* (Optional)
*/
@Nullable
private String action;
/**
* Any other arbitrary data captured by the agent, optionally provided by the user
*/
private final SpanContext context = new SpanContext();
private final Composite composite = new Composite();
@Nullable
private Throwable stacktrace;
@Nullable
private AbstractSpan<?> parent;
@Nullable
private Transaction transaction;
@Nullable
private List<StackFrame> stackFrames;
/**
* If a span is non-discardable, all the spans leading up to it are non-discardable as well
*/
public void setNonDiscardable() {
if (isDiscardable()) {
getTraceContext().setNonDiscardable();
if (parent != null) {
parent.setNonDiscardable();
}
}
}
public Span(ElasticApmTracer tracer) {
super(tracer);
this.stacktraceConfiguration = tracer.getConfig(StacktraceConfiguration.class);
}
public <T> Span start(TraceContext.ChildContextCreator<T> childContextCreator, T parentContext, Baggage parentBaggage, long epochMicros) {
this.baggage = parentBaggage;
childContextCreator.asChildOf(traceContext, parentContext);
if (parentContext instanceof Transaction) {
this.transaction = (Transaction) parentContext;
this.parent = this.transaction;
} else if (parentContext instanceof Span) {
final Span parentSpan = (Span) parentContext;
this.parent = parentSpan;
this.transaction = parentSpan.transaction;
}
return start(epochMicros);
}
private Span start(long epochMicros) {
if (transaction != null) {
SpanCount spanCount = transaction.getSpanCount();
if (transaction.isSpanLimitReached()) {
if (epochMicros - lastSpanMaxWarningTimestamp > MAX_LOG_INTERVAL_MICRO_SECS) {
lastSpanMaxWarningTimestamp = epochMicros;
logger.warn("Max spans ({}) for transaction {} has been reached. For this transaction and possibly others, further spans will be dropped. See config param 'transaction_max_spans'.",
tracer.getConfig(CoreConfiguration.class).getTransactionMaxSpans(), transaction);
}
logger.debug("Span exceeds transaction_max_spans {}", this);
traceContext.setRecorded(false);
spanCount.getDropped().incrementAndGet();
}
spanCount.getTotal().incrementAndGet();
}
if (epochMicros >= 0) {
setStartTimestamp(epochMicros);
} else {
setStartTimestampNow();
}
if (logger.isDebugEnabled()) {
logger.debug("startSpan {}", this);
if (logger.isTraceEnabled()) {
logger.trace("starting span at",
new RuntimeException("this exception is just used to record where the span has been started from"));
}
}
onAfterStart();
return this;
}
@Override
protected void onAfterStart() {
super.onAfterStart();
if (parent != null) {
this.parent.incrementReferences();
this.parent.onChildStart(getTimestamp());
}
}
/**
* Any other arbitrary data captured by the agent, optionally provided by the user
*/
@Override
public SpanContext getContext() {
return context;
}
public boolean isComposite() {
return composite.getCount() > 0;
}
public Composite getComposite() {
return composite;
}
@Override
public Span withSubtype(@Nullable String subtype) {
this.subtype = normalizeEmpty(subtype);
return this;
}
@Override
public Span withAction(@Nullable String action) {
this.action = normalizeEmpty(action);
return this;
}
/**
* Sets span.type, span.subtype and span.action. If no subtype and action are provided, assumes the legacy usage of hierarchical
* typing system and attempts to split the type by dots to discover subtype and action.
* TODO: remove in 2.0 - no need for that once we decide to drop support for old agent usages
*/
@Deprecated
public void setType(@Nullable String type, @Nullable String subtype, @Nullable String action) {
if (type != null && (subtype == null || subtype.isEmpty()) && (action == null || action.isEmpty())) {
// hierarchical typing - pre 1.4; we need to split
String temp = type;
int indexOfFirstDot = temp.indexOf(".");
if (indexOfFirstDot > 0) {
type = temp.substring(0, indexOfFirstDot);
int indexOfSecondDot = temp.indexOf(".", indexOfFirstDot + 1);
if (indexOfSecondDot > 0) {
subtype = temp.substring(indexOfFirstDot + 1, indexOfSecondDot);
if (indexOfSecondDot + 1 < temp.length()) {
action = temp.substring(indexOfSecondDot + 1);
}
}
}
}
withType(type);
withSubtype(subtype);
withAction(action);
}
@Nullable
public Throwable getStacktrace() {
return stacktrace;
}
@Override
@Nullable
public String getSubtype() {
return subtype;
}
@Override
@Nullable
public String getAction() {
return action;
}
@Override
public void beforeEnd(long epochMicros) {
// set outcome when not explicitly set by user nor instrumentation
if (outcomeNotSet()) {
Outcome outcome;
if (context.getHttp().hasContent()) {
// HTTP client spans
outcome = ResultUtil.getOutcomeByHttpClientStatus(context.getHttp().getStatusCode());
} else {
// span types & sub-types for which we consider getting an exception as a failure
outcome = hasCapturedExceptions() ? Outcome.FAILURE : Outcome.SUCCESS;
}
withOutcome(outcome);
}
// auto-infer context.destination.service.resource as per spec:
// https://github.com/elastic/apm/blob/main/specs/agents/tracing-spans-destination.md#contextdestinationserviceresource
ServiceTarget serviceTarget = getContext().getServiceTarget();
if (isExit() && !serviceTarget.hasContent() && !serviceTarget.isSetByUser()) {
Db db = context.getDb();
Message message = context.getMessage();
Url httpUrl = context.getHttp().getInternalUrl();
String targetServiceType = (subtype != null) ? subtype : type;
if (db.hasContent()) {
serviceTarget.withType(targetServiceType).withName(db.getInstance());
} else if (message.hasContent()) {
serviceTarget.withType(targetServiceType).withName(message.getQueueName());
} else if (httpUrl.hasContent()) {
// direct modification of destination resource to ensure compatibility
serviceTarget.withType("http")
.withHostPortName(httpUrl.getHostname(), httpUrl.getPort())
.withNameOnlyDestinationResource();
} else {
serviceTarget.withType(targetServiceType);
}
}
if (transaction != null) {
transaction.incrementTimer(type, subtype, getSelfDuration());
}
if (parent != null) {
parent.onChildEnd(epochMicros);
}
}
@Override
protected void afterEnd() {
// capture stack trace when the span ends, relies on this method being called synchronously from the instrumentation
long spanStackTraceMinDurationMs = stacktraceConfiguration.getSpanStackTraceMinDurationMs();
if (spanStackTraceMinDurationMs >= 0 && isSampled() && stackFrames == null) {
if (getDurationMs() >= spanStackTraceMinDurationMs) {
this.stacktrace = new Throwable();
}
}
// Why do we increment references of this span here?
// The only thing preventing the "this"-span from being recycled is the initial reference increment in onAfterStart()
// There are multiple ways in afterEnd() on how this reference may be decremented and therefore potentially causing recycling:
// - we call tracer.endSpan() for this span and the span is dropped / not reported for some reason
// - we call tracer.endSpan() for this span and the span is reported and released afterwards (=> recycled on the reporter thread!)
// - we successfully set parent.bufferedSpan to "this".
// - a span on a different thread with the same parent can now call tracer.endSpan() for parent.bufferedSpan (=this span)
// - the parent span is ended on a different thread and calls tracer.endSpan() for parent.bufferedSpan (=this span)
// By incrementing the reference count here, we guarantee that the "this" span is only recycled AFTER we decrement the reference count again
this.incrementReferences();
try {
if (transaction != null && transaction.isSpanCompressionEnabled() && parent != null) {
Span parentBuffered = parent.bufferedSpan.incrementReferencesAndGet();
try {
//per the reference, if it is not compression-eligible or if its parent has already ended, it is reported immediately
if (parent.isFinished() || !isCompressionEligible()) {
if (parentBuffered != null) {
if (parent.bufferedSpan.compareAndSet(parentBuffered, null)) {
this.tracer.endSpan(parentBuffered);
logger.trace("parent span compression buffer was set to null and {} was ended", parentBuffered);
}
}
this.tracer.endSpan(this);
return;
}
//since it wasn't reported, this span gets buffered
if (parentBuffered == null) {
if (!parent.bufferedSpan.compareAndSet(null, this)) {
// the failed update would ideally lead to a compression attempt with the new buffer,
// but we're dropping the compression attempt to keep things simple and avoid looping so this stays wait-free
// this doesn't exactly diverge from the spec, but it can lead to non-optimal compression under high load
this.tracer.endSpan(this);
} else {
logger.trace("parent span compression buffer was set to {}", this);
}
return;
}
//still trying to buffer this span
if (!parentBuffered.tryToCompress(this)) {
// we couldn't compress so replace the buffer with this
if (parent.bufferedSpan.compareAndSet(parentBuffered, this)) {
this.tracer.endSpan(parentBuffered);
logger.trace("parent span compression buffer was set to {} and {} was ended", this, parentBuffered);
} else {
// the failed update would ideally lead to a compression attempt with the new buffer,
// but we're dropping the compression attempt to keep things simple and avoid looping so this stays wait-free
// this doesn't exactly diverge from the spec, but it can lead to non-optimal compression under high load
this.tracer.endSpan(this);
}
} else {
if (isSampled() && transaction != null) {
transaction.getSpanCount().getDropped().incrementAndGet();
}
//drop the span by removing the reference allocated in onAfterStart() because it has been compressed
decrementReferences();
}
} finally {
if (parentBuffered != null) {
parentBuffered.decrementReferences();
}
}
} else {
this.tracer.endSpan(this);
}
} finally {
if (parent != null) {
//this needs to happen before this.decrementReferences(), because otherwise "this" might have been recycled already
parent.decrementReferences();
}
this.decrementReferences();
}
}
private boolean isCompressionEligible() {
return isExit() && isDiscardable() && (outcomeNotSet() || getOutcome() == Outcome.SUCCESS);
}
private boolean tryToCompress(Span sibling) {
boolean canBeCompressed = isComposite() ? tryToCompressComposite(sibling) : tryToCompressRegular(sibling);
if (!canBeCompressed) {
return false;
}
do {
long currentTimestamp = timestamp.get();
if (currentTimestamp <= sibling.timestamp.get()) {
break;
}
if (timestamp.compareAndSet(currentTimestamp, sibling.timestamp.get())) {
break;
}
} while (true);
do {
long currentEndTimestamp = endTimestamp.get();
if (sibling.endTimestamp.get() <= currentEndTimestamp) {
break;
}
if (endTimestamp.compareAndSet(currentEndTimestamp, sibling.endTimestamp.get())) {
break;
}
} while (true);
composite.increaseCount();
composite.increaseSum(sibling.getDuration());
return true;
}
private boolean tryToCompressRegular(Span sibling) {
if (!isSameKind(sibling)) {
return false;
}
long currentDuration = getDuration();
if (isComposite()) {
return tryToCompressComposite(sibling);
}
if (CharSequenceUtils.equals(name, sibling.name)) {
long maxExactMatchDuration = transaction.getSpanCompressionExactMatchMaxDurationUs();
if (currentDuration <= maxExactMatchDuration && sibling.getDuration() <= maxExactMatchDuration) {
if (!composite.init(currentDuration, "exact_match")) {
return tryToCompressComposite(sibling);
}
return true;
}
return false;
}
long maxSameKindDuration = transaction.getSpanCompressionSameKindMaxDurationUs();
if (currentDuration <= maxSameKindDuration && sibling.getDuration() <= maxSameKindDuration) {
if (!composite.init(currentDuration, "same_kind")) {
return tryToCompressComposite(sibling);
}
setCompressedSpanName();
return true;
}
return false;
}
private void setCompressedSpanName() {
name.setLength(0);
ServiceTarget serviceTarget = context.getServiceTarget();
String serviceType = serviceTarget.getType();
CharSequence serviceName = serviceTarget.getName();
name.append("Calls to ");
if (serviceType == null && serviceName == null) {
name.append("unknown");
} else {
boolean hasType = serviceType != null;
if (hasType) {
name.append(serviceType);
}
if (serviceName != null) {
if (hasType) {
name.append('/');
}
name.append(serviceName);
}
}
}
private boolean tryToCompressComposite(Span sibling) {
String compressionStrategy = composite.getCompressionStrategy();
if (compressionStrategy == null) {
//lose the compression rather than retry, so that the application proceeds with minimum delay
return false;
}
switch (compressionStrategy) {
case "exact_match":
long maxExactMatchDuration = transaction.getSpanCompressionExactMatchMaxDurationUs();
return isSameKind(sibling) && CharSequenceUtils.equals(name, sibling.name) && sibling.getDuration() <= maxExactMatchDuration;
case "same_kind":
long maxSameKindDuration = transaction.getSpanCompressionSameKindMaxDurationUs();
return isSameKind(sibling) && sibling.getDuration() <= maxSameKindDuration;
default:
}
return false;
}
private boolean isSameKind(Span other) {
ServiceTarget serviceTarget = context.getServiceTarget();
ServiceTarget otherServiceTarget = other.context.getServiceTarget();
return Objects.equals(type, other.type)
&& Objects.equals(subtype, other.subtype)
&& Objects.equals(serviceTarget.getType(), otherServiceTarget.getType())
&& CharSequenceUtils.equals(serviceTarget.getName(), otherServiceTarget.getName());
}
@Override
public void resetState() {
super.resetState();
context.resetState();
composite.resetState();
stacktrace = null;
subtype = null;
action = null;
parent = null;
transaction = null;
// recycling this array list by clear()-ing it doesn't seem worth it
// it's used in the context of profiling-inferred spans which entails allocations anyways
// when trying to recycle this list by clearing it, we increase the static memory overhead of the agent
// because all spans in the pool contain that list even if they are not used as inferred spans
stackFrames = null;
}
@Override
public String toString() {
return String.format("'%s' %s (%s)", name, traceContext, Integer.toHexString(System.identityHashCode(this)));
}
public Span withStacktrace(Throwable stacktrace) {
this.stacktrace = stacktrace;
return this;
}
@Override
public void incrementReferences() {
if (transaction != null) {
transaction.incrementReferences();
}
super.incrementReferences();
}
@Override
public void decrementReferences() {
if (transaction != null) {
transaction.decrementReferences();
}
super.decrementReferences();
}
@Override
protected void recycle() {
tracer.recycle(this);
}
@Override
protected Span thiz() {
return this;
}
public void setStackTrace(List<StackFrame> stackTrace) {
this.stackFrames = stackTrace;
}
@Nullable
public List<StackFrame> getStackFrames() {
return stackFrames;
}
@Override
public Transaction getParentTransaction() {
return transaction;
}
@Nullable
public AbstractSpan<?> getParent() {
return parent;
}
}