25
25
import com .google .cloud .ServiceOptions ;
26
26
import com .google .cloud .datastore .execution .AggregationQueryExecutor ;
27
27
import com .google .cloud .datastore .spi .v1 .DatastoreRpc ;
28
+ import com .google .cloud .datastore .telemetry .TraceUtil .Context ;
28
29
import com .google .common .base .MoreObjects ;
29
30
import com .google .common .base .Preconditions ;
30
31
import com .google .common .collect .AbstractIterator ;
52
53
import java .util .Optional ;
53
54
import java .util .Set ;
54
55
import java .util .concurrent .Callable ;
56
+ import javax .annotation .Nonnull ;
55
57
56
58
final class DatastoreImpl extends BaseService <DatastoreOptions > implements Datastore {
57
59
@@ -103,13 +105,18 @@ static class ReadWriteTransactionCallable<T> implements Callable<T> {
103
105
private final TransactionCallable <T > callable ;
104
106
private volatile TransactionOptions options ;
105
107
private volatile Transaction transaction ;
108
+ @ Nonnull private final Context transactionTraceContext ;
106
109
107
110
ReadWriteTransactionCallable (
108
- Datastore datastore , TransactionCallable <T > callable , TransactionOptions options ) {
111
+ Datastore datastore ,
112
+ TransactionCallable <T > callable ,
113
+ TransactionOptions options ,
114
+ @ Nonnull Context parentTraceContext ) {
109
115
this .datastore = datastore ;
110
116
this .callable = callable ;
111
117
this .options = options ;
112
118
this .transaction = null ;
119
+ this .transactionTraceContext = parentTraceContext ;
113
120
}
114
121
115
122
Datastore getDatastore () {
@@ -132,8 +139,9 @@ void setPrevTransactionId(ByteString transactionId) {
132
139
133
140
@ Override
134
141
public T call () throws DatastoreException {
135
- transaction = datastore .newTransaction (options );
136
- try {
142
+ try (com .google .cloud .datastore .telemetry .TraceUtil .Scope ignored =
143
+ transactionTraceContext .makeCurrent ()) {
144
+ transaction = datastore .newTransaction (options );
137
145
T value = callable .run (transaction );
138
146
transaction .commit ();
139
147
return value ;
@@ -154,36 +162,41 @@ public T call() throws DatastoreException {
154
162
155
163
@ Override
156
164
public <T > T runInTransaction (final TransactionCallable <T > callable ) {
157
- Span span = traceUtil .startSpan (TraceUtil .SPAN_NAME_TRANSACTION );
158
- try (Scope scope = traceUtil .getTracer ().withSpan (span )) {
165
+ com .google .cloud .datastore .telemetry .TraceUtil .Span span =
166
+ otelTraceUtil .startSpan (
167
+ com .google .cloud .datastore .telemetry .TraceUtil .SPAN_NAME_TRANSACTION_RUN );
168
+ try (com .google .cloud .datastore .telemetry .TraceUtil .Scope ignored = span .makeCurrent ()) {
159
169
return RetryHelper .runWithRetries (
160
- new ReadWriteTransactionCallable <T >(this , callable , null ),
170
+ new ReadWriteTransactionCallable <T >(this , callable , null , otelTraceUtil . currentContext () ),
161
171
retrySettings ,
162
172
TRANSACTION_EXCEPTION_HANDLER ,
163
173
getOptions ().getClock ());
164
174
} catch (RetryHelperException e ) {
165
- span .setStatus ( Status . UNKNOWN . withDescription ( e . getMessage ()) );
175
+ span .end ( e );
166
176
throw DatastoreException .translateAndThrow (e );
167
177
} finally {
168
- span .end (TraceUtil . END_SPAN_OPTIONS );
178
+ span .end ();
169
179
}
170
180
}
171
181
172
182
@ Override
173
183
public <T > T runInTransaction (
174
184
final TransactionCallable <T > callable , TransactionOptions transactionOptions ) {
175
- Span span = traceUtil .startSpan (TraceUtil .SPAN_NAME_TRANSACTION );
176
- try (Scope scope = traceUtil .getTracer ().withSpan (span )) {
185
+ com .google .cloud .datastore .telemetry .TraceUtil .Span span =
186
+ otelTraceUtil .startSpan (
187
+ com .google .cloud .datastore .telemetry .TraceUtil .SPAN_NAME_TRANSACTION_RUN );
188
+ try (com .google .cloud .datastore .telemetry .TraceUtil .Scope ignored = span .makeCurrent ()) {
177
189
return RetryHelper .runWithRetries (
178
- new ReadWriteTransactionCallable <T >(this , callable , transactionOptions ),
190
+ new ReadWriteTransactionCallable <T >(
191
+ this , callable , transactionOptions , otelTraceUtil .currentContext ()),
179
192
retrySettings ,
180
193
TRANSACTION_EXCEPTION_HANDLER ,
181
194
getOptions ().getClock ());
182
195
} catch (RetryHelperException e ) {
183
- span .setStatus ( Status . UNKNOWN . withDescription ( e . getMessage ()) );
196
+ span .end ( e );
184
197
throw DatastoreException .translateAndThrow (e );
185
198
} finally {
186
- span .end (TraceUtil . END_SPAN_OPTIONS );
199
+ span .end ();
187
200
}
188
201
}
189
202
@@ -634,10 +647,14 @@ private com.google.datastore.v1.CommitResponse commitMutation(
634
647
635
648
com .google .datastore .v1 .CommitResponse commit (
636
649
final com .google .datastore .v1 .CommitRequest requestPb ) {
637
- com .google .cloud .datastore .telemetry .TraceUtil .Span span =
638
- otelTraceUtil .startSpan (com .google .cloud .datastore .telemetry .TraceUtil .SPAN_NAME_COMMIT );
650
+ final boolean isTransactional =
651
+ requestPb .hasTransaction () || requestPb .hasSingleUseTransaction ();
652
+ final String spanName =
653
+ isTransactional
654
+ ? com .google .cloud .datastore .telemetry .TraceUtil .SPAN_NAME_TRANSACTION_COMMIT
655
+ : com .google .cloud .datastore .telemetry .TraceUtil .SPAN_NAME_COMMIT ;
656
+ com .google .cloud .datastore .telemetry .TraceUtil .Span span = otelTraceUtil .startSpan (spanName );
639
657
span .setAttribute ("isTransactional" , requestPb .hasTransaction ());
640
-
641
658
try (com .google .cloud .datastore .telemetry .TraceUtil .Scope ignored = span .makeCurrent ()) {
642
659
return RetryHelper .runWithRetries (
643
660
() -> datastoreRpc .commit (requestPb ),
0 commit comments