34
34
import com .google .datastore .v1 .ReserveIdsRequest ;
35
35
import com .google .datastore .v1 .TransactionOptions ;
36
36
import com .google .protobuf .ByteString ;
37
+ import io .opencensus .common .Scope ;
38
+ import io .opencensus .trace .Span ;
39
+ import io .opencensus .trace .Status ;
37
40
import java .util .ArrayList ;
38
41
import java .util .Arrays ;
39
42
import java .util .Collections ;
@@ -53,6 +56,7 @@ final class DatastoreImpl extends BaseService<DatastoreOptions> implements Datas
53
56
TransactionExceptionHandler .build ();
54
57
private static final ExceptionHandler TRANSACTION_OPERATION_EXCEPTION_HANDLER =
55
58
TransactionOperationExceptionHandler .build ();
59
+ private final TraceUtil traceUtil = TraceUtil .getInstance ();;
56
60
57
61
DatastoreImpl (DatastoreOptions options ) {
58
62
super (options );
@@ -132,30 +136,36 @@ public T call() throws DatastoreException {
132
136
133
137
@ Override
134
138
public <T > T runInTransaction (final TransactionCallable <T > callable ) {
135
- final DatastoreImpl self = this ;
136
- try {
139
+ Span span = traceUtil . startSpan ( TraceUtil . SPAN_NAME_TRANSACTION ) ;
140
+ try ( Scope scope = traceUtil . getTracer (). withSpan ( span )) {
137
141
return RetryHelper .runWithRetries (
138
- new ReadWriteTransactionCallable <T >(self , callable , null ),
142
+ new ReadWriteTransactionCallable <T >(this , callable , null ),
139
143
retrySettings ,
140
144
TRANSACTION_EXCEPTION_HANDLER ,
141
145
getOptions ().getClock ());
142
146
} catch (RetryHelperException e ) {
147
+ span .setStatus (Status .UNKNOWN .withDescription (e .getMessage ()));
143
148
throw DatastoreException .translateAndThrow (e );
149
+ } finally {
150
+ span .end (TraceUtil .END_SPAN_OPTIONS );
144
151
}
145
152
}
146
153
147
154
@ Override
148
155
public <T > T runInTransaction (
149
156
final TransactionCallable <T > callable , TransactionOptions transactionOptions ) {
150
- final DatastoreImpl self = this ;
151
- try {
157
+ Span span = traceUtil . startSpan ( TraceUtil . SPAN_NAME_TRANSACTION ) ;
158
+ try ( Scope scope = traceUtil . getTracer (). withSpan ( span )) {
152
159
return RetryHelper .runWithRetries (
153
- new ReadWriteTransactionCallable <T >(self , callable , transactionOptions ),
160
+ new ReadWriteTransactionCallable <T >(this , callable , transactionOptions ),
154
161
retrySettings ,
155
162
TRANSACTION_EXCEPTION_HANDLER ,
156
163
getOptions ().getClock ());
157
164
} catch (RetryHelperException e ) {
165
+ span .setStatus (Status .UNKNOWN .withDescription (e .getMessage ()));
158
166
throw DatastoreException .translateAndThrow (e );
167
+ } finally {
168
+ span .end (TraceUtil .END_SPAN_OPTIONS );
159
169
}
160
170
}
161
171
@@ -175,7 +185,8 @@ <T> QueryResults<T> run(com.google.datastore.v1.ReadOptions readOptionsPb, Query
175
185
176
186
com .google .datastore .v1 .RunQueryResponse runQuery (
177
187
final com .google .datastore .v1 .RunQueryRequest requestPb ) {
178
- try {
188
+ Span span = traceUtil .startSpan (TraceUtil .SPAN_NAME_RUNQUERY );
189
+ try (Scope scope = traceUtil .getTracer ().withSpan (span )) {
179
190
return RetryHelper .runWithRetries (
180
191
new Callable <com .google .datastore .v1 .RunQueryResponse >() {
181
192
@ Override
@@ -189,7 +200,10 @@ public com.google.datastore.v1.RunQueryResponse call() throws DatastoreException
189
200
: TRANSACTION_OPERATION_EXCEPTION_HANDLER ,
190
201
getOptions ().getClock ());
191
202
} catch (RetryHelperException e ) {
203
+ span .setStatus (Status .UNKNOWN .withDescription (e .getMessage ()));
192
204
throw DatastoreException .translateAndThrow (e );
205
+ } finally {
206
+ span .end (TraceUtil .END_SPAN_OPTIONS );
193
207
}
194
208
}
195
209
@@ -229,7 +243,8 @@ public List<Key> allocateId(IncompleteKey... keys) {
229
243
230
244
private com .google .datastore .v1 .AllocateIdsResponse allocateIds (
231
245
final com .google .datastore .v1 .AllocateIdsRequest requestPb ) {
232
- try {
246
+ Span span = traceUtil .startSpan (TraceUtil .SPAN_NAME_ALLOCATEIDS );
247
+ try (Scope scope = traceUtil .getTracer ().withSpan (span )) {
233
248
return RetryHelper .runWithRetries (
234
249
new Callable <com .google .datastore .v1 .AllocateIdsResponse >() {
235
250
@ Override
@@ -241,7 +256,10 @@ public com.google.datastore.v1.AllocateIdsResponse call() throws DatastoreExcept
241
256
EXCEPTION_HANDLER ,
242
257
getOptions ().getClock ());
243
258
} catch (RetryHelperException e ) {
259
+ span .setStatus (Status .UNKNOWN .withDescription (e .getMessage ()));
244
260
throw DatastoreException .translateAndThrow (e );
261
+ } finally {
262
+ span .end (TraceUtil .END_SPAN_OPTIONS );
245
263
}
246
264
}
247
265
@@ -389,7 +407,8 @@ protected Entity computeNext() {
389
407
390
408
com .google .datastore .v1 .LookupResponse lookup (
391
409
final com .google .datastore .v1 .LookupRequest requestPb ) {
392
- try {
410
+ Span span = traceUtil .startSpan (TraceUtil .SPAN_NAME_LOOKUP );
411
+ try (Scope scope = traceUtil .getTracer ().withSpan (span )) {
393
412
return RetryHelper .runWithRetries (
394
413
new Callable <com .google .datastore .v1 .LookupResponse >() {
395
414
@ Override
@@ -403,7 +422,10 @@ public com.google.datastore.v1.LookupResponse call() throws DatastoreException {
403
422
: TRANSACTION_OPERATION_EXCEPTION_HANDLER ,
404
423
getOptions ().getClock ());
405
424
} catch (RetryHelperException e ) {
425
+ span .setStatus (Status .UNKNOWN .withDescription (e .getMessage ()));
406
426
throw DatastoreException .translateAndThrow (e );
427
+ } finally {
428
+ span .end (TraceUtil .END_SPAN_OPTIONS );
407
429
}
408
430
}
409
431
@@ -425,7 +447,8 @@ public List<Key> reserveIds(Key... keys) {
425
447
426
448
com .google .datastore .v1 .ReserveIdsResponse reserveIds (
427
449
final com .google .datastore .v1 .ReserveIdsRequest requestPb ) {
428
- try {
450
+ Span span = traceUtil .startSpan (TraceUtil .SPAN_NAME_RESERVEIDS );
451
+ try (Scope scope = traceUtil .getTracer ().withSpan (span )) {
429
452
return RetryHelper .runWithRetries (
430
453
new Callable <com .google .datastore .v1 .ReserveIdsResponse >() {
431
454
@ Override
@@ -437,7 +460,10 @@ public com.google.datastore.v1.ReserveIdsResponse call() throws DatastoreExcepti
437
460
EXCEPTION_HANDLER ,
438
461
getOptions ().getClock ());
439
462
} catch (RetryHelperException e ) {
463
+ span .setStatus (Status .UNKNOWN .withDescription (e .getMessage ()));
440
464
throw DatastoreException .translateAndThrow (e );
465
+ } finally {
466
+ span .end (TraceUtil .END_SPAN_OPTIONS );
441
467
}
442
468
}
443
469
@@ -529,7 +555,8 @@ private com.google.datastore.v1.CommitResponse commitMutation(
529
555
530
556
com .google .datastore .v1 .CommitResponse commit (
531
557
final com .google .datastore .v1 .CommitRequest requestPb ) {
532
- try {
558
+ Span span = traceUtil .startSpan (TraceUtil .SPAN_NAME_COMMIT );
559
+ try (Scope scope = traceUtil .getTracer ().withSpan (span )) {
533
560
return RetryHelper .runWithRetries (
534
561
new Callable <com .google .datastore .v1 .CommitResponse >() {
535
562
@ Override
@@ -543,7 +570,10 @@ public com.google.datastore.v1.CommitResponse call() throws DatastoreException {
543
570
: TRANSACTION_OPERATION_EXCEPTION_HANDLER ,
544
571
getOptions ().getClock ());
545
572
} catch (RetryHelperException e ) {
573
+ span .setStatus (Status .UNKNOWN .withDescription (e .getMessage ()));
546
574
throw DatastoreException .translateAndThrow (e );
575
+ } finally {
576
+ span .end (TraceUtil .END_SPAN_OPTIONS );
547
577
}
548
578
}
549
579
@@ -554,7 +584,8 @@ ByteString requestTransactionId(
554
584
555
585
com .google .datastore .v1 .BeginTransactionResponse beginTransaction (
556
586
final com .google .datastore .v1 .BeginTransactionRequest requestPb ) {
557
- try {
587
+ Span span = traceUtil .startSpan (TraceUtil .SPAN_NAME_BEGINTRANSACTION );
588
+ try (Scope scope = traceUtil .getTracer ().withSpan (span )) {
558
589
return RetryHelper .runWithRetries (
559
590
new Callable <com .google .datastore .v1 .BeginTransactionResponse >() {
560
591
@ Override
@@ -567,7 +598,10 @@ public com.google.datastore.v1.BeginTransactionResponse call()
567
598
EXCEPTION_HANDLER ,
568
599
getOptions ().getClock ());
569
600
} catch (RetryHelperException e ) {
601
+ span .setStatus (Status .UNKNOWN .withDescription (e .getMessage ()));
570
602
throw DatastoreException .translateAndThrow (e );
603
+ } finally {
604
+ span .end (TraceUtil .END_SPAN_OPTIONS );
571
605
}
572
606
}
573
607
@@ -579,7 +613,8 @@ void rollbackTransaction(ByteString transaction) {
579
613
}
580
614
581
615
void rollback (final com .google .datastore .v1 .RollbackRequest requestPb ) {
582
- try {
616
+ Span span = traceUtil .startSpan (TraceUtil .SPAN_NAME_ROLLBACK );
617
+ try (Scope scope = traceUtil .getTracer ().withSpan (span )) {
583
618
RetryHelper .runWithRetries (
584
619
new Callable <Void >() {
585
620
@ Override
@@ -592,7 +627,10 @@ public Void call() throws DatastoreException {
592
627
EXCEPTION_HANDLER ,
593
628
getOptions ().getClock ());
594
629
} catch (RetryHelperException e ) {
630
+ span .setStatus (Status .UNKNOWN .withDescription (e .getMessage ()));
595
631
throw DatastoreException .translateAndThrow (e );
632
+ } finally {
633
+ span .end (TraceUtil .END_SPAN_OPTIONS );
596
634
}
597
635
}
598
636
}
0 commit comments