Skip to content
Merged
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ Samples are in the [`samples/`](https://github.com/googleapis/nodejs-spanner/tre
| Creates an incremental backup schedule | [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/create-incremental-backup-schedule.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-spanner&page=editor&open_in_editor=samples/create-incremental-backup-schedule.js,samples/README.md) |
| Create-instance-without-default-backup-schedules | [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/create-instance-without-default-backup-schedules.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-spanner&page=editor&open_in_editor=samples/create-instance-without-default-backup-schedules.js,samples/README.md) |
| CRUD | [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/crud.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-spanner&page=editor&open_in_editor=samples/crud.js,samples/README.md) |
| Adds split points to a database. | [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/database-add-split-points.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-spanner&page=editor&open_in_editor=samples/database-add-split-points.js,samples/README.md) |
| Creates a new database with a specific default leader | [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/database-create-with-default-leader.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-spanner&page=editor&open_in_editor=samples/database-create-with-default-leader.js,samples/README.md) |
| Database-create-with-encryption-key | [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/database-create-with-encryption-key.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-spanner&page=editor&open_in_editor=samples/database-create-with-encryption-key.js,samples/README.md) |
| Database-create-with-multiple-kms-keys | [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/database-create-with-multiple-kms-keys.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-spanner&page=editor&open_in_editor=samples/database-create-with-multiple-kms-keys.js,samples/README.md) |
Expand Down
201 changes: 114 additions & 87 deletions observability-test/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@

class FakeSession {
calledWith_: IArguments;
formattedName_: any;

Check warning on line 98 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 @@ -148,7 +148,7 @@
setQueuedMutations(mutation) {
this._queuedMutations = mutation;
}
setReadWriteTransactionOptions(options: RunTransactionOptions) {}

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

View workflow job for this annotation

GitHub Actions / lint

'options' is defined but never used
commit(
options?: CommitOptions,
callback?: CommitCallback
Expand Down Expand Up @@ -938,54 +938,60 @@

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

database.getTransaction(async err => {
assert.strictEqual(err, fakeError);
database.getTransaction(
{requestOptions: {transactionTag: 'transaction-tag'}},
async err => {
assert.strictEqual(err, fakeError);

await provider.forceFlush();
traceExporter.forceFlush();
const spans = traceExporter.getFinishedSpans();
assert.strictEqual(spans.length, 1, 'Exactly 1 span expected');
withAllSpansHaveDBName(spans);
await provider.forceFlush();
traceExporter.forceFlush();
const spans = traceExporter.getFinishedSpans();
assert.strictEqual(spans.length, 1, 'Exactly 1 span expected');
withAllSpansHaveDBName(spans);

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

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

// In the event of a sessionPool error, we should not have events.
const expectedEventNames = [];
assert.deepStrictEqual(
actualEventNames,
expectedEventNames,
`event names mismatch:\n\tGot: ${actualEventNames}\n\tWant: ${expectedEventNames}`
);
const expectedSpanNames = ['CloudSpanner.Database.getTransaction'];
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(
'pool error',
firstSpan.status.message,
'Mismatched span status message'
);
// In the event of a sessionPool error, we should not have events.
const expectedEventNames = [];
assert.deepStrictEqual(
actualEventNames,
expectedEventNames,
`event names mismatch:\n\tGot: ${actualEventNames}\n\tWant: ${expectedEventNames}`
);

done();
});
// 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(
'pool error',
firstSpan.status.message,
'Mismatched span status message'
);
assert.strictEqual(
spans[0].attributes['transaction.tag'],
'transaction-tag'
);
done();
}
);
});

it('with no errors', done => {
Expand Down Expand Up @@ -1342,7 +1348,10 @@
'Using Session',
];
assert.deepStrictEqual(actualEventNames, expectedEventNames);

assert.strictEqual(
spans[0].attributes['transaction.tag'],
'batch-write-tag'
);
done();
});

Expand Down Expand Up @@ -1475,52 +1484,59 @@
callback(fakeErr)
);

database.runTransaction(err => {
assert.strictEqual(err, fakeErr);
database.runTransaction(
{requestOptions: {transactionTag: 'transaction-tag'}},
err => {
assert.strictEqual(err, fakeErr);

const spans = traceExporter.getFinishedSpans();
assert.strictEqual(spans.length, 1, 'Exactly 1 span expected');
withAllSpansHaveDBName(spans);
const spans = traceExporter.getFinishedSpans();
assert.strictEqual(spans.length, 1, 'Exactly 1 span expected');
withAllSpansHaveDBName(spans);

const actualSpanNames: string[] = [];
const actualEventNames: string[] = [];
spans.forEach(span => {
actualSpanNames.push(span.name);
span.events.forEach(event => {
actualEventNames.push(event.name);
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.runTransaction'];
assert.deepStrictEqual(
actualSpanNames,
expectedSpanNames,
`span names mismatch:\n\tGot: ${actualSpanNames}\n\tWant: ${expectedSpanNames}`
);
const expectedSpanNames = ['CloudSpanner.Database.runTransaction'];
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(
'getting a session',
firstSpan.status.message,
'Mismatched span status message'
);
// 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(
'getting a session',
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}`
);
// We don't expect events.
const expectedEventNames = [];
assert.deepStrictEqual(
actualEventNames,
expectedEventNames,
`Unexpected events:\n\tGot: ${actualEventNames}\n\tWant: ${expectedEventNames}`
);

done();
});
assert.strictEqual(
spans[0].attributes['transaction.tag'],
'transaction-tag'
);
done();
}
);
});

it('with other errors when running the transaction', done => {
Expand Down Expand Up @@ -1601,11 +1617,14 @@
.stub(FakeAsyncTransactionRunner.prototype, 'run')
.resolves(fakeValue);

const value = await database.runTransactionAsync(async txn => {
const result = await txn.run('SELECT 1');
await txn.commit();
return result;
});
const value = await database.runTransactionAsync(
{requestOptions: {transactionTag: 'transaction-tag'}},
async txn => {
const result = await txn.run('SELECT 1');
await txn.commit();
return result;
}
);

assert.strictEqual(value, fakeValue);

Expand Down Expand Up @@ -1649,6 +1668,10 @@
expectedEventNames,
`Unexpected events:\n\tGot: ${actualEventNames}\n\tWant: ${expectedEventNames}`
);
assert.strictEqual(
spans[0].attributes['transaction.tag'],
'transaction-tag'
);
});

it('with error', async () => {
Expand Down Expand Up @@ -1713,6 +1736,7 @@
sql: 'SELECT * FROM table',
a: 'b',
c: 'd',
requestOptions: {requestTag: 'request-tag'},
};
let fakeSessionFactory: FakeSessionFactory;
let fakeSession: FakeSession;
Expand Down Expand Up @@ -1805,6 +1829,7 @@
`Unexpected events:\n\tGot: ${actualEventNames}\n\tWant: ${expectedEventNames}`
);

assert.strictEqual(spans[0].attributes['request.tag'], 'request-tag');
done();
});
});
Expand Down Expand Up @@ -1962,6 +1987,7 @@
key: 'k999',
thing: 'abc',
},
requestOptions: {requestTag: 'request-tag'},
};

let fakeSessionFactory: FakeSessionFactory;
Expand Down Expand Up @@ -2070,6 +2096,7 @@
`Unexpected events:\n\tGot: ${actualEventNames}\n\tWant: ${expectedEventNames}`
);

assert.strictEqual(spans[0].attributes['request.tag'], 'request-tag');
done();
});
});
Expand Down
6 changes: 0 additions & 6 deletions observability-test/observability.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
SEMATTRS_DB_NAME,
SEMATTRS_DB_SQL_TABLE,
SEMATTRS_DB_STATEMENT,
SEMATTRS_DB_SYSTEM,

Check warning on line 42 in observability-test/observability.ts

View workflow job for this annotation

GitHub Actions / lint

'SEMATTRS_DB_SYSTEM' is assigned a value but never used
SEMATTRS_EXCEPTION_MESSAGE,
} = require('@opentelemetry/semantic-conventions');

Expand Down Expand Up @@ -160,12 +160,6 @@
'Missing gcp.client.repo attribute'
);

assert.equal(
span.attributes[SEMATTRS_DB_SYSTEM],
'spanner',
'Missing DB_SYSTEM attribute'
);

assert.equal(
span.attributes[SEMATTRS_DB_SQL_TABLE],
'table',
Expand Down
Loading
Loading