-
Notifications
You must be signed in to change notification settings - Fork 775
/
Span.java
428 lines (395 loc) · 14.8 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
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/
package io.opentelemetry.api.trace;
import static java.util.concurrent.TimeUnit.NANOSECONDS;
import static java.util.concurrent.TimeUnit.SECONDS;
import io.opentelemetry.api.common.AttributeKey;
import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.api.internal.ApiUsageLogger;
import io.opentelemetry.context.Context;
import io.opentelemetry.context.ImplicitContextKeyed;
import java.time.Instant;
import java.util.concurrent.TimeUnit;
import javax.annotation.Nullable;
import javax.annotation.concurrent.ThreadSafe;
/**
* An interface that represents a span. It has an associated {@link SpanContext}.
*
* <p>Spans are created by the {@link SpanBuilder#startSpan} method.
*
* <p>{@code Span} <b>must</b> be ended by calling {@link #end()}.
*/
@ThreadSafe
public interface Span extends ImplicitContextKeyed {
/**
* Returns the {@link Span} from the current {@link Context}, falling back to a default, no-op
* {@link Span} if there is no span in the current context.
*/
static Span current() {
Span span = Context.current().get(SpanContextKey.KEY);
return span == null ? getInvalid() : span;
}
/**
* Returns the {@link Span} from the specified {@link Context}, falling back to a default, no-op
* {@link Span} if there is no span in the context.
*/
static Span fromContext(Context context) {
if (context == null) {
ApiUsageLogger.log("context is null");
return Span.getInvalid();
}
Span span = context.get(SpanContextKey.KEY);
return span == null ? getInvalid() : span;
}
/**
* Returns the {@link Span} from the specified {@link Context}, or {@code null} if there is no
* span in the context.
*/
@Nullable
static Span fromContextOrNull(Context context) {
if (context == null) {
ApiUsageLogger.log("context is null");
return null;
}
return context.get(SpanContextKey.KEY);
}
/**
* Returns an invalid {@link Span}. An invalid {@link Span} is used when tracing is disabled,
* usually because there is no OpenTelemetry SDK installed.
*/
static Span getInvalid() {
return PropagatedSpan.INVALID;
}
/**
* Returns a non-recording {@link Span} that holds the provided {@link SpanContext} but has no
* functionality. It will not be exported and all tracing operations are no-op, but it can be used
* to propagate a valid {@link SpanContext} downstream.
*/
static Span wrap(SpanContext spanContext) {
if (spanContext == null) {
ApiUsageLogger.log("context is null");
return getInvalid();
}
if (!spanContext.isValid()) {
return getInvalid();
}
return PropagatedSpan.create(spanContext);
}
/**
* Sets an attribute to the {@code Span}. If the {@code Span} previously contained a mapping for
* the key, the old value is replaced by the specified value.
*
* <p>If a null or empty String {@code value} is passed in, the behavior is undefined, and hence
* strongly discouraged.
*
* <p>Note: It is strongly recommended to use {@link #setAttribute(AttributeKey, Object)}, and
* pre-allocate your keys, if possible.
*
* @param key the key for this attribute.
* @param value the value for this attribute.
* @return this.
*/
default Span setAttribute(String key, String value) {
return setAttribute(AttributeKey.stringKey(key), value);
}
/**
* Sets an attribute to the {@code Span}. If the {@code Span} previously contained a mapping for
* the key, the old value is replaced by the specified value.
*
* <p>Note: It is strongly recommended to use {@link #setAttribute(AttributeKey, Object)}, and
* pre-allocate your keys, if possible.
*
* @param key the key for this attribute.
* @param value the value for this attribute.
* @return this.
*/
default Span setAttribute(String key, long value) {
return setAttribute(AttributeKey.longKey(key), value);
}
/**
* Sets an attribute to the {@code Span}. If the {@code Span} previously contained a mapping for
* the key, the old value is replaced by the specified value.
*
* <p>Note: It is strongly recommended to use {@link #setAttribute(AttributeKey, Object)}, and
* pre-allocate your keys, if possible.
*
* @param key the key for this attribute.
* @param value the value for this attribute.
* @return this.
*/
default Span setAttribute(String key, double value) {
return setAttribute(AttributeKey.doubleKey(key), value);
}
/**
* Sets an attribute to the {@code Span}. If the {@code Span} previously contained a mapping for
* the key, the old value is replaced by the specified value.
*
* <p>Note: It is strongly recommended to use {@link #setAttribute(AttributeKey, Object)}, and
* pre-allocate your keys, if possible.
*
* @param key the key for this attribute.
* @param value the value for this attribute.
* @return this.
*/
default Span setAttribute(String key, boolean value) {
return setAttribute(AttributeKey.booleanKey(key), value);
}
/**
* Sets an attribute to the {@code Span}. If the {@code Span} previously contained a mapping for
* the key, the old value is replaced by the specified value.
*
* <p>Note: the behavior of null values is undefined, and hence strongly discouraged.
*
* @param key the key for this attribute.
* @param value the value for this attribute.
* @return this.
*/
<T> Span setAttribute(AttributeKey<T> key, T value);
/**
* Sets an attribute to the {@code Span}. If the {@code Span} previously contained a mapping for
* the key, the old value is replaced by the specified value.
*
* @param key the key for this attribute.
* @param value the value for this attribute.
* @return this.
*/
default Span setAttribute(AttributeKey<Long> key, int value) {
return setAttribute(key, (long) value);
}
/**
* Sets attributes to the {@link Span}. If the {@link Span} previously contained a mapping for any
* of the keys, the old values are replaced by the specified values.
*
* @param attributes the attributes
* @return this.
* @since 1.2.0
*/
@SuppressWarnings("unchecked")
default Span setAllAttributes(Attributes attributes) {
if (attributes == null || attributes.isEmpty()) {
return this;
}
attributes.forEach(
(attributeKey, value) -> this.setAttribute((AttributeKey<Object>) attributeKey, value));
return this;
}
/**
* Adds an event to the {@link Span}. The timestamp of the event will be the current time.
*
* @param name the name of the event.
* @return this.
*/
default Span addEvent(String name) {
return addEvent(name, Attributes.empty());
}
/**
* Adds an event to the {@link Span} with the given {@code timestamp}, as nanos since epoch. Note,
* this {@code timestamp} is not the same as {@link System#nanoTime()} but may be computed using
* it, for example, by taking a difference of readings from {@link System#nanoTime()} and adding
* to the span start time.
*
* <p>When possible, it is preferred to use {@link #addEvent(String)} at the time the event
* occurred.
*
* @param name the name of the event.
* @param timestamp the explicit event timestamp since epoch.
* @param unit the unit of the timestamp
* @return this.
*/
default Span addEvent(String name, long timestamp, TimeUnit unit) {
return addEvent(name, Attributes.empty(), timestamp, unit);
}
/**
* Adds an event to the {@link Span} with the given {@code timestamp}, as nanos since epoch. Note,
* this {@code timestamp} is not the same as {@link System#nanoTime()} but may be computed using
* it, for example, by taking a difference of readings from {@link System#nanoTime()} and adding
* to the span start time.
*
* <p>When possible, it is preferred to use {@link #addEvent(String)} at the time the event
* occurred.
*
* @param name the name of the event.
* @param timestamp the explicit event timestamp since epoch.
* @return this.
*/
default Span addEvent(String name, Instant timestamp) {
if (timestamp == null) {
return addEvent(name);
}
return addEvent(
name, SECONDS.toNanos(timestamp.getEpochSecond()) + timestamp.getNano(), NANOSECONDS);
}
/**
* Adds an event to the {@link Span} with the given {@link Attributes}. The timestamp of the event
* will be the current time.
*
* @param name the name of the event.
* @param attributes the attributes that will be added; these are associated with this event, not
* the {@code Span} as for {@code setAttribute()}.
* @return this.
*/
Span addEvent(String name, Attributes attributes);
/**
* Adds an event to the {@link Span} with the given {@link Attributes} and {@code timestamp}.
* Note, this {@code timestamp} is not the same as {@link System#nanoTime()} but may be computed
* using it, for example, by taking a difference of readings from {@link System#nanoTime()} and
* adding to the span start time.
*
* <p>When possible, it is preferred to use {@link #addEvent(String)} at the time the event
* occurred.
*
* @param name the name of the event.
* @param attributes the attributes that will be added; these are associated with this event, not
* the {@code Span} as for {@code setAttribute()}.
* @param timestamp the explicit event timestamp since epoch.
* @param unit the unit of the timestamp
* @return this.
*/
Span addEvent(String name, Attributes attributes, long timestamp, TimeUnit unit);
/**
* Adds an event to the {@link Span} with the given {@link Attributes} and {@code timestamp}.
* Note, this {@code timestamp} is not the same as {@link System#nanoTime()} but may be computed
* using it, for example, by taking a difference of readings from {@link System#nanoTime()} and
* adding to the span start time.
*
* <p>When possible, it is preferred to use {@link #addEvent(String)} at the time the event
* occurred.
*
* @param name the name of the event.
* @param attributes the attributes that will be added; these are associated with this event, not
* the {@code Span} as for {@code setAttribute()}.
* @param timestamp the explicit event timestamp since epoch.
* @return this.
*/
default Span addEvent(String name, Attributes attributes, Instant timestamp) {
if (timestamp == null) {
return addEvent(name, attributes);
}
return addEvent(
name,
attributes,
SECONDS.toNanos(timestamp.getEpochSecond()) + timestamp.getNano(),
NANOSECONDS);
}
/**
* Sets the status to the {@code Span}.
*
* <p>If used, this will override the default {@code Span} status. Default status code is {@link
* StatusCode#UNSET}.
*
* <p>Only the value of the last call will be recorded, and implementations are free to ignore
* previous calls.
*
* @param statusCode the {@link StatusCode} to set.
* @return this.
*/
default Span setStatus(StatusCode statusCode) {
return setStatus(statusCode, "");
}
/**
* Sets the status to the {@code Span}.
*
* <p>If used, this will override the default {@code Span} status. Default status code is {@link
* StatusCode#UNSET}.
*
* <p>Only the value of the last call will be recorded, and implementations are free to ignore
* previous calls.
*
* @param statusCode the {@link StatusCode} to set.
* @param description the description of the {@code Status}.
* @return this.
*/
Span setStatus(StatusCode statusCode, String description);
/**
* Records information about the {@link Throwable} to the {@link Span}.
*
* <p>Note that the EXCEPTION_ESCAPED value from the Semantic Conventions cannot be determined by
* this function. You should record this attribute manually using {@link
* #recordException(Throwable, Attributes)} if you know that an exception is escaping.
*
* @param exception the {@link Throwable} to record.
* @return this.
*/
default Span recordException(Throwable exception) {
return recordException(exception, Attributes.empty());
}
/**
* Records information about the {@link Throwable} to the {@link Span}.
*
* @param exception the {@link Throwable} to record.
* @param additionalAttributes the additional {@link Attributes} to record.
* @return this.
*/
Span recordException(Throwable exception, Attributes additionalAttributes);
/**
* Updates the {@code Span} name.
*
* <p>If used, this will override the name provided via {@code Span.Builder}.
*
* <p>Upon this update, any sampling behavior based on {@code Span} name will depend on the
* implementation.
*
* @param name the {@code Span} name.
* @return this.
*/
Span updateName(String name);
/**
* Marks the end of {@code Span} execution.
*
* <p>Only the timing of the first end call for a given {@code Span} will be recorded, and
* implementations are free to ignore all further calls.
*/
void end();
/**
* Marks the end of {@code Span} execution with the specified timestamp.
*
* <p>Only the timing of the first end call for a given {@code Span} will be recorded, and
* implementations are free to ignore all further calls.
*
* <p>Use this method for specifying explicit end options, such as end {@code Timestamp}. When no
* explicit values are required, use {@link #end()}.
*
* @param timestamp the explicit timestamp from the epoch, for this {@code Span}. {@code 0}
* indicates current time should be used.
* @param unit the unit of the timestamp
*/
void end(long timestamp, TimeUnit unit);
/**
* Marks the end of {@code Span} execution with the specified timestamp.
*
* <p>Only the timing of the first end call for a given {@code Span} will be recorded, and
* implementations are free to ignore all further calls.
*
* <p>Use this method for specifying explicit end options, such as end {@code Timestamp}. When no
* explicit values are required, use {@link #end()}.
*
* @param timestamp the explicit timestamp from the epoch, for this {@code Span}. {@code 0}
* indicates current time should be used.
*/
default void end(Instant timestamp) {
if (timestamp == null) {
end();
return;
}
end(SECONDS.toNanos(timestamp.getEpochSecond()) + timestamp.getNano(), NANOSECONDS);
}
/**
* Returns the {@code SpanContext} associated with this {@code Span}.
*
* @return the {@code SpanContext} associated with this {@code Span}.
*/
SpanContext getSpanContext();
/**
* Returns {@code true} if this {@code Span} records tracing events (e.g. {@link
* #addEvent(String)}, {@link #setAttribute(String, long)}).
*
* @return {@code true} if this {@code Span} records tracing events.
*/
boolean isRecording();
@Override
default Context storeInContext(Context context) {
return context.with(SpanContextKey.KEY, this);
}
}