-
Notifications
You must be signed in to change notification settings - Fork 863
/
Copy pathSpan.java
394 lines (366 loc) · 12.4 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
/*
* 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 java.util.List;
import java.util.Map;
/**
* 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
*/
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 recieving 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.
*
* @param key the key for this attribute.
* @param value the value for this attribute.
* @since 0.1.0
*/
void setAttribute(String key, 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}.
*
* @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 setAttributes()}.
* @since 0.1.0
*/
void addEvent(String name, Map<String, AttributeValue> attributes);
/**
* Adds an event to the {@code Span}.
*
* @param event the event to add.
* @since 0.1.0
*/
void addEvent(Event event);
/**
* Adds a {@link Link} to the {@code Span}.
*
* <p>Used (for example) in batching operations, where a single batch handler processes multiple
* requests from different traces.
*
* @param link the link to add.
* @since 0.1.0
*/
void addLink(Link link);
/**
* 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 with the default options.
*
* <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();
/**
* 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 events (e.g, {@link #addEvent(String)}.
*
* @return {@code true} if this {@code Span} records events.
* @since 0.1.0
*/
boolean isRecordingEvents();
/**
* {@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.spanBuilderWithRemoteParent(
* getTraceContextFromMetadata(metadata), rpcName).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.spanBuilderWithExplicitParent("MyChildSpan", 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 {@link Sampler} to use. If not set, the implementation will provide a default.
*
* <p>Observe this is used only as a hint for the underlying implementation, which will decide
* whether to sample or not this {@code Span}.
*
* @param sampler the {@code Sampler} to use when determining sampling for a {@code Span}.
* @return this.
* @since 0.1.0
*/
Builder setSampler(Sampler sampler);
/**
* 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.
*
* @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);
/**
* Adds a {@code List} of {@link Link}s to the newly created {@code Span}.
*
* @param links the {@code List} of {@link Link}s to be added.
* @return this.
* @throws NullPointerException if {@code link} is {@code null}.
* @since 0.1.0
* @see #addLink(Link)
*/
Builder addLinks(List<Link> links);
/**
* Sets the option to record events even if not sampled for the newly created {@code Span}. If
* not called, the implementation will provide a default.
*
* @param recordEvents new value determining if this {@code Span} should have events recorded.
* @return this.
* @since 0.1.0
*/
Builder setRecordEvents(boolean recordEvents);
/**
* 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);
/**
* 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.spanBuilderWithExplicitParent("MyChildSpan", 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();
}
}