@google-cloud/spanner 7.14.0 → 7.15.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -30,8 +30,10 @@ const session_pool_1 = require("./session-pool");
30
30
  const table_1 = require("./table");
31
31
  const transaction_runner_1 = require("./transaction-runner");
32
32
  const common_1 = require("./common");
33
+ const stream_1 = require("stream");
33
34
  const arrify = require("arrify");
34
35
  const snakeCase = require("lodash.snakecase");
36
+ const instrument_1 = require("./instrument");
35
37
  /**
36
38
  * Create a Database object to interact with a Cloud Spanner database.
37
39
  *
@@ -142,15 +144,26 @@ class Database extends common.GrpcServiceObject {
142
144
  typeof poolOptions === 'function'
143
145
  ? new poolOptions(this, null)
144
146
  : new session_pool_1.SessionPool(this, poolOptions);
147
+ const sessionPoolInstance = this.pool_;
148
+ if (sessionPoolInstance) {
149
+ sessionPoolInstance._observabilityOptions =
150
+ instance._observabilityOptions;
151
+ }
145
152
  if (typeof poolOptions === 'object') {
146
153
  this.databaseRole = poolOptions.databaseRole || null;
147
154
  }
148
155
  this.formattedName_ = formattedName_;
149
156
  this.instance = instance;
157
+ this._observabilityOptions = instance._observabilityOptions;
158
+ this._traceConfig = {
159
+ opts: this._observabilityOptions,
160
+ dbName: this.formattedName_,
161
+ };
150
162
  this.resourceHeader_ = {
151
163
  [common_1.CLOUD_RESOURCE_HEADER]: this.formattedName_,
152
164
  };
153
165
  this.request = instance.request;
166
+ this._observabilityOptions = instance._observabilityOptions;
154
167
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
155
168
  this.requestStream = instance.requestStream;
156
169
  this.pool_.on('error', this.emit.bind(this, 'error'));
@@ -203,23 +216,29 @@ class Database extends common.GrpcServiceObject {
203
216
  if (this._getSpanner().routeToLeaderEnabled) {
204
217
  (0, common_1.addLeaderAwareRoutingHeader)(headers);
205
218
  }
206
- this.request({
207
- client: 'SpannerClient',
208
- method: 'batchCreateSessions',
209
- reqOpts,
210
- gaxOpts: options.gaxOptions,
211
- headers: headers,
212
- }, (err, resp) => {
213
- if (err) {
214
- callback(err, null, resp);
215
- return;
216
- }
217
- const sessions = (resp.session || []).map(metadata => {
218
- const session = this.session(metadata.name);
219
- session.metadata = metadata;
220
- return session;
219
+ (0, instrument_1.startTrace)('Database.batchCreateSessions', this._traceConfig, span => {
220
+ this.request({
221
+ client: 'SpannerClient',
222
+ method: 'batchCreateSessions',
223
+ reqOpts,
224
+ gaxOpts: options.gaxOptions,
225
+ headers: headers,
226
+ }, (err, resp) => {
227
+ if (err) {
228
+ (0, instrument_1.setSpanError)(span, err);
229
+ span.end();
230
+ callback(err, null, resp);
231
+ return;
232
+ }
233
+ const sessions = (resp.session || []).map(metadata => {
234
+ const session = this.session(metadata.name);
235
+ session._observabilityOptions = this._traceConfig.opts;
236
+ session.metadata = metadata;
237
+ return session;
238
+ });
239
+ span.end();
240
+ callback(null, sessions, resp);
221
241
  });
222
- callback(null, sessions, resp);
223
242
  });
224
243
  }
225
244
  /**
@@ -253,6 +272,7 @@ class Database extends common.GrpcServiceObject {
253
272
  const id = identifier.transaction;
254
273
  const transaction = new batch_transaction_1.BatchTransaction(session, options);
255
274
  transaction.id = id;
275
+ transaction._observabilityOptions = this._traceConfig.opts;
256
276
  transaction.readTimestamp = identifier.timestamp;
257
277
  return transaction;
258
278
  }
@@ -269,19 +289,32 @@ class Database extends common.GrpcServiceObject {
269
289
  const options = typeof optionsOrCallback === 'object'
270
290
  ? optionsOrCallback
271
291
  : {};
272
- this.pool_.getSession((err, session) => {
273
- if (err) {
274
- callback(err, null, undefined);
275
- return;
276
- }
277
- const transaction = this.batchTransaction({ session: session }, options);
278
- this._releaseOnEnd(session, transaction);
279
- transaction.begin((err, resp) => {
292
+ return (0, instrument_1.startTrace)('Database.createBatchTransaction', this._traceConfig, span => {
293
+ this.pool_.getSession((err, session) => {
280
294
  if (err) {
281
- callback(err, null, resp);
295
+ (0, instrument_1.setSpanError)(span, err);
296
+ span.end();
297
+ callback(err, null, undefined);
282
298
  return;
283
299
  }
284
- callback(null, transaction, resp);
300
+ const transaction = this.batchTransaction({ session: session }, options);
301
+ this._releaseOnEnd(session, transaction, span);
302
+ transaction.begin((err, resp) => {
303
+ if (err) {
304
+ (0, instrument_1.setSpanError)(span, err);
305
+ if ((0, session_pool_1.isSessionNotFoundError)(err)) {
306
+ span.addEvent('No session available', {
307
+ 'session.id': session === null || session === void 0 ? void 0 : session.id,
308
+ });
309
+ }
310
+ span.end();
311
+ callback(err, null, resp);
312
+ return;
313
+ }
314
+ span.addEvent('Using Session', { 'session.id': session === null || session === void 0 ? void 0 : session.id });
315
+ span.end();
316
+ callback(null, transaction, resp);
317
+ });
285
318
  });
286
319
  });
287
320
  }
@@ -329,6 +362,7 @@ class Database extends common.GrpcServiceObject {
329
362
  }
330
363
  const tableName = schema.match(/CREATE TABLE `*([^\s`(]+)/)[1];
331
364
  const table = this.table(tableName);
365
+ table._observabilityOptions = this._traceConfig.opts;
332
366
  callback(null, table, operation, resp);
333
367
  });
334
368
  }
@@ -342,14 +376,18 @@ class Database extends common.GrpcServiceObject {
342
376
  * @param {Transaction} transaction The transaction to observe.
343
377
  * @returns {Transaction}
344
378
  */
345
- _releaseOnEnd(session, transaction) {
379
+ _releaseOnEnd(session, transaction, span) {
346
380
  transaction.once('end', () => {
347
381
  try {
348
382
  this.pool_.release(session);
349
383
  }
350
384
  catch (e) {
385
+ (0, instrument_1.setSpanErrorAndException)(span, e);
351
386
  this.emit('error', e);
352
387
  }
388
+ finally {
389
+ span.end();
390
+ }
353
391
  });
354
392
  }
355
393
  delete(optionsOrCallback, cb) {
@@ -526,25 +564,32 @@ class Database extends common.GrpcServiceObject {
526
564
  delete gaxOpts.pageSize;
527
565
  delete gaxOpts.pageToken;
528
566
  }
529
- this.request({
530
- client: 'SpannerClient',
531
- method: 'listSessions',
532
- reqOpts,
533
- gaxOpts,
534
- headers: this.resourceHeader_,
535
- }, (err, sessions, nextPageRequest, ...args) => {
536
- let sessionInstances = null;
537
- if (sessions) {
538
- sessionInstances = sessions.map(metadata => {
539
- const session = self.session(metadata.name);
540
- session.metadata = metadata;
541
- return session;
542
- });
543
- }
544
- const nextQuery = nextPageRequest
545
- ? extend({}, options, nextPageRequest)
546
- : null;
547
- callback(err, sessionInstances, nextQuery, ...args);
567
+ return (0, instrument_1.startTrace)('Database.getSessions', this._traceConfig, span => {
568
+ this.request({
569
+ client: 'SpannerClient',
570
+ method: 'listSessions',
571
+ reqOpts,
572
+ gaxOpts,
573
+ headers: this.resourceHeader_,
574
+ }, (err, sessions, nextPageRequest, ...args) => {
575
+ if (err) {
576
+ (0, instrument_1.setSpanError)(span, err);
577
+ }
578
+ let sessionInstances = null;
579
+ if (sessions) {
580
+ sessionInstances = sessions.map(metadata => {
581
+ const session = self.session(metadata.name);
582
+ session.metadata = metadata;
583
+ session._observabilityOptions = this._traceConfig.opts;
584
+ return session;
585
+ });
586
+ }
587
+ span.end();
588
+ const nextQuery = nextPageRequest
589
+ ? extend({}, options, nextPageRequest)
590
+ : null;
591
+ callback(err, sessionInstances, nextQuery, ...args);
592
+ });
548
593
  });
549
594
  }
550
595
  /**
@@ -618,27 +663,39 @@ class Database extends common.GrpcServiceObject {
618
663
  const options = typeof optionsOrCallback === 'object'
619
664
  ? optionsOrCallback
620
665
  : {};
621
- this.pool_.getSession((err, session) => {
622
- if (err) {
623
- callback(err);
624
- return;
625
- }
626
- const snapshot = session.snapshot(options, this.queryOptions_);
627
- snapshot.begin(err => {
666
+ return (0, instrument_1.startTrace)('Database.getSnapshot', this._traceConfig, span => {
667
+ this.pool_.getSession((err, session) => {
628
668
  if (err) {
629
- if ((0, session_pool_1.isSessionNotFoundError)(err)) {
630
- session.lastError = err;
631
- this.pool_.release(session);
632
- this.getSnapshot(options, callback);
633
- }
634
- else {
635
- this.pool_.release(session);
636
- callback(err);
637
- }
669
+ (0, instrument_1.setSpanError)(span, err);
670
+ span.end();
671
+ callback(err);
638
672
  return;
639
673
  }
640
- this._releaseOnEnd(session, snapshot);
641
- callback(err, snapshot);
674
+ const snapshot = session.snapshot(options, this.queryOptions_);
675
+ snapshot.begin(err => {
676
+ if (err) {
677
+ (0, instrument_1.setSpanError)(span, err);
678
+ if ((0, session_pool_1.isSessionNotFoundError)(err)) {
679
+ span.addEvent('No session available', {
680
+ 'session.id': session === null || session === void 0 ? void 0 : session.id,
681
+ });
682
+ session.lastError = err;
683
+ this.pool_.release(session);
684
+ span.end();
685
+ this.getSnapshot(options, callback);
686
+ }
687
+ else {
688
+ span.addEvent('Using Session', { 'session.id': session === null || session === void 0 ? void 0 : session.id });
689
+ this.pool_.release(session);
690
+ span.end();
691
+ callback(err);
692
+ }
693
+ return;
694
+ }
695
+ this._releaseOnEnd(session, snapshot, span);
696
+ span.end();
697
+ callback(err, snapshot);
698
+ });
642
699
  });
643
700
  });
644
701
  }
@@ -649,20 +706,34 @@ class Database extends common.GrpcServiceObject {
649
706
  const options = typeof optionsOrCallback === 'object' && optionsOrCallback
650
707
  ? optionsOrCallback
651
708
  : {};
652
- this.pool_.getSession((err, session, transaction) => {
653
- if (options.requestOptions) {
654
- transaction.requestOptions = Object.assign(transaction.requestOptions || {}, options.requestOptions);
655
- }
656
- if (options.optimisticLock) {
657
- transaction.useOptimisticLock();
658
- }
659
- if (options.excludeTxnFromChangeStreams) {
660
- transaction.excludeTxnFromChangeStreams();
661
- }
662
- if (!err) {
663
- this._releaseOnEnd(session, transaction);
664
- }
665
- cb(err, transaction);
709
+ return (0, instrument_1.startTrace)('Database.getTransaction', this._traceConfig, span => {
710
+ this.pool_.getSession((err, session, transaction) => {
711
+ if (options.requestOptions) {
712
+ transaction.requestOptions = Object.assign(transaction.requestOptions || {}, options.requestOptions);
713
+ }
714
+ if (options.optimisticLock) {
715
+ transaction.useOptimisticLock();
716
+ }
717
+ if (options.excludeTxnFromChangeStreams) {
718
+ transaction.excludeTxnFromChangeStreams();
719
+ }
720
+ if (!err) {
721
+ span.addEvent('Using Session', { 'session.id': session === null || session === void 0 ? void 0 : session.id });
722
+ transaction._observabilityOptions = this._observabilityOptions;
723
+ this._releaseOnEnd(session, transaction, span);
724
+ }
725
+ else if ((0, session_pool_1.isSessionNotFoundError)(err)) {
726
+ span.addEvent('No session available', {
727
+ 'session.id': session === null || session === void 0 ? void 0 : session.id,
728
+ });
729
+ (0, instrument_1.setSpanError)(span, err);
730
+ }
731
+ else {
732
+ (0, instrument_1.setSpanError)(span, err);
733
+ }
734
+ span.end();
735
+ cb(err, transaction);
736
+ });
666
737
  });
667
738
  }
668
739
  async getOperations(optionsOrCallback) {
@@ -716,6 +787,8 @@ class Database extends common.GrpcServiceObject {
716
787
  callback(err, null);
717
788
  return;
718
789
  }
790
+ const span = (0, instrument_1.getActiveOrNoopSpan)();
791
+ span.addEvent('Using Session', { 'session.id': session === null || session === void 0 ? void 0 : session.id });
719
792
  config.reqOpts.session = session.formattedName_;
720
793
  this.request(config, (err, ...args) => {
721
794
  pool.release(session);
@@ -756,10 +829,17 @@ class Database extends common.GrpcServiceObject {
756
829
  }
757
830
  waitForSessionStream.on('reading', () => {
758
831
  pool.getSession((err, session_) => {
832
+ const span = (0, instrument_1.getActiveOrNoopSpan)();
759
833
  if (err) {
834
+ if ((0, session_pool_1.isSessionNotFoundError)(err)) {
835
+ span.addEvent('No session available', {
836
+ 'session.id': session === null || session === void 0 ? void 0 : session.id,
837
+ });
838
+ }
760
839
  destroyStream(err);
761
840
  return;
762
841
  }
842
+ span.addEvent('Using Session', { 'session.id': session_ === null || session_ === void 0 ? void 0 : session_.id });
763
843
  session = session_;
764
844
  config.reqOpts.session = session.formattedName_;
765
845
  requestStream = self.requestStream(config);
@@ -813,28 +893,53 @@ class Database extends common.GrpcServiceObject {
813
893
  const options = typeof optionsOrCallback === 'object'
814
894
  ? optionsOrCallback
815
895
  : {};
816
- this.runStream(query, options)
817
- .on('error', callback)
818
- .on('response', response => {
819
- if (response.metadata) {
820
- metadata = response.metadata;
821
- }
822
- })
823
- .on('stats', _stats => (stats = _stats))
824
- .on('data', row => {
825
- rows.push(row);
826
- })
827
- .on('end', () => {
828
- callback(null, rows, stats, metadata);
896
+ const traceConfig = {
897
+ sql: query,
898
+ ...this._traceConfig,
899
+ };
900
+ return (0, instrument_1.startTrace)('Database.run', traceConfig, span => {
901
+ this.runStream(query, options)
902
+ .on('error', err => {
903
+ (0, instrument_1.setSpanError)(span, err);
904
+ span.end();
905
+ callback(err, rows, stats, metadata);
906
+ })
907
+ .on('response', response => {
908
+ if (response.metadata) {
909
+ metadata = response.metadata;
910
+ }
911
+ })
912
+ .on('stats', _stats => (stats = _stats))
913
+ .on('data', row => {
914
+ rows.push(row);
915
+ })
916
+ .on('end', () => {
917
+ span.end();
918
+ callback(null, rows, stats, metadata);
919
+ });
829
920
  });
830
921
  }
831
922
  runPartitionedUpdate(query, callback) {
832
- this.pool_.getSession((err, session) => {
833
- if (err) {
834
- callback(err, 0);
835
- return;
836
- }
837
- this._runPartitionedUpdate(session, query, callback);
923
+ const traceConfig = {
924
+ sql: query,
925
+ ...this._traceConfig,
926
+ };
927
+ return (0, instrument_1.startTrace)('Database.runPartitionedUpdate', traceConfig, span => {
928
+ this.pool_.getSession((err, session) => {
929
+ if (err) {
930
+ (0, instrument_1.setSpanError)(span, err);
931
+ span.end();
932
+ callback(err, 0);
933
+ return;
934
+ }
935
+ this._runPartitionedUpdate(session, query, (err, count) => {
936
+ if (err) {
937
+ (0, instrument_1.setSpanError)(span, err);
938
+ }
939
+ span.end();
940
+ callback(err, count);
941
+ });
942
+ });
838
943
  });
839
944
  }
840
945
  _runPartitionedUpdate(session, query, callback) {
@@ -992,47 +1097,69 @@ class Database extends common.GrpcServiceObject {
992
1097
  */
993
1098
  runStream(query, options) {
994
1099
  const proxyStream = through.obj();
995
- this.pool_.getSession((err, session) => {
996
- if (err) {
997
- proxyStream.destroy(err);
998
- return;
999
- }
1000
- const snapshot = session.snapshot(options, this.queryOptions_);
1001
- this._releaseOnEnd(session, snapshot);
1002
- let dataReceived = false;
1003
- let dataStream = snapshot.runStream(query);
1004
- const endListener = () => snapshot.end();
1005
- dataStream
1006
- .once('data', () => (dataReceived = true))
1007
- .once('error', err => {
1008
- if (!dataReceived &&
1009
- (0, session_pool_1.isSessionNotFoundError)(err)) {
1010
- // If it is a 'Session not found' error and we have not yet received
1011
- // any data, we can safely retry the query on a new session.
1012
- // Register the error on the session so the pool can discard it.
1013
- if (session) {
1014
- session.lastError = err;
1015
- }
1016
- // Remove the current data stream from the end user stream.
1017
- dataStream.unpipe(proxyStream);
1018
- dataStream.removeListener('end', endListener);
1019
- dataStream.end();
1020
- snapshot.end();
1021
- // Create a new data stream and add it to the end user stream.
1022
- dataStream = this.runStream(query, options);
1023
- dataStream.pipe(proxyStream);
1024
- }
1025
- else {
1100
+ const traceConfig = {
1101
+ sql: query,
1102
+ ...this._traceConfig,
1103
+ };
1104
+ return (0, instrument_1.startTrace)('Database.runStream', traceConfig, span => {
1105
+ this.pool_.getSession((err, session) => {
1106
+ if (err) {
1107
+ (0, instrument_1.setSpanError)(span, err);
1026
1108
  proxyStream.destroy(err);
1109
+ span.end();
1110
+ return;
1111
+ }
1112
+ span.addEvent('Using Session', { 'session.id': session === null || session === void 0 ? void 0 : session.id });
1113
+ const snapshot = session.snapshot(options, this.queryOptions_);
1114
+ this._releaseOnEnd(session, snapshot, span);
1115
+ let dataReceived = false;
1116
+ let dataStream = snapshot.runStream(query);
1117
+ const endListener = () => {
1027
1118
  snapshot.end();
1119
+ };
1120
+ dataStream
1121
+ .once('data', () => (dataReceived = true))
1122
+ .once('error', err => {
1123
+ (0, instrument_1.setSpanError)(span, err);
1124
+ if (!dataReceived &&
1125
+ (0, session_pool_1.isSessionNotFoundError)(err)) {
1126
+ // If it is a 'Session not found' error and we have not yet received
1127
+ // any data, we can safely retry the query on a new session.
1128
+ // Register the error on the session so the pool can discard it.
1129
+ if (session) {
1130
+ session.lastError = err;
1131
+ }
1132
+ span.addEvent('No session available', {
1133
+ 'session.id': session === null || session === void 0 ? void 0 : session.id,
1134
+ });
1135
+ // Remove the current data stream from the end user stream.
1136
+ dataStream.unpipe(proxyStream);
1137
+ dataStream.removeListener('end', endListener);
1138
+ dataStream.end();
1139
+ snapshot.end();
1140
+ span.end();
1141
+ // Create a new data stream and add it to the end user stream.
1142
+ dataStream = this.runStream(query, options);
1143
+ dataStream.pipe(proxyStream);
1144
+ }
1145
+ else {
1146
+ proxyStream.destroy(err);
1147
+ snapshot.end();
1148
+ }
1149
+ })
1150
+ .on('stats', stats => proxyStream.emit('stats', stats))
1151
+ .on('response', response => proxyStream.emit('response', response))
1152
+ .once('end', endListener)
1153
+ .pipe(proxyStream);
1154
+ });
1155
+ (0, stream_1.finished)(proxyStream, err => {
1156
+ if (err) {
1157
+ (0, instrument_1.setSpanError)(span, err);
1028
1158
  }
1029
- })
1030
- .on('stats', stats => proxyStream.emit('stats', stats))
1031
- .on('response', response => proxyStream.emit('response', response))
1032
- .once('end', endListener)
1033
- .pipe(proxyStream);
1159
+ span.end();
1160
+ });
1161
+ return proxyStream;
1034
1162
  });
1035
- return proxyStream;
1036
1163
  }
1037
1164
  runTransaction(optionsOrRunFn, fn) {
1038
1165
  const runFn = typeof optionsOrRunFn === 'function'
@@ -1041,32 +1168,50 @@ class Database extends common.GrpcServiceObject {
1041
1168
  const options = typeof optionsOrRunFn === 'object' && optionsOrRunFn
1042
1169
  ? optionsOrRunFn
1043
1170
  : {};
1044
- this.pool_.getSession((err, session, transaction) => {
1045
- if (err && (0, session_pool_1.isSessionNotFoundError)(err)) {
1046
- this.runTransaction(options, runFn);
1047
- return;
1048
- }
1049
- if (err) {
1050
- runFn(err);
1051
- return;
1052
- }
1053
- if (options.optimisticLock) {
1054
- transaction.useOptimisticLock();
1055
- }
1056
- if (options.excludeTxnFromChangeStreams) {
1057
- transaction.excludeTxnFromChangeStreams();
1058
- }
1059
- const release = this.pool_.release.bind(this.pool_, session);
1060
- const runner = new transaction_runner_1.TransactionRunner(session, transaction, runFn, options);
1061
- runner.run().then(release, err => {
1062
- if ((0, session_pool_1.isSessionNotFoundError)(err)) {
1063
- release();
1171
+ (0, instrument_1.startTrace)('Database.runTransaction', this._traceConfig, span => {
1172
+ this.pool_.getSession((err, session, transaction) => {
1173
+ if (err) {
1174
+ (0, instrument_1.setSpanError)(span, err);
1175
+ }
1176
+ if (err && (0, session_pool_1.isSessionNotFoundError)(err)) {
1177
+ span.addEvent('No session available', {
1178
+ 'session.id': session === null || session === void 0 ? void 0 : session.id,
1179
+ });
1180
+ span.end();
1064
1181
  this.runTransaction(options, runFn);
1182
+ return;
1065
1183
  }
1066
- else {
1067
- setImmediate(runFn, err);
1068
- release();
1184
+ if (err) {
1185
+ span.end();
1186
+ runFn(err);
1187
+ return;
1188
+ }
1189
+ transaction._observabilityOptions = this._observabilityOptions;
1190
+ if (options.optimisticLock) {
1191
+ transaction.useOptimisticLock();
1069
1192
  }
1193
+ if (options.excludeTxnFromChangeStreams) {
1194
+ transaction.excludeTxnFromChangeStreams();
1195
+ }
1196
+ const release = () => {
1197
+ this.pool_.release(session);
1198
+ span.end();
1199
+ };
1200
+ const runner = new transaction_runner_1.TransactionRunner(session, transaction, runFn, options);
1201
+ runner.run().then(release, err => {
1202
+ (0, instrument_1.setSpanError)(span, err);
1203
+ if ((0, session_pool_1.isSessionNotFoundError)(err)) {
1204
+ span.addEvent('No session available', {
1205
+ 'session.id': session === null || session === void 0 ? void 0 : session.id,
1206
+ });
1207
+ release();
1208
+ this.runTransaction(options, runFn);
1209
+ }
1210
+ else {
1211
+ setImmediate(runFn, err);
1212
+ release();
1213
+ }
1214
+ });
1070
1215
  });
1071
1216
  });
1072
1217
  }
@@ -1136,34 +1281,48 @@ class Database extends common.GrpcServiceObject {
1136
1281
  const options = typeof optionsOrRunFn === 'object'
1137
1282
  ? optionsOrRunFn
1138
1283
  : {};
1284
+ let sessionId = '';
1139
1285
  const getSession = this.pool_.getSession.bind(this.pool_);
1140
- // Loop to retry 'Session not found' errors.
1141
- // (and yes, we like while (true) more than for (;;) here)
1142
- // eslint-disable-next-line no-constant-condition
1143
- while (true) {
1144
- try {
1145
- const [session, transaction] = await (0, promisify_1.promisify)(getSession)();
1146
- transaction.requestOptions = Object.assign(transaction.requestOptions || {}, options.requestOptions);
1147
- if (options.optimisticLock) {
1148
- transaction.useOptimisticLock();
1149
- }
1150
- if (options.excludeTxnFromChangeStreams) {
1151
- transaction.excludeTxnFromChangeStreams();
1152
- }
1153
- const runner = new transaction_runner_1.AsyncTransactionRunner(session, transaction, runFn, options);
1286
+ return (0, instrument_1.startTrace)('Database.runTransactionAsync', this._traceConfig, async (span) => {
1287
+ // Loop to retry 'Session not found' errors.
1288
+ // (and yes, we like while (true) more than for (;;) here)
1289
+ // eslint-disable-next-line no-constant-condition
1290
+ while (true) {
1154
1291
  try {
1155
- return await runner.run();
1156
- }
1157
- finally {
1158
- this.pool_.release(session);
1292
+ const [session, transaction] = await (0, promisify_1.promisify)(getSession)();
1293
+ transaction.requestOptions = Object.assign(transaction.requestOptions || {}, options.requestOptions);
1294
+ if (options.optimisticLock) {
1295
+ transaction.useOptimisticLock();
1296
+ }
1297
+ if (options.excludeTxnFromChangeStreams) {
1298
+ transaction.excludeTxnFromChangeStreams();
1299
+ }
1300
+ sessionId = session === null || session === void 0 ? void 0 : session.id;
1301
+ span.addEvent('Using Session', { 'session.id': sessionId });
1302
+ const runner = new transaction_runner_1.AsyncTransactionRunner(session, transaction, runFn, options);
1303
+ try {
1304
+ return await runner.run();
1305
+ }
1306
+ catch (e) {
1307
+ (0, instrument_1.setSpanErrorAndException)(span, e);
1308
+ throw e;
1309
+ }
1310
+ finally {
1311
+ span.end();
1312
+ this.pool_.release(session);
1313
+ }
1159
1314
  }
1160
- }
1161
- catch (e) {
1162
- if (!(0, session_pool_1.isSessionNotFoundError)(e)) {
1163
- throw e;
1315
+ catch (e) {
1316
+ if (!(0, session_pool_1.isSessionNotFoundError)(e)) {
1317
+ span.addEvent('No session available', {
1318
+ 'session.id': sessionId,
1319
+ });
1320
+ span.end();
1321
+ throw e;
1322
+ }
1164
1323
  }
1165
1324
  }
1166
- }
1325
+ });
1167
1326
  }
1168
1327
  /**
1169
1328
  * Write a batch of mutations to Spanner.
@@ -1225,52 +1384,66 @@ class Database extends common.GrpcServiceObject {
1225
1384
  */
1226
1385
  batchWriteAtLeastOnce(mutationGroups, options) {
1227
1386
  const proxyStream = through.obj();
1228
- this.pool_.getSession((err, session) => {
1229
- if (err) {
1230
- proxyStream.destroy(err);
1231
- return;
1232
- }
1233
- const gaxOpts = extend(true, {}, options === null || options === void 0 ? void 0 : options.gaxOptions);
1234
- const reqOpts = Object.assign({}, {
1235
- session: session.formattedName_,
1236
- mutationGroups: mutationGroups.map(mg => mg.proto()),
1237
- requestOptions: options === null || options === void 0 ? void 0 : options.requestOptions,
1238
- excludeTxnFromChangeStream: options === null || options === void 0 ? void 0 : options.excludeTxnFromChangeStreams,
1239
- });
1240
- let dataReceived = false;
1241
- let dataStream = this.requestStream({
1242
- client: 'SpannerClient',
1243
- method: 'batchWrite',
1244
- reqOpts,
1245
- gaxOpts,
1246
- headers: this.resourceHeader_,
1247
- });
1248
- dataStream
1249
- .once('data', () => (dataReceived = true))
1250
- .once('error', err => {
1251
- if (!dataReceived &&
1252
- (0, session_pool_1.isSessionNotFoundError)(err)) {
1253
- // If there's a 'Session not found' error and we have not yet received
1254
- // any data, we can safely retry the writes on a new session.
1255
- // Register the error on the session so the pool can discard it.
1256
- if (session) {
1257
- session.lastError = err;
1258
- }
1259
- // Remove the current data stream from the end user stream.
1260
- dataStream.unpipe(proxyStream);
1261
- dataStream.end();
1262
- // Create a new stream and add it to the end user stream.
1263
- dataStream = this.batchWriteAtLeastOnce(mutationGroups, options);
1264
- dataStream.pipe(proxyStream);
1265
- }
1266
- else {
1387
+ return (0, instrument_1.startTrace)('Database.batchWriteAtLeastOnce', this._traceConfig, span => {
1388
+ this.pool_.getSession((err, session) => {
1389
+ if (err) {
1267
1390
  proxyStream.destroy(err);
1391
+ (0, instrument_1.setSpanError)(span, err);
1392
+ span.end();
1393
+ return;
1268
1394
  }
1269
- })
1270
- .once('end', () => this.pool_.release(session))
1271
- .pipe(proxyStream);
1395
+ span.addEvent('Using Session', { 'session.id': session === null || session === void 0 ? void 0 : session.id });
1396
+ const gaxOpts = extend(true, {}, options === null || options === void 0 ? void 0 : options.gaxOptions);
1397
+ const reqOpts = Object.assign({}, {
1398
+ session: session.formattedName_,
1399
+ mutationGroups: mutationGroups.map(mg => mg.proto()),
1400
+ requestOptions: options === null || options === void 0 ? void 0 : options.requestOptions,
1401
+ excludeTxnFromChangeStream: options === null || options === void 0 ? void 0 : options.excludeTxnFromChangeStreams,
1402
+ });
1403
+ let dataReceived = false;
1404
+ let dataStream = this.requestStream({
1405
+ client: 'SpannerClient',
1406
+ method: 'batchWrite',
1407
+ reqOpts,
1408
+ gaxOpts,
1409
+ headers: this.resourceHeader_,
1410
+ });
1411
+ dataStream
1412
+ .once('data', () => (dataReceived = true))
1413
+ .once('error', err => {
1414
+ (0, instrument_1.setSpanError)(span, err);
1415
+ if (!dataReceived &&
1416
+ (0, session_pool_1.isSessionNotFoundError)(err)) {
1417
+ // If there's a 'Session not found' error and we have not yet received
1418
+ // any data, we can safely retry the writes on a new session.
1419
+ // Register the error on the session so the pool can discard it.
1420
+ if (session) {
1421
+ session.lastError = err;
1422
+ }
1423
+ span.addEvent('No session available', {
1424
+ 'session.id': session === null || session === void 0 ? void 0 : session.id,
1425
+ });
1426
+ // Remove the current data stream from the end user stream.
1427
+ dataStream.unpipe(proxyStream);
1428
+ dataStream.end();
1429
+ span.end();
1430
+ // Create a new stream and add it to the end user stream.
1431
+ dataStream = this.batchWriteAtLeastOnce(mutationGroups, options);
1432
+ dataStream.pipe(proxyStream);
1433
+ }
1434
+ else {
1435
+ span.end();
1436
+ proxyStream.destroy(err);
1437
+ }
1438
+ })
1439
+ .once('end', () => {
1440
+ span.end();
1441
+ this.pool_.release(session);
1442
+ })
1443
+ .pipe(proxyStream);
1444
+ });
1445
+ return proxyStream;
1272
1446
  });
1273
- return proxyStream;
1274
1447
  }
1275
1448
  writeAtLeastOnce(mutations, optionsOrCallback, callback) {
1276
1449
  const cb = typeof optionsOrCallback === 'function'
@@ -1279,18 +1452,40 @@ class Database extends common.GrpcServiceObject {
1279
1452
  const options = typeof optionsOrCallback === 'object' && optionsOrCallback
1280
1453
  ? optionsOrCallback
1281
1454
  : {};
1282
- this.pool_.getSession((err, session, transaction) => {
1283
- if (err && (0, session_pool_1.isSessionNotFoundError)(err)) {
1284
- this.writeAtLeastOnce(mutations, options, cb);
1285
- return;
1286
- }
1287
- if (err) {
1288
- cb(err);
1289
- return;
1290
- }
1291
- this._releaseOnEnd(session, transaction);
1292
- transaction === null || transaction === void 0 ? void 0 : transaction.setQueuedMutations(mutations.proto());
1293
- return transaction === null || transaction === void 0 ? void 0 : transaction.commit(options, cb);
1455
+ return (0, instrument_1.startTrace)('Database.writeAtLeastOnce', this._traceConfig, span => {
1456
+ this.pool_.getSession((err, session, transaction) => {
1457
+ if (err && (0, session_pool_1.isSessionNotFoundError)(err)) {
1458
+ span.addEvent('No session available', {
1459
+ 'session.id': session === null || session === void 0 ? void 0 : session.id,
1460
+ });
1461
+ span.end();
1462
+ this.writeAtLeastOnce(mutations, options, cb);
1463
+ return;
1464
+ }
1465
+ if (err) {
1466
+ (0, instrument_1.setSpanError)(span, err);
1467
+ span.end();
1468
+ cb(err);
1469
+ return;
1470
+ }
1471
+ span.addEvent('Using Session', { 'session.id': session === null || session === void 0 ? void 0 : session.id });
1472
+ this._releaseOnEnd(session, transaction, span);
1473
+ try {
1474
+ transaction === null || transaction === void 0 ? void 0 : transaction.setQueuedMutations(mutations.proto());
1475
+ return transaction === null || transaction === void 0 ? void 0 : transaction.commit(options, (err, resp) => {
1476
+ if (err) {
1477
+ (0, instrument_1.setSpanError)(span, err);
1478
+ }
1479
+ span.end();
1480
+ cb(err, resp);
1481
+ });
1482
+ }
1483
+ catch (e) {
1484
+ (0, instrument_1.setSpanErrorAndException)(span, e);
1485
+ span.end();
1486
+ throw e;
1487
+ }
1488
+ });
1294
1489
  });
1295
1490
  }
1296
1491
  /**