From 47a921976f4ab0f4946d0374216daa21aeddbebd Mon Sep 17 00:00:00 2001 From: Alka Trivedi Date: Thu, 9 Jan 2025 10:53:34 +0530 Subject: [PATCH] refactor: test --- observability-test/database.ts | 24 ++++++++++++++++++------ package.json | 2 +- samples/package.json | 2 +- src/index.ts | 2 ++ src/instrument.ts | 9 +++++++++ 5 files changed, 31 insertions(+), 8 deletions(-) diff --git a/observability-test/database.ts b/observability-test/database.ts index 3e61b7bf4..c7d389857 100644 --- a/observability-test/database.ts +++ b/observability-test/database.ts @@ -503,7 +503,6 @@ describe('Database', () => { }); describe('getSnapshot', () => { - let fakePool: FakeSessionPool; let fakeSessionFactory: FakeSessionFactory; let fakeSession: FakeSession; let fakeMultiplexedSession: FakeMultiplexedSession; @@ -2117,12 +2116,11 @@ describe('Database', () => { a: 'b', c: 'd', }; - - let fakePool: FakeSessionPool; let fakeSessionFactory: FakeSessionFactory; let fakeSession: FakeSession; let fakeSession2: FakeSession; let fakeMultiplexedSession: FakeMultiplexedSession; + let fakeMultiplexedSession2: FakeMultiplexedSession; let fakeSnapshot: FakeTransaction; let fakeSnapshot2: FakeTransaction; let fakeStream: Transform; @@ -2142,16 +2140,23 @@ describe('Database', () => { beforeEach(() => { fakeSessionFactory = database.sessionFactory_; fakeMultiplexedSession = new FakeMultiplexedSession(); + fakeMultiplexedSession2 = new FakeMultiplexedSession(); fakeSnapshot = new FakeTransaction( {} as google.spanner.v1.TransactionOptions.ReadOnly ); + fakeSnapshot2 = new FakeTransaction( + {} as google.spanner.v1.TransactionOptions.ReadOnly + ); fakeStream = through.obj(); + fakeStream2 = through.obj(); getSessionStub = ( sandbox.stub(fakeSessionFactory, 'getSession') as sinon.SinonStub ) .onFirstCall() - .callsFake(callback => callback(null, fakeMultiplexedSession)); + .callsFake(callback => callback(null, fakeMultiplexedSession)) + .onSecondCall() + .callsFake(callback => callback(null, fakeMultiplexedSession2)); ( sandbox.stub(fakeMultiplexedSession, 'snapshot') as sinon.SinonStub @@ -2159,6 +2164,8 @@ describe('Database', () => { sandbox.stub(fakeSnapshot, 'runStream').returns(fakeStream); + sandbox.stub(fakeSnapshot2, 'runStream').returns(fakeStream2); + sandbox.stub(fakeSessionFactory, 'isMultiplexedEnabled').returns(true); }); @@ -2274,6 +2281,8 @@ describe('Database', () => { code: grpc.status.NOT_FOUND, message: 'Session not found', } as grpc.ServiceError; + const endStub = sandbox.stub(fakeSnapshot, 'end'); + const endStub2 = sandbox.stub(fakeSnapshot2, 'end'); let rows = 0; database @@ -2281,6 +2290,8 @@ describe('Database', () => { .on('data', () => rows++) .on('error', err => { assert.strictEqual(err, sessionNotFoundError); + assert.strictEqual(endStub.callCount, 1); + assert.strictEqual(endStub2.callCount, 0); assert.strictEqual(rows, 0); provider.forceFlush(); @@ -2330,12 +2341,13 @@ describe('Database', () => { }); fakeStream.emit('error', sessionNotFoundError); + fakeStream2.push('row1'); + fakeStream2.push(null); }); }); describe('when GOOGLE_CLOUD_SPANNER_MULTIPLEXED_SESSION is disable', () => { beforeEach(() => { - fakePool = database.pool_; fakeSessionFactory = database.sessionFactory_; fakeSession = new FakeSession(); fakeSession2 = new FakeSession(); @@ -2498,7 +2510,7 @@ describe('Database', () => { await traceExporter.forceFlush(); const spans = traceExporter.getFinishedSpans(); - assert.strictEqual(spans.length, 2, 'Exactly 1 span expected'); + assert.strictEqual(spans.length, 2, 'Exactly 2 spans expected'); withAllSpansHaveDBName(spans); const actualSpanNames: string[] = []; diff --git a/package.json b/package.json index a633f96b1..15d9004b1 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "@google-cloud/spanner", "description": "Cloud Spanner Client Library for Node.js", - "version": "7.17.1", + "version": "7.16.0", "license": "Apache-2.0", "author": "Google Inc.", "engines": { diff --git a/samples/package.json b/samples/package.json index 4b31e02b7..82383126f 100644 --- a/samples/package.json +++ b/samples/package.json @@ -17,7 +17,7 @@ "dependencies": { "@google-cloud/kms": "^4.0.0", "@google-cloud/precise-date": "^4.0.0", - "@google-cloud/spanner": "^7.17.1", + "@google-cloud/spanner": "^7.16.0", "protobufjs": "^7.0.0", "yargs": "^17.0.0" }, diff --git a/src/index.ts b/src/index.ts index 9281484c9..f0f88a801 100644 --- a/src/index.ts +++ b/src/index.ts @@ -85,6 +85,7 @@ import * as v1 from './v1'; import { ObservabilityOptions, ensureInitialContextManagerSet, + ensureContextPropagation, } from './instrument'; // eslint-disable-next-line @typescript-eslint/no-var-requires @@ -377,6 +378,7 @@ class Spanner extends GrpcService { this._observabilityOptions?.enableEndToEndTracing ); ensureInitialContextManagerSet(); + ensureContextPropagation(); } /** diff --git a/src/instrument.ts b/src/instrument.ts index e4cb8d283..2c7998733 100644 --- a/src/instrument.ts +++ b/src/instrument.ts @@ -27,6 +27,7 @@ import { Span, SpanStatusCode, context, + propagation, trace, INVALID_SPAN_CONTEXT, ROOT_CONTEXT, @@ -99,6 +100,8 @@ const { AsyncHooksContextManager, } = require('@opentelemetry/context-async-hooks'); +const {W3CTraceContextPropagator} = require('@opentelemetry/core'); + /* * This function ensures that async/await works correctly by * checking if context.active() returns an invalid/unset context @@ -118,8 +121,14 @@ function ensureInitialContextManagerSet() { } } +function ensureContextPropagation() { + propagation.setGlobalPropagator(new W3CTraceContextPropagator()); +} + export {ensureInitialContextManagerSet}; +export {ensureContextPropagation}; + /** * startTrace begins an active span in the current active context * and passes it back to the set callback function. Each span will