Skip to content

Commit 5f6b4d0

Browse files
committed
Address some review feedback
1 parent 9949a4d commit 5f6b4d0

File tree

2 files changed

+25
-22
lines changed

2 files changed

+25
-22
lines changed

src/instrument.ts

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,6 @@ interface traceConfig {
8989
tableName?: string;
9090
dbName?: string;
9191
opts?: ObservabilityOptions;
92-
that?: Object;
9392
}
9493

9594
const SPAN_NAMESPACE_PREFIX = 'CloudSpanner'; // TODO: discuss & standardize this prefix.
@@ -167,12 +166,7 @@ export function startTrace<T>(
167166
// If at all the invoked function throws an exception,
168167
// record the exception and then end this span.
169168
try {
170-
if (config.that) {
171-
const fn = cb.bind(config.that);
172-
return fn(span);
173-
} else {
174-
return cb(span);
175-
}
169+
return cb(span);
176170
} catch (e) {
177171
setSpanErrorAndException(span, e as Error);
178172
span.end();

src/transaction.ts

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -466,9 +466,6 @@ export class Snapshot extends EventEmitter {
466466
this._update(resp);
467467
}
468468

469-
// begin simply issues an RPC to signal a state change to the backend,
470-
// and doesn't invoke any other subsequent code hence it is paramount
471-
// to invoke span.end() before returning results to the callback.
472469
span.end();
473470
callback!(err, resp);
474471
}
@@ -723,15 +720,21 @@ export class Snapshot extends EventEmitter {
723720
transaction.id = this.id;
724721
}
725722

726-
if (resumeToken) {
723+
attempt++;
724+
725+
if (!resumeToken) {
726+
if (attempt === 1) {
727+
span.addEvent('Starting stream');
728+
} else {
729+
span.addEvent('Re-attempting start stream', {attempt: attempt});
730+
}
731+
} else {
727732
span.addEvent('Resuming stream', {
728733
resume_token: resumeToken!.toString(),
729734
attempt: attempt,
730735
});
731736
}
732737

733-
attempt++;
734-
735738
return this.requestStream({
736739
client: 'SpannerClient',
737740
method: 'streamingRead',
@@ -756,13 +759,15 @@ export class Snapshot extends EventEmitter {
756759
this._update(response.metadata!.transaction);
757760
}
758761
})
759-
.on('error', async err => {
762+
.on('error', err => {
760763
setSpanError(span, err);
761764
const wasAborted = isErrorAborted(err);
762765
if (!this.id && this._useInRunner && !wasAborted) {
763-
// It is paramount for us to await this invocation to
764-
// .begin() to complete before we invoke span.end();
765-
await this.begin();
766+
// TODO: re-examine if this.begin() should still exist and if
767+
// an await is needed: with await the generated begin span
768+
// will look wonky and out of order. Please ses
769+
// https://github.com/googleapis/nodejs-spanner/issues/2170
770+
this.begin();
766771
} else {
767772
if (wasAborted) {
768773
span.addEvent('Stream broken. Not safe to retry', {
@@ -1297,8 +1302,10 @@ export class Snapshot extends EventEmitter {
12971302
return startTrace('Snapshot.runStream', traceConfig, span => {
12981303
let attempt = 0;
12991304
const makeRequest = (resumeToken?: ResumeToken): Readable => {
1305+
attempt++;
1306+
13001307
if (!resumeToken) {
1301-
if (attempt === 0) {
1308+
if (attempt === 1) {
13021309
span.addEvent('Starting stream');
13031310
} else {
13041311
span.addEvent('Re-attempting start stream', {attempt: attempt});
@@ -1322,8 +1329,6 @@ export class Snapshot extends EventEmitter {
13221329
}
13231330
}
13241331

1325-
attempt++;
1326-
13271332
return this.requestStream({
13281333
client: 'SpannerClient',
13291334
method: 'executeStreamingSql',
@@ -1348,12 +1353,15 @@ export class Snapshot extends EventEmitter {
13481353
this._update(response.metadata!.transaction);
13491354
}
13501355
})
1351-
.on('error', async err => {
1356+
.on('error', err => {
13521357
setSpanError(span, err as Error);
13531358
const wasAborted = isErrorAborted(err);
13541359
if (!this.id && this._useInRunner && !wasAborted) {
13551360
span.addEvent('Stream broken. Safe to retry');
1356-
await this.begin();
1361+
// TODO: Examine the consequence of not awaiting this method,
1362+
// as the span might appear out of order.
1363+
// Please see https://github.com/googleapis/nodejs-spanner/issues/2170
1364+
this.begin();
13571365
} else {
13581366
if (wasAborted) {
13591367
span.addEvent('Stream broken. Not safe to retry', {
@@ -1378,6 +1386,7 @@ export class Snapshot extends EventEmitter {
13781386
span.end();
13791387
});
13801388
}
1389+
13811390
return resultStream;
13821391
});
13831392
}

0 commit comments

Comments
 (0)