Skip to content

Commit

Permalink
test: check for SessionPool annotations
Browse files Browse the repository at this point in the history
  • Loading branch information
odeke-em committed Jul 24, 2024
1 parent 0a49599 commit 62244a8
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/instrument.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ export function getTracer(config?: traceConfig) {
if (defaultTracerProvider) {
return defaultTracerProvider.getTracer(TRACER_NAME);
}
// Otherwise use the global tracer still named 'nodejs-spanner'
// Otherwise use the global tracer.
return trace.getTracer(TRACER_NAME);
}

Expand Down
93 changes: 93 additions & 0 deletions test/session-pool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,23 @@ import * as sp from '../src/session-pool';
import {Transaction} from '../src/transaction';
import {grpc} from 'google-gax';

const {ContextManager} = require('@opentelemetry/api');
const {
AsyncHooksContextManager,
} = require('@opentelemetry/context-async-hooks');
const {
AlwaysOnSampler,
NodeTracerProvider,
InMemorySpanExporter,
} = require('@opentelemetry/sdk-trace-node');
const {SimpleSpanProcessor} = require('@opentelemetry/sdk-trace-base');
const {
disableContextAndManager,
setGlobalContextManager,
getTracer,
setTracerProvider,
} = require('../src/instrument');

let pQueueOverride: typeof PQueue | null = null;

function FakePQueue(options) {
Expand Down Expand Up @@ -1349,6 +1366,82 @@ describe('SessionPool', () => {
});
});
});

describe('observability annotations on active span', () => {
const projectId = process.env.SPANNER_TEST_PROJECTID || 'orijtech';
const exporter = new InMemorySpanExporter();
const sampler = new AlwaysOnSampler();

let provider: typeof NodeTracerProvider;
// let contextManager: typeof ContextManager;
const contextManager = new AsyncHooksContextManager();
setGlobalContextManager(contextManager);

beforeEach(() => {
provider = new NodeTracerProvider({
sampler: sampler,
exporter: exporter,
});
provider.addSpanProcessor(new SimpleSpanProcessor(exporter));
provider.register();
setTracerProvider(provider);

sessionPool.isOpen = true;
sessionPool._isValidSession = () => true;
});

afterEach(async () => {
exporter.forceFlush();
exporter.reset();
});

after(async () => {
disableContextAndManager(contextManager);
await provider.shutdown();
});

it('acquire', () => {
getTracer().startActiveSpan('testSessionPool.acquire', async span => {
const fakeSession = createSession();
const now = Date.now();

const stub = sandbox
.stub(sessionPool, '_getSession')
.resolves(fakeSession);
const session = await sessionPool._acquire();
const [startTime] = stub.getCall(0).args;

assert(isAround(startTime, now));
assert.strictEqual(session, fakeSession);

// TODO: Investigate why the context at this
// point is NOT the same context as was used in
// the "await sessionPool._acquire() call.
await sessionPool._release(session);
span.end();

const spans = exporter.getFinishedSpans();
assert.strictEqual(
spans.length,
1,
'exactly 1 span should have been exported'
);
const span0 = spans[0];
assert.strictEqual(!span0.events, false, 'events must be set');
assert.strictEqual(span0.events.length > 0, true, 'events must be set');
const events = span0.events;

// Sort the events by earliest time of occurence.
events.sort((evtA, evtB) => {
return evtA.time < evtB.time;
});

// Now check to see that we at least acquired a valid session.
const event0 = events[0];
assert.strictEqual(event0.name, 'acquired a valid session');
});
});
});
});

function isAround(actual, expected) {
Expand Down

0 comments on commit 62244a8

Please sign in to comment.