-
Notifications
You must be signed in to change notification settings - Fork 852
/
Span.java
579 lines (539 loc) · 19.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
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
/*
* Copyright 2019, OpenTelemetry Authors
*
* Licensed 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 io.opentelemetry.trace;
import io.opentelemetry.common.AttributeValue;
import java.util.Map;
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 Builder#startSpan} method.
*
* <p>{@code Span} <b>must</b> be ended by calling {@link #end()}.
*
* @since 0.1.0
*/
@ThreadSafe
public interface Span {
/**
* Type of span. Can be used to specify additional relationships between spans in addition to a
* parent/child relationship.
*
* @since 0.1.0
*/
enum Kind {
/**
* Default value. Indicates that the span is used internally.
*
* @since 0.1.0
*/
INTERNAL,
/**
* Indicates that the span covers server-side handling of an RPC or other remote request.
*
* @since 0.1.0
*/
SERVER,
/**
* Indicates that the span covers the client-side wrapper around an RPC or other remote request.
*
* @since 0.1.0
*/
CLIENT,
/**
* Indicates that the span describes producer sending a message to a broker. Unlike client and
* server, there is no direct critical path latency relationship between producer and consumer
* spans.
*
* @since 0.1.0
*/
PRODUCER,
/**
* Indicates that the span describes consumer receiving a message from a broker. Unlike client
* and server, there is no direct critical path latency relationship between producer and
* consumer spans.
*
* @since 0.1.0
*/
CONSUMER
}
/**
* 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 attribute will be silently
* dropped. Note: this behavior could change in the future.
*
* @param key the key for this attribute.
* @param value the value for this attribute.
* @since 0.1.0
*/
void setAttribute(String key, @Nullable String 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.
* @since 0.1.0
*/
void setAttribute(String key, long 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.
* @since 0.1.0
*/
void setAttribute(String key, double 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.
* @since 0.1.0
*/
void setAttribute(String key, boolean 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.
* @since 0.1.0
*/
void setAttribute(String key, AttributeValue value);
/**
* Adds an event to the {@code Span}.
*
* @param name the name of the event.
* @since 0.1.0
*/
void addEvent(String name);
/**
* Adds an event to the {@code Span}.
*
* <p>Use this method to specify an explicit event timestamp. If not called, the implementation
* will use the current timestamp value, which should be the default case.
*
* <p>Important: this is NOT equivalent with System.nanoTime().
*
* @param name the name of the event.
* @param timestamp the explicit event timestamp in nanos since epoch.
* @since 0.1.0
*/
void addEvent(String name, long timestamp);
/**
* Adds an event to the {@code Span}.
*
* @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()}.
* @since 0.1.0
*/
void addEvent(String name, Map<String, AttributeValue> attributes);
/**
* Adds an event to the {@code Span}.
*
* <p>Use this method to specify an explicit event timestamp. If not called, the implementation
* will use the current timestamp value, which should be the default case.
*
* <p>Important: this is NOT equivalent with System.nanoTime().
*
* @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 in nanos since epoch.
* @since 0.1.0
*/
void addEvent(String name, Map<String, AttributeValue> attributes, long timestamp);
/**
* Adds an event to the {@code Span}.
*
* @param event the event to add.
* @since 0.1.0
*/
void addEvent(Event event);
/**
* Adds an event to the {@code Span}.
*
* <p>Use this method to specify an explicit event timestamp. If not alled, the implementation
* will use the current timestamp value, which should be the default case.
*
* <p>Important: this is NOT equivalent with System.nanoTime().
*
* @param event the event to add.
* @param timestamp the explicit event timestamp in nanos since epoch.
* @since 0.1.0
*/
void addEvent(Event event, long timestamp);
/**
* Sets the {@link Status} to the {@code Span}.
*
* <p>If used, this will override the default {@code Span} status. Default is {@link Status#OK}.
*
* <p>Only the value of the last call will be recorded, and implementations are free to ignore
* previous calls.
*
* @param status the {@link Status} to set.
* @since 0.1.0
*/
void setStatus(Status status);
/**
* 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.
* @since 0.1
*/
void 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.
*
* @since 0.1.0
*/
void end();
/**
* Marks the end of {@code Span} execution with the specified {@link EndSpanOptions}.
*
* <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 endOptions the explicit {@link EndSpanOptions} for this {@code Span}.
* @since 0.1.0
*/
void end(EndSpanOptions endOptions);
/**
* Returns the {@code SpanContext} associated with this {@code Span}.
*
* @return the {@code SpanContext} associated with this {@code Span}.
* @since 0.1.0
*/
SpanContext getContext();
/**
* 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.
* @since 0.1.0
*/
boolean isRecording();
/**
* {@link Builder} is used to construct {@link Span} instances which define arbitrary scopes of
* code that are sampled for distributed tracing as a single atomic unit.
*
* <p>This is a simple example where all the work is being done within a single scope and a single
* thread and the Context is automatically propagated:
*
* <pre>{@code
* class MyClass {
* private static final Tracer tracer = OpenTelemetry.getTracer();
* void doWork {
* // Create a Span as a child of the current Span.
* Span span = tracer.spanBuilder("MyChildSpan").startSpan();
* try (Scope ss = tracer.withSpan(span)) {
* tracer.getCurrentSpan().addEvent("my event");
* doSomeWork(); // Here the new span is in the current Context, so it can be used
* // implicitly anywhere down the stack.
* } finally {
* span.end();
* }
* }
* }
* }</pre>
*
* <p>There might be cases where you do not perform all the work inside one static scope and the
* Context is automatically propagated:
*
* <pre>{@code
* class MyRpcServerInterceptorListener implements RpcServerInterceptor.Listener {
* private static final Tracer tracer = OpenTelemetry.getTracer();
* private Span mySpan;
*
* public MyRpcInterceptor() {}
*
* public void onRequest(String rpcName, Metadata metadata) {
* // Create a Span as a child of the remote Span.
* mySpan = tracer.spanBuilder(rpcName)
* .setParent(getTraceContextFromMetadata(metadata)).startSpan();
* }
*
* public void onExecuteHandler(ServerCallHandler serverCallHandler) {
* try (Scope ws = tracer.withSpan(mySpan)) {
* tracer.getCurrentSpan().addEvent("Start rpc execution.");
* serverCallHandler.run(); // Here the new span is in the current Context, so it can be
* // used implicitly anywhere down the stack.
* }
* }
*
* // Called when the RPC is canceled and guaranteed onComplete will not be called.
* public void onCancel() {
* // IMPORTANT: DO NOT forget to ended the Span here as the work is done.
* mySpan.setStatus(Status.CANCELLED);
* mySpan.end();
* }
*
* // Called when the RPC is done and guaranteed onCancel will not be called.
* public void onComplete(RpcStatus rpcStatus) {
* // IMPORTANT: DO NOT forget to ended the Span here as the work is done.
* mySpan.setStatus(rpcStatusToCanonicalTraceStatus(status);
* mySpan.end();
* }
* }
* }</pre>
*
* <p>This is a simple example where all the work is being done within a single scope and the
* Context is manually propagated:
*
* <pre>{@code
* class MyClass {
* private static final Tracer tracer = OpenTelemetry.getTracer();
* void DoWork(Span parent) {
* Span childSpan = tracer.spanBuilder("MyChildSpan")
* .setParent(parent).startSpan();
* childSpan.addEvent("my event");
* try {
* doSomeWork(childSpan); // Manually propagate the new span down the stack.
* } finally {
* // To make sure we end the span even in case of an exception.
* childSpan.end(); // Manually end the span.
* }
* }
* }
* }</pre>
*
* <p>If your Java version is less than Java SE 7, see {@link Builder#startSpan} for usage
* examples.
*
* @since 0.1.0
*/
interface Builder {
/**
* Sets the parent {@code Span} to use. If not set, the value of {@code Tracer.getCurrentSpan()}
* at {@link #startSpan()} time will be used as parent.
*
* <p>This <b>must</b> be used to create a {@code Span} when manual Context propagation is used
* OR when creating a root {@code Span} with a parent with an invalid {@link SpanContext}.
*
* <p>Observe this is the preferred method when the parent is a {@code Span} created within the
* process. Using its {@code SpanContext} as parent remains as a valid, albeit inefficient,
* operation.
*
* <p>If called multiple times, only the last specified value will be used. Observe that the
* state defined by a previous call to {@link #setNoParent()} will be discarded.
*
* @param parent the {@code Span} used as parent.
* @return this.
* @throws NullPointerException if {@code parent} is {@code null}.
* @see #setNoParent()
* @since 0.1.0
*/
Builder setParent(Span parent);
/**
* Sets the parent {@link SpanContext} to use. If not set, the value of {@code
* Tracer.getCurrentSpan()} at {@link #startSpan()} time will be used as parent.
*
* <p>Similar to {@link #setParent(Span parent)} but this <b>must</b> be used to create a {@code
* Span} when the parent is in a different process. This is only intended for use by RPC systems
* or similar.
*
* <p>If no {@link SpanContext} is available, users must call {@link #setNoParent()} in order to
* create a root {@code Span} for a new trace.
*
* <p>If called multiple times, only the last specified value will be used. Observe that the
* state defined by a previous call to {@link #setNoParent()} will be discarded.
*
* @param remoteParent the {@link SpanContext} used as parent.
* @return this.
* @throws NullPointerException if {@code remoteParent} is {@code null}.
* @see #setParent(Span parent)
* @see #setNoParent()
* @since 0.1.0
*/
Builder setParent(SpanContext remoteParent);
/**
* Sets the option to become a root {@code Span} for a new trace. If not set, the value of
* {@code Tracer.getCurrentSpan()} at {@link #startSpan()} time will be used as parent.
*
* <p>Observe that any previously set parent will be discarded.
*
* @return this.
* @since 0.1.0
*/
Builder setNoParent();
/**
* Adds a {@link Link} to the newly created {@code Span}.
*
* @param spanContext the context of the linked {@code Span}.
* @return this.
* @throws NullPointerException if {@code spanContext} is {@code null}.
* @see #addLink(Link)
* @since 0.1.0
*/
Builder addLink(SpanContext spanContext);
/**
* Adds a {@link Link} to the newly created {@code Span}.
*
* @param spanContext the context of the linked {@code Span}.
* @param attributes the attributes of the {@code Link}.
* @return this.
* @throws NullPointerException if {@code spanContext} is {@code null}.
* @throws NullPointerException if {@code attributes} is {@code null}.
* @see #addLink(Link)
* @since 0.1.0
*/
Builder addLink(SpanContext spanContext, Map<String, AttributeValue> attributes);
/**
* Adds a {@link Link} to the newly created {@code Span}.
*
* <p>Links are used to link {@link Span}s in different traces. Used (for example) in batching
* operations, where a single batch handler processes multiple requests from different traces or
* the same trace.
*
* @param link the {@link Link} to be added.
* @return this.
* @throws NullPointerException if {@code link} is {@code null}.
* @since 0.1.0
*/
Builder addLink(Link link);
/**
* Sets an attribute to the newly created {@code Span}. If {@code Span.Builder} 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 attribute will be silently
* dropped. Note: this behavior could change in the future.
*
* @param key the key for this attribute.
* @param value the value for this attribute.
* @return this.
* @throws NullPointerException if {@code key} is {@code null}.
* @since 0.3.0
*/
Builder setAttribute(String key, @Nullable String value);
/**
* Sets an attribute to the newly created {@code Span}. If {@code Span.Builder} 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.
* @throws NullPointerException if {@code key} is {@code null}.
* @since 0.3.0
*/
Builder setAttribute(String key, long value);
/**
* Sets an attribute to the newly created {@code Span}. If {@code Span.Builder} 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.
* @throws NullPointerException if {@code key} is {@code null}.
* @since 0.3.0
*/
Builder setAttribute(String key, double value);
/**
* Sets an attribute to the newly created {@code Span}. If {@code Span.Builder} 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.
* @throws NullPointerException if {@code key} is {@code null}.
* @since 0.3.0
*/
Builder setAttribute(String key, boolean value);
/**
* Sets an attribute to the newly created {@code Span}. If {@code Span.Builder} 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.
* @throws NullPointerException if {@code key} is {@code null}.
* @throws NullPointerException if {@code value} is {@code null}.
* @since 0.3.0
*/
Builder setAttribute(String key, AttributeValue value);
/**
* Sets the {@link Span.Kind} for the newly created {@code Span}. If not called, the
* implementation will provide a default value {@link Span.Kind#INTERNAL}.
*
* @param spanKind the kind of the newly created {@code Span}.
* @return this.
* @since 0.1.0
*/
Builder setSpanKind(Span.Kind spanKind);
/**
* Sets an explicit start timestamp for the newly created {@code Span}.
*
* <p>Use this method to specify an explicit start timestamp. If not called, the implementation
* will use the timestamp value at {@link #startSpan()} time, which should be the default case.
*
* <p>Important this is NOT equivalent with System.nanoTime().
*
* @param startTimestamp the explicit start timestamp of the newly created {@code Span} in nanos
* since epoch.
* @return this.
* @since 0.1.0
*/
Builder setStartTimestamp(long startTimestamp);
/**
* Starts a new {@link Span}.
*
* <p>Users <b>must</b> manually call {@link Span#end()} to end this {@code Span}.
*
* <p>Does not install the newly created {@code Span} to the current Context.
*
* <p>Example of usage:
*
* <pre>{@code
* class MyClass {
* private static final Tracer tracer = OpenTelemetry.getTracer();
* void DoWork(Span parent) {
* Span childSpan = tracer.spanBuilder("MyChildSpan")
* .setParent(parent)
* .startSpan();
* childSpan.addEvent("my event");
* try {
* doSomeWork(childSpan); // Manually propagate the new span down the stack.
* } finally {
* // To make sure we end the span even in case of an exception.
* childSpan.end(); // Manually end the span.
* }
* }
* }
* }</pre>
*
* @return the newly created {@code Span}.
* @since 0.1.0
*/
Span startSpan();
}
}