Skip to content

Commit baf14ac

Browse files
committed
Address feedback from code review
1 parent 5f207d4 commit baf14ac

File tree

4 files changed

+33
-74
lines changed

4 files changed

+33
-74
lines changed

observability-test/database.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -641,7 +641,7 @@ describe('Database', () => {
641641
);
642642

643643
// Ensure that the first span actually produced an error that was recorded.
644-
const parentSpan = spans[1];
644+
const parentSpan = spans[0];
645645
assert.strictEqual(
646646
SpanStatusCode.ERROR,
647647
parentSpan.status.code,
@@ -654,7 +654,7 @@ describe('Database', () => {
654654
);
655655

656656
// Ensure that the second span is a child of the first span.
657-
const secondRetrySpan = spans[0];
657+
const secondRetrySpan = spans[1];
658658
assert.ok(
659659
parentSpan.spanContext().traceId,
660660
'Expected that the initial parent span has a defined traceId'

observability-test/spanner.ts

+1
Original file line numberDiff line numberDiff line change
@@ -459,6 +459,7 @@ describe('EndToEnd', () => {
459459
assert.ifError(err);
460460
await transaction!.run('SELECT 1');
461461
await transaction!.commit();
462+
await transaction!.end();
462463
await traceExporter.forceFlush();
463464

464465
const spans = traceExporter.getFinishedSpans();

src/database.ts

+28-65
Original file line numberDiff line numberDiff line change
@@ -1131,9 +1131,7 @@ class Database extends common.GrpcServiceObject {
11311131
setSpanErrorAndException(span, e as Error);
11321132
this.emit('error', e);
11331133
} finally {
1134-
if (span.isRecording()) {
1135-
span.end();
1136-
}
1134+
span.end();
11371135
}
11381136
});
11391137
}
@@ -2104,11 +2102,8 @@ class Database extends common.GrpcServiceObject {
21042102
});
21052103
session!.lastError = err;
21062104
this.pool_.release(session!);
2107-
this.getSnapshot(options, callback!);
2108-
// Explicitly requested in code review that this span.end() be
2109-
// moved out of this.getSnapshot, and that there will a later refactor,
2110-
// similar to https://github.com/googleapis/nodejs-spanner/issues/2159
21112105
span.end();
2106+
this.getSnapshot(options, callback!);
21122107
} else {
21132108
span.addEvent('Using Session', {'session.id': session?.id});
21142109
this.pool_.release(session!);
@@ -3084,12 +3079,10 @@ class Database extends common.GrpcServiceObject {
30843079
dataStream.removeListener('end', endListener);
30853080
dataStream.end();
30863081
snapshot.end();
3082+
span.end();
30873083
// Create a new data stream and add it to the end user stream.
30883084
dataStream = this.runStream(query, options);
30893085
dataStream.pipe(proxyStream);
3090-
// Explicitly invoking span.end() here,
3091-
// instead of inside dataStream.on('error').
3092-
span.end();
30933086
} else {
30943087
proxyStream.destroy(err);
30953088
snapshot.end();
@@ -3219,26 +3212,6 @@ class Database extends common.GrpcServiceObject {
32193212
? (optionsOrRunFn as RunTransactionOptions)
32203213
: {};
32213214

3222-
const retry = (span: Span) => {
3223-
this.runTransaction(options, (err, txn) => {
3224-
if (err) {
3225-
setSpanError(span, err);
3226-
span.end();
3227-
runFn!(err, null);
3228-
return;
3229-
}
3230-
3231-
txn!.once('end', () => {
3232-
span.end();
3233-
});
3234-
txn!.once('error', err => {
3235-
setSpanError(span, err!);
3236-
span.end();
3237-
});
3238-
runFn!(null, txn!);
3239-
});
3240-
};
3241-
32423215
startTrace('Database.runTransaction', this._traceConfig, span => {
32433216
this.pool_.getSession((err, session?, transaction?) => {
32443217
if (err) {
@@ -3249,7 +3222,8 @@ class Database extends common.GrpcServiceObject {
32493222
span.addEvent('No session available', {
32503223
'session.id': session?.id,
32513224
});
3252-
retry(span);
3225+
span.end();
3226+
this.runTransaction(options, runFn!);
32533227
return;
32543228
}
32553229

@@ -3267,19 +3241,9 @@ class Database extends common.GrpcServiceObject {
32673241
transaction!.excludeTxnFromChangeStreams();
32683242
}
32693243

3270-
// Our span should only be ended if the
3271-
// transaction either errored or was ended.
3272-
transaction!.once('error', err => {
3273-
setSpanError(span, err!);
3274-
span.end();
3275-
});
3276-
3277-
transaction!.once('end', () => {
3278-
span.end();
3279-
});
3280-
32813244
const release = () => {
32823245
this.pool_.release(session!);
3246+
span.end();
32833247
};
32843248

32853249
const runner = new TransactionRunner(
@@ -3289,21 +3253,26 @@ class Database extends common.GrpcServiceObject {
32893253
options
32903254
);
32913255

3292-
runner.run().then(release, err => {
3293-
setSpanError(span, err);
3256+
runner
3257+
.run()
3258+
.then(release, err => {
3259+
setSpanError(span, err);
32943260

3295-
if (isSessionNotFoundError(err)) {
3296-
span.addEvent('No session available', {
3297-
'session.id': session?.id,
3298-
});
3299-
release();
3300-
retry(span);
3301-
} else {
3302-
setImmediate(runFn!, err);
3303-
release();
3304-
span.end();
3305-
}
3306-
});
3261+
if (isSessionNotFoundError(err)) {
3262+
span.addEvent('No session available', {
3263+
'session.id': session?.id,
3264+
});
3265+
release();
3266+
this.runTransaction(options, runFn!);
3267+
} else {
3268+
release();
3269+
setImmediate(runFn!, err);
3270+
}
3271+
})
3272+
.catch(e => {
3273+
setSpanErrorAndException(span, e as Error);
3274+
throw e;
3275+
});
33073276
});
33083277
});
33093278
}
@@ -3557,13 +3526,13 @@ class Database extends common.GrpcServiceObject {
35573526
// Remove the current data stream from the end user stream.
35583527
dataStream.unpipe(proxyStream);
35593528
dataStream.end();
3529+
span.end();
35603530
// Create a new stream and add it to the end user stream.
35613531
dataStream = this.batchWriteAtLeastOnce(
35623532
mutationGroups,
35633533
options
35643534
);
35653535
dataStream.pipe(proxyStream);
3566-
span.end();
35673536
} else {
35683537
span.end();
35693538
proxyStream.destroy(err);
@@ -3662,14 +3631,8 @@ class Database extends common.GrpcServiceObject {
36623631
span.addEvent('No session available', {
36633632
'session.id': session?.id,
36643633
});
3665-
// Retry this method.
3666-
this.writeAtLeastOnce(mutations, options, (err, resp) => {
3667-
if (err) {
3668-
setSpanError(span, err);
3669-
}
3670-
span.end();
3671-
cb!(err, resp);
3672-
});
3634+
span.end();
3635+
this.writeAtLeastOnce(mutations, options, cb!);
36733636
return;
36743637
}
36753638
if (err) {

src/transaction.ts

+2-7
Original file line numberDiff line numberDiff line change
@@ -762,10 +762,7 @@ export class Snapshot extends EventEmitter {
762762
setSpanError(span, err);
763763
const wasAborted = isErrorAborted(err);
764764
if (!this.id && this._useInRunner && !wasAborted) {
765-
// TODO: re-examine if this.begin() should still exist and if
766-
// an await is needed: with await the generated begin span
767-
// will look wonky and out of order. Please ses
768-
// https://github.com/googleapis/nodejs-spanner/issues/2170
765+
// TODO: resolve https://github.com/googleapis/nodejs-spanner/issues/2170
769766
this.begin();
770767
} else {
771768
if (wasAborted) {
@@ -1357,9 +1354,7 @@ export class Snapshot extends EventEmitter {
13571354
const wasAborted = isErrorAborted(err);
13581355
if (!this.id && this._useInRunner && !wasAborted) {
13591356
span.addEvent('Stream broken. Safe to retry');
1360-
// TODO: Examine the consequence of not awaiting this method,
1361-
// as the span might appear out of order.
1362-
// Please see https://github.com/googleapis/nodejs-spanner/issues/2170
1357+
// TODO: resolve https://github.com/googleapis/nodejs-spanner/issues/2170
13631358
this.begin();
13641359
} else {
13651360
if (wasAborted) {

0 commit comments

Comments
 (0)