Skip to content

Commit edc3804

Browse files
committed
observability: trace BatchTransaction and Table
This change is part of a series of changes to add OpenTelemetry traces, focused on BatchTransaction and Table. Updates googleapis#2079 Built from PR googleapis#2087 Updates googleapis#2114
1 parent 3300ab5 commit edc3804

File tree

2 files changed

+111
-79
lines changed

2 files changed

+111
-79
lines changed

src/batch-transaction.ts

+85-59
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import {
2525
CLOUD_RESOURCE_HEADER,
2626
addLeaderAwareRoutingHeader,
2727
} from '../src/common';
28+
import {startTrace, setSpanError} from './instrument';
2829

2930
export interface TransactionIdentifier {
3031
session: string | Session;
@@ -136,21 +137,30 @@ class BatchTransaction extends Snapshot {
136137
delete reqOpts.gaxOptions;
137138
delete reqOpts.types;
138139

139-
const headers: {[k: string]: string} = {};
140-
if (this._getSpanner().routeToLeaderEnabled) {
141-
addLeaderAwareRoutingHeader(headers);
142-
}
140+
return startTrace('BatchTransaction.createQueryPartitions', query, span => {
141+
const headers: {[k: string]: string} = {};
142+
if (this._getSpanner().routeToLeaderEnabled) {
143+
addLeaderAwareRoutingHeader(headers);
144+
}
145+
146+
this.createPartitions_(
147+
{
148+
client: 'SpannerClient',
149+
method: 'partitionQuery',
150+
reqOpts,
151+
gaxOpts: query.gaxOptions,
152+
headers: headers,
153+
},
154+
(err, partitions, resp) => {
155+
if (err) {
156+
setSpanError(span, err);
157+
}
143158

144-
this.createPartitions_(
145-
{
146-
client: 'SpannerClient',
147-
method: 'partitionQuery',
148-
reqOpts,
149-
gaxOpts: query.gaxOptions,
150-
headers: headers,
151-
},
152-
callback
153-
);
159+
span.end();
160+
callback(err, partitions, resp);
161+
}
162+
);
163+
});
154164
}
155165
/**
156166
* Generic create partition method. Handles common parameters used in both
@@ -163,37 +173,43 @@ class BatchTransaction extends Snapshot {
163173
* @param {function} callback Callback function.
164174
*/
165175
createPartitions_(config, callback) {
166-
const query = extend({}, config.reqOpts, {
167-
session: this.session.formattedName_,
168-
transaction: {id: this.id},
169-
});
170-
config.reqOpts = extend({}, query);
171-
config.headers = {
172-
[CLOUD_RESOURCE_HEADER]: (this.session.parent as Database).formattedName_,
173-
};
174-
delete query.partitionOptions;
175-
this.session.request(config, (err, resp) => {
176-
if (err) {
177-
callback(err, null, resp);
178-
return;
179-
}
180-
181-
const partitions = resp.partitions.map(partition => {
182-
return extend({}, query, partition);
176+
return startTrace('BatchTransaction.createPartitions', {}, span => {
177+
const query = extend({}, config.reqOpts, {
178+
session: this.session.formattedName_,
179+
transaction: {id: this.id},
183180
});
181+
config.reqOpts = extend({}, query);
182+
config.headers = {
183+
[CLOUD_RESOURCE_HEADER]: (this.session.parent as Database)
184+
.formattedName_,
185+
};
186+
delete query.partitionOptions;
187+
this.session.request(config, (err, resp) => {
188+
if (err) {
189+
setSpanError(span, err);
190+
span.end();
191+
callback(err, null, resp);
192+
return;
193+
}
184194

185-
if (resp.transaction) {
186-
const {id, readTimestamp} = resp.transaction;
195+
const partitions = resp.partitions.map(partition => {
196+
return extend({}, query, partition);
197+
});
187198

188-
this.id = id;
199+
if (resp.transaction) {
200+
const {id, readTimestamp} = resp.transaction;
189201

190-
if (readTimestamp) {
191-
this.readTimestampProto = readTimestamp;
192-
this.readTimestamp = new PreciseDate(readTimestamp);
202+
this.id = id;
203+
204+
if (readTimestamp) {
205+
this.readTimestampProto = readTimestamp;
206+
this.readTimestamp = new PreciseDate(readTimestamp);
207+
}
193208
}
194-
}
195209

196-
callback(null, partitions, resp);
210+
span.end();
211+
callback(null, partitions, resp);
212+
});
197213
});
198214
}
199215
/**
@@ -226,29 +242,38 @@ class BatchTransaction extends Snapshot {
226242
* @returns {Promise<CreateReadPartitionsResponse>}
227243
*/
228244
createReadPartitions(options, callback) {
229-
const reqOpts = Object.assign({}, options, {
230-
keySet: Snapshot.encodeKeySet(options),
231-
});
245+
return startTrace('BatchTransaction.createReadPartitions', {}, span => {
246+
const reqOpts = Object.assign({}, options, {
247+
keySet: Snapshot.encodeKeySet(options),
248+
});
232249

233-
delete reqOpts.gaxOptions;
234-
delete reqOpts.keys;
235-
delete reqOpts.ranges;
250+
delete reqOpts.gaxOptions;
251+
delete reqOpts.keys;
252+
delete reqOpts.ranges;
236253

237-
const headers: {[k: string]: string} = {};
238-
if (this._getSpanner().routeToLeaderEnabled) {
239-
addLeaderAwareRoutingHeader(headers);
240-
}
254+
const headers: {[k: string]: string} = {};
255+
if (this._getSpanner().routeToLeaderEnabled) {
256+
addLeaderAwareRoutingHeader(headers);
257+
}
258+
259+
this.createPartitions_(
260+
{
261+
client: 'SpannerClient',
262+
method: 'partitionRead',
263+
reqOpts,
264+
gaxOpts: options.gaxOptions,
265+
headers: headers,
266+
},
267+
(err, partitions, resp) => {
268+
if (err) {
269+
setSpanError(span, err);
270+
}
241271

242-
this.createPartitions_(
243-
{
244-
client: 'SpannerClient',
245-
method: 'partitionRead',
246-
reqOpts,
247-
gaxOpts: options.gaxOptions,
248-
headers: headers,
249-
},
250-
callback
251-
);
272+
span.end();
273+
callback(err, partitions, resp);
274+
}
275+
);
276+
});
252277
}
253278
/**
254279
* Executes partition.
@@ -322,6 +347,7 @@ class BatchTransaction extends Snapshot {
322347
* ```
323348
*/
324349
executeStream(partition) {
350+
// TODO: Instrument the streams with Otel.
325351
if (is.string(partition.table)) {
326352
return this.createReadStream(partition.table, partition);
327353
}

src/table.ts

+26-20
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import {
3131
import {google as databaseAdmin} from '../protos/protos';
3232
import {Schema, LongRunningCallback} from './common';
3333
import IRequestOptions = databaseAdmin.spanner.v1.IRequestOptions;
34+
import {startTrace, setSpanError} from './instrument';
3435

3536
export type Key = string | string[];
3637

@@ -1072,29 +1073,34 @@ class Table {
10721073
options: MutateRowsOptions | CallOptions = {},
10731074
callback: CommitCallback
10741075
): void {
1075-
const requestOptions =
1076-
'requestOptions' in options ? options.requestOptions : {};
1076+
startTrace('Table.' + method, {}, span => {
1077+
const requestOptions =
1078+
'requestOptions' in options ? options.requestOptions : {};
10771079

1078-
const excludeTxnFromChangeStreams =
1079-
'excludeTxnFromChangeStreams' in options
1080-
? options.excludeTxnFromChangeStreams
1081-
: false;
1080+
const excludeTxnFromChangeStreams =
1081+
'excludeTxnFromChangeStreams' in options
1082+
? options.excludeTxnFromChangeStreams
1083+
: false;
10821084

1083-
this.database.runTransaction(
1084-
{
1085-
requestOptions: requestOptions,
1086-
excludeTxnFromChangeStreams: excludeTxnFromChangeStreams,
1087-
},
1088-
(err, transaction) => {
1089-
if (err) {
1090-
callback(err);
1091-
return;
1092-
}
1085+
this.database.runTransaction(
1086+
{
1087+
requestOptions: requestOptions,
1088+
excludeTxnFromChangeStreams: excludeTxnFromChangeStreams,
1089+
},
1090+
(err, transaction) => {
1091+
if (err) {
1092+
setSpanError(span, err);
1093+
span.end();
1094+
callback(err);
1095+
return;
1096+
}
10931097

1094-
transaction![method](this.name, rows as Key[]);
1095-
transaction!.commit(options, callback);
1096-
}
1097-
);
1098+
span.end();
1099+
transaction![method](this.name, rows as Key[]);
1100+
transaction!.commit(options, callback);
1101+
}
1102+
);
1103+
});
10981104
}
10991105
}
11001106

0 commit comments

Comments
 (0)