@google-cloud/spanner 7.14.0 → 7.16.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.
@@ -22,6 +22,7 @@ const extend = require("extend");
22
22
  const is = require("is");
23
23
  const transaction_1 = require("./transaction");
24
24
  const common_1 = require("../src/common");
25
+ const instrument_1 = require("./instrument");
25
26
  /**
26
27
  * Use a BatchTransaction object to create partitions and read/query against
27
28
  * your Cloud Spanner database.
@@ -123,17 +124,33 @@ class BatchTransaction extends transaction_1.Snapshot {
123
124
  const reqOpts = Object.assign({}, query, transaction_1.Snapshot.encodeParams(query));
124
125
  delete reqOpts.gaxOptions;
125
126
  delete reqOpts.types;
126
- const headers = {};
127
- if (this._getSpanner().routeToLeaderEnabled) {
128
- (0, common_1.addLeaderAwareRoutingHeader)(headers);
129
- }
130
- this.createPartitions_({
131
- client: 'SpannerClient',
132
- method: 'partitionQuery',
133
- reqOpts,
134
- gaxOpts: query.gaxOptions,
135
- headers: headers,
136
- }, callback);
127
+ const traceConfig = {
128
+ sql: query,
129
+ opts: this._observabilityOptions,
130
+ dbName: this.getDBName(),
131
+ };
132
+ return (0, instrument_1.startTrace)('BatchTransaction.createQueryPartitions', traceConfig, span => {
133
+ const headers = {};
134
+ if (this._getSpanner().routeToLeaderEnabled) {
135
+ (0, common_1.addLeaderAwareRoutingHeader)(headers);
136
+ }
137
+ this.createPartitions_({
138
+ client: 'SpannerClient',
139
+ method: 'partitionQuery',
140
+ reqOpts,
141
+ gaxOpts: query.gaxOptions,
142
+ headers: headers,
143
+ }, (err, partitions, resp) => {
144
+ if (err) {
145
+ (0, instrument_1.setSpanError)(span, err);
146
+ }
147
+ span.end();
148
+ callback(err, partitions, resp);
149
+ });
150
+ });
151
+ }
152
+ getDBName() {
153
+ return this.session.parent.formattedName_;
137
154
  }
138
155
  /**
139
156
  * Generic create partition method. Handles common parameters used in both
@@ -146,32 +163,42 @@ class BatchTransaction extends transaction_1.Snapshot {
146
163
  * @param {function} callback Callback function.
147
164
  */
148
165
  createPartitions_(config, callback) {
149
- const query = extend({}, config.reqOpts, {
150
- session: this.session.formattedName_,
151
- transaction: { id: this.id },
152
- });
153
- config.reqOpts = extend({}, query);
154
- config.headers = {
155
- [common_1.CLOUD_RESOURCE_HEADER]: this.session.parent.formattedName_,
166
+ const traceConfig = {
167
+ opts: this._observabilityOptions,
168
+ dbName: this.getDBName(),
156
169
  };
157
- delete query.partitionOptions;
158
- this.session.request(config, (err, resp) => {
159
- if (err) {
160
- callback(err, null, resp);
161
- return;
162
- }
163
- const partitions = resp.partitions.map(partition => {
164
- return extend({}, query, partition);
170
+ return (0, instrument_1.startTrace)('BatchTransaction.createPartitions_', traceConfig, span => {
171
+ const query = extend({}, config.reqOpts, {
172
+ session: this.session.formattedName_,
173
+ transaction: { id: this.id },
165
174
  });
166
- if (resp.transaction) {
167
- const { id, readTimestamp } = resp.transaction;
168
- this.id = id;
169
- if (readTimestamp) {
170
- this.readTimestampProto = readTimestamp;
171
- this.readTimestamp = new precise_date_1.PreciseDate(readTimestamp);
175
+ config.reqOpts = extend({}, query);
176
+ config.headers = {
177
+ [common_1.CLOUD_RESOURCE_HEADER]: this.session.parent
178
+ .formattedName_,
179
+ };
180
+ delete query.partitionOptions;
181
+ this.session.request(config, (err, resp) => {
182
+ if (err) {
183
+ (0, instrument_1.setSpanError)(span, err);
184
+ span.end();
185
+ callback(err, null, resp);
186
+ return;
172
187
  }
173
- }
174
- callback(null, partitions, resp);
188
+ const partitions = resp.partitions.map(partition => {
189
+ return extend({}, query, partition);
190
+ });
191
+ if (resp.transaction) {
192
+ const { id, readTimestamp } = resp.transaction;
193
+ this.id = id;
194
+ if (readTimestamp) {
195
+ this.readTimestampProto = readTimestamp;
196
+ this.readTimestamp = new precise_date_1.PreciseDate(readTimestamp);
197
+ }
198
+ }
199
+ span.end();
200
+ callback(null, partitions, resp);
201
+ });
175
202
  });
176
203
  }
177
204
  /**
@@ -204,23 +231,35 @@ class BatchTransaction extends transaction_1.Snapshot {
204
231
  * @returns {Promise<CreateReadPartitionsResponse>}
205
232
  */
206
233
  createReadPartitions(options, callback) {
207
- const reqOpts = Object.assign({}, options, {
208
- keySet: transaction_1.Snapshot.encodeKeySet(options),
234
+ const traceConfig = {
235
+ opts: this._observabilityOptions,
236
+ dbName: this.getDBName(),
237
+ };
238
+ return (0, instrument_1.startTrace)('BatchTransaction.createReadPartitions', traceConfig, span => {
239
+ const reqOpts = Object.assign({}, options, {
240
+ keySet: transaction_1.Snapshot.encodeKeySet(options),
241
+ });
242
+ delete reqOpts.gaxOptions;
243
+ delete reqOpts.keys;
244
+ delete reqOpts.ranges;
245
+ const headers = {};
246
+ if (this._getSpanner().routeToLeaderEnabled) {
247
+ (0, common_1.addLeaderAwareRoutingHeader)(headers);
248
+ }
249
+ this.createPartitions_({
250
+ client: 'SpannerClient',
251
+ method: 'partitionRead',
252
+ reqOpts,
253
+ gaxOpts: options.gaxOptions,
254
+ headers: headers,
255
+ }, (err, partitions, resp) => {
256
+ if (err) {
257
+ (0, instrument_1.setSpanError)(span, err);
258
+ }
259
+ span.end();
260
+ callback(err, partitions, resp);
261
+ });
209
262
  });
210
- delete reqOpts.gaxOptions;
211
- delete reqOpts.keys;
212
- delete reqOpts.ranges;
213
- const headers = {};
214
- if (this._getSpanner().routeToLeaderEnabled) {
215
- (0, common_1.addLeaderAwareRoutingHeader)(headers);
216
- }
217
- this.createPartitions_({
218
- client: 'SpannerClient',
219
- method: 'partitionRead',
220
- reqOpts,
221
- gaxOpts: options.gaxOptions,
222
- headers: headers,
223
- }, callback);
224
263
  }
225
264
  /**
226
265
  * Executes partition.
@@ -294,6 +333,7 @@ class BatchTransaction extends transaction_1.Snapshot {
294
333
  * ```
295
334
  */
296
335
  executeStream(partition) {
336
+ // TODO: Instrument the streams with Otel.
297
337
  if (is.string(partition.table)) {
298
338
  return this.createReadStream(partition.table, partition);
299
339
  }
@@ -33,6 +33,7 @@ import IPolicy = google.iam.v1.IPolicy;
33
33
  import Policy = google.iam.v1.Policy;
34
34
  import FieldMask = google.protobuf.FieldMask;
35
35
  import IDatabase = google.spanner.admin.database.v1.IDatabase;
36
+ import { ObservabilityOptions } from './instrument';
36
37
  export type GetDatabaseRolesCallback = RequestCallback<IDatabaseRole, databaseAdmin.spanner.admin.database.v1.IListDatabaseRolesResponse>;
37
38
  export type GetDatabaseRolesResponse = PagedResponse<IDatabaseRole, databaseAdmin.spanner.admin.database.v1.IListDatabaseRolesResponse>;
38
39
  type SetDatabaseMetadataCallback = ResourceCallback<GaxOperation, IOperation>;
@@ -103,6 +104,7 @@ export interface CreateSessionOptions {
103
104
  } | null;
104
105
  databaseRole?: string | null;
105
106
  gaxOptions?: CallOptions;
107
+ multiplexed?: boolean;
106
108
  }
107
109
  export interface GetIamPolicyOptions {
108
110
  requestedPolicyVersion?: number | null;
@@ -175,6 +177,8 @@ declare class Database extends common.GrpcServiceObject {
175
177
  request: DatabaseRequest;
176
178
  databaseRole?: string | null;
177
179
  databaseDialect?: EnumKey<typeof databaseAdmin.spanner.admin.database.v1.DatabaseDialect> | null;
180
+ _observabilityOptions?: ObservabilityOptions;
181
+ private _traceConfig;
178
182
  constructor(instance: Instance, name: string, poolOptions?: SessionPoolConstructor | SessionPoolOptions, queryOptions?: spannerClient.spanner.v1.ExecuteSqlRequest.IQueryOptions);
179
183
  /**
180
184
  * @typedef {array} SetDatabaseMetadataResponse
@@ -1861,7 +1865,7 @@ declare class Database extends common.GrpcServiceObject {
1861
1865
  *
1862
1866
  * @example
1863
1867
  * ```
1864
- * const {Spanner} = require('@google-cloud/spanner');
1868
+ * const {Spanner, MutationSet} = require('@google-cloud/spanner');
1865
1869
  * const spanner = new Spanner();
1866
1870
  *
1867
1871
  * const instance = spanner.instance('my-instance');
@@ -1879,7 +1883,7 @@ declare class Database extends common.GrpcServiceObject {
1879
1883
  * });
1880
1884
  *
1881
1885
  * try {
1882
- * const [response, err] = await database.writeAtLeastOnce(mutations, {});
1886
+ * const [response] = await database.writeAtLeastOnce(mutations, {});
1883
1887
  * console.log(response.commitTimestamp);
1884
1888
  * } catch(err) {
1885
1889
  * console.log("Error: ", err);