Skip to content

Commit 0e5e067

Browse files
committed
feat: (observability) trace Database.batchCreateSessions + SessionPool.createSessions
This change adds tracing for Database.batchCreateSessions as well as SessionPool.createSessions which was raised as a big need. This change is a premise to finishing up tracing Transaction. While here, also folded in the async/await fix to avoid day+ long code review lag and then 3+ hours just to run tests per PR: OpenTelemetry cannot work correctly for async/await if there isn't a set AsyncHooksManager, but we should not burden our customers with this type of specialist knowledge, their code should just work and this change performs such a check. Later on we shall file a feature request with the OpenTelemetry-JS API group to give us a hook to detect if we've got a live asyncHooksManager instead of this mandatory comparison to ROOT_CONTEXT each time. Fixes #2146 Updates #2079 Spun out of PR #2122 Supersedes PR #2147
1 parent 5237e11 commit 0e5e067

File tree

7 files changed

+656
-193
lines changed

7 files changed

+656
-193
lines changed

observability-test/database.ts

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,115 @@ describe('Database', () => {
375375
});
376376
});
377377

378+
describe('batchCreateSessions', () => {
379+
it('without error', done => {
380+
const ARGS = [null, [{}]];
381+
database.request = (config, callback) => {
382+
callback(...ARGS);
383+
};
384+
385+
database.batchCreateSessions(10, (err, sessions) => {
386+
assert.ifError(err);
387+
assert.ok(sessions);
388+
389+
traceExporter.forceFlush();
390+
const spans = traceExporter.getFinishedSpans();
391+
392+
const actualSpanNames: string[] = [];
393+
const actualEventNames: string[] = [];
394+
spans.forEach(span => {
395+
actualSpanNames.push(span.name);
396+
span.events.forEach(event => {
397+
actualEventNames.push(event.name);
398+
});
399+
});
400+
401+
const expectedSpanNames = ['CloudSpanner.Database.batchCreateSessions'];
402+
assert.deepStrictEqual(
403+
actualSpanNames,
404+
expectedSpanNames,
405+
`span names mismatch:\n\tGot: ${actualSpanNames}\n\tWant: ${expectedSpanNames}`
406+
);
407+
408+
// Ensure that the span actually produced an error that was recorded.
409+
const firstSpan = spans[0];
410+
assert.strictEqual(
411+
SpanStatusCode.UNSET,
412+
firstSpan.status.code,
413+
'Unexpected span status code'
414+
);
415+
assert.strictEqual(
416+
undefined,
417+
firstSpan.status.message,
418+
'Mismatched span status message'
419+
);
420+
421+
// We don't expect events.
422+
const expectedEventNames = [];
423+
assert.deepStrictEqual(
424+
actualEventNames,
425+
expectedEventNames,
426+
`Unexpected events:\n\tGot: ${actualEventNames}\n\tWant: ${expectedEventNames}`
427+
);
428+
429+
done();
430+
});
431+
});
432+
433+
it('with error', done => {
434+
const ARGS = [new Error('batchCreateSessions.error'), null];
435+
database.request = (config, callback) => {
436+
callback(...ARGS);
437+
};
438+
439+
database.batchCreateSessions(10, (err, sessions) => {
440+
assert.ok(err);
441+
assert.ok(!sessions);
442+
traceExporter.forceFlush();
443+
const spans = traceExporter.getFinishedSpans();
444+
445+
const actualSpanNames: string[] = [];
446+
const actualEventNames: string[] = [];
447+
spans.forEach(span => {
448+
actualSpanNames.push(span.name);
449+
span.events.forEach(event => {
450+
actualEventNames.push(event.name);
451+
});
452+
});
453+
454+
const expectedSpanNames = ['CloudSpanner.Database.batchCreateSessions'];
455+
assert.deepStrictEqual(
456+
actualSpanNames,
457+
expectedSpanNames,
458+
`span names mismatch:\n\tGot: ${actualSpanNames}\n\tWant: ${expectedSpanNames}`
459+
);
460+
461+
// Ensure that the span actually produced an error that was recorded.
462+
const firstSpan = spans[0];
463+
assert.strictEqual(
464+
SpanStatusCode.ERROR,
465+
firstSpan.status.code,
466+
'Expected an ERROR span status'
467+
);
468+
assert.strictEqual(
469+
'batchCreateSessions.error',
470+
firstSpan.status.message,
471+
'Mismatched span status message'
472+
);
473+
474+
// We don't expect events.
475+
const expectedEventNames = [];
476+
assert.deepStrictEqual(
477+
actualEventNames,
478+
expectedEventNames,
479+
`Unexpected events:\n\tGot: ${actualEventNames}\n\tWant: ${expectedEventNames}`
480+
);
481+
482+
done();
483+
});
484+
});
485+
});
486+
378487
describe('getSnapshot', () => {
379488
let fakePool: FakeSessionPool;
380489
let fakeSession: FakeSession;

0 commit comments

Comments
 (0)