Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: (observability) trace Database.batchCreateSessions + SessionPool.createSessions #2145

109 changes: 109 additions & 0 deletions observability-test/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@

class FakeSession {
calledWith_: IArguments;
formattedName_: any;

Check warning on line 87 in observability-test/database.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type
constructor() {
this.calledWith_ = arguments;
}
Expand Down Expand Up @@ -375,6 +375,115 @@
});
});

describe('batchCreateSessions', () => {
it('without error', done => {
surbhigarg92 marked this conversation as resolved.
Show resolved Hide resolved
const ARGS = [null, [{}]];
database.request = (config, callback) => {
callback(...ARGS);
};

database.batchCreateSessions(10, (err, sessions) => {
surbhigarg92 marked this conversation as resolved.
Show resolved Hide resolved
assert.ifError(err);
assert.ok(sessions);

traceExporter.forceFlush();
const spans = traceExporter.getFinishedSpans();

const actualSpanNames: string[] = [];
const actualEventNames: string[] = [];
spans.forEach(span => {
actualSpanNames.push(span.name);
span.events.forEach(event => {
actualEventNames.push(event.name);
});
});

const expectedSpanNames = ['CloudSpanner.Database.batchCreateSessions'];
assert.deepStrictEqual(
actualSpanNames,
expectedSpanNames,
`span names mismatch:\n\tGot: ${actualSpanNames}\n\tWant: ${expectedSpanNames}`
);

// Ensure that the span didn't encounter an error.
const firstSpan = spans[0];
assert.strictEqual(
SpanStatusCode.UNSET,
firstSpan.status.code,
'Unexpected span status code'
);
assert.strictEqual(
undefined,
firstSpan.status.message,
'Mismatched span status message'
);

// We don't expect events.
const expectedEventNames = [];
assert.deepStrictEqual(
actualEventNames,
expectedEventNames,
`Unexpected events:\n\tGot: ${actualEventNames}\n\tWant: ${expectedEventNames}`
);

done();
});
});

it('with error', done => {
surbhigarg92 marked this conversation as resolved.
Show resolved Hide resolved
const ARGS = [new Error('batchCreateSessions.error'), null];
database.request = (config, callback) => {
callback(...ARGS);
};

database.batchCreateSessions(10, (err, sessions) => {
surbhigarg92 marked this conversation as resolved.
Show resolved Hide resolved
assert.ok(err);
assert.ok(!sessions);
traceExporter.forceFlush();
const spans = traceExporter.getFinishedSpans();

const actualSpanNames: string[] = [];
const actualEventNames: string[] = [];
spans.forEach(span => {
actualSpanNames.push(span.name);
span.events.forEach(event => {
actualEventNames.push(event.name);
});
});

const expectedSpanNames = ['CloudSpanner.Database.batchCreateSessions'];
assert.deepStrictEqual(
actualSpanNames,
expectedSpanNames,
`span names mismatch:\n\tGot: ${actualSpanNames}\n\tWant: ${expectedSpanNames}`
);

// Ensure that the span actually produced an error that was recorded.
const firstSpan = spans[0];
assert.strictEqual(
SpanStatusCode.ERROR,
firstSpan.status.code,
'Expected an ERROR span status'
);
assert.strictEqual(
'batchCreateSessions.error',
firstSpan.status.message,
'Mismatched span status message'
);

// We don't expect events.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// We don't expect events.
// Ensure events are not present

const expectedEventNames = [];
assert.deepStrictEqual(
actualEventNames,
expectedEventNames,
`Unexpected events:\n\tGot: ${actualEventNames}\n\tWant: ${expectedEventNames}`
);

done();
});
});
});

describe('getSnapshot', () => {
let fakePool: FakeSessionPool;
let fakeSession: FakeSession;
Expand All @@ -382,7 +491,7 @@

let beginSnapshotStub: sinon.SinonStub;
let getSessionStub: sinon.SinonStub;
let snapshotStub: sinon.SinonStub;

Check warning on line 494 in observability-test/database.ts

View workflow job for this annotation

GitHub Actions / lint

'snapshotStub' is assigned a value but never used

beforeEach(() => {
fakePool = database.pool_;
Expand All @@ -409,7 +518,7 @@

getSessionStub.callsFake(callback => callback(fakeError, null));

database.getSnapshot((err, snapshot) => {

Check warning on line 521 in observability-test/database.ts

View workflow job for this annotation

GitHub Actions / lint

'snapshot' is defined but never used
assert.strictEqual(err, fakeError);
traceExporter.forceFlush();
const spans = traceExporter.getFinishedSpans();
Expand Down
Loading
Loading