@@ -25,6 +25,7 @@ import {
25
25
CLOUD_RESOURCE_HEADER ,
26
26
addLeaderAwareRoutingHeader ,
27
27
} from '../src/common' ;
28
+ import { startTrace , setSpanError } from './instrument' ;
28
29
29
30
export interface TransactionIdentifier {
30
31
session : string | Session ;
@@ -136,21 +137,30 @@ class BatchTransaction extends Snapshot {
136
137
delete reqOpts . gaxOptions ;
137
138
delete reqOpts . types ;
138
139
139
- const headers : { [ k : string ] : string } = { } ;
140
- if ( this . _getSpanner ( ) . routeToLeaderEnabled ) {
141
- addLeaderAwareRoutingHeader ( headers ) ;
142
- }
140
+ return startTrace ( 'BatchTransaction.createQueryPartitions' , query , span => {
141
+ const headers : { [ k : string ] : string } = { } ;
142
+ if ( this . _getSpanner ( ) . routeToLeaderEnabled ) {
143
+ addLeaderAwareRoutingHeader ( headers ) ;
144
+ }
145
+
146
+ this . createPartitions_ (
147
+ {
148
+ client : 'SpannerClient' ,
149
+ method : 'partitionQuery' ,
150
+ reqOpts,
151
+ gaxOpts : query . gaxOptions ,
152
+ headers : headers ,
153
+ } ,
154
+ ( err , partitions , resp ) => {
155
+ if ( err ) {
156
+ setSpanError ( span , err ) ;
157
+ }
143
158
144
- this . createPartitions_ (
145
- {
146
- client : 'SpannerClient' ,
147
- method : 'partitionQuery' ,
148
- reqOpts,
149
- gaxOpts : query . gaxOptions ,
150
- headers : headers ,
151
- } ,
152
- callback
153
- ) ;
159
+ span . end ( ) ;
160
+ callback ( err , partitions , resp ) ;
161
+ }
162
+ ) ;
163
+ } ) ;
154
164
}
155
165
/**
156
166
* Generic create partition method. Handles common parameters used in both
@@ -163,37 +173,43 @@ class BatchTransaction extends Snapshot {
163
173
* @param {function } callback Callback function.
164
174
*/
165
175
createPartitions_ ( config , callback ) {
166
- const query = extend ( { } , config . reqOpts , {
167
- session : this . session . formattedName_ ,
168
- transaction : { id : this . id } ,
169
- } ) ;
170
- config . reqOpts = extend ( { } , query ) ;
171
- config . headers = {
172
- [ CLOUD_RESOURCE_HEADER ] : ( this . session . parent as Database ) . formattedName_ ,
173
- } ;
174
- delete query . partitionOptions ;
175
- this . session . request ( config , ( err , resp ) => {
176
- if ( err ) {
177
- callback ( err , null , resp ) ;
178
- return ;
179
- }
180
-
181
- const partitions = resp . partitions . map ( partition => {
182
- return extend ( { } , query , partition ) ;
176
+ return startTrace ( 'BatchTransaction.createPartitions' , { } , span => {
177
+ const query = extend ( { } , config . reqOpts , {
178
+ session : this . session . formattedName_ ,
179
+ transaction : { id : this . id } ,
183
180
} ) ;
181
+ config . reqOpts = extend ( { } , query ) ;
182
+ config . headers = {
183
+ [ CLOUD_RESOURCE_HEADER ] : ( this . session . parent as Database )
184
+ . formattedName_ ,
185
+ } ;
186
+ delete query . partitionOptions ;
187
+ this . session . request ( config , ( err , resp ) => {
188
+ if ( err ) {
189
+ setSpanError ( span , err ) ;
190
+ span . end ( ) ;
191
+ callback ( err , null , resp ) ;
192
+ return ;
193
+ }
184
194
185
- if ( resp . transaction ) {
186
- const { id, readTimestamp} = resp . transaction ;
195
+ const partitions = resp . partitions . map ( partition => {
196
+ return extend ( { } , query , partition ) ;
197
+ } ) ;
187
198
188
- this . id = id ;
199
+ if ( resp . transaction ) {
200
+ const { id, readTimestamp} = resp . transaction ;
189
201
190
- if ( readTimestamp ) {
191
- this . readTimestampProto = readTimestamp ;
192
- this . readTimestamp = new PreciseDate ( readTimestamp ) ;
202
+ this . id = id ;
203
+
204
+ if ( readTimestamp ) {
205
+ this . readTimestampProto = readTimestamp ;
206
+ this . readTimestamp = new PreciseDate ( readTimestamp ) ;
207
+ }
193
208
}
194
- }
195
209
196
- callback ( null , partitions , resp ) ;
210
+ span . end ( ) ;
211
+ callback ( null , partitions , resp ) ;
212
+ } ) ;
197
213
} ) ;
198
214
}
199
215
/**
@@ -226,29 +242,38 @@ class BatchTransaction extends Snapshot {
226
242
* @returns {Promise<CreateReadPartitionsResponse> }
227
243
*/
228
244
createReadPartitions ( options , callback ) {
229
- const reqOpts = Object . assign ( { } , options , {
230
- keySet : Snapshot . encodeKeySet ( options ) ,
231
- } ) ;
245
+ return startTrace ( 'BatchTransaction.createReadPartitions' , { } , span => {
246
+ const reqOpts = Object . assign ( { } , options , {
247
+ keySet : Snapshot . encodeKeySet ( options ) ,
248
+ } ) ;
232
249
233
- delete reqOpts . gaxOptions ;
234
- delete reqOpts . keys ;
235
- delete reqOpts . ranges ;
250
+ delete reqOpts . gaxOptions ;
251
+ delete reqOpts . keys ;
252
+ delete reqOpts . ranges ;
236
253
237
- const headers : { [ k : string ] : string } = { } ;
238
- if ( this . _getSpanner ( ) . routeToLeaderEnabled ) {
239
- addLeaderAwareRoutingHeader ( headers ) ;
240
- }
254
+ const headers : { [ k : string ] : string } = { } ;
255
+ if ( this . _getSpanner ( ) . routeToLeaderEnabled ) {
256
+ addLeaderAwareRoutingHeader ( headers ) ;
257
+ }
258
+
259
+ this . createPartitions_ (
260
+ {
261
+ client : 'SpannerClient' ,
262
+ method : 'partitionRead' ,
263
+ reqOpts,
264
+ gaxOpts : options . gaxOptions ,
265
+ headers : headers ,
266
+ } ,
267
+ ( err , partitions , resp ) => {
268
+ if ( err ) {
269
+ setSpanError ( span , err ) ;
270
+ }
241
271
242
- this . createPartitions_ (
243
- {
244
- client : 'SpannerClient' ,
245
- method : 'partitionRead' ,
246
- reqOpts,
247
- gaxOpts : options . gaxOptions ,
248
- headers : headers ,
249
- } ,
250
- callback
251
- ) ;
272
+ span . end ( ) ;
273
+ callback ( err , partitions , resp ) ;
274
+ }
275
+ ) ;
276
+ } ) ;
252
277
}
253
278
/**
254
279
* Executes partition.
@@ -322,6 +347,7 @@ class BatchTransaction extends Snapshot {
322
347
* ```
323
348
*/
324
349
executeStream ( partition ) {
350
+ // TODO: Instrument the streams with Otel.
325
351
if ( is . string ( partition . table ) ) {
326
352
return this . createReadStream ( partition . table , partition ) ;
327
353
}
0 commit comments