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

chore: refactor observability tests #2177

Merged
merged 25 commits into from
Oct 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
c62584c
chore: integration test fix
surbhigarg92 Jan 11, 2024
e7d385b
Merge branch 'googleapis:main' into main
surbhigarg92 Mar 4, 2024
e57b897
Merge branch 'googleapis:main' into main
surbhigarg92 Mar 4, 2024
c0c935e
Merge branch 'googleapis:main' into main
surbhigarg92 Mar 8, 2024
8875f2c
Merge branch 'googleapis:main' into main
surbhigarg92 Mar 26, 2024
a903eec
Merge branch 'googleapis:main' into main
surbhigarg92 Apr 9, 2024
50914dc
Merge branch 'googleapis:main' into main
surbhigarg92 Jun 17, 2024
95aa578
Merge branch 'googleapis:main' into main
surbhigarg92 Jun 26, 2024
738804b
Merge branch 'googleapis:main' into main
surbhigarg92 Jul 3, 2024
b53a82f
Merge branch 'googleapis:main' into main
surbhigarg92 Jul 31, 2024
eb858ac
Merge branch 'googleapis:main' into main
surbhigarg92 Aug 6, 2024
163699b
Merge branch 'googleapis:main' into main
surbhigarg92 Aug 8, 2024
0f6fda3
Merge branch 'googleapis:main' into main
surbhigarg92 Sep 23, 2024
4b173ba
Merge branch 'googleapis:main' into main
surbhigarg92 Sep 30, 2024
4261105
Merge branch 'googleapis:main' into main
surbhigarg92 Oct 10, 2024
6a77a21
Merge branch 'googleapis:main' into main
surbhigarg92 Oct 21, 2024
c597b74
Merge remote-tracking branch 'upstream/main'
surbhigarg92 Oct 21, 2024
56df6b4
Merge branch 'googleapis:main' into main
surbhigarg92 Oct 22, 2024
bd4647c
Merge branch 'googleapis:main' into main
surbhigarg92 Oct 24, 2024
4b2f196
Merge branch 'googleapis:main' into main
surbhigarg92 Oct 28, 2024
4e85d45
traces tests refactoring
surbhigarg92 Oct 28, 2024
a588465
feat: (observability): trace Database.runPartitionedUpdate (#2176)
odeke-em Oct 28, 2024
530d664
moving additional attributes to separate PR
surbhigarg92 Oct 30, 2024
fed579d
Merge branch 'main' into traces_fix
alkatrivedi Oct 30, 2024
790e6ad
Merge branch 'main' into traces_fix
alkatrivedi Oct 30, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
174 changes: 83 additions & 91 deletions observability-test/batch-transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,103 +153,95 @@ describe('BatchTransaction', () => {
};

it('createQueryPartitions', done => {
const REQUEST = sandbox.stub();

const res = batchTransaction.createQueryPartitions(
QUERY,
(err, part, resp) => {
assert.ifError(err);
traceExporter.forceFlush();
const spans = traceExporter.getFinishedSpans();
assert.strictEqual(spans.length, 2, 'Exactly 2 spans expected');

// Sort the spans by duration.
spans.sort((spanA, spanB) => {
spanA.duration < spanB.duration;
});

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

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

// Ensure that createPartitions_ is a child span of createQueryPartitions.
const spanCreatePartitions_ = spans[0];
const spanCreateQueryPartitions = spans[1];
assert.ok(
spanCreateQueryPartitions.spanContext().traceId,
'Expected that createQueryPartitions has a defined traceId'
);
assert.ok(
spanCreatePartitions_.spanContext().traceId,
'Expected that createPartitions_ has a defined traceId'
);
assert.deepStrictEqual(
spanCreatePartitions_.spanContext().traceId,
spanCreateQueryPartitions.spanContext().traceId,
'Expected that both spans share a traceId'
);
assert.ok(
spanCreateQueryPartitions.spanContext().spanId,
'Expected that createQueryPartitions has a defined spanId'
);
assert.ok(
spanCreatePartitions_.spanContext().spanId,
'Expected that createPartitions_ has a defined spanId'
);
assert.deepStrictEqual(
spanCreatePartitions_.parentSpanId,
spanCreateQueryPartitions.spanContext().spanId,
'Expected that createQueryPartitions is the parent to createPartitions_'
);
done();
}
);
batchTransaction.createQueryPartitions(QUERY, err => {
assert.ifError(err);
traceExporter.forceFlush();
const spans = traceExporter.getFinishedSpans();
assert.strictEqual(spans.length, 2, 'Exactly 2 spans expected');

// Sort the spans by duration.
spans.sort((spanA, spanB) => {
spanA.duration < spanB.duration;
});

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

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

// Ensure that createPartitions_ is a child span of createQueryPartitions.
const spanCreatePartitions_ = spans[0];
const spanCreateQueryPartitions = spans[1];
assert.ok(
spanCreateQueryPartitions.spanContext().traceId,
'Expected that createQueryPartitions has a defined traceId'
);
assert.ok(
spanCreatePartitions_.spanContext().traceId,
'Expected that createPartitions_ has a defined traceId'
);
assert.deepStrictEqual(
spanCreatePartitions_.spanContext().traceId,
spanCreateQueryPartitions.spanContext().traceId,
'Expected that both spans share a traceId'
);
assert.ok(
spanCreateQueryPartitions.spanContext().spanId,
'Expected that createQueryPartitions has a defined spanId'
);
assert.ok(
spanCreatePartitions_.spanContext().spanId,
'Expected that createPartitions_ has a defined spanId'
);
assert.deepStrictEqual(
spanCreatePartitions_.parentSpanId,
spanCreateQueryPartitions.spanContext().spanId,
'Expected that createQueryPartitions is the parent to createPartitions_'
);
done();
});
});

it('createReadPartitions', done => {
const REQUEST = sandbox.stub();
const response = {};
REQUEST.callsFake((_, callback) => callback(null, response));

const res = batchTransaction.createReadPartitions(
QUERY,
(err, part, resp) => {
assert.ifError(err);
traceExporter.forceFlush();
const spans = traceExporter.getFinishedSpans();
assert.strictEqual(spans.length, 2, 'Exactly 2 spans expected');

// Sort the spans by duration.
spans.sort((spanA, spanB) => {
spanA.duration < spanB.duration;
});

const actualSpanNames: string[] = [];
spans.forEach(span => {
actualSpanNames.push(span.name);
});
const expectedSpanNames = [
'CloudSpanner.BatchTransaction.createPartitions_',
'CloudSpanner.BatchTransaction.createReadPartitions',
];
assert.deepStrictEqual(
actualSpanNames,
expectedSpanNames,
`span names mismatch:\n\tGot: ${actualSpanNames}\n\tWant: ${expectedSpanNames}`
);
done();
}
);
batchTransaction.createReadPartitions(QUERY, err => {
assert.ifError(err);
traceExporter.forceFlush();
const spans = traceExporter.getFinishedSpans();
assert.strictEqual(spans.length, 2, 'Exactly 2 spans expected');

// Sort the spans by duration.
spans.sort((spanA, spanB) => {
spanA.duration < spanB.duration;
});

const actualSpanNames: string[] = [];
spans.forEach(span => {
actualSpanNames.push(span.name);
});
const expectedSpanNames = [
'CloudSpanner.BatchTransaction.createPartitions_',
'CloudSpanner.BatchTransaction.createReadPartitions',
];
assert.deepStrictEqual(
actualSpanNames,
expectedSpanNames,
`span names mismatch:\n\tGot: ${actualSpanNames}\n\tWant: ${expectedSpanNames}`
);
done();
});
});
});
31 changes: 8 additions & 23 deletions observability-test/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@

class FakeSession {
calledWith_: IArguments;
formattedName_: any;

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

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type

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

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type

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

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type

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

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type

Check warning on line 96 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 @@ -507,7 +507,6 @@

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

beforeEach(() => {
fakePool = database.pool_;
Expand All @@ -524,9 +523,7 @@
sandbox.stub(fakePool, 'getSession') as sinon.SinonStub
).callsFake(callback => callback(null, fakeSession));

snapshotStub = sandbox
.stub(fakeSession, 'snapshot')
.returns(fakeSnapshot);
sandbox.stub(fakeSession, 'snapshot').returns(fakeSnapshot);
});

it('with error', done => {
Expand Down Expand Up @@ -1175,7 +1172,7 @@

it('with error on null mutation should catch thrown error', done => {
try {
database.writeAtLeastOnce(null, (err, res) => {});
database.writeAtLeastOnce(null, () => {});
} catch (err) {
// Performing a substring search on the error because
// depending on the version of Node.js, the error might be either of:
Expand Down Expand Up @@ -1250,7 +1247,6 @@
let fakeSession: FakeSession;
let fakeDataStream: Transform;
let getSessionStub: sinon.SinonStub;
let requestStreamStub: sinon.SinonStub;

const options = {
requestOptions: {
Expand All @@ -1269,9 +1265,7 @@
sandbox.stub(fakePool, 'getSession') as sinon.SinonStub
).callsFake(callback => callback(null, fakeSession));

requestStreamStub = sandbox
.stub(database, 'requestStream')
.returns(fakeDataStream);
sandbox.stub(database, 'requestStream').returns(fakeDataStream);
});

it('on retry with "Session not found" error', done => {
Expand Down Expand Up @@ -1320,7 +1314,6 @@
'Expected an ERROR span status'
);

const errorMessage = firstSpan.status.message;
assert.deepStrictEqual(
firstSpan.status.message,
sessionNotFoundError.message
Expand Down Expand Up @@ -1658,7 +1651,7 @@
.throws(ourException);

assert.rejects(async () => {
const value = await database.runTransactionAsync(async txn => {
await database.runTransactionAsync(async txn => {
const result = await txn.run('SELECT 1');
await txn.commit();
return result;
Expand Down Expand Up @@ -1724,8 +1717,6 @@
let fakeStream2: Transform;

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

beforeEach(() => {
fakePool = database.pool_;
Expand All @@ -1746,15 +1737,11 @@
.onSecondCall()
.callsFake(callback => callback(null, fakeSession2));

snapshotStub = sandbox
.stub(fakeSession, 'snapshot')
.returns(fakeSnapshot);
sandbox.stub(fakeSession, 'snapshot').returns(fakeSnapshot);

sandbox.stub(fakeSession2, 'snapshot').returns(fakeSnapshot2);

runStreamStub = sandbox
.stub(fakeSnapshot, 'runStream')
.returns(fakeStream);
sandbox.stub(fakeSnapshot, 'runStream').returns(fakeStream);

sandbox.stub(fakeSnapshot2, 'runStream').returns(fakeStream2);
});
Expand Down Expand Up @@ -1975,7 +1962,6 @@

let getSessionStub;
let beginStub;
let runUpdateStub;

beforeEach(() => {
fakePool = database.pool_;
Expand All @@ -1996,7 +1982,7 @@
sandbox.stub(fakePartitionedDml, 'begin') as sinon.SinonStub
).callsFake(callback => callback(null));

runUpdateStub = (
(
sandbox.stub(fakePartitionedDml, 'runUpdate') as sinon.SinonStub
).callsFake((_, callback) => callback(null));
});
Expand Down Expand Up @@ -2031,7 +2017,6 @@

it('with pool errors', done => {
const fakeError = new Error('err');
const fakeCallback = sandbox.spy();

getSessionStub.callsFake(callback => callback(fakeError));
database.runPartitionedUpdate(QUERY, async (err, rowCount) => {
Expand Down Expand Up @@ -2132,7 +2117,7 @@
sandbox.stub(fakePool, 'release') as sinon.SinonStub
).withArgs(fakeSession);

database.runPartitionedUpdate(QUERY, async (err, rowCount) => {
database.runPartitionedUpdate(QUERY, async () => {
const exportResults = await getTraceExportResults();
const actualSpanNames = exportResults.spanNames;
const spans = exportResults.spans;
Expand Down
46 changes: 46 additions & 0 deletions observability-test/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,25 @@ import * as assert from 'assert';
const {ReadableSpan} = require('@opentelemetry/sdk-trace-base');
import {SEMATTRS_DB_NAME} from '@opentelemetry/semantic-conventions';

export const batchCreateSessionsEvents = [
'Requesting 25 sessions',
'Creating 25 sessions',
'Requested for 25 sessions returned 25',
];

export const waitingSessionsEvents = [
'Acquiring session',
'Waiting for a session to become available',
'Acquired session',
'Using Session',
];

export const cacheSessionEvents = [
'Acquiring session',
'Cache hit: has usable session',
'Acquired session',
];

/**
* This utility exists as a test helper because mocha has builtin "context"
* and referring to context causes type/value collision errors.
Expand Down Expand Up @@ -47,3 +66,30 @@ export function generateWithAllSpansHaveDBName(dbName: String): Function {
});
};
}

export async function verifySpansAndEvents(
traceExporter,
expectedSpans,
expectedEvents
) {
await traceExporter.forceFlush();
const spans = traceExporter.getFinishedSpans();
const actualEventNames: string[] = [];
const actualSpanNames: string[] = [];
spans.forEach(span => {
actualSpanNames.push(span.name);
span.events.forEach(event => {
actualEventNames.push(event.name);
});
});
assert.deepStrictEqual(
actualSpanNames,
expectedSpans,
`span names mismatch:\n\tGot: ${actualSpanNames}\n\tWant: ${expectedSpans}`
);
assert.deepStrictEqual(
actualEventNames,
expectedEvents,
`Unexpected events:\n\tGot: ${actualEventNames}\n\tWant: ${expectedEvents}`
);
}
Loading
Loading