Skip to content

Commit 563b571

Browse files
committed
feat: (observability): trace Database.runTransactionAsync
Extracted out of PR googleapis#2158, this change traces Database.runTransactionAsync. However, testing isn't effective because of bugs such as googleapis#2166.
1 parent 51bc8a7 commit 563b571

File tree

3 files changed

+161
-38
lines changed

3 files changed

+161
-38
lines changed

observability-test/database.ts

+69
Original file line numberDiff line numberDiff line change
@@ -1565,6 +1565,75 @@ describe('Database', () => {
15651565
});
15661566
});
15671567

1568+
describe('runTransactionAsync', () => {
1569+
const SESSION = new FakeSession();
1570+
const TRANSACTION = new FakeTransaction(
1571+
{} as google.spanner.v1.TransactionOptions.ReadWrite
1572+
);
1573+
1574+
let pool: FakeSessionPool;
1575+
1576+
beforeEach(() => {
1577+
pool = database.pool_;
1578+
1579+
(sandbox.stub(pool, 'getSession') as sinon.SinonStub).callsFake(
1580+
callback => {
1581+
callback(null, SESSION, TRANSACTION);
1582+
}
1583+
);
1584+
});
1585+
1586+
it('with no error', async() => {
1587+
await database.runTransactionAsync(async (txn) => {
1588+
const [rows] = txn.run('SELECT 1');
1589+
await txn.commit();
1590+
});
1591+
1592+
const spans = traceExporter.getFinishedSpans();
1593+
withAllSpansHaveDBName(spans);
1594+
1595+
const actualSpanNames: string[] = [];
1596+
const actualEventNames: string[] = [];
1597+
spans.forEach(span => {
1598+
actualSpanNames.push(span.name);
1599+
span.events.forEach(event => {
1600+
actualEventNames.push(event.name);
1601+
});
1602+
});
1603+
1604+
const expectedSpanNames = ['CloudSpanner.Database.runTransactionAsync'];
1605+
assert.deepStrictEqual(
1606+
actualSpanNames,
1607+
expectedSpanNames,
1608+
`span names mismatch:\n\tGot: ${actualSpanNames}\n\tWant: ${expectedSpanNames}`
1609+
);
1610+
1611+
// Ensure that the span actually produced an error that was recorded.
1612+
const firstSpan = spans[0];
1613+
assert.strictEqual(
1614+
SpanStatusCode.ERROR,
1615+
firstSpan.status.code,
1616+
'Expected an ERROR span status'
1617+
);
1618+
assert.strictEqual(
1619+
'getting a session',
1620+
firstSpan.status.message,
1621+
'Mismatched span status message'
1622+
);
1623+
1624+
// We don't expect events.
1625+
const expectedEventNames = [];
1626+
assert.deepStrictEqual(
1627+
actualEventNames,
1628+
expectedEventNames,
1629+
`Unexpected events:\n\tGot: ${actualEventNames}\n\tWant: ${expectedEventNames}`
1630+
);
1631+
1632+
done();
1633+
1634+
});
1635+
});
1636+
15681637
describe('runStream', () => {
15691638
const QUERY = {
15701639
sql: 'SELECT * FROM table',

observability-test/spanner.ts

+46
Original file line numberDiff line numberDiff line change
@@ -468,6 +468,52 @@ describe('EndToEnd', () => {
468468
});
469469
});
470470

471+
it('runTransactionAsync', async () => {
472+
const withAllSpansHaveDBName = generateWithAllSpansHaveDBName(
473+
database.formattedName_
474+
);
475+
await database.runTransactionAsync(async transaction => {
476+
const [rows] = await transaction!.run('SELECT 1');
477+
});
478+
479+
traceExporter.forceFlush();
480+
const spans = traceExporter.getFinishedSpans();
481+
withAllSpansHaveDBName(spans);
482+
483+
const actualEventNames: string[] = [];
484+
const actualSpanNames: string[] = [];
485+
spans.forEach(span => {
486+
actualSpanNames.push(span.name);
487+
span.events.forEach(event => {
488+
actualEventNames.push(event.name);
489+
});
490+
});
491+
492+
const expectedSpanNames = [
493+
'CloudSpanner.Snapshot.runStream',
494+
'CloudSpanner.Snapshot.run',
495+
'CloudSpanner.Database.runTransactionAsync',
496+
];
497+
assert.deepStrictEqual(
498+
actualSpanNames,
499+
expectedSpanNames,
500+
`span names mismatch:\n\tGot: ${actualSpanNames}\n\tWant: ${expectedSpanNames}`
501+
);
502+
503+
const expectedEventNames = [
504+
'Transaction Creation Done',
505+
'Acquiring session',
506+
'Cache hit: has usable session',
507+
'Acquired session',
508+
'Using Session',
509+
];
510+
assert.deepStrictEqual(
511+
actualEventNames,
512+
expectedEventNames,
513+
`Unexpected events:\n\tGot: ${actualEventNames}\n\tWant: ${expectedEventNames}`
514+
);
515+
});
516+
471517
it('writeAtLeastOnce', done => {
472518
const withAllSpansHaveDBName = generateWithAllSpansHaveDBName(
473519
database.formattedName_

src/database.ts

+46-38
Original file line numberDiff line numberDiff line change
@@ -3363,46 +3363,54 @@ class Database extends common.GrpcServiceObject {
33633363

33643364
let sessionId = '';
33653365
const getSession = this.pool_.getSession.bind(this.pool_);
3366-
const span = getActiveOrNoopSpan();
3367-
// Loop to retry 'Session not found' errors.
3368-
// (and yes, we like while (true) more than for (;;) here)
3369-
// eslint-disable-next-line no-constant-condition
3370-
while (true) {
3371-
try {
3372-
const [session, transaction] = await promisify(getSession)();
3373-
transaction.requestOptions = Object.assign(
3374-
transaction.requestOptions || {},
3375-
options.requestOptions
3376-
);
3377-
if (options.optimisticLock) {
3378-
transaction.useOptimisticLock();
3379-
}
3380-
if (options.excludeTxnFromChangeStreams) {
3381-
transaction.excludeTxnFromChangeStreams();
3382-
}
3383-
sessionId = session?.id;
3384-
span.addEvent('Using Session', {'session.id': sessionId});
3385-
const runner = new AsyncTransactionRunner<T>(
3386-
session,
3387-
transaction,
3388-
runFn,
3389-
options
3390-
);
3391-
3392-
try {
3393-
return await runner.run();
3394-
} finally {
3395-
this.pool_.release(session);
3396-
}
3397-
} catch (e) {
3398-
if (!isSessionNotFoundError(e as ServiceError)) {
3399-
span.addEvent('No session available', {
3400-
'session.id': sessionId,
3401-
});
3402-
throw e;
3366+
return startTrace(
3367+
'Database.runTransactionAsync',
3368+
this._traceConfig,
3369+
async span => {
3370+
// Loop to retry 'Session not found' errors.
3371+
// (and yes, we like while (true) more than for (;;) here)
3372+
// eslint-disable-next-line no-constant-condition
3373+
while (true) {
3374+
try {
3375+
const [session, transaction] = await promisify(getSession)();
3376+
transaction.requestOptions = Object.assign(
3377+
transaction.requestOptions || {},
3378+
options.requestOptions
3379+
);
3380+
if (options.optimisticLock) {
3381+
transaction.useOptimisticLock();
3382+
}
3383+
if (options.excludeTxnFromChangeStreams) {
3384+
transaction.excludeTxnFromChangeStreams();
3385+
}
3386+
sessionId = session?.id;
3387+
span.addEvent('Using Session', {'session.id': sessionId});
3388+
const runner = new AsyncTransactionRunner<T>(
3389+
session,
3390+
transaction,
3391+
runFn,
3392+
options
3393+
);
3394+
3395+
try {
3396+
const result = await runner.run();
3397+
span.end();
3398+
return result;
3399+
} finally {
3400+
this.pool_.release(session);
3401+
}
3402+
} catch (e) {
3403+
if (!isSessionNotFoundError(e as ServiceError)) {
3404+
span.addEvent('No session available', {
3405+
'session.id': sessionId,
3406+
});
3407+
span.end();
3408+
throw e;
3409+
}
3410+
}
34033411
}
34043412
}
3405-
}
3413+
);
34063414
}
34073415

34083416
/**

0 commit comments

Comments
 (0)