forked from open-telemetry/opentelemetry-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSdkSpan.java
More file actions
662 lines (597 loc) · 18.9 KB
/
SdkSpan.java
File metadata and controls
662 lines (597 loc) · 18.9 KB
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
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/
package io.opentelemetry.sdk.trace;
import io.opentelemetry.api.common.AttributeKey;
import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.api.internal.GuardedBy;
import io.opentelemetry.api.trace.Span;
import io.opentelemetry.api.trace.SpanContext;
import io.opentelemetry.api.trace.SpanKind;
import io.opentelemetry.api.trace.StatusCode;
import io.opentelemetry.context.Context;
import io.opentelemetry.sdk.common.Clock;
import io.opentelemetry.sdk.common.InstrumentationScopeInfo;
import io.opentelemetry.sdk.internal.AttributeUtil;
import io.opentelemetry.sdk.internal.AttributesMap;
import io.opentelemetry.sdk.internal.InstrumentationScopeUtil;
import io.opentelemetry.sdk.resources.Resource;
import io.opentelemetry.sdk.trace.data.EventData;
import io.opentelemetry.sdk.trace.data.ExceptionEventData;
import io.opentelemetry.sdk.trace.data.LinkData;
import io.opentelemetry.sdk.trace.data.SpanData;
import io.opentelemetry.sdk.trace.data.StatusData;
import io.opentelemetry.sdk.trace.internal.ExtendedSpanProcessor;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.Nullable;
import javax.annotation.concurrent.ThreadSafe;
/** Implementation for the {@link Span} class that records trace events. */
@ThreadSafe
final class SdkSpan implements ReadWriteSpan {
private static final Logger logger = Logger.getLogger(SdkSpan.class.getName());
// The config used when constructing this Span.
private final SpanLimits spanLimits;
// Contains the identifiers associated with this Span.
private final SpanContext context;
// The parent SpanContext of this span. Invalid if this is a root span.
private final SpanContext parentSpanContext;
// Handler called when the span starts and ends.
private final SpanProcessor spanProcessor;
// The kind of the span.
private final SpanKind kind;
// The clock used to get the time.
private final AnchoredClock clock;
// The resource associated with this span.
private final Resource resource;
// instrumentation scope of the named tracer which created this span
private final InstrumentationScopeInfo instrumentationScopeInfo;
// The start time of the span.
private final long startEpochNanos;
// Lock used to internally guard the mutable state of this instance
private final Object lock = new Object();
@GuardedBy("lock")
private String name;
// Set of recorded attributes. DO NOT CALL any other method that changes the ordering of events.
@GuardedBy("lock")
@Nullable
private AttributesMap attributes;
// List of recorded events.
@GuardedBy("lock")
@Nullable
private List<EventData> events;
// Number of events recorded.
@GuardedBy("lock")
private int totalRecordedEvents = 0;
// The displayed name of the span.
// List of recorded links to parent and child spans.
@GuardedBy("lock")
@Nullable
List<LinkData> links;
// Number of links recorded.
@GuardedBy("lock")
private int totalRecordedLinks;
// The status of the span.
@GuardedBy("lock")
private StatusData status = StatusData.unset();
// The end time of the span.
@GuardedBy("lock")
private long endEpochNanos;
private enum EndState {
NOT_ENDED,
ENDING,
ENDED
}
@GuardedBy("lock")
private EndState hasEnded;
/**
* The thread on which {@link #end()} is called and which will be invoking the {@link
* SpanProcessor}s. This field is used to ensure that only this thread may modify the span while
* it is in state {@link EndState#ENDING} to prevent concurrent updates outside of {@link
* ExtendedSpanProcessor#onEnding(ReadWriteSpan)}.
*/
@GuardedBy("lock")
@Nullable
private Thread spanEndingThread;
private SdkSpan(
SpanContext context,
String name,
InstrumentationScopeInfo instrumentationScopeInfo,
SpanKind kind,
SpanContext parentSpanContext,
SpanLimits spanLimits,
SpanProcessor spanProcessor,
AnchoredClock clock,
Resource resource,
@Nullable AttributesMap attributes,
@Nullable List<LinkData> links,
int totalRecordedLinks,
long startEpochNanos) {
this.context = context;
this.instrumentationScopeInfo = instrumentationScopeInfo;
this.parentSpanContext = parentSpanContext;
this.links = links;
this.totalRecordedLinks = totalRecordedLinks;
this.name = name;
this.kind = kind;
this.spanProcessor = spanProcessor;
this.resource = resource;
this.hasEnded = EndState.NOT_ENDED;
this.clock = clock;
this.startEpochNanos = startEpochNanos;
this.attributes = attributes;
this.spanLimits = spanLimits;
}
/**
* Creates and starts a span with the given configuration.
*
* @param context supplies the trace_id and span_id for the newly started span.
* @param name the displayed name for the new span.
* @param kind the span kind.
* @param parentSpan the parent span, or {@link Span#getInvalid()} if this span is a root span.
* @param spanLimits limits applied to this span.
* @param spanProcessor handler called when the span starts and ends.
* @param tracerClock the tracer's clock
* @param resource the resource associated with this span.
* @param attributes the attributes set during span creation.
* @param links the links set during span creation, may be truncated. The list MUST be immutable.
* @return a new and started span.
*/
static SdkSpan startSpan(
SpanContext context,
String name,
InstrumentationScopeInfo instrumentationScopeInfo,
SpanKind kind,
Span parentSpan,
Context parentContext,
SpanLimits spanLimits,
SpanProcessor spanProcessor,
Clock tracerClock,
Resource resource,
@Nullable AttributesMap attributes,
@Nullable List<LinkData> links,
int totalRecordedLinks,
long userStartEpochNanos) {
boolean createdAnchoredClock;
AnchoredClock clock;
if (parentSpan instanceof SdkSpan) {
SdkSpan parentRecordEventsSpan = (SdkSpan) parentSpan;
clock = parentRecordEventsSpan.clock;
createdAnchoredClock = false;
} else {
clock = AnchoredClock.create(tracerClock);
createdAnchoredClock = true;
}
long startEpochNanos;
if (userStartEpochNanos != 0) {
startEpochNanos = userStartEpochNanos;
} else if (createdAnchoredClock) {
// If this is a new AnchoredClock, the start time is now, so just use it to avoid
// recomputing current time.
startEpochNanos = clock.startTime();
} else {
// AnchoredClock created in the past, so need to compute now.
startEpochNanos = clock.now();
}
SdkSpan span =
new SdkSpan(
context,
name,
instrumentationScopeInfo,
kind,
parentSpan.getSpanContext(),
spanLimits,
spanProcessor,
clock,
resource,
attributes,
links,
totalRecordedLinks,
startEpochNanos);
// Call onStart here instead of calling in the constructor to make sure the span is completely
// initialized.
if (spanProcessor.isStartRequired()) {
spanProcessor.onStart(parentContext, span);
}
return span;
}
@Override
public SpanData toSpanData() {
// Copy within synchronized context
synchronized (lock) {
return SpanWrapper.create(
this,
getImmutableLinks(),
getImmutableTimedEvents(),
getImmutableAttributes(),
(attributes == null) ? 0 : attributes.getTotalAddedValues(),
totalRecordedEvents,
totalRecordedLinks,
status,
name,
endEpochNanos,
hasEnded == EndState.ENDED);
}
}
@Override
@Nullable
public <T> T getAttribute(AttributeKey<T> key) {
synchronized (lock) {
return attributes == null ? null : attributes.get(key);
}
}
@Override
public Attributes getAttributes() {
synchronized (lock) {
return attributes == null ? Attributes.empty() : attributes.immutableCopy();
}
}
@Override
public boolean hasEnded() {
synchronized (lock) {
return hasEnded == EndState.ENDED;
}
}
@Override
public SpanContext getSpanContext() {
return context;
}
@Override
public SpanContext getParentSpanContext() {
return parentSpanContext;
}
/**
* Returns the name of the {@code Span}.
*
* @return the name of the {@code Span}.
*/
@Override
public String getName() {
synchronized (lock) {
return name;
}
}
@Override
@Deprecated
public io.opentelemetry.sdk.common.InstrumentationLibraryInfo getInstrumentationLibraryInfo() {
return InstrumentationScopeUtil.toInstrumentationLibraryInfo(getInstrumentationScopeInfo());
}
@Override
public InstrumentationScopeInfo getInstrumentationScopeInfo() {
return instrumentationScopeInfo;
}
/**
* Returns the latency of the {@code Span} in nanos. If still active then returns now() - start
* time.
*
* @return the latency of the {@code Span} in nanos.
*/
@Override
public long getLatencyNanos() {
synchronized (lock) {
return (hasEnded == EndState.NOT_ENDED ? clock.now() : endEpochNanos) - startEpochNanos;
}
}
/** Returns the {@link AnchoredClock} used by this {@link Span}. */
AnchoredClock getClock() {
return clock;
}
@Override
public <T> ReadWriteSpan setAttribute(AttributeKey<T> key, T value) {
if (key == null || key.getKey().isEmpty() || value == null) {
return this;
}
synchronized (lock) {
if (!isModifiableByCurrentThread()) {
logger.log(Level.FINE, "Calling setAttribute() on an ended Span.");
return this;
}
if (attributes == null) {
attributes =
AttributesMap.create(
spanLimits.getMaxNumberOfAttributes(), spanLimits.getMaxAttributeValueLength());
}
attributes.put(key, value);
}
return this;
}
@GuardedBy("lock")
private boolean isModifiableByCurrentThread() {
return hasEnded == EndState.NOT_ENDED
|| (hasEnded == EndState.ENDING && Thread.currentThread() == spanEndingThread);
}
@Override
public ReadWriteSpan addEvent(String name) {
if (name == null) {
return this;
}
addTimedEvent(EventData.create(clock.now(), name, Attributes.empty(), 0));
return this;
}
@Override
public ReadWriteSpan addEvent(String name, long timestamp, TimeUnit unit) {
if (name == null || unit == null) {
return this;
}
addTimedEvent(EventData.create(unit.toNanos(timestamp), name, Attributes.empty(), 0));
return this;
}
@Override
public ReadWriteSpan addEvent(String name, Attributes attributes) {
if (name == null) {
return this;
}
if (attributes == null) {
attributes = Attributes.empty();
}
int totalAttributeCount = attributes.size();
addTimedEvent(
EventData.create(
clock.now(),
name,
AttributeUtil.applyAttributesLimit(
attributes,
spanLimits.getMaxNumberOfAttributesPerEvent(),
spanLimits.getMaxAttributeValueLength()),
totalAttributeCount));
return this;
}
@Override
public ReadWriteSpan addEvent(String name, Attributes attributes, long timestamp, TimeUnit unit) {
if (name == null || unit == null) {
return this;
}
if (attributes == null) {
attributes = Attributes.empty();
}
int totalAttributeCount = attributes.size();
addTimedEvent(
EventData.create(
unit.toNanos(timestamp),
name,
AttributeUtil.applyAttributesLimit(
attributes,
spanLimits.getMaxNumberOfAttributesPerEvent(),
spanLimits.getMaxAttributeValueLength()),
totalAttributeCount));
return this;
}
private void addTimedEvent(EventData timedEvent) {
synchronized (lock) {
if (!isModifiableByCurrentThread()) {
logger.log(Level.FINE, "Calling addEvent() on an ended Span.");
return;
}
if (events == null) {
events = new ArrayList<>();
}
if (events.size() < spanLimits.getMaxNumberOfEvents()) {
events.add(timedEvent);
}
totalRecordedEvents++;
}
}
@Override
public ReadWriteSpan setStatus(StatusCode statusCode, @Nullable String description) {
if (statusCode == null) {
return this;
}
synchronized (lock) {
if (!isModifiableByCurrentThread()) {
logger.log(Level.FINE, "Calling setStatus() on an ended Span.");
return this;
}
// If current status is OK, ignore further attempts to change it
if (this.status.getStatusCode() == StatusCode.OK) {
logger.log(Level.FINE, "Calling setStatus() on a Span that is already set to OK.");
return this;
}
// Ignore attempts to set status to UNSET
if (statusCode == StatusCode.UNSET) {
logger.log(Level.FINE, "Ignoring call to setStatus() with status UNSET.");
return this;
}
// Ignore description when status is not ERROR
if (description != null && statusCode != StatusCode.ERROR) {
logger.log(Level.FINE, "Ignoring setStatus() description since status is not ERROR.");
description = null;
}
this.status = StatusData.create(statusCode, description);
}
return this;
}
@Override
public ReadWriteSpan recordException(Throwable exception) {
recordException(exception, Attributes.empty());
return this;
}
@Override
public ReadWriteSpan recordException(Throwable exception, Attributes additionalAttributes) {
if (exception == null) {
return this;
}
if (additionalAttributes == null) {
additionalAttributes = Attributes.empty();
}
AttributesMap attributes =
AttributesMap.create(
spanLimits.getMaxNumberOfAttributes(), spanLimits.getMaxAttributeValueLength());
AttributeUtil.addExceptionAttributes(exception, attributes::put);
additionalAttributes.forEach(attributes::put);
addTimedEvent(
ExceptionEventData.create(
clock.now(), exception, attributes, attributes.getTotalAddedValues()));
return this;
}
@Override
public ReadWriteSpan updateName(String name) {
if (name == null) {
return this;
}
synchronized (lock) {
if (!isModifiableByCurrentThread()) {
logger.log(Level.FINE, "Calling updateName() on an ended Span.");
return this;
}
this.name = name;
}
return this;
}
@Override
public Span addLink(SpanContext spanContext, Attributes attributes) {
if (spanContext == null || !spanContext.isValid()) {
return this;
}
if (attributes == null) {
attributes = Attributes.empty();
}
LinkData link =
LinkData.create(
spanContext,
AttributeUtil.applyAttributesLimit(
attributes,
spanLimits.getMaxNumberOfAttributesPerLink(),
spanLimits.getMaxAttributeValueLength()));
synchronized (lock) {
if (!isModifiableByCurrentThread()) {
logger.log(Level.FINE, "Calling addLink() on an ended Span.");
return this;
}
if (links == null) {
links = new ArrayList<>();
}
if (links.size() < spanLimits.getMaxNumberOfLinks()) {
links.add(link);
}
totalRecordedLinks++;
}
return this;
}
@Override
public void end() {
endInternal(clock.now());
}
@Override
public void end(long timestamp, TimeUnit unit) {
if (unit == null) {
unit = TimeUnit.NANOSECONDS;
}
endInternal(timestamp == 0 ? clock.now() : unit.toNanos(timestamp));
}
private void endInternal(long endEpochNanos) {
synchronized (lock) {
if (hasEnded != EndState.NOT_ENDED) {
logger.log(Level.FINE, "Calling end() on an ended or ending Span.");
return;
}
this.endEpochNanos = endEpochNanos;
spanEndingThread = Thread.currentThread();
hasEnded = EndState.ENDING;
}
if (spanProcessor instanceof ExtendedSpanProcessor) {
ExtendedSpanProcessor extendedSpanProcessor = (ExtendedSpanProcessor) spanProcessor;
if (extendedSpanProcessor.isOnEndingRequired()) {
extendedSpanProcessor.onEnding(this);
}
}
synchronized (lock) {
hasEnded = EndState.ENDED;
}
if (spanProcessor.isEndRequired()) {
spanProcessor.onEnd(this);
}
}
@Override
public boolean isRecording() {
synchronized (lock) {
return hasEnded != EndState.ENDED;
}
}
Resource getResource() {
return resource;
}
@Override
public SpanKind getKind() {
return kind;
}
long getStartEpochNanos() {
return startEpochNanos;
}
@GuardedBy("lock")
private List<EventData> getImmutableTimedEvents() {
if (events == null) {
return Collections.emptyList();
}
// if the span has ended, then the events are unmodifiable
// so we can return them directly and save copying all the data.
if (hasEnded == EndState.ENDED) {
return Collections.unmodifiableList(events);
}
return Collections.unmodifiableList(new ArrayList<>(events));
}
@GuardedBy("lock")
private Attributes getImmutableAttributes() {
if (attributes == null || attributes.isEmpty()) {
return Attributes.empty();
}
// if the span has ended, then the attributes are unmodifiable,
// so we can return them directly and save copying all the data.
if (hasEnded == EndState.ENDED) {
return attributes;
}
// otherwise, make a copy of the data into an immutable container.
return attributes.immutableCopy();
}
@GuardedBy("lock")
private List<LinkData> getImmutableLinks() {
if (links == null || links.isEmpty()) {
return Collections.emptyList();
}
return Collections.unmodifiableList(links);
}
@Override
public String toString() {
String name;
String attributes;
String status;
long totalRecordedEvents;
long endEpochNanos;
long totalRecordedLinks;
synchronized (lock) {
name = this.name;
attributes = String.valueOf(this.attributes);
status = String.valueOf(this.status);
totalRecordedEvents = this.totalRecordedEvents;
endEpochNanos = this.endEpochNanos;
totalRecordedLinks = this.totalRecordedLinks;
}
return "SdkSpan{traceId="
+ context.getTraceId()
+ ", spanId="
+ context.getSpanId()
+ ", parentSpanContext="
+ parentSpanContext
+ ", name="
+ name
+ ", kind="
+ kind
+ ", attributes="
+ attributes
+ ", status="
+ status
+ ", totalRecordedEvents="
+ totalRecordedEvents
+ ", totalRecordedLinks="
+ totalRecordedLinks
+ ", startEpochNanos="
+ startEpochNanos
+ ", endEpochNanos="
+ endEpochNanos
+ "}";
}
}