@google-cloud/spanner 7.19.1 → 7.21.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.
@@ -40,6 +40,13 @@ export interface SessionFactoryInterface {
40
40
  * @param {GetSessionCallback} callback The callback function.
41
41
  */
42
42
  getSession(callback: GetSessionCallback): void;
43
+ /**
44
+ * When called returns a session for paritioned dml.
45
+ *
46
+ * @name SessionFactoryInterface#getSessionForPartitionedOps
47
+ * @param {GetSessionCallback} callback The callback function.
48
+ */
49
+ getSessionForPartitionedOps(callback: GetSessionCallback): void;
43
50
  /**
44
51
  * When called returns the pool object.
45
52
  *
@@ -75,6 +82,7 @@ export declare class SessionFactory extends common.GrpcServiceObject implements
75
82
  multiplexedSession_: MultiplexedSessionInterface;
76
83
  pool_: SessionPoolInterface;
77
84
  isMultiplexed: boolean;
85
+ isMultiplexedPartitionOps: boolean;
78
86
  constructor(database: Database, name: String, poolOptions?: SessionPoolConstructor | SessionPoolOptions);
79
87
  /**
80
88
  * Retrieves a session, either a regular session or a multiplexed session, based on the environment variable configuration.
@@ -85,6 +93,18 @@ export declare class SessionFactory extends common.GrpcServiceObject implements
85
93
  * @param {GetSessionCallback} callback The callback function.
86
94
  */
87
95
  getSession(callback: GetSessionCallback): void;
96
+ /**
97
+ * Retrieves a session for partitioned operations, selecting the appropriate session type
98
+ * based on whether multiplexed sessions are enabled.
99
+ *
100
+ * If multiplexed sessions are enabled for partitioned ops this methods delegates the request to `getSession()`, which returns
101
+ * either a multiplexed session or a regular session based on the configuration.
102
+ *
103
+ * If the multiplexed sessions are disabled, a session is retrieved from the regular session pool.
104
+ *
105
+ * @param {GetSessionCallback} callback The callback function.
106
+ */
107
+ getSessionForPartitionedOps(callback: GetSessionCallback): void;
88
108
  /**
89
109
  * Returns the regular session pool object.
90
110
  *
@@ -92,9 +112,11 @@ export declare class SessionFactory extends common.GrpcServiceObject implements
92
112
  */
93
113
  getPool(): SessionPoolInterface;
94
114
  /**
95
- * Releases a session back to the session pool.
115
+ * Releases a regular session back to the session pool.
116
+ *
117
+ * This methods does not release a multiplexed session.
96
118
  *
97
- * This method returns a session to the pool after it is no longer needed.
119
+ * It returns a session to the pool after it is no longer needed.
98
120
  * It is a no-op for multiplexed sessions.
99
121
  *
100
122
  * @param {Session} session - The session to be released. This should be an instance of `Session` that was
@@ -47,6 +47,11 @@ class SessionFactory extends common.GrpcServiceObject {
47
47
  process.env.GOOGLE_CLOUD_SPANNER_MULTIPLEXED_SESSIONS === 'true'
48
48
  ? (this.isMultiplexed = true)
49
49
  : (this.isMultiplexed = false);
50
+ // set the isMultiplexedPartitionedOps property to true if multiplexed session is enabled for paritioned ops, otherwise set the property to false
51
+ this.isMultiplexedPartitionOps =
52
+ process.env.GOOGLE_CLOUD_SPANNER_MULTIPLEXED_SESSIONS === 'true' &&
53
+ process.env.GOOGLE_CLOUD_SPANNER_MULTIPLEXED_SESSIONS_PARTITIONED_OPS ===
54
+ 'true';
50
55
  // Multiplexed sessions should only be created if its enabled.
51
56
  if (this.isMultiplexed) {
52
57
  this.multiplexedSession_.on('error', this.emit.bind(database, 'error'));
@@ -67,6 +72,22 @@ class SessionFactory extends common.GrpcServiceObject {
67
72
  : this.pool_;
68
73
  sessionHandler.getSession((err, session, transaction) => callback(err, session, transaction));
69
74
  }
75
+ /**
76
+ * Retrieves a session for partitioned operations, selecting the appropriate session type
77
+ * based on whether multiplexed sessions are enabled.
78
+ *
79
+ * If multiplexed sessions are enabled for partitioned ops this methods delegates the request to `getSession()`, which returns
80
+ * either a multiplexed session or a regular session based on the configuration.
81
+ *
82
+ * If the multiplexed sessions are disabled, a session is retrieved from the regular session pool.
83
+ *
84
+ * @param {GetSessionCallback} callback The callback function.
85
+ */
86
+ getSessionForPartitionedOps(callback) {
87
+ this.isMultiplexedPartitionOps
88
+ ? this.getSession(callback)
89
+ : this.pool_.getSession(callback);
90
+ }
70
91
  /**
71
92
  * Returns the regular session pool object.
72
93
  *
@@ -76,9 +97,11 @@ class SessionFactory extends common.GrpcServiceObject {
76
97
  return this.pool_;
77
98
  }
78
99
  /**
79
- * Releases a session back to the session pool.
100
+ * Releases a regular session back to the session pool.
101
+ *
102
+ * This methods does not release a multiplexed session.
80
103
  *
81
- * This method returns a session to the pool after it is no longer needed.
104
+ * It returns a session to the pool after it is no longer needed.
82
105
  * It is a no-op for multiplexed sessions.
83
106
  *
84
107
  * @param {Session} session - The session to be released. This should be an instance of `Session` that was
@@ -87,7 +110,8 @@ class SessionFactory extends common.GrpcServiceObject {
87
110
  * @throws {Error} If the session is invalid or cannot be released.
88
111
  */
89
112
  release(session) {
90
- if (!this.isMultiplexed) {
113
+ var _a;
114
+ if (!((_a = session.metadata) === null || _a === void 0 ? void 0 : _a.multiplexed)) {
91
115
  this.pool_.release(session);
92
116
  }
93
117
  }
@@ -22,6 +22,7 @@ import { Snapshot, Transaction, PartitionedDml, TimestampBounds } from './transa
22
22
  import { google } from '../protos/protos';
23
23
  import { Database } from './database';
24
24
  import { NormalCallback } from './common';
25
+ import { ObservabilityOptions } from './instrument';
25
26
  import { grpc, CallOptions } from 'google-gax';
26
27
  import IRequestOptions = google.spanner.v1.IRequestOptions;
27
28
  export type GetSessionResponse = [Session, r.Response];
@@ -95,6 +96,7 @@ export declare class Session extends common.GrpcServiceObject {
95
96
  commonHeaders_: {
96
97
  [k: string]: string;
97
98
  };
99
+ _observabilityOptions?: ObservabilityOptions;
98
100
  constructor(database: Database, name?: string);
99
101
  /**
100
102
  * Delete a session.
@@ -254,5 +256,6 @@ export declare class Session extends common.GrpcServiceObject {
254
256
  * @returns {Spanner}
255
257
  */
256
258
  private _getSpanner;
259
+ private channelId;
257
260
  }
258
261
  export {};
@@ -25,6 +25,7 @@ const promisify_1 = require("@google-cloud/promisify");
25
25
  const extend = require("extend");
26
26
  const transaction_1 = require("./transaction");
27
27
  const common_1 = require("./common");
28
+ const request_id_header_1 = require("./request_id_header");
28
29
  /**
29
30
  * Create a Session object to interact with a Cloud Spanner session.
30
31
  *
@@ -199,7 +200,8 @@ class Session extends common.GrpcServiceObject {
199
200
  });
200
201
  },
201
202
  });
202
- this.commonHeaders_ = (0, common_1.getCommonHeaders)(this.parent.formattedName_, (_a = database._observabilityOptions) === null || _a === void 0 ? void 0 : _a.enableEndToEndTracing);
203
+ this._observabilityOptions = database._observabilityOptions;
204
+ this.commonHeaders_ = (0, common_1.getCommonHeaders)(this.parent.formattedName_, (_a = this._observabilityOptions) === null || _a === void 0 ? void 0 : _a.enableEndToEndTracing);
203
205
  this.request = database.request;
204
206
  this.requestStream = database.requestStream;
205
207
  if (name) {
@@ -212,12 +214,13 @@ class Session extends common.GrpcServiceObject {
212
214
  const reqOpts = {
213
215
  name: this.formattedName_,
214
216
  };
217
+ const database = this.parent;
215
218
  return this.request({
216
219
  client: 'SpannerClient',
217
220
  method: 'deleteSession',
218
221
  reqOpts,
219
222
  gaxOpts,
220
- headers: this.commonHeaders_,
223
+ headers: (0, request_id_header_1.injectRequestIDIntoHeaders)(this.commonHeaders_, this, (0, request_id_header_1.nextNthRequest)(database), 1),
221
224
  }, callback);
222
225
  }
223
226
  getMetadata(optionsOrCallback, cb) {
@@ -230,12 +233,13 @@ class Session extends common.GrpcServiceObject {
230
233
  if (this._getSpanner().routeToLeaderEnabled) {
231
234
  (0, common_1.addLeaderAwareRoutingHeader)(headers);
232
235
  }
236
+ const database = this.parent;
233
237
  return this.request({
234
238
  client: 'SpannerClient',
235
239
  method: 'getSession',
236
240
  reqOpts,
237
241
  gaxOpts,
238
- headers: headers,
242
+ headers: (0, request_id_header_1.injectRequestIDIntoHeaders)(headers, this.session, (0, request_id_header_1.nextNthRequest)(database), 1),
239
243
  }, (err, resp) => {
240
244
  if (resp) {
241
245
  resp.databaseRole = resp.creatorRole;
@@ -252,12 +256,13 @@ class Session extends common.GrpcServiceObject {
252
256
  session: this.formattedName_,
253
257
  sql: 'SELECT 1',
254
258
  };
259
+ const database = this.parent;
255
260
  return this.request({
256
261
  client: 'SpannerClient',
257
262
  method: 'executeSql',
258
263
  reqOpts,
259
264
  gaxOpts,
260
- headers: this.commonHeaders_,
265
+ headers: (0, request_id_header_1.injectRequestIDIntoHeaders)(this.commonHeaders_, this, (0, request_id_header_1.nextNthRequest)(database), 1),
261
266
  }, callback);
262
267
  }
263
268
  /**
@@ -335,6 +340,10 @@ class Session extends common.GrpcServiceObject {
335
340
  _getSpanner() {
336
341
  return this.parent.parent.parent;
337
342
  }
343
+ channelId() {
344
+ // The Node.js client does not use a gRPC channel pool, so this always returns 1.
345
+ return 1;
346
+ }
338
347
  }
339
348
  exports.Session = Session;
340
349
  /*! Developer Documentation
@@ -21,6 +21,8 @@ import { google as databaseAdmin } from '../protos/protos';
21
21
  import { Schema, LongRunningCallback } from './common';
22
22
  import IRequestOptions = databaseAdmin.spanner.v1.IRequestOptions;
23
23
  import { ObservabilityOptions } from './instrument';
24
+ import { google } from '../protos/protos';
25
+ import IsolationLevel = google.spanner.v1.TransactionOptions.IsolationLevel;
24
26
  export type Key = string | string[];
25
27
  export type CreateTableResponse = [
26
28
  Table,
@@ -33,6 +35,7 @@ export type DropTableCallback = UpdateSchemaCallback;
33
35
  interface MutateRowsOptions extends CommitOptions {
34
36
  requestOptions?: Omit<IRequestOptions, 'requestTag'>;
35
37
  excludeTxnFromChangeStreams?: boolean;
38
+ isolationLevel?: IsolationLevel;
36
39
  }
37
40
  export type DeleteRowsCallback = CommitCallback;
38
41
  export type DeleteRowsResponse = CommitResponse;
@@ -19,6 +19,8 @@ exports.Table = void 0;
19
19
  const promisify_1 = require("@google-cloud/promisify");
20
20
  const through = require("through2");
21
21
  const instrument_1 = require("./instrument");
22
+ const protos_1 = require("../protos/protos");
23
+ var IsolationLevel = protos_1.google.spanner.v1.TransactionOptions.IsolationLevel;
22
24
  const GOOGLE_STANDARD_SQL = 'GOOGLE_STANDARD_SQL';
23
25
  const POSTGRESQL = 'POSTGRESQL';
24
26
  /**
@@ -233,19 +235,25 @@ class Table {
233
235
  * @param {function} callback The callback function.
234
236
  */
235
237
  _mutate(method, rows, options = {}, callback) {
238
+ var _a;
236
239
  const traceConfig = {
237
240
  opts: this._observabilityOptions,
238
241
  tableName: this.name,
239
242
  dbName: this.getDBName(),
243
+ transactionTag: (_a = options === null || options === void 0 ? void 0 : options.requestOptions) === null || _a === void 0 ? void 0 : _a.transactionTag,
240
244
  };
241
245
  (0, instrument_1.startTrace)('Table.' + method, traceConfig, span => {
242
246
  const requestOptions = 'requestOptions' in options ? options.requestOptions : {};
243
247
  const excludeTxnFromChangeStreams = 'excludeTxnFromChangeStreams' in options
244
248
  ? options.excludeTxnFromChangeStreams
245
249
  : false;
250
+ const isolationLevel = 'isolationLevel' in options
251
+ ? options.isolationLevel
252
+ : IsolationLevel.ISOLATION_LEVEL_UNSPECIFIED;
246
253
  this.database.runTransaction({
247
254
  requestOptions: requestOptions,
248
255
  excludeTxnFromChangeStreams: excludeTxnFromChangeStreams,
256
+ isolationLevel: isolationLevel,
249
257
  }, (err, transaction) => {
250
258
  if (err) {
251
259
  (0, instrument_1.setSpanError)(span, err);
@@ -19,6 +19,7 @@ import { Transaction } from './transaction';
19
19
  import { NormalCallback } from './common';
20
20
  import { google } from '../protos/protos';
21
21
  import IRequestOptions = google.spanner.v1.IRequestOptions;
22
+ import IsolationLevel = google.spanner.v1.TransactionOptions.IsolationLevel;
22
23
  /**
23
24
  * @typedef {object} RunTransactionOptions
24
25
  * @property {number} [timeout] The maximum amount of time (in ms) that a
@@ -29,6 +30,7 @@ export interface RunTransactionOptions {
29
30
  requestOptions?: Pick<IRequestOptions, 'transactionTag'>;
30
31
  optimisticLock?: boolean;
31
32
  excludeTxnFromChangeStreams?: boolean;
33
+ isolationLevel?: IsolationLevel;
32
34
  }
33
35
  /**
34
36
  * A function to execute in the context of a transaction.
@@ -22,6 +22,8 @@ const google_gax_1 = require("google-gax");
22
22
  const protobufjs_1 = require("protobufjs");
23
23
  const through = require("through2");
24
24
  const session_pool_1 = require("./session-pool");
25
+ const protos_1 = require("../protos/protos");
26
+ var IsolationLevel = protos_1.google.spanner.v1.TransactionOptions.IsolationLevel;
25
27
  // eslint-disable-next-line @typescript-eslint/no-var-requires
26
28
  const jsonProtos = require('../protos/protos.json');
27
29
  const RETRY_INFO = 'google.rpc.retryinfo-bin';
@@ -65,7 +67,10 @@ class Runner {
65
67
  this.session = session;
66
68
  this.transaction = transaction;
67
69
  this.transaction.useInRunner();
68
- const defaults = { timeout: 3600000 };
70
+ const defaults = {
71
+ timeout: 3600000,
72
+ isolationLevel: IsolationLevel.ISOLATION_LEVEL_UNSPECIFIED,
73
+ };
69
74
  this.options = Object.assign(defaults, options);
70
75
  }
71
76
  /**
@@ -119,12 +124,7 @@ class Runner {
119
124
  return transaction;
120
125
  }
121
126
  const transaction = this.session.transaction(this.session.parent.queryOptions_);
122
- if (this.options.optimisticLock) {
123
- transaction.useOptimisticLock();
124
- }
125
- if (this.options.excludeTxnFromChangeStreams) {
126
- transaction.excludeTxnFromChangeStreams();
127
- }
127
+ transaction.setReadWriteTransactionOptions(this.options);
128
128
  if (this.attempts > 0) {
129
129
  await transaction.begin();
130
130
  }
@@ -29,7 +29,8 @@ import { google } from '../protos/protos';
29
29
  import IQueryOptions = google.spanner.v1.ExecuteSqlRequest.IQueryOptions;
30
30
  import IRequestOptions = google.spanner.v1.IRequestOptions;
31
31
  import { Spanner } from '.';
32
- import { ObservabilityOptions } from './instrument';
32
+ import { ObservabilityOptions, traceConfig } from './instrument';
33
+ import { RunTransactionOptions } from './transaction-runner';
33
34
  export type Rows = Array<Row | Json>;
34
35
  export interface TimestampBounds {
35
36
  strong?: boolean;
@@ -246,6 +247,7 @@ export declare class Snapshot extends EventEmitter {
246
247
  };
247
248
  requestOptions?: Pick<IRequestOptions, 'transactionTag'>;
248
249
  _observabilityOptions?: ObservabilityOptions;
250
+ _traceConfig: traceConfig;
249
251
  protected _dbName?: string;
250
252
  /**
251
253
  * The transaction ID.
@@ -1515,6 +1517,7 @@ export declare class Transaction extends Dml {
1515
1517
  * included in change streams.
1516
1518
  */
1517
1519
  excludeTxnFromChangeStreams(): void;
1520
+ setReadWriteTransactionOptions(options: RunTransactionOptions): void;
1518
1521
  }
1519
1522
  /**
1520
1523
  * MutationSet represent a set of changes to be applied atomically to a Cloud Spanner
@@ -29,8 +29,10 @@ const instrument_1 = require("./instrument");
29
29
  const protos_1 = require("../protos/protos");
30
30
  const common_1 = require("./common");
31
31
  const protos_2 = require("../protos/protos");
32
+ var IsolationLevel = protos_2.google.spanner.v1.TransactionOptions.IsolationLevel;
32
33
  var ReadLockMode = protos_2.google.spanner.v1.TransactionOptions.ReadWrite.ReadLockMode;
33
34
  const instrument_2 = require("./instrument");
35
+ const request_id_header_1 = require("./request_id_header");
34
36
  const RETRY_INFO_TYPE = 'type.googleapis.com/google.rpc.retryinfo';
35
37
  const RETRY_INFO_BIN = 'google.rpc.retryinfo-bin';
36
38
  /**
@@ -148,8 +150,13 @@ class Snapshot extends events_1.EventEmitter {
148
150
  this._inlineBeginStarted = false;
149
151
  this._observabilityOptions = session._observabilityOptions;
150
152
  this.commonHeaders_ = (0, common_1.getCommonHeaders)(this._dbName, (_a = this._observabilityOptions) === null || _a === void 0 ? void 0 : _a.enableEndToEndTracing);
153
+ this._traceConfig = {
154
+ opts: this._observabilityOptions,
155
+ dbName: this._dbName,
156
+ };
151
157
  }
152
158
  begin(gaxOptionsOrCallback, cb) {
159
+ var _a;
153
160
  const gaxOpts = typeof gaxOptionsOrCallback === 'object' ? gaxOptionsOrCallback : {};
154
161
  const callback = typeof gaxOptionsOrCallback === 'function' ? gaxOptionsOrCallback : cb;
155
162
  const session = this.session.formattedName_;
@@ -171,18 +178,17 @@ class Snapshot extends events_1.EventEmitter {
171
178
  this._options.partitionedDml !== undefined)) {
172
179
  (0, common_1.addLeaderAwareRoutingHeader)(headers);
173
180
  }
174
- const traceConfig = {
175
- opts: this._observabilityOptions,
176
- dbName: this._dbName,
177
- };
178
- return (0, instrument_2.startTrace)('Snapshot.begin', traceConfig, span => {
181
+ return (0, instrument_2.startTrace)('Snapshot.begin', {
182
+ transactionTag: (_a = this.requestOptions) === null || _a === void 0 ? void 0 : _a.transactionTag,
183
+ ...this._traceConfig,
184
+ }, span => {
179
185
  span.addEvent('Begin Transaction');
180
186
  this.request({
181
187
  client: 'SpannerClient',
182
188
  method: 'beginTransaction',
183
189
  reqOpts,
184
190
  gaxOpts,
185
- headers: headers,
191
+ headers: (0, request_id_header_1.injectRequestIDIntoHeaders)(headers, this.session),
186
192
  }, (err, resp) => {
187
193
  if (err) {
188
194
  (0, instrument_2.setSpanError)(span, err);
@@ -366,7 +372,7 @@ class Snapshot extends events_1.EventEmitter {
366
372
  * ```
367
373
  */
368
374
  createReadStream(table, request = {}) {
369
- var _a, _b;
375
+ var _a, _b, _c;
370
376
  const { gaxOptions, json, jsonOptions, maxResumeRetries, requestOptions, columnsMetadata, } = request;
371
377
  const keySet = Snapshot.encodeKeySet(request);
372
378
  const transaction = {};
@@ -405,13 +411,16 @@ class Snapshot extends events_1.EventEmitter {
405
411
  (0, common_1.addLeaderAwareRoutingHeader)(headers);
406
412
  }
407
413
  const traceConfig = {
414
+ ...this._traceConfig,
408
415
  tableName: table,
409
- opts: this._observabilityOptions,
410
- dbName: this._dbName,
416
+ transactionTag: (_c = this.requestOptions) === null || _c === void 0 ? void 0 : _c.transactionTag,
417
+ requestTag: requestOptions === null || requestOptions === void 0 ? void 0 : requestOptions.requestTag,
411
418
  };
412
419
  return (0, instrument_2.startTrace)('Snapshot.createReadStream', traceConfig, span => {
413
420
  var _a;
414
421
  let attempt = 0;
422
+ const database = this.session.parent;
423
+ const nthRequest = (0, request_id_header_1.nextNthRequest)(database);
415
424
  const makeRequest = (resumeToken) => {
416
425
  if (this.id && transaction.begin) {
417
426
  delete transaction.begin;
@@ -437,7 +446,7 @@ class Snapshot extends events_1.EventEmitter {
437
446
  method: 'streamingRead',
438
447
  reqOpts: Object.assign({}, reqOpts, { resumeToken }),
439
448
  gaxOpts: gaxOptions,
440
- headers: headers,
449
+ headers: (0, request_id_header_1.injectRequestIDIntoHeaders)(headers, this.session, nthRequest, attempt),
441
450
  });
442
451
  };
443
452
  const resultStream = (_a = (0, partial_result_stream_1.partialResultStream)(this._wrapWithIdWaiter(makeRequest), {
@@ -547,12 +556,10 @@ class Snapshot extends events_1.EventEmitter {
547
556
  request = requestOrCallback;
548
557
  callback = cb;
549
558
  }
550
- const traceConfig = {
559
+ return (0, instrument_2.startTrace)('Snapshot.read', {
551
560
  tableName: table,
552
- opts: this._observabilityOptions,
553
- dbName: this._dbName,
554
- };
555
- return (0, instrument_2.startTrace)('Snapshot.read', traceConfig, span => {
561
+ ...this._traceConfig,
562
+ }, span => {
556
563
  this.createReadStream(table, request)
557
564
  .on('error', err => {
558
565
  const e = err;
@@ -571,12 +578,10 @@ class Snapshot extends events_1.EventEmitter {
571
578
  const rows = [];
572
579
  let stats;
573
580
  let metadata;
574
- const traceConfig = {
575
- sql: query,
576
- opts: this._observabilityOptions,
577
- dbName: this._dbName,
578
- };
579
- (0, instrument_2.startTrace)('Snapshot.run', traceConfig, span => {
581
+ (0, instrument_2.startTrace)('Snapshot.run', {
582
+ ...query,
583
+ ...this._traceConfig,
584
+ }, span => {
580
585
  return this.runStream(query)
581
586
  .on('error', (err, rows, stats, metadata) => {
582
587
  (0, instrument_2.setSpanError)(span, err);
@@ -699,6 +704,7 @@ class Snapshot extends events_1.EventEmitter {
699
704
  * ```
700
705
  */
701
706
  runStream(query) {
707
+ var _a;
702
708
  if (typeof query === 'string') {
703
709
  query = { sql: query };
704
710
  }
@@ -746,12 +752,15 @@ class Snapshot extends events_1.EventEmitter {
746
752
  (0, common_1.addLeaderAwareRoutingHeader)(headers);
747
753
  }
748
754
  const traceConfig = {
749
- opts: this._observabilityOptions,
750
- dbName: this._dbName,
755
+ transactionTag: (_a = this.requestOptions) === null || _a === void 0 ? void 0 : _a.transactionTag,
756
+ requestTag: requestOptions === null || requestOptions === void 0 ? void 0 : requestOptions.requestTag,
751
757
  ...query,
758
+ ...this._traceConfig,
752
759
  };
753
760
  return (0, instrument_2.startTrace)('Snapshot.runStream', traceConfig, span => {
754
761
  let attempt = 0;
762
+ const database = this.session.parent;
763
+ const nthRequest = (0, request_id_header_1.nextNthRequest)(database);
755
764
  const makeRequest = (resumeToken) => {
756
765
  attempt++;
757
766
  if (!resumeToken) {
@@ -785,7 +794,7 @@ class Snapshot extends events_1.EventEmitter {
785
794
  method: 'executeStreamingSql',
786
795
  reqOpts: Object.assign({}, reqOpts, { resumeToken }),
787
796
  gaxOpts: gaxOptions,
788
- headers: headers,
797
+ headers: (0, request_id_header_1.injectRequestIDIntoHeaders)(headers, this.session, nthRequest, attempt),
789
798
  });
790
799
  };
791
800
  const resultStream = (0, partial_result_stream_1.partialResultStream)(this._wrapWithIdWaiter(makeRequest), {
@@ -1034,15 +1043,16 @@ exports.Snapshot = Snapshot;
1034
1043
  */
1035
1044
  class Dml extends Snapshot {
1036
1045
  runUpdate(query, callback) {
1046
+ var _a, _b;
1037
1047
  if (typeof query === 'string') {
1038
1048
  query = { sql: query };
1039
1049
  }
1040
- const traceConfig = {
1041
- opts: this._observabilityOptions,
1042
- dbName: this._dbName,
1050
+ return (0, instrument_2.startTrace)('Dml.runUpdate', {
1043
1051
  ...query,
1044
- };
1045
- return (0, instrument_2.startTrace)('Dml.runUpdate', traceConfig, span => {
1052
+ ...this._traceConfig,
1053
+ transactionTag: (_a = this.requestOptions) === null || _a === void 0 ? void 0 : _a.transactionTag,
1054
+ requestTag: (_b = query.requestOptions) === null || _b === void 0 ? void 0 : _b.requestTag,
1055
+ }, span => {
1046
1056
  this.run(query, (err, rows, stats) => {
1047
1057
  let rowCount = 0;
1048
1058
  if (stats && stats.rowCount) {
@@ -1154,10 +1164,11 @@ class Transaction extends Dml {
1154
1164
  super(session, undefined, queryOptions);
1155
1165
  this._queuedMutations = [];
1156
1166
  this._options = { readWrite: options };
1167
+ this._options.isolationLevel = IsolationLevel.ISOLATION_LEVEL_UNSPECIFIED;
1157
1168
  this.requestOptions = requestOptions;
1158
1169
  }
1159
1170
  batchUpdate(queries, optionsOrCallback, cb) {
1160
- var _a, _b;
1171
+ var _a, _b, _c;
1161
1172
  const options = typeof optionsOrCallback === 'object' ? optionsOrCallback : {};
1162
1173
  const callback = typeof optionsOrCallback === 'function' ? optionsOrCallback : cb;
1163
1174
  const gaxOpts = 'gaxOptions' in options
@@ -1188,20 +1199,23 @@ class Transaction extends Dml {
1188
1199
  else {
1189
1200
  transaction.begin = this._options;
1190
1201
  }
1202
+ const requestOptionsWithTag = this.configureTagOptions(false, (_b = (_a = this.requestOptions) === null || _a === void 0 ? void 0 : _a.transactionTag) !== null && _b !== void 0 ? _b : undefined, options.requestOptions);
1191
1203
  const reqOpts = {
1192
1204
  session: this.session.formattedName_,
1193
- requestOptions: this.configureTagOptions(false, (_b = (_a = this.requestOptions) === null || _a === void 0 ? void 0 : _a.transactionTag) !== null && _b !== void 0 ? _b : undefined, options.requestOptions),
1205
+ requestOptions: requestOptionsWithTag,
1194
1206
  transaction,
1195
1207
  seqno: this._seqno++,
1196
1208
  statements,
1197
1209
  };
1198
- const headers = this.commonHeaders_;
1210
+ const database = this.session.parent;
1211
+ const headers = (0, request_id_header_1.injectRequestIDIntoHeaders)(this.commonHeaders_, this.session, (0, request_id_header_1.nextNthRequest)(database), 1);
1199
1212
  if (this._getSpanner().routeToLeaderEnabled) {
1200
1213
  (0, common_1.addLeaderAwareRoutingHeader)(headers);
1201
1214
  }
1202
1215
  const traceConfig = {
1203
- opts: this._observabilityOptions,
1204
- dbName: this._dbName,
1216
+ ...this._traceConfig,
1217
+ transactionTag: requestOptionsWithTag === null || requestOptionsWithTag === void 0 ? void 0 : requestOptionsWithTag.transactionTag,
1218
+ requestTag: (_c = options === null || options === void 0 ? void 0 : options.requestOptions) === null || _c === void 0 ? void 0 : _c.requestTag,
1205
1219
  };
1206
1220
  return (0, instrument_2.startTrace)('Transaction.batchUpdate', traceConfig, span => {
1207
1221
  this.request({
@@ -1269,6 +1283,7 @@ class Transaction extends Dml {
1269
1283
  this._queuedMutations = mutation;
1270
1284
  }
1271
1285
  commit(optionsOrCallback, cb) {
1286
+ var _a;
1272
1287
  const options = typeof optionsOrCallback === 'object' ? optionsOrCallback : {};
1273
1288
  const callback = typeof optionsOrCallback === 'function' ? optionsOrCallback : cb;
1274
1289
  const gaxOpts = 'gaxOptions' in options ? options.gaxOptions : options;
@@ -1276,11 +1291,10 @@ class Transaction extends Dml {
1276
1291
  const session = this.session.formattedName_;
1277
1292
  const requestOptions = options.requestOptions;
1278
1293
  const reqOpts = { mutations, session, requestOptions };
1279
- const traceConfig = {
1280
- opts: this._observabilityOptions,
1281
- dbName: this._dbName,
1282
- };
1283
- return (0, instrument_2.startTrace)('Transaction.commit', traceConfig, span => {
1294
+ return (0, instrument_2.startTrace)('Transaction.commit', {
1295
+ transactionTag: (_a = this.requestOptions) === null || _a === void 0 ? void 0 : _a.transactionTag,
1296
+ ...this._traceConfig,
1297
+ }, span => {
1284
1298
  if (this.id) {
1285
1299
  reqOpts.transactionId = this.id;
1286
1300
  }
@@ -1317,12 +1331,13 @@ class Transaction extends Dml {
1317
1331
  (0, common_1.addLeaderAwareRoutingHeader)(headers);
1318
1332
  }
1319
1333
  span.addEvent('Starting Commit');
1334
+ const database = this.session.parent;
1320
1335
  this.request({
1321
1336
  client: 'SpannerClient',
1322
1337
  method: 'commit',
1323
1338
  reqOpts,
1324
1339
  gaxOpts: gaxOpts,
1325
- headers: headers,
1340
+ headers: (0, request_id_header_1.injectRequestIDIntoHeaders)(headers, this.session, (0, request_id_header_1.nextNthRequest)(database), 1),
1326
1341
  }, (err, resp) => {
1327
1342
  this.end();
1328
1343
  if (err) {
@@ -1567,11 +1582,7 @@ class Transaction extends Dml {
1567
1582
  rollback(gaxOptionsOrCallback, cb) {
1568
1583
  const gaxOpts = typeof gaxOptionsOrCallback === 'object' ? gaxOptionsOrCallback : {};
1569
1584
  const callback = typeof gaxOptionsOrCallback === 'function' ? gaxOptionsOrCallback : cb;
1570
- const traceConfig = {
1571
- opts: this._observabilityOptions,
1572
- dbName: this._dbName,
1573
- };
1574
- return (0, instrument_2.startTrace)('Transaction.rollback', traceConfig, span => {
1585
+ return (0, instrument_2.startTrace)('Transaction.rollback', this._traceConfig, span => {
1575
1586
  if (!this.id) {
1576
1587
  span.addEvent('Transaction ID is unknown, nothing to rollback.');
1577
1588
  span.end();
@@ -1736,6 +1747,27 @@ class Transaction extends Dml {
1736
1747
  excludeTxnFromChangeStreams() {
1737
1748
  this._options.excludeTxnFromChangeStreams = true;
1738
1749
  }
1750
+ setReadWriteTransactionOptions(options) {
1751
+ /**
1752
+ * Set optimistic concurrency control for the transaction.
1753
+ */
1754
+ if (options === null || options === void 0 ? void 0 : options.optimisticLock) {
1755
+ this._options.readWrite.readLockMode = ReadLockMode.OPTIMISTIC;
1756
+ }
1757
+ /**
1758
+ * Set option excludeTxnFromChangeStreams=true to exclude read/write transactions
1759
+ * from being tracked in change streams.
1760
+ */
1761
+ if (options === null || options === void 0 ? void 0 : options.excludeTxnFromChangeStreams) {
1762
+ this._options.excludeTxnFromChangeStreams = true;
1763
+ }
1764
+ /**
1765
+ * Set isolation level .
1766
+ */
1767
+ this._options.isolationLevel = (options === null || options === void 0 ? void 0 : options.isolationLevel)
1768
+ ? options === null || options === void 0 ? void 0 : options.isolationLevel
1769
+ : this._getSpanner().defaultTransactionOptions.isolationLevel;
1770
+ }
1739
1771
  }
1740
1772
  exports.Transaction = Transaction;
1741
1773
  /*! Developer Documentation
@@ -1971,12 +2003,10 @@ class PartitionedDml extends Dml {
1971
2003
  this._options.excludeTxnFromChangeStreams = true;
1972
2004
  }
1973
2005
  runUpdate(query, callback) {
1974
- const traceConfig = {
1975
- sql: query,
1976
- opts: this._observabilityOptions,
1977
- dbName: this._dbName,
1978
- };
1979
- return (0, instrument_2.startTrace)('PartitionedDml.runUpdate', traceConfig, span => {
2006
+ return (0, instrument_2.startTrace)('PartitionedDml.runUpdate', {
2007
+ ...query,
2008
+ ...this._traceConfig,
2009
+ }, span => {
1980
2010
  super.runUpdate(query, (err, count) => {
1981
2011
  if (err) {
1982
2012
  (0, instrument_2.setSpanError)(span, err);
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@google-cloud/spanner",
3
3
  "description": "Cloud Spanner Client Library for Node.js",
4
- "version": "7.19.1",
4
+ "version": "7.21.0",
5
5
  "license": "Apache-2.0",
6
6
  "author": "Google Inc.",
7
7
  "engines": {