forked from open-telemetry/opentelemetry-java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Tracer.java
339 lines (327 loc) · 12.6 KB
/
Tracer.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
/*
* 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 com.google.errorprone.annotations.MustBeClosed;
import io.opentelemetry.context.Scope;
import io.opentelemetry.context.propagation.BinaryFormat;
import io.opentelemetry.context.propagation.HttpTextFormat;
import io.opentelemetry.resource.Resource;
import javax.annotation.Nullable;
/**
* Tracer is a simple, interface for {@link Span} creation and in-process context interaction.
*
* <p>Users may choose to use manual or automatic Context propagation. Because of that this class
* offers APIs to facilitate both usages.
*
* <p>The automatic context propagation is done using {@link io.grpc.Context} which is a gRPC
* independent implementation for in-process Context propagation mechanism which can carry
* scoped-values across API boundaries and between threads. Users of the library must propagate the
* {@link io.grpc.Context} between different threads.
*
* <p>Example usage with automatic context propagation:
*
* <pre>{@code
* class MyClass {
* private static final Tracer tracer = Trace.getTracer();
* void doWork() {
* Span span = tracer.spanBuilder("MyClass.DoWork").startSpan();
* try(Scope ss = tracer.withSpan(span)) {
* tracer.getCurrentSpan().addEvent("Starting the work.");
* doWorkInternal();
* tracer.getCurrentSpan().addEvent("Finished working.");
* } finally {
* span.end();
* }
* }
* }
* }</pre>
*
* <p>Example usage with manual context propagation:
*
* <pre>{@code
* class MyClass {
* private static final Tracer tracer = Trace.getTracer();
* void doWork(Span parent) {
* Span childSpan = tracer.spanBuilderWithExplicitParent("MyChildSpan", parent).startSpan();
* childSpan.addEvent("Starting the work.");
* 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>
*
* @since 0.1.0
*/
public interface Tracer {
/**
* Gets the current Span from the current Context.
*
* <p>To install a {@link Span} to the current Context use {@link #withSpan(Span)}.
*
* <p>startSpan methods do NOT modify the current Context {@code Span}.
*
* @return a default {@code Span} that does nothing and has an invalid {@link SpanContext} if no
* {@code Span} is associated with the current Context, otherwise the current {@code Span}
* from the Context.
* @since 0.1.0
*/
Span getCurrentSpan();
/**
* Enters the scope of code where the given {@link Span} is in the current Context, and returns an
* object that represents that scope. The scope is exited when the returned object is closed.
*
* <p>Supports try-with-resource idiom.
*
* <p>Can be called with {@link BlankSpan} to enter a scope of code where tracing is stopped.
*
* <p>Example of usage:
*
* <pre>{@code
* private static Tracer tracer = Trace.getTracer();
* void doWork() {
* // Create a Span as a child of the current Span.
* Span span = tracer.spanBuilder("my span").startSpan();
* try (Scope ws = tracer.withSpan(span)) {
* tracer.getCurrentSpan().addEvent("my event");
* doSomeOtherWork(); // Here "span" is the current Span.
* }
* span.end();
* }
* }</pre>
*
* <p>Prior to Java SE 7, you can use a finally block to ensure that a resource is closed
* regardless of whether the try statement completes normally or abruptly.
*
* <p>Example of usage prior to Java SE7:
*
* <pre>{@code
* private static Tracer tracer = Trace.getTracer();
* void doWork() {
* // Create a Span as a child of the current Span.
* Span span = tracer.spanBuilder("my span").startSpan();
* Scope ws = tracer.withSpan(span);
* try {
* tracer.getCurrentSpan().addEvent("my event");
* doSomeOtherWork(); // Here "span" is the current Span.
* } finally {
* ws.close();
* }
* span.end();
* }
* }</pre>
*
* @param span The {@link Span} to be set to the current Context.
* @return an object that defines a scope where the given {@link Span} will be set to the current
* Context.
* @throws NullPointerException if {@code span} is {@code null}.
* @since 0.1.0
*/
@MustBeClosed
Scope withSpan(Span span);
/**
* Returns a {@link Span.Builder} to create and start a new child {@link Span} as a child of to
* the current {@code Span} if any, otherwise creates a root {@code Span}.
*
* <p>See {@link Span.Builder} for usage examples.
*
* <p>This <b>must</b> be used to create a {@code Span} when automatic Context propagation is
* used.
*
* <p>This is equivalent with:
*
* <pre>{@code
* tracer.spanBuilderWithExplicitParent("MySpanName",tracer.getCurrentSpan());
* }</pre>
*
* @param spanName The name of the returned Span.
* @return a {@code Span.Builder} to create and start a new {@code Span}.
* @throws NullPointerException if {@code spanName} is {@code null}.
* @since 0.1.0
*/
Span.Builder spanBuilder(String spanName);
/**
* Returns a {@link Span.Builder} to create and start a new child {@link Span} (or root if parent
* is {@code null} or has an invalid {@link SpanContext}), with parent being the designated {@code
* Span}.
*
* <p>See {@link Span.Builder} for usage examples.
*
* <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 {@code null} parent.
*
* @param spanName The name of the returned Span.
* @param parent The parent of the returned Span. If {@code null} the {@code Span.Builder} will
* build a root {@code Span}.
* @return a {@code Span.Builder} to create and start a new {@code Span}.
* @throws NullPointerException if {@code spanName} is {@code null}.
* @since 0.1.0
*/
Span.Builder spanBuilderWithExplicitParent(String spanName, @Nullable Span parent);
/**
* Returns a {@link Span.Builder} to create and start a new child {@link Span} (or root if parent
* is {@code null}), with parent being the remote {@link Span} designated by the {@link
* SpanContext}.
*
* <p>See {@link Span.Builder} for usage examples.
*
* <p>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} OR fail to parse the {@link SpanContext} on the server side, users
* must call this method with a {@code null} remote parent {@code SpanContext}.
*
* @param spanName The name of the returned Span.
* @param remoteParentSpanContext The remote parent of the returned Span.
* @return a {@code Span.Builder} to create and start a new {@code Span}.
* @throws NullPointerException if {@code spanName} is {@code null}.
* @since 0.1.0
*/
Span.Builder spanBuilderWithRemoteParent(
String spanName, @Nullable SpanContext remoteParentSpanContext);
/**
* Sets the {@link Resource} to be associated with all {@link Span} and {@link SpanData} objects
* recorded by this {@link Tracer}.
*
* @param resource Resource to be associated with all {@link Span} and {@link SpanData} objects.
*/
void setResource(Resource resource);
/**
* Gets the {@link Resource} that is associating with all the {@link Span} and {@link SpanData}
* objects recorded by this {@link Tracer}.
*
* @return {@link Resource} that is associating with all {@link Span} and {@link SpanData}
* objects.
*/
Resource getResource();
/**
* Records a {@link SpanData}. This API allows to send a pre-populated span object to the
* exporter. Sampling and recording decisions as well as other collection optimizations is a
* responsibility of a caller. Note, the {@link SpanContext} object on the span population with
* the values that will allow correlation of telemetry is also a caller responsibility.
*
* @param span Span Data to be reported to all exporters.
*/
void recordSpanData(SpanData span);
/**
* Returns the {@link BinaryFormat} for this implementation.
*
* <p>If no implementation is provided then no-op implementation will be used.
*
* <p>Usually this will be the W3C Trace Context as the binary format. For more details, see <a
* href="https://github.com/w3c/trace-context">trace-context</a>.
*
* <p>Example of usage on the client:
*
* <pre>{@code
* private static final Tracer tracer = Trace.getTracer();
* private static final BinaryFormat binaryFormat =
* Trace.getTracer().getBinaryFormat();
* void onSendRequest() {
* Span span = tracer.spanBuilder("MyRequest").setSpanKind(Span.Kind.CLIENT).startSpan();
* try (Scope ss = tracer.withSpan(span)) {
* byte[] binaryValue = binaryFormat.toByteArray(tracer.getCurrentContext().context());
* // Send the request including the binaryValue and wait for the response.
* } finally {
* span.end();
* }
* }
* }</pre>
*
* <p>Example of usage on the server:
*
* <pre>{@code
* private static final Tracer tracer = Trace.getTracer();
* private static final BinaryFormat binaryFormat =
* Trace.getTracer().getBinaryFormat();
* void onRequestReceived() {
* // Get the binaryValue from the request.
* SpanContext spanContext = SpanContext.INVALID;
* if (binaryValue != null) {
* spanContext = binaryFormat.fromByteArray(binaryValue);
* }
* Span span = tracer.spanBuilderWithRemoteParent("MyRequest", spanContext)
* .setSpanKind(Span.Kind.SERVER).startSpan();
* try (Scope ss = tracer.withSpan(span)) {
* // Handle request and send response back.
* } finally {
* span.end();
* }
* }
* }</pre>
*
* @return the {@code BinaryFormat} for this implementation.
* @since 0.1.0
*/
BinaryFormat<SpanContext> getBinaryFormat();
/**
* Returns the {@link HttpTextFormat} for this implementation.
*
* <p>If no implementation is provided then no-op implementation will be used.
*
* <p>Usually this will be the W3C Trace Context as the HTTP text format. For more details, see <a
* href="https://github.com/w3c/trace-context">trace-context</a>.
*
* <p>Example of usage on the client:
*
* <pre>{@code
* private static final Tracer tracer = Trace.getTracer();
* private static final HttpTextFormat textFormat = Trace.getTracer().getHttpTextFormat();
* private static final HttpTextFormat.Setter setter =
* new HttpTextFormat.Setter<HttpURLConnection>() {
* public void put(HttpURLConnection carrier, String key, String value) {
* carrier.setRequestProperty(field, value);
* }
* }
*
* void makeHttpRequest() {
* Span span = tracer.spanBuilder("MyRequest").setSpanKind(Span.Kind.CLIENT).startSpan();
* try (Scope s = tracer.withSpan(span)) {
* HttpURLConnection connection =
* (HttpURLConnection) new URL("http://myserver").openConnection();
* textFormat.inject(span.getContext(), connection, httpURLConnectionSetter);
* // Send the request, wait for response and maybe set the status if not ok.
* }
* span.end(); // Can set a status.
* }
* }</pre>
*
* <p>Example of usage on the server:
*
* <pre>{@code
* private static final Tracer tracer = Trace.getTracer();
* private static final HttpTextFormat textFormat = Trace.getTracer().getHttpTextFormat();
* private static final HttpTextFormat.Getter<HttpRequest> getter = ...;
*
* void onRequestReceived(HttpRequest request) {
* SpanContext spanContext = textFormat.extract(request, getter);
* Span span = tracer.spanBuilderWithRemoteParent("MyRequest", spanContext)
* .setSpanKind(Span.Kind.SERVER).startSpan();
* try (Scope s = tracer.withSpan(span)) {
* // Handle request and send response back.
* }
* span.end()
* }
* }</pre>
*
* @return the {@code HttpTextFormat} for this implementation.
* @since 0.1.0
*/
HttpTextFormat<SpanContext> getHttpTextFormat();
}