This repository has been archived by the owner on Sep 26, 2023. It is now read-only.
/
OpencensusTracer.java
492 lines (442 loc) · 18.2 KB
/
OpencensusTracer.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
/*
* Copyright 2019 Google LLC
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following disclaimer
* in the documentation and/or other materials provided with the
* distribution.
* * Neither the name of Google LLC nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package com.google.api.gax.tracing;
import com.google.api.core.BetaApi;
import com.google.api.core.InternalApi;
import com.google.api.gax.rpc.ApiException;
import com.google.api.gax.rpc.StatusCode;
import com.google.api.gax.rpc.StatusCode.Code;
import com.google.api.gax.tracing.ApiTracerFactory.OperationType;
import com.google.common.base.Preconditions;
import com.google.common.base.Strings;
import io.opencensus.trace.AttributeValue;
import io.opencensus.trace.EndSpanOptions;
import io.opencensus.trace.Span;
import io.opencensus.trace.Status;
import io.opencensus.trace.Status.CanonicalCode;
import io.opencensus.trace.Tracer;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.atomic.AtomicLong;
import javax.annotation.Nonnull;
import org.threeten.bp.Duration;
/**
* Implementation of {@link ApiTracer} that uses OpenCensus.
*
* <p>This implementation wraps an OpenCensus {@link Span} for every tracer and annotates that
* {@link Span} with various events throughout the lifecycle of the logical operation.
*
* <p>Each span will be named {@code ClientName.MethodName} and will have the following attributes:
*
* <dl>
* <dt>{@code attempt count}
* <dd>The Number of attempts sent before the logical operation completed
* <dt>{@code status}
* <dd>The status code of the last attempt
* <dt>{@code total response count}
* <dd>The number of messages received across all of the attempts. This will only be set for
* server streaming and bidi RPCs.
* <dt>{@code total request count}
* <dd>The number of messages sent across all of the attempts. This will only be set for client
* streaming and bidi RPCs.
* <dt>{@code batch count}
* <dd>For batch requests, the number of elements in the request.
* <dt>{@code batch size}
* <dd>For batch requests, the byte size of the request.
* </dl>
*
* <p>The spans will contain the following annotations:
*
* <ul>
* <li>{@code Attempt cancelled} with the following attributes:
* <dl>
* <dt>{@code attempt}
* <dd>Zero based sequential attempt number
* <dt>{@code attempt request count}
* <dd>The number of requests sent in this attempt. This will only be set for client
* streaming and bidi RPCs.
* <dt>{@code attempt response count}
* <dd>The number of responses received in this attempt. This will only be set for server
* streaming and bidi RPCs.
* <dt>{@code connection}
* <dd>The UUID of the connection which the attempt was sent.
* </dl>
* <li>{@code Attempt failed, scheduling next attempt} with the following attributes:
* <dl>
* <dt>{@code attempt}
* <dd>Zero based sequential attempt number
* <dt>{@code status}
* <dd>The status code of the failed attempt
* <dt>{@code delay}
* <dd>The number of milliseconds to wait before trying again
* <dt>{@code attempt request count}
* <dd>The number of requests sent in this attempt. This will only be set for client
* streaming and bidi RPCs.
* <dt>{@code attempt response count}
* <dd>The number of responses received in this attempt. This will only be set for server
* streaming and bidi RPCs.
* <dt>{@code connection}
* <dd>The UUID of the connection which the attempt was sent.
* </dl>
* <li>{@code Attempts exhausted} with the following attributes:
* <dl>
* <dt>{@code attempt}
* <dd>Zero based sequential attempt number
* <dt>{@code status}
* <dd>The status code of the failed attempt
* <dt>{@code attempt request count}
* <dd>The number of requests sent in this attempt. This will only be set for client
* streaming and bidi RPCs.
* <dt>{@code attempt response count}
* <dd>The number of responses received in this attempt. This will only be set for server
* streaming and bidi RPCs.
* <dt>{@code connection}
* <dd>The UUID of the connection which the attempt was sent.
* </dl>
* <li>{@code Attempt failed, error not retryable} with the following attributes:
* <dl>
* <dt>{@code attempt}
* <dd>Zero based sequential attempt number
* <dt>{@code status}
* <dd>The status code of the failed attempt
* <dt>{@code attempt request count}
* <dd>The number of requests sent in this attempt. This will only be set for client
* streaming and bidi RPCs.
* <dt>{@code attempt response count}
* <dd>The number of responses received in this attempt. This will only be set for server
* streaming and bidi RPCs.
* <dt>{@code connection}
* <dd>The UUID of the connection which the attempt was sent.
* </dl>
* <li>{@code Attempt succeeded} with the following attributes:
* <dl>
* <dt>{@code attempt}
* <dd>Zero based sequential attempt number
* <dt>{@code attempt request count}
* <dd>The number of requests sent in this attempt. This will only be set for client
* streaming and bidi RPCs.
* <dt>{@code attempt response count}
* <dd>The number of responses received in this attempt. This will only be set for server
* streaming and bidi RPCs.
* <dt>{@code connection}
* <dd>The UUID of the connection which the attempt was sent.
* </dl>
* </ul>
*
* <p>Long running operations, which are composed of an initial RPC to start the operation and a
* number of polling RPCs will be represented as a tree of spans. The top level span will be named
* after the initial RPC name suffixed with "Operation" and will have the following annotations:
*
* <ul>
* <li>{@code Operation started}
* <li>{@code Operation failed to start} with the following attributes:
* <dl>
* <dt>{@code status}
* <dd>The status code of why the operation failed to start
* </dl>
* <li>{@code Polling was cancelled} with the following attributes:
* <dl>
* <dt>{@code attempt}
* <dd>Zero based sequential poll number.
* <dt>{@code attempt request count}
* </dl>
* <li>{@code Scheduling next poll} with the following attributes:
* <dl>
* <dt>{@code attempt}
* <dd>Zero based sequential poll number
* <dt>{@code status}
* <dd>OK if the poll succeeded, but the operation is still running.
* <dt>{@code delay}
* <dd>The number of milliseconds to wait before polling again
* </dl>
* <li>{@code Polling attempts exhausted} with the following attributes:
* <dl>
* <dt>{@code attempt}
* <dd>Zero based sequential poll number
* <dt>{@code status}
* <dd>OK if the poll succeeded, but the operation is still running.
* </dl>
* <li>{@code Polling failed} with the following attributes:
* <dl>
* <dt>{@code attempt}
* <dd>Zero based sequential poll number
* <dt>{@code status}
* <dd>OK if the poll succeeded, but the operation is still running.
* </dl>
* <li>{@code Polling completed} with the following attributes:
* <dl>
* <dt>{@code attempt}
* <dd>Zero based sequential poll number
* </dl>
* </ul>
*
* <p>The toplevel long running operation span will also contain child spans to describe the retry
* attempts for the initial RPC and each poll as described in the general span section above.
*
* <p>This class is thread compatible. It expects callers to follow grpc's threading model: there is
* only one thread that invokes the operation* and attempt* methods. Please see {@link
* com.google.api.gax.rpc.ApiStreamObserver} for more information.
*/
@BetaApi("Surface for tracing is not yet stable")
public class OpencensusTracer extends BaseApiTracer {
private final Tracer tracer;
private final Span span;
private final OperationType operationType;
private volatile String lastConnectionId;
private volatile long currentAttemptId;
private AtomicLong attemptSentMessages = new AtomicLong(0);
private long attemptReceivedMessages = 0;
private AtomicLong totalSentMessages = new AtomicLong(0);
private long totalReceivedMessages = 0;
OpencensusTracer(
@Nonnull Tracer tracer, @Nonnull Span span, @Nonnull OperationType operationType) {
this.tracer = Preconditions.checkNotNull(tracer, "tracer can't be null");
this.span = Preconditions.checkNotNull(span, "span can't be null");
this.operationType = Preconditions.checkNotNull(operationType, "operationType can't be null");
}
Span getSpan() {
return span;
}
/** {@inheritDoc} */
@Override
public Scope inScope() {
// Suppressing to make MustBeClosedChecker happy. The scope will be closed but indirectly
// (inside new Scope()), and the checker cannot recognize it.
@SuppressWarnings("MustBeClosedChecker")
final io.opencensus.common.Scope scope = tracer.withSpan(span);
return new Scope() {
@Override
public void close() {
scope.close();
}
};
}
/** {@inheritDoc} */
@Override
public void operationSucceeded() {
Map<String, AttributeValue> attributes = baseOperationAttributes();
span.putAttributes(attributes);
span.end();
}
/** {@inheritDoc} */
@Override
public void operationCancelled() {
Map<String, AttributeValue> attributes = baseOperationAttributes();
span.putAttributes(attributes);
span.end(
EndSpanOptions.builder()
.setStatus(Status.CANCELLED.withDescription("Cancelled by caller"))
.build());
}
/** {@inheritDoc} */
@Override
public void operationFailed(Throwable error) {
Map<String, AttributeValue> attributes = baseOperationAttributes();
span.putAttributes(attributes);
span.end(EndSpanOptions.builder().setStatus(convertErrorToStatus(error)).build());
}
/** {@inheritDoc} */
@Override
public void lroStartFailed(Throwable error) {
Map<String, AttributeValue> attributes = new HashMap<>();
populateError(attributes, error);
span.addAnnotation("Operation failed to start", attributes);
}
/** {@inheritDoc} */
@Override
public void lroStartSucceeded() {
span.addAnnotation("Operation started");
}
/** {@inheritDoc} */
@Override
public void connectionSelected(String id) {
lastConnectionId = id;
}
/** {@inheritDoc} */
@Override
public void attemptStarted(int attemptNumber) {
currentAttemptId = attemptNumber;
attemptSentMessages.set(0);
attemptReceivedMessages = 0;
// NOTE: no annotations are added because they don't provide any semantic value.
// This simply is used for state management.
}
/** {@inheritDoc} */
@Override
public void attemptStarted(Object request, int attemptNumber) {
attemptStarted(attemptNumber);
}
/** {@inheritDoc} */
@Override
public void attemptSucceeded() {
Map<String, AttributeValue> attributes = baseAttemptAttributes();
// Same infrastructure is used for both polling and retries, so need to disambiguate it here.
if (operationType == OperationType.LongRunning) {
span.addAnnotation("Polling completed", attributes);
} else {
span.addAnnotation("Attempt succeeded", attributes);
}
}
/** {@inheritDoc} */
@Override
public void attemptCancelled() {
Map<String, AttributeValue> attributes = baseAttemptAttributes();
// Same infrastructure is used for both polling and retries, so need to disambiguate it here.
if (operationType == OperationType.LongRunning) {
span.addAnnotation("Polling was cancelled", attributes);
} else {
span.addAnnotation("Attempt cancelled", attributes);
}
lastConnectionId = null;
}
/** {@inheritDoc} */
@Override
public void attemptFailed(Throwable error, Duration delay) {
Map<String, AttributeValue> attributes = baseAttemptAttributes();
attributes.put("delay ms", AttributeValue.longAttributeValue(delay.toMillis()));
populateError(attributes, error);
// Same infrastructure is used for both polling and retries, so need to disambiguate it here.
if (operationType == OperationType.LongRunning) {
// The poll RPC was successful, but it indicated that the operation is still running.
span.addAnnotation("Scheduling next poll", attributes);
} else {
span.addAnnotation("Attempt failed, scheduling next attempt", attributes);
}
lastConnectionId = null;
}
/** {@inheritDoc} */
@Override
public void attemptFailedRetriesExhausted(Throwable error) {
Map<String, AttributeValue> attributes = baseAttemptAttributes();
populateError(attributes, error);
// Same infrastructure is used for both polling and retries, so need to disambiguate it here.
if (operationType == OperationType.LongRunning) {
span.addAnnotation("Polling attempts exhausted", attributes);
} else {
span.addAnnotation("Attempts exhausted", attributes);
}
lastConnectionId = null;
}
/** {@inheritDoc} */
@Override
public void attemptPermanentFailure(Throwable error) {
Map<String, AttributeValue> attributes = baseAttemptAttributes();
populateError(attributes, error);
// Same infrastructure is used for both polling and retries, so need to disambiguate it here.
if (operationType == OperationType.LongRunning) {
span.addAnnotation("Polling failed", attributes);
} else {
span.addAnnotation("Attempt failed, error not retryable", attributes);
}
lastConnectionId = null;
}
/** {@inheritDoc} */
@Override
public void responseReceived() {
attemptReceivedMessages++;
totalReceivedMessages++;
}
/** {@inheritDoc} */
@Override
public void requestSent() {
attemptSentMessages.incrementAndGet();
totalSentMessages.incrementAndGet();
}
/** {@inheritDoc} */
@Override
public void batchRequestSent(long elementCount, long requestSize) {
span.putAttribute("batch count", AttributeValue.longAttributeValue(elementCount));
span.putAttribute("batch size", AttributeValue.longAttributeValue(requestSize));
}
private Map<String, AttributeValue> baseOperationAttributes() {
HashMap<String, AttributeValue> attributes = new HashMap<>();
attributes.put("attempt count", AttributeValue.longAttributeValue(currentAttemptId + 1));
long localTotalSentMessages = totalSentMessages.get();
if (localTotalSentMessages > 0) {
attributes.put(
"total request count", AttributeValue.longAttributeValue(localTotalSentMessages));
}
if (totalReceivedMessages > 0) {
attributes.put(
"total response count", AttributeValue.longAttributeValue(totalReceivedMessages));
}
return attributes;
}
private Map<String, AttributeValue> baseAttemptAttributes() {
HashMap<String, AttributeValue> attributes = new HashMap<>();
populateAttemptNumber(attributes);
long localAttemptSentMessages = attemptSentMessages.get();
if (localAttemptSentMessages > 0) {
attributes.put(
"attempt request count", AttributeValue.longAttributeValue(localAttemptSentMessages));
}
if (attemptReceivedMessages > 0) {
attributes.put(
"attempt response count", AttributeValue.longAttributeValue(attemptReceivedMessages));
}
String localLastConnectionId = lastConnectionId;
if (localLastConnectionId != null) {
attributes.put("connection", AttributeValue.stringAttributeValue(localLastConnectionId));
}
return attributes;
}
private void populateAttemptNumber(Map<String, AttributeValue> attributes) {
attributes.put("attempt", AttributeValue.longAttributeValue(currentAttemptId));
}
private void populateError(Map<String, AttributeValue> attributes, Throwable error) {
if (error == null) {
attributes.put("status", AttributeValue.stringAttributeValue("OK"));
return;
}
Status status = convertErrorToStatus(error);
attributes.put(
"status", AttributeValue.stringAttributeValue(status.getCanonicalCode().toString()));
if (!Strings.isNullOrEmpty(status.getDescription())) {
attributes.put(
"status message", AttributeValue.stringAttributeValue(status.getDescription()));
}
}
@InternalApi("Visible for testing")
static Status convertErrorToStatus(Throwable error) {
StatusCode.Code gaxCode = Code.UNKNOWN;
if (error instanceof ApiException) {
gaxCode = ((ApiException) error).getStatusCode().getCode();
} else if (error.getCause() instanceof ApiException) {
gaxCode = ((ApiException) error.getCause()).getStatusCode().getCode();
}
Status.CanonicalCode code;
try {
code = Status.CanonicalCode.valueOf(gaxCode.name());
} catch (IllegalArgumentException e) {
code = CanonicalCode.UNKNOWN;
}
return code.toStatus().withDescription(error.getMessage());
}
}