@@ -49,6 +49,10 @@ import {StatusCode} from './status-code';
49
49
// eslint-disable-next-line no-undef
50
50
import GrpcStatus = FirebaseFirestore . GrpcStatus ;
51
51
import api = google . firestore . v1 ;
52
+ import {
53
+ ATTRIBUTE_KEY_DOC_COUNT ,
54
+ SPAN_NAME_BULK_WRITER_COMMIT ,
55
+ } from './telemetry/trace-util' ;
52
56
53
57
/*!
54
58
* The maximum number of writes that can be in a single batch.
@@ -243,55 +247,63 @@ class BulkCommitBatch extends WriteBatch {
243
247
}
244
248
245
249
async bulkCommit ( options : { requestTag ?: string } = { } ) : Promise < void > {
246
- const tag = options ?. requestTag ?? requestTag ( ) ;
247
-
248
- // Capture the error stack to preserve stack tracing across async calls.
249
- const stack = Error ( ) . stack ! ;
250
-
251
- let response : api . IBatchWriteResponse ;
252
- try {
253
- logger (
254
- 'BulkCommitBatch.bulkCommit' ,
255
- tag ,
256
- `Sending next batch with ${ this . _opCount } writes`
257
- ) ;
258
- const retryCodes = getRetryCodes ( 'batchWrite' ) ;
259
- response = await this . _commit <
260
- api . BatchWriteRequest ,
261
- api . BatchWriteResponse
262
- > ( { retryCodes, methodName : 'batchWrite' , requestTag : tag } ) ;
263
- } catch ( err ) {
264
- // Map the failure to each individual write's result.
265
- const ops = Array . from ( { length : this . pendingOps . length } ) ;
266
- response = {
267
- writeResults : ops . map ( ( ) => {
268
- return { } ;
269
- } ) ,
270
- status : ops . map ( ( ) => err ) ,
271
- } ;
272
- }
273
-
274
- for ( let i = 0 ; i < ( response . writeResults || [ ] ) . length ; ++ i ) {
275
- // Since delete operations currently do not have write times, use a
276
- // sentinel Timestamp value.
277
- // TODO(b/158502664): Use actual delete timestamp.
278
- const DELETE_TIMESTAMP_SENTINEL = Timestamp . fromMillis ( 0 ) ;
279
-
280
- const status = ( response . status || [ ] ) [ i ] ;
281
- if ( status . code === StatusCode . OK ) {
282
- const updateTime = Timestamp . fromProto (
283
- response . writeResults ! [ i ] . updateTime || DELETE_TIMESTAMP_SENTINEL
284
- ) ;
285
- this . pendingOps [ i ] . onSuccess ( new WriteResult ( updateTime ) ) ;
286
- } else {
287
- const error =
288
- new ( require ( 'google-gax/build/src/fallback' ) . GoogleError ) (
289
- status . message || undefined
250
+ return this . _firestore . _traceUtil . startActiveSpan (
251
+ SPAN_NAME_BULK_WRITER_COMMIT ,
252
+ async ( ) => {
253
+ const tag = options ?. requestTag ?? requestTag ( ) ;
254
+
255
+ // Capture the error stack to preserve stack tracing across async calls.
256
+ const stack = Error ( ) . stack ! ;
257
+
258
+ let response : api . IBatchWriteResponse ;
259
+ try {
260
+ logger (
261
+ 'BulkCommitBatch.bulkCommit' ,
262
+ tag ,
263
+ `Sending next batch with ${ this . _opCount } writes`
290
264
) ;
291
- error . code = status . code as number ;
292
- this . pendingOps [ i ] . onError ( wrapError ( error , stack ) ) ;
265
+ const retryCodes = getRetryCodes ( 'batchWrite' ) ;
266
+ response = await this . _commit <
267
+ api . BatchWriteRequest ,
268
+ api . BatchWriteResponse
269
+ > ( { retryCodes, methodName : 'batchWrite' , requestTag : tag } ) ;
270
+ } catch ( err ) {
271
+ // Map the failure to each individual write's result.
272
+ const ops = Array . from ( { length : this . pendingOps . length } ) ;
273
+ response = {
274
+ writeResults : ops . map ( ( ) => {
275
+ return { } ;
276
+ } ) ,
277
+ status : ops . map ( ( ) => err ) ,
278
+ } ;
279
+ }
280
+
281
+ for ( let i = 0 ; i < ( response . writeResults || [ ] ) . length ; ++ i ) {
282
+ // Since delete operations currently do not have write times, use a
283
+ // sentinel Timestamp value.
284
+ // TODO(b/158502664): Use actual delete timestamp.
285
+ const DELETE_TIMESTAMP_SENTINEL = Timestamp . fromMillis ( 0 ) ;
286
+
287
+ const status = ( response . status || [ ] ) [ i ] ;
288
+ if ( status . code === StatusCode . OK ) {
289
+ const updateTime = Timestamp . fromProto (
290
+ response . writeResults ! [ i ] . updateTime || DELETE_TIMESTAMP_SENTINEL
291
+ ) ;
292
+ this . pendingOps [ i ] . onSuccess ( new WriteResult ( updateTime ) ) ;
293
+ } else {
294
+ const error =
295
+ new ( require ( 'google-gax/build/src/fallback' ) . GoogleError ) (
296
+ status . message || undefined
297
+ ) ;
298
+ error . code = status . code as number ;
299
+ this . pendingOps [ i ] . onError ( wrapError ( error , stack ) ) ;
300
+ }
301
+ }
302
+ } ,
303
+ {
304
+ [ ATTRIBUTE_KEY_DOC_COUNT ] : this . _opCount ,
293
305
}
294
- }
306
+ ) ;
295
307
}
296
308
297
309
/**
0 commit comments