Skip to content

Commit cbc86fa

Browse files
authored
feat(observability): fix bugs found from product review + negative cases (#2158)
* feat(observability): fix bugs found from product review + negative cases This change adds recording of retry span annotations, catching cases in which exceptions where thrown but spans were not ended while testing out and visually confirming the results. * Address merge from main * Ensure Database.getSnapshot correct span ending + tests * Proper placement of Transaction span.end() * Adjust tests to ensure transaction.end() where necessary * Address some review feedback * Handle moving 2nd getSnapshot() retry to itself * Remove dataStream.once span nesting * End spans even on dataStream retry * De-flake EndToEnd Database.runStream test with by ignoring grpc.CANCELLED * test: remove async vestige * Attempt to isolate and de-flake * More test failure debugs * hotfix to allow tests to run without blocking testers * Address feedback from code review * EndToEnd test setup reduced to avoid flakes * Revert Database.runTransaction runner ordering * Uncommitted change for Database.getSnapshot: end span before sending back response
1 parent 52fd5a9 commit cbc86fa

File tree

9 files changed

+1226
-174
lines changed

9 files changed

+1226
-174
lines changed

observability-test/database.ts

+30-18
Original file line numberDiff line numberDiff line change
@@ -605,7 +605,7 @@ describe('Database', () => {
605605
// pool, so that the pool can remove it from its inventory.
606606
const releaseStub = sandbox.stub(fakePool, 'release');
607607

608-
database.getSnapshot((err, snapshot) => {
608+
database.getSnapshot(async (err, snapshot) => {
609609
assert.ifError(err);
610610
assert.strictEqual(snapshot, fakeSnapshot2);
611611
// The first session that error should already have been released back
@@ -616,8 +616,9 @@ describe('Database', () => {
616616
snapshot.emit('end');
617617
assert.strictEqual(releaseStub.callCount, 2);
618618

619+
await provider.forceFlush();
620+
await traceExporter.forceFlush();
619621
const spans = traceExporter.getFinishedSpans();
620-
assert.strictEqual(spans.length, 2, 'Exactly 2 spans expected');
621622
withAllSpansHaveDBName(spans);
622623

623624
const actualSpanNames: string[] = [];
@@ -640,7 +641,7 @@ describe('Database', () => {
640641
);
641642

642643
// Ensure that the first span actually produced an error that was recorded.
643-
const parentSpan = spans[1];
644+
const parentSpan = spans[0];
644645
assert.strictEqual(
645646
SpanStatusCode.ERROR,
646647
parentSpan.status.code,
@@ -653,7 +654,7 @@ describe('Database', () => {
653654
);
654655

655656
// Ensure that the second span is a child of the first span.
656-
const secondRetrySpan = spans[0];
657+
const secondRetrySpan = spans[1];
657658
assert.ok(
658659
parentSpan.spanContext().traceId,
659660
'Expected that the initial parent span has a defined traceId'
@@ -774,6 +775,7 @@ describe('Database', () => {
774775
callback(null, RESPONSE);
775776
},
776777
once() {},
778+
end() {},
777779
};
778780

779781
database.batchTransaction = (identifier, options) => {
@@ -782,10 +784,14 @@ describe('Database', () => {
782784
return fakeTransaction;
783785
};
784786

785-
database.createBatchTransaction(opts, (err, transaction, resp) => {
787+
database.createBatchTransaction(opts, async (err, transaction, resp) => {
786788
assert.strictEqual(err, null);
787789
assert.strictEqual(transaction, fakeTransaction);
788790
assert.strictEqual(resp, RESPONSE);
791+
transaction!.end();
792+
793+
await provider.forceFlush();
794+
traceExporter.forceFlush();
789795
const spans = traceExporter.getFinishedSpans();
790796
assert.strictEqual(spans.length, 1, 'Exactly 1 span expected');
791797
withAllSpansHaveDBName(spans);
@@ -839,8 +845,8 @@ describe('Database', () => {
839845
begin(callback) {
840846
callback(error, RESPONSE);
841847
},
842-
843848
once() {},
849+
end() {},
844850
};
845851

846852
database.batchTransaction = () => {
@@ -926,9 +932,11 @@ describe('Database', () => {
926932

927933
getSessionStub.callsFake(callback => callback(fakeError));
928934

929-
database.getTransaction(err => {
935+
database.getTransaction(async err => {
930936
assert.strictEqual(err, fakeError);
931937

938+
await provider.forceFlush();
939+
traceExporter.forceFlush();
932940
const spans = traceExporter.getFinishedSpans();
933941
assert.strictEqual(spans.length, 1, 'Exactly 1 span expected');
934942
withAllSpansHaveDBName(spans);
@@ -978,6 +986,7 @@ describe('Database', () => {
978986
database.getTransaction((err, transaction) => {
979987
assert.ifError(err);
980988
assert.strictEqual(transaction, fakeTransaction);
989+
transaction!.end();
981990

982991
const spans = traceExporter.getFinishedSpans();
983992
withAllSpansHaveDBName(spans);
@@ -1869,11 +1878,14 @@ describe('Database', () => {
18691878
.on('error', err => {
18701879
assert.fail(err);
18711880
})
1872-
.on('end', () => {
1881+
.on('end', async () => {
18731882
assert.strictEqual(endStub.callCount, 1);
18741883
assert.strictEqual(endStub2.callCount, 1);
18751884
assert.strictEqual(rows, 1);
18761885

1886+
await provider.forceFlush();
1887+
await traceExporter.forceFlush();
1888+
18771889
const spans = traceExporter.getFinishedSpans();
18781890
assert.strictEqual(spans.length, 2, 'Exactly 1 span expected');
18791891
withAllSpansHaveDBName(spans);
@@ -1898,35 +1910,35 @@ describe('Database', () => {
18981910
);
18991911

19001912
// Ensure that the span actually produced an error that was recorded.
1901-
const secondSpan = spans[1];
1902-
assert.strictEqual(
1913+
const lastSpan = spans[0];
1914+
assert.deepStrictEqual(
19031915
SpanStatusCode.ERROR,
1904-
secondSpan.status.code,
1916+
lastSpan.status.code,
19051917
'Expected an ERROR span status'
19061918
);
1907-
assert.strictEqual(
1919+
assert.deepStrictEqual(
19081920
'Session not found',
1909-
secondSpan.status.message,
1921+
lastSpan.status.message,
19101922
'Mismatched span status message'
19111923
);
19121924

19131925
// Ensure that the final span that got retries did not error.
1914-
const firstSpan = spans[0];
1915-
assert.strictEqual(
1926+
const firstSpan = spans[1];
1927+
assert.deepStrictEqual(
19161928
SpanStatusCode.UNSET,
19171929
firstSpan.status.code,
1918-
'Unexpected an span status code'
1930+
'Unexpected span status code'
19191931
);
1920-
assert.strictEqual(
1932+
assert.deepStrictEqual(
19211933
undefined,
19221934
firstSpan.status.message,
19231935
'Unexpected span status message'
19241936
);
19251937

19261938
const expectedEventNames = [
1927-
'Using Session',
19281939
'Using Session',
19291940
'No session available',
1941+
'Using Session',
19301942
];
19311943
assert.deepStrictEqual(
19321944
actualEventNames,

0 commit comments

Comments
 (0)