@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.
@@ -25,10 +25,12 @@ const is = require("is");
25
25
  const stream_1 = require("stream");
26
26
  const codec_1 = require("./codec");
27
27
  const partial_result_stream_1 = require("./partial-result-stream");
28
+ const instrument_1 = require("./instrument");
28
29
  const protos_1 = require("../protos/protos");
29
30
  const common_1 = require("./common");
30
31
  const protos_2 = require("../protos/protos");
31
32
  var ReadLockMode = protos_2.google.spanner.v1.TransactionOptions.ReadWrite.ReadLockMode;
33
+ const instrument_2 = require("./instrument");
32
34
  const RETRY_INFO_TYPE = 'type.googleapis.com/google.rpc.retryinfo';
33
35
  const RETRY_INFO_BIN = 'google.rpc.retryinfo-bin';
34
36
  /**
@@ -140,11 +142,13 @@ class Snapshot extends events_1.EventEmitter {
140
142
  this.requestStream = session.requestStream.bind(session);
141
143
  const readOnly = Snapshot.encodeTimestampBounds(options || {});
142
144
  this._options = { readOnly };
145
+ this._dbName = this.session.parent.formattedName_;
143
146
  this.resourceHeader_ = {
144
- [common_1.CLOUD_RESOURCE_HEADER]: this.session.parent.formattedName_,
147
+ [common_1.CLOUD_RESOURCE_HEADER]: this._dbName,
145
148
  };
146
149
  this._waitingRequests = [];
147
150
  this._inlineBeginStarted = false;
151
+ this._observabilityOptions = session._observabilityOptions;
148
152
  }
149
153
  begin(gaxOptionsOrCallback, cb) {
150
154
  const gaxOpts = typeof gaxOptionsOrCallback === 'object' ? gaxOptionsOrCallback : {};
@@ -168,19 +172,28 @@ class Snapshot extends events_1.EventEmitter {
168
172
  this._options.partitionedDml !== undefined)) {
169
173
  (0, common_1.addLeaderAwareRoutingHeader)(headers);
170
174
  }
171
- this.request({
172
- client: 'SpannerClient',
173
- method: 'beginTransaction',
174
- reqOpts,
175
- gaxOpts,
176
- headers: headers,
177
- }, (err, resp) => {
178
- if (err) {
175
+ const traceConfig = {
176
+ opts: this._observabilityOptions,
177
+ dbName: this._dbName,
178
+ };
179
+ return (0, instrument_2.startTrace)('Snapshot.begin', traceConfig, span => {
180
+ span.addEvent('Begin Transaction');
181
+ this.request({
182
+ client: 'SpannerClient',
183
+ method: 'beginTransaction',
184
+ reqOpts,
185
+ gaxOpts,
186
+ headers: headers,
187
+ }, (err, resp) => {
188
+ if (err) {
189
+ (0, instrument_2.setSpanError)(span, err);
190
+ }
191
+ else {
192
+ this._update(resp);
193
+ }
194
+ span.end();
179
195
  callback(err, resp);
180
- return;
181
- }
182
- this._update(resp);
183
- callback(null, resp);
196
+ });
184
197
  });
185
198
  }
186
199
  /**
@@ -354,7 +367,7 @@ class Snapshot extends events_1.EventEmitter {
354
367
  * ```
355
368
  */
356
369
  createReadStream(table, request = {}) {
357
- var _a, _b, _c;
370
+ var _a, _b;
358
371
  const { gaxOptions, json, jsonOptions, maxResumeRetries, requestOptions, columnsMetadata, } = request;
359
372
  const keySet = Snapshot.encodeKeySet(request);
360
373
  const transaction = {};
@@ -392,37 +405,83 @@ class Snapshot extends events_1.EventEmitter {
392
405
  this._options.partitionedDml !== undefined)) {
393
406
  (0, common_1.addLeaderAwareRoutingHeader)(headers);
394
407
  }
395
- const makeRequest = (resumeToken) => {
396
- if (this.id && transaction.begin) {
397
- delete transaction.begin;
398
- transaction.id = this.id;
399
- }
400
- return this.requestStream({
401
- client: 'SpannerClient',
402
- method: 'streamingRead',
403
- reqOpts: Object.assign({}, reqOpts, { resumeToken }),
404
- gaxOpts: gaxOptions,
405
- headers: headers,
406
- });
408
+ const traceConfig = {
409
+ tableName: table,
410
+ opts: this._observabilityOptions,
411
+ dbName: this._dbName,
407
412
  };
408
- return (_c = (0, partial_result_stream_1.partialResultStream)(this._wrapWithIdWaiter(makeRequest), {
409
- json,
410
- jsonOptions,
411
- maxResumeRetries,
412
- columnsMetadata,
413
- gaxOptions,
414
- })) === null || _c === void 0 ? void 0 : _c.on('response', response => {
415
- if (response.metadata && response.metadata.transaction && !this.id) {
416
- this._update(response.metadata.transaction);
417
- }
418
- }).on('error', err => {
419
- const isServiceError = err && typeof err === 'object' && 'code' in err;
420
- if (!this.id &&
421
- this._useInRunner &&
422
- !(isServiceError &&
423
- err.code === google_gax_1.grpc.status.ABORTED)) {
424
- this.begin();
413
+ return (0, instrument_2.startTrace)('Snapshot.createReadStream', traceConfig, span => {
414
+ var _a;
415
+ let attempt = 0;
416
+ const makeRequest = (resumeToken) => {
417
+ if (this.id && transaction.begin) {
418
+ delete transaction.begin;
419
+ transaction.id = this.id;
420
+ }
421
+ attempt++;
422
+ if (!resumeToken) {
423
+ if (attempt === 1) {
424
+ span.addEvent('Starting stream');
425
+ }
426
+ else {
427
+ span.addEvent('Re-attempting start stream', { attempt: attempt });
428
+ }
429
+ }
430
+ else {
431
+ span.addEvent('Resuming stream', {
432
+ resume_token: resumeToken.toString(),
433
+ attempt: attempt,
434
+ });
435
+ }
436
+ return this.requestStream({
437
+ client: 'SpannerClient',
438
+ method: 'streamingRead',
439
+ reqOpts: Object.assign({}, reqOpts, { resumeToken }),
440
+ gaxOpts: gaxOptions,
441
+ headers: headers,
442
+ });
443
+ };
444
+ const resultStream = (_a = (0, partial_result_stream_1.partialResultStream)(this._wrapWithIdWaiter(makeRequest), {
445
+ json,
446
+ jsonOptions,
447
+ maxResumeRetries,
448
+ columnsMetadata,
449
+ gaxOptions,
450
+ })) === null || _a === void 0 ? void 0 : _a.on('response', response => {
451
+ if (response.metadata && response.metadata.transaction && !this.id) {
452
+ this._update(response.metadata.transaction);
453
+ }
454
+ }).on('error', err => {
455
+ var _a;
456
+ (0, instrument_2.setSpanError)(span, err);
457
+ const wasAborted = isErrorAborted(err);
458
+ if (!this.id && this._useInRunner && !wasAborted) {
459
+ // TODO: resolve https://github.com/googleapis/nodejs-spanner/issues/2170
460
+ this.begin();
461
+ }
462
+ else {
463
+ if (wasAborted) {
464
+ span.addEvent('Stream broken. Not safe to retry', {
465
+ 'transaction.id': (_a = this.id) === null || _a === void 0 ? void 0 : _a.toString(),
466
+ });
467
+ }
468
+ }
469
+ span.end();
470
+ }).on('end', err => {
471
+ if (err) {
472
+ (0, instrument_2.setSpanError)(span, err);
473
+ }
474
+ span.end();
475
+ });
476
+ if (resultStream instanceof stream_1.Stream) {
477
+ (0, stream_1.finished)(resultStream, err => {
478
+ if (err) {
479
+ (0, instrument_2.setSpanError)(span, err);
480
+ }
481
+ span.end();
482
+ });
425
483
  }
484
+ return resultStream;
426
485
  });
427
486
  }
428
487
  /**
@@ -489,28 +548,57 @@ class Snapshot extends events_1.EventEmitter {
489
548
  request = requestOrCallback;
490
549
  callback = cb;
491
550
  }
492
- this.createReadStream(table, request)
493
- .on('error', callback)
494
- .on('data', row => rows.push(row))
495
- .on('end', () => callback(null, rows));
551
+ const traceConfig = {
552
+ tableName: table,
553
+ opts: this._observabilityOptions,
554
+ dbName: this._dbName,
555
+ };
556
+ return (0, instrument_2.startTrace)('Snapshot.read', traceConfig, span => {
557
+ this.createReadStream(table, request)
558
+ .on('error', err => {
559
+ const e = err;
560
+ (0, instrument_2.setSpanError)(span, e);
561
+ span.end();
562
+ callback(e, null);
563
+ })
564
+ .on('data', row => rows.push(row))
565
+ .on('end', () => {
566
+ span.end();
567
+ callback(null, rows);
568
+ });
569
+ });
496
570
  }
497
571
  run(query, callback) {
498
572
  const rows = [];
499
573
  let stats;
500
574
  let metadata;
501
- this.runStream(query)
502
- .on('error', callback)
503
- .on('response', response => {
504
- if (response.metadata) {
505
- metadata = response.metadata;
506
- if (metadata.transaction && !this.id) {
507
- this._update(metadata.transaction);
575
+ const traceConfig = {
576
+ sql: query,
577
+ opts: this._observabilityOptions,
578
+ dbName: this._dbName,
579
+ };
580
+ (0, instrument_2.startTrace)('Snapshot.run', traceConfig, span => {
581
+ return this.runStream(query)
582
+ .on('error', (err, rows, stats, metadata) => {
583
+ (0, instrument_2.setSpanError)(span, err);
584
+ span.end();
585
+ callback(err, rows, stats, metadata);
586
+ })
587
+ .on('response', response => {
588
+ if (response.metadata) {
589
+ metadata = response.metadata;
590
+ if (metadata.transaction && !this.id) {
591
+ this._update(metadata.transaction);
592
+ }
508
593
  }
509
- }
510
- })
511
- .on('data', row => rows.push(row))
512
- .on('stats', _stats => (stats = _stats))
513
- .on('end', () => callback(null, rows, stats, metadata));
594
+ })
595
+ .on('data', row => rows.push(row))
596
+ .on('stats', _stats => (stats = _stats))
597
+ .on('end', () => {
598
+ span.end();
599
+ callback(null, rows, stats, metadata);
600
+ });
601
+ });
514
602
  }
515
603
  /**
516
604
  * ExecuteSql request options. This includes all standard ExecuteSqlRequest
@@ -658,45 +746,94 @@ class Snapshot extends events_1.EventEmitter {
658
746
  this._options.partitionedDml !== undefined)) {
659
747
  (0, common_1.addLeaderAwareRoutingHeader)(headers);
660
748
  }
661
- const makeRequest = (resumeToken) => {
662
- if (!reqOpts || (this.id && !reqOpts.transaction.id)) {
663
- try {
664
- sanitizeRequest();
749
+ const traceConfig = {
750
+ opts: this._observabilityOptions,
751
+ dbName: this._dbName,
752
+ ...query,
753
+ };
754
+ return (0, instrument_2.startTrace)('Snapshot.runStream', traceConfig, span => {
755
+ let attempt = 0;
756
+ const makeRequest = (resumeToken) => {
757
+ attempt++;
758
+ if (!resumeToken) {
759
+ if (attempt === 1) {
760
+ span.addEvent('Starting stream');
761
+ }
762
+ else {
763
+ span.addEvent('Re-attempting start stream', { attempt: attempt });
764
+ }
665
765
  }
666
- catch (e) {
667
- const errorStream = new stream_1.PassThrough();
668
- setImmediate(() => errorStream.destroy(e));
669
- return errorStream;
766
+ else {
767
+ span.addEvent('Resuming stream', {
768
+ resume_token: resumeToken.toString(),
769
+ attempt: attempt,
770
+ });
670
771
  }
671
- }
672
- return this.requestStream({
673
- client: 'SpannerClient',
674
- method: 'executeStreamingSql',
675
- reqOpts: Object.assign({}, reqOpts, { resumeToken }),
676
- gaxOpts: gaxOptions,
677
- headers: headers,
772
+ if (!reqOpts || (this.id && !reqOpts.transaction.id)) {
773
+ try {
774
+ sanitizeRequest();
775
+ }
776
+ catch (e) {
777
+ const errorStream = new stream_1.PassThrough();
778
+ (0, instrument_2.setSpanErrorAndException)(span, e);
779
+ span.end();
780
+ setImmediate(() => errorStream.destroy(e));
781
+ return errorStream;
782
+ }
783
+ }
784
+ return this.requestStream({
785
+ client: 'SpannerClient',
786
+ method: 'executeStreamingSql',
787
+ reqOpts: Object.assign({}, reqOpts, { resumeToken }),
788
+ gaxOpts: gaxOptions,
789
+ headers: headers,
790
+ });
791
+ };
792
+ const resultStream = (0, partial_result_stream_1.partialResultStream)(this._wrapWithIdWaiter(makeRequest), {
793
+ json,
794
+ jsonOptions,
795
+ maxResumeRetries,
796
+ columnsMetadata,
797
+ gaxOptions,
798
+ })
799
+ .on('response', response => {
800
+ if (response.metadata && response.metadata.transaction && !this.id) {
801
+ this._update(response.metadata.transaction);
802
+ }
803
+ })
804
+ .on('error', err => {
805
+ var _a;
806
+ (0, instrument_2.setSpanError)(span, err);
807
+ const wasAborted = isErrorAborted(err);
808
+ if (!this.id && this._useInRunner && !wasAborted) {
809
+ span.addEvent('Stream broken. Safe to retry');
810
+ // TODO: resolve https://github.com/googleapis/nodejs-spanner/issues/2170
811
+ this.begin();
812
+ }
813
+ else {
814
+ if (wasAborted) {
815
+ span.addEvent('Stream broken. Not safe to retry', {
816
+ 'transaction.id': (_a = this.id) === null || _a === void 0 ? void 0 : _a.toString(),
817
+ });
818
+ }
819
+ }
820
+ span.end();
821
+ })
822
+ .on('end', err => {
823
+ if (err) {
824
+ (0, instrument_2.setSpanError)(span, err);
825
+ }
826
+ span.end();
678
827
  });
679
- };
680
- return (0, partial_result_stream_1.partialResultStream)(this._wrapWithIdWaiter(makeRequest), {
681
- json,
682
- jsonOptions,
683
- maxResumeRetries,
684
- columnsMetadata,
685
- gaxOptions,
686
- })
687
- .on('response', response => {
688
- if (response.metadata && response.metadata.transaction && !this.id) {
689
- this._update(response.metadata.transaction);
690
- }
691
- })
692
- .on('error', err => {
693
- const isServiceError = err && typeof err === 'object' && 'code' in err;
694
- if (!this.id &&
695
- this._useInRunner &&
696
- !(isServiceError &&
697
- err.code === google_gax_1.grpc.status.ABORTED)) {
698
- this.begin();
828
+ if (resultStream instanceof stream_1.Stream) {
829
+ (0, stream_1.finished)(resultStream, err => {
830
+ if (err) {
831
+ (0, instrument_2.setSpanError)(span, err);
832
+ }
833
+ span.end();
834
+ });
699
835
  }
836
+ return resultStream;
700
837
  });
701
838
  }
702
839
  /**
@@ -825,6 +962,8 @@ class Snapshot extends events_1.EventEmitter {
825
962
  const { id, readTimestamp } = resp;
826
963
  this.id = id;
827
964
  this.metadata = resp;
965
+ const span = (0, instrument_1.getActiveOrNoopSpan)();
966
+ span.addEvent('Transaction Creation Done', { id: this.id.toString() });
828
967
  if (readTimestamp) {
829
968
  this.readTimestampProto = readTimestamp;
830
969
  this.readTimestamp = new precise_date_1.PreciseDate(readTimestamp);
@@ -898,12 +1037,23 @@ class Dml extends Snapshot {
898
1037
  if (typeof query === 'string') {
899
1038
  query = { sql: query };
900
1039
  }
901
- this.run(query, (err, rows, stats) => {
902
- let rowCount = 0;
903
- if (stats && stats.rowCount) {
904
- rowCount = Math.floor(stats[stats.rowCount]);
905
- }
906
- callback(err, rowCount);
1040
+ const traceConfig = {
1041
+ opts: this._observabilityOptions,
1042
+ dbName: this._dbName,
1043
+ ...query,
1044
+ };
1045
+ return (0, instrument_2.startTrace)('Dml.runUpdate', traceConfig, span => {
1046
+ this.run(query, (err, rows, stats) => {
1047
+ let rowCount = 0;
1048
+ if (stats && stats.rowCount) {
1049
+ rowCount = Math.floor(stats[stats.rowCount]);
1050
+ }
1051
+ if (err) {
1052
+ (0, instrument_2.setSpanError)(span, err);
1053
+ }
1054
+ span.end();
1055
+ callback(err, rowCount);
1056
+ });
907
1057
  });
908
1058
  }
909
1059
  }
@@ -1049,41 +1199,51 @@ class Transaction extends Dml {
1049
1199
  if (this._getSpanner().routeToLeaderEnabled) {
1050
1200
  (0, common_1.addLeaderAwareRoutingHeader)(headers);
1051
1201
  }
1052
- this.request({
1053
- client: 'SpannerClient',
1054
- method: 'executeBatchDml',
1055
- reqOpts,
1056
- gaxOpts,
1057
- headers: headers,
1058
- }, (err, resp) => {
1059
- var _a;
1060
- let batchUpdateError;
1061
- if (err) {
1062
- const rowCounts = [];
1063
- batchUpdateError = Object.assign(err, { rowCounts });
1064
- callback(batchUpdateError, rowCounts, resp);
1065
- return;
1066
- }
1067
- const { resultSets, status } = resp;
1068
- for (const resultSet of resultSets) {
1069
- if (!this.id && ((_a = resultSet.metadata) === null || _a === void 0 ? void 0 : _a.transaction)) {
1070
- this._update(resultSet.metadata.transaction);
1202
+ const traceConfig = {
1203
+ opts: this._observabilityOptions,
1204
+ dbName: this._dbName,
1205
+ };
1206
+ return (0, instrument_2.startTrace)('Transaction.batchUpdate', traceConfig, span => {
1207
+ this.request({
1208
+ client: 'SpannerClient',
1209
+ method: 'executeBatchDml',
1210
+ reqOpts,
1211
+ gaxOpts,
1212
+ headers: headers,
1213
+ }, (err, resp) => {
1214
+ var _a;
1215
+ let batchUpdateError;
1216
+ if (err) {
1217
+ const rowCounts = [];
1218
+ batchUpdateError = Object.assign(err, { rowCounts });
1219
+ (0, instrument_2.setSpanError)(span, batchUpdateError);
1220
+ span.end();
1221
+ callback(batchUpdateError, rowCounts, resp);
1222
+ return;
1071
1223
  }
1072
- }
1073
- const rowCounts = resultSets.map(({ stats }) => {
1074
- return ((stats &&
1075
- Number(stats[stats.rowCount])) ||
1076
- 0);
1077
- });
1078
- if (status && status.code !== 0) {
1079
- const error = new Error(status.message);
1080
- batchUpdateError = Object.assign(error, {
1081
- code: status.code,
1082
- metadata: Transaction.extractKnownMetadata(status.details),
1083
- rowCounts,
1224
+ const { resultSets, status } = resp;
1225
+ for (const resultSet of resultSets) {
1226
+ if (!this.id && ((_a = resultSet.metadata) === null || _a === void 0 ? void 0 : _a.transaction)) {
1227
+ this._update(resultSet.metadata.transaction);
1228
+ }
1229
+ }
1230
+ const rowCounts = resultSets.map(({ stats }) => {
1231
+ return ((stats &&
1232
+ Number(stats[stats.rowCount])) ||
1233
+ 0);
1084
1234
  });
1085
- }
1086
- callback(batchUpdateError, rowCounts, resp);
1235
+ if (status && status.code !== 0) {
1236
+ const error = new Error(status.message);
1237
+ batchUpdateError = Object.assign(error, {
1238
+ code: status.code,
1239
+ metadata: Transaction.extractKnownMetadata(status.details),
1240
+ rowCounts,
1241
+ });
1242
+ (0, instrument_2.setSpanError)(span, batchUpdateError);
1243
+ }
1244
+ span.end();
1245
+ callback(batchUpdateError, rowCounts, resp);
1246
+ });
1087
1247
  });
1088
1248
  }
1089
1249
  static extractKnownMetadata(details) {
@@ -1116,43 +1276,70 @@ class Transaction extends Dml {
1116
1276
  const session = this.session.formattedName_;
1117
1277
  const requestOptions = options.requestOptions;
1118
1278
  const reqOpts = { mutations, session, requestOptions };
1119
- if (this.id) {
1120
- reqOpts.transactionId = this.id;
1121
- }
1122
- else if (!this._useInRunner) {
1123
- reqOpts.singleUseTransaction = this._options;
1124
- }
1125
- else {
1126
- this.begin().then(() => this.commit(options, callback), callback);
1127
- return;
1128
- }
1129
- if ('returnCommitStats' in options &&
1130
- options.returnCommitStats) {
1131
- reqOpts.returnCommitStats = options.returnCommitStats;
1132
- }
1133
- if ('maxCommitDelay' in options &&
1134
- options.maxCommitDelay) {
1135
- reqOpts.maxCommitDelay = options.maxCommitDelay;
1136
- }
1137
- reqOpts.requestOptions = Object.assign(requestOptions || {}, this.requestOptions);
1138
- const headers = this.resourceHeader_;
1139
- if (this._getSpanner().routeToLeaderEnabled) {
1140
- (0, common_1.addLeaderAwareRoutingHeader)(headers);
1141
- }
1142
- this.request({
1143
- client: 'SpannerClient',
1144
- method: 'commit',
1145
- reqOpts,
1146
- gaxOpts: gaxOpts,
1147
- headers: headers,
1148
- }, (err, resp) => {
1149
- this.end();
1150
- if (resp && resp.commitTimestamp) {
1151
- this.commitTimestampProto = resp.commitTimestamp;
1152
- this.commitTimestamp = new precise_date_1.PreciseDate(resp.commitTimestamp);
1279
+ const traceConfig = {
1280
+ opts: this._observabilityOptions,
1281
+ dbName: this._dbName,
1282
+ };
1283
+ return (0, instrument_2.startTrace)('Transaction.commit', traceConfig, span => {
1284
+ if (this.id) {
1285
+ reqOpts.transactionId = this.id;
1286
+ }
1287
+ else if (!this._useInRunner) {
1288
+ reqOpts.singleUseTransaction = this._options;
1153
1289
  }
1154
- err = Transaction.decorateCommitError(err, mutations);
1155
- callback(err, resp);
1290
+ else {
1291
+ this.begin().then(() => {
1292
+ this.commit(options, (err, resp) => {
1293
+ if (err) {
1294
+ (0, instrument_2.setSpanError)(span, err);
1295
+ }
1296
+ span.end();
1297
+ callback(err, resp);
1298
+ });
1299
+ }, err => {
1300
+ (0, instrument_2.setSpanError)(span, err);
1301
+ span.end();
1302
+ callback(err, null);
1303
+ });
1304
+ return;
1305
+ }
1306
+ if ('returnCommitStats' in options &&
1307
+ options.returnCommitStats) {
1308
+ reqOpts.returnCommitStats = options.returnCommitStats;
1309
+ }
1310
+ if ('maxCommitDelay' in options &&
1311
+ options.maxCommitDelay) {
1312
+ reqOpts.maxCommitDelay = options.maxCommitDelay;
1313
+ }
1314
+ reqOpts.requestOptions = Object.assign(requestOptions || {}, this.requestOptions);
1315
+ const headers = this.resourceHeader_;
1316
+ if (this._getSpanner().routeToLeaderEnabled) {
1317
+ (0, common_1.addLeaderAwareRoutingHeader)(headers);
1318
+ }
1319
+ span.addEvent('Starting Commit');
1320
+ this.request({
1321
+ client: 'SpannerClient',
1322
+ method: 'commit',
1323
+ reqOpts,
1324
+ gaxOpts: gaxOpts,
1325
+ headers: headers,
1326
+ }, (err, resp) => {
1327
+ this.end();
1328
+ if (err) {
1329
+ span.addEvent('Commit failed');
1330
+ (0, instrument_2.setSpanError)(span, err);
1331
+ }
1332
+ else {
1333
+ span.addEvent('Commit Done');
1334
+ }
1335
+ if (resp && resp.commitTimestamp) {
1336
+ this.commitTimestampProto = resp.commitTimestamp;
1337
+ this.commitTimestamp = new precise_date_1.PreciseDate(resp.commitTimestamp);
1338
+ }
1339
+ err = Transaction.decorateCommitError(err, mutations);
1340
+ span.end();
1341
+ callback(err, resp);
1342
+ });
1156
1343
  });
1157
1344
  }
1158
1345
  /**
@@ -1380,29 +1567,42 @@ class Transaction extends Dml {
1380
1567
  rollback(gaxOptionsOrCallback, cb) {
1381
1568
  const gaxOpts = typeof gaxOptionsOrCallback === 'object' ? gaxOptionsOrCallback : {};
1382
1569
  const callback = typeof gaxOptionsOrCallback === 'function' ? gaxOptionsOrCallback : cb;
1383
- if (!this.id) {
1384
- callback(new Error('Transaction ID is unknown, nothing to rollback.'));
1385
- return;
1386
- }
1387
- const session = this.session.formattedName_;
1388
- const transactionId = this.id;
1389
- const reqOpts = {
1390
- session,
1391
- transactionId,
1570
+ const traceConfig = {
1571
+ opts: this._observabilityOptions,
1572
+ dbName: this._dbName,
1392
1573
  };
1393
- const headers = this.resourceHeader_;
1394
- if (this._getSpanner().routeToLeaderEnabled) {
1395
- (0, common_1.addLeaderAwareRoutingHeader)(headers);
1396
- }
1397
- this.request({
1398
- client: 'SpannerClient',
1399
- method: 'rollback',
1400
- reqOpts,
1401
- gaxOpts,
1402
- headers: headers,
1403
- }, (err) => {
1404
- this.end();
1405
- callback(err);
1574
+ return (0, instrument_2.startTrace)('Transaction.rollback', traceConfig, span => {
1575
+ if (!this.id) {
1576
+ const err = new Error('Transaction ID is unknown, nothing to rollback.');
1577
+ (0, instrument_2.setSpanError)(span, err);
1578
+ span.end();
1579
+ callback(err);
1580
+ return;
1581
+ }
1582
+ const session = this.session.formattedName_;
1583
+ const transactionId = this.id;
1584
+ const reqOpts = {
1585
+ session,
1586
+ transactionId,
1587
+ };
1588
+ const headers = this.resourceHeader_;
1589
+ if (this._getSpanner().routeToLeaderEnabled) {
1590
+ (0, common_1.addLeaderAwareRoutingHeader)(headers);
1591
+ }
1592
+ this.request({
1593
+ client: 'SpannerClient',
1594
+ method: 'rollback',
1595
+ reqOpts,
1596
+ gaxOpts,
1597
+ headers: headers,
1598
+ }, (err) => {
1599
+ if (err) {
1600
+ (0, instrument_2.setSpanError)(span, err);
1601
+ }
1602
+ span.end();
1603
+ this.end();
1604
+ callback(err);
1605
+ });
1406
1606
  });
1407
1607
  }
1408
1608
  /**
@@ -1772,13 +1972,30 @@ class PartitionedDml extends Dml {
1772
1972
  this._options.excludeTxnFromChangeStreams = true;
1773
1973
  }
1774
1974
  runUpdate(query, callback) {
1775
- super.runUpdate(query, (err, count) => {
1776
- this.end();
1777
- callback(err, count);
1975
+ const traceConfig = {
1976
+ sql: query,
1977
+ opts: this._observabilityOptions,
1978
+ dbName: this._dbName,
1979
+ };
1980
+ return (0, instrument_2.startTrace)('PartitionedDml.runUpdate', traceConfig, span => {
1981
+ super.runUpdate(query, (err, count) => {
1982
+ if (err) {
1983
+ (0, instrument_2.setSpanError)(span, err);
1984
+ }
1985
+ this.end();
1986
+ span.end();
1987
+ callback(err, count);
1988
+ });
1778
1989
  });
1779
1990
  }
1780
1991
  }
1781
1992
  exports.PartitionedDml = PartitionedDml;
1993
+ function isErrorAborted(err) {
1994
+ return (err &&
1995
+ typeof err === 'object' &&
1996
+ 'code' in err &&
1997
+ err.code === google_gax_1.grpc.status.ABORTED);
1998
+ }
1782
1999
  /*! Developer Documentation
1783
2000
  *
1784
2001
  * All async methods (except for streams) return a Promise in the event