Skip to content

Commit

Permalink
feat: (observability) propagate database name for every span generate…
Browse files Browse the repository at this point in the history
…d to aid in quick debugging

With this change customers shall always be able to identify which
database is being connected to.

Updates #2079
  • Loading branch information
odeke-em committed Oct 10, 2024
1 parent f489c94 commit 614c69b
Show file tree
Hide file tree
Showing 10 changed files with 253 additions and 62 deletions.
41 changes: 37 additions & 4 deletions observability-test/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import {Instance, Spanner} from '../src';
import * as pfy from '@google-cloud/promisify';
import {grpc} from 'google-gax';
import {MockError} from '../test/mockserver/mockspanner';
const {generateWithAllSpansHaveDBName} = require('./helper');

const fakePfy = extend({}, pfy, {
promisifyAll(klass, options) {
Expand Down Expand Up @@ -235,16 +236,20 @@ describe('Database', () => {
DatabaseCached = Object.assign({}, Database);
});

const withAllSpansHaveDBName = generateWithAllSpansHaveDBName(
INSTANCE.formattedName_ + '/databases/' + NAME
);

beforeEach(() => {
fakeCodec.encode = util.noop;
extend(Database, DatabaseCached);
database = new Database(INSTANCE, NAME, POOL_OPTIONS);
database.parent = INSTANCE;
database.databaseRole = 'parent_role';
database._observabilityOptions = {
INSTANCE._observabilityOptions = {
tracerProvider: provider,
enableExtendedTracing: false,
};
database = new Database(INSTANCE, NAME, POOL_OPTIONS);
database.parent = INSTANCE;
database.databaseRole = 'parent_role';
const gaxOpts = {};
const options: {
a: string;
Expand Down Expand Up @@ -285,6 +290,8 @@ describe('Database', () => {
const spans = traceExporter.getFinishedSpans();
assert.strictEqual(spans.length, 1, 'Exactly 1 span expected');

withAllSpansHaveDBName(spans);

const actualSpanNames: string[] = [];
const actualEventNames: string[] = [];
spans.forEach(span => {
Expand Down Expand Up @@ -333,6 +340,7 @@ describe('Database', () => {
traceExporter.forceFlush();
const spans = traceExporter.getFinishedSpans();
assert.strictEqual(spans.length, 1, 'Exactly 1 span expected');
withAllSpansHaveDBName(spans);

const actualSpanNames: string[] = [];
const actualEventNames: string[] = [];
Expand Down Expand Up @@ -523,6 +531,7 @@ describe('Database', () => {
traceExporter.forceFlush();
const spans = traceExporter.getFinishedSpans();
assert.strictEqual(spans.length, 1, 'Exactly 1 span expected');
withAllSpansHaveDBName(spans);

const actualSpanNames: string[] = [];
const actualEventNames: string[] = [];
Expand Down Expand Up @@ -604,6 +613,7 @@ describe('Database', () => {

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

const actualSpanNames: string[] = [];
const actualEventNames: string[] = [];
Expand Down Expand Up @@ -706,6 +716,8 @@ describe('Database', () => {

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

const actualEventNames: string[] = [];
const actualSpanNames: string[] = [];
spans.forEach(span => {
Expand Down Expand Up @@ -771,6 +783,7 @@ describe('Database', () => {
assert.strictEqual(resp, RESPONSE);
const spans = traceExporter.getFinishedSpans();
assert.strictEqual(spans.length, 1, 'Exactly 1 span expected');
withAllSpansHaveDBName(spans);

const actualEventNames: string[] = [];
const actualSpanNames: string[] = [];
Expand Down Expand Up @@ -836,6 +849,8 @@ describe('Database', () => {

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

const actualSpanNames: string[] = [];
const actualEventNames: string[] = [];
spans.forEach(span => {
Expand Down Expand Up @@ -911,6 +926,8 @@ describe('Database', () => {

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

const actualEventNames: string[] = [];
const actualSpanNames: string[] = [];
spans.forEach(span => {
Expand Down Expand Up @@ -958,6 +975,8 @@ describe('Database', () => {
assert.strictEqual(transaction, fakeTransaction);

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

assert.strictEqual(spans.length, 1, 'Exactly 1 span expected');
const actualEventNames: string[] = [];
const actualSpanNames: string[] = [];
Expand Down Expand Up @@ -1037,6 +1056,7 @@ describe('Database', () => {

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

const actualEventNames: string[] = [];
const actualSpanNames: string[] = [];
Expand Down Expand Up @@ -1091,6 +1111,7 @@ describe('Database', () => {

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

const actualEventNames: string[] = [];
const actualSpanNames: string[] = [];
Expand Down Expand Up @@ -1148,6 +1169,8 @@ describe('Database', () => {

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

const actualSpanNames: string[] = [];
const actualEventNames: string[] = [];
spans.forEach(span => {
Expand Down Expand Up @@ -1222,6 +1245,8 @@ describe('Database', () => {

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

const actualSpanNames: string[] = [];
const actualEventNames: string[] = [];
spans.forEach(span => {
Expand Down Expand Up @@ -1273,6 +1298,8 @@ describe('Database', () => {

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

const actualSpanNames: string[] = [];
const actualEventNames: string[] = [];
spans.forEach(span => {
Expand Down Expand Up @@ -1376,6 +1403,8 @@ describe('Database', () => {

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

const actualEventNames: string[] = [];
const actualSpanNames: string[] = [];
spans.forEach(span => {
Expand Down Expand Up @@ -1427,6 +1456,8 @@ describe('Database', () => {

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

const actualEventNames: string[] = [];
const actualSpanNames: string[] = [];
spans.forEach(span => {
Expand Down Expand Up @@ -1491,6 +1522,8 @@ describe('Database', () => {

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

const actualSpanNames: string[] = [];
const actualEventNames: string[] = [];
spans.forEach(span => {
Expand Down
15 changes: 15 additions & 0 deletions observability-test/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
*/

import {ContextManager, context} from '@opentelemetry/api';
import * as assert from 'assert';
const {ReadableSpan} = require('@opentelemetry/sdk-trace-base');
import {SEMATTRS_DB_NAME} from '@opentelemetry/semantic-conventions';

/**
* This utility exists as a test helper because mocha has builtin "context"
Expand All @@ -32,3 +35,15 @@ export function disableContextAndManager(manager: ContextManager) {
manager.disable();
context.disable();
}

export function generateWithAllSpansHaveDBName(dbName: String): Function {
return function (spans: (typeof ReadableSpan)[]) {
spans.forEach(span => {
assert.deepStrictEqual(
span.attributes[SEMATTRS_DB_NAME],
dbName,
`Span ${span.name} has mismatched DB_NAME`
);
});
};
}
Loading

0 comments on commit 614c69b

Please sign in to comment.