diff --git a/OBSERVABILITY.md b/OBSERVABILITY.md index 71ab18226..0d03b06fb 100644 --- a/OBSERVABILITY.md +++ b/OBSERVABILITY.md @@ -80,7 +80,27 @@ const spanner = new Spanner({ observabilityOptions: { tracerProvider: provider, enableExtendedTracing: true, - }, + } +}), +``` + +#### End to end tracing + +In addition to client-side tracing, you can opt in for end-to-end tracing. End-to-end tracing helps you understand and debug latency issues that are specific to Spanner. Refer [here](https://cloud.google.com/spanner/docs/tracing-overview) for more information. + +You can opt-in by either: + +* Setting the environment variable `SPANNER_ENABLE_END_TO_END_TRACING=true` before your application is started +* In code, setting `enableEndToEndTracing: true` in your SpannerOptions before creating the Cloud Spanner client + +```javascript +const spanner = new Spanner({ + projectId: projectId, + observabilityOptions: { + tracerProvider: provider, + enableEndToEndTracing: true, + } +}), ``` #### OpenTelemetry gRPC instrumentation diff --git a/observability-test/spanner.ts b/observability-test/spanner.ts index c60549776..5f74ca036 100644 --- a/observability-test/spanner.ts +++ b/observability-test/spanner.ts @@ -26,6 +26,7 @@ import * as mockInstanceAdmin from '../test/mockserver/mockinstanceadmin'; import * as mockDatabaseAdmin from '../test/mockserver/mockdatabaseadmin'; import * as sinon from 'sinon'; import {Row} from '../src/partial-result-stream'; +import {END_TO_END_TRACING_HEADER} from '../src/common'; const { AlwaysOnSampler, NodeTracerProvider, @@ -1930,3 +1931,59 @@ describe('Traces for ExecuteStream broken stream retries', () => { ); }); }); + +describe('End to end tracing headers', () => { + let server: grpc.Server; + let spanner: Spanner; + let spannerMock: mock.MockSpanner; + let observabilityOptions: typeof ObservabilityOptions; + + beforeEach(async () => { + observabilityOptions = { + enableEndToEndTracing: true, + }; + + const setupResult = await setup(observabilityOptions); + spanner = setupResult.spanner; + server = setupResult.server; + spannerMock = setupResult.spannerMock; + }); + + afterEach(async () => { + spannerMock.resetRequests(); + spanner.close(); + server.tryShutdown(() => {}); + }); + + it('run', done => { + const instance = spanner.instance('instance'); + const database = instance.database('database'); + database.getTransaction((err, tx) => { + assert.ifError(err); + + tx!.run('SELECT 1', async () => { + tx!.end(); + let metadataCountWithE2EHeader = 0; + let metadataCountWithTraceParent = 0; + spannerMock.getMetadata().forEach(metadata => { + if (metadata.get(END_TO_END_TRACING_HEADER)[0] !== undefined) { + metadataCountWithE2EHeader++; + assert.strictEqual( + metadata.get(END_TO_END_TRACING_HEADER)[0], + 'true' + ); + } + if (metadata.get('traceparent')[0] !== undefined) { + metadataCountWithTraceParent++; + } + }); + + // Batch Create Session request and Select 1 request. + assert.strictEqual(spannerMock.getRequests().length, 2); + assert.strictEqual(metadataCountWithE2EHeader, 2); + assert.strictEqual(metadataCountWithTraceParent, 2); + done(); + }); + }); + }); +}); diff --git a/package.json b/package.json index 3f880bd82..15d9004b1 100644 --- a/package.json +++ b/package.json @@ -60,6 +60,7 @@ "@google-cloud/promisify": "^4.0.0", "@grpc/proto-loader": "^0.7.0", "@opentelemetry/api": "^1.9.0", + "@opentelemetry/core": "^1.27.0", "@opentelemetry/context-async-hooks": "^1.26.0", "@opentelemetry/semantic-conventions": "^1.25.1", "@types/big.js": "^6.0.0", diff --git a/samples/observability-traces.js b/samples/observability-traces.js index f8c802d4d..024223acd 100644 --- a/samples/observability-traces.js +++ b/samples/observability-traces.js @@ -76,6 +76,7 @@ async function main( observabilityOptions: { tracerProvider: provider, enableExtendedTracing: true, + enableEndToEndTracing: true, }, }); diff --git a/src/common.ts b/src/common.ts index 634df1210..6eaa85984 100644 --- a/src/common.ts +++ b/src/common.ts @@ -81,6 +81,11 @@ export const CLOUD_RESOURCE_HEADER = 'google-cloud-resource-prefix'; */ export const LEADER_AWARE_ROUTING_HEADER = 'x-goog-spanner-route-to-leader'; +/* + * END TO END TRACING header. + */ +export const END_TO_END_TRACING_HEADER = 'x-goog-spanner-end-to-end-tracing'; + /** * Add Leader aware routing header to existing header list. * @param headers Existing header list. @@ -88,3 +93,25 @@ export const LEADER_AWARE_ROUTING_HEADER = 'x-goog-spanner-route-to-leader'; export function addLeaderAwareRoutingHeader(headers: {[k: string]: string}) { headers[LEADER_AWARE_ROUTING_HEADER] = 'true'; } + +/** + * Returns common headers to add. + * @param headers Common header list. + */ +export function getCommonHeaders( + resourceName: string, + enableTracing?: boolean +) { + const headers: {[k: string]: string} = {}; + + if ( + process.env.SPANNER_ENABLE_END_TO_END_TRACING === 'true' || + enableTracing + ) { + headers[END_TO_END_TRACING_HEADER] = 'true'; + } + + headers[CLOUD_RESOURCE_HEADER] = resourceName; + + return headers; +} diff --git a/src/database.ts b/src/database.ts index e8b91480f..a780092aa 100644 --- a/src/database.ts +++ b/src/database.ts @@ -91,6 +91,7 @@ import { ResourceCallback, Schema, addLeaderAwareRoutingHeader, + getCommonHeaders, } from './common'; import {finished, Duplex, Readable, Transform} from 'stream'; import {PreciseDate} from '@google-cloud/precise-date'; @@ -340,7 +341,7 @@ class Database extends common.GrpcServiceObject { formattedName_: string; pool_: SessionPoolInterface; queryOptions_?: spannerClient.spanner.v1.ExecuteSqlRequest.IQueryOptions; - resourceHeader_: {[k: string]: string}; + commonHeaders_: {[k: string]: string}; request: DatabaseRequest; databaseRole?: string | null; labels?: {[k: string]: string} | null; @@ -471,11 +472,13 @@ class Database extends common.GrpcServiceObject { dbName: this.formattedName_, }; - this.resourceHeader_ = { - [CLOUD_RESOURCE_HEADER]: this.formattedName_, - }; this.request = instance.request; this._observabilityOptions = instance._observabilityOptions; + this.commonHeaders_ = getCommonHeaders( + this.formattedName_, + this._observabilityOptions?.enableEndToEndTracing + ); + // eslint-disable-next-line @typescript-eslint/no-explicit-any this.requestStream = instance.requestStream as any; this.pool_.on('error', this.emit.bind(this, 'error')); @@ -582,7 +585,7 @@ class Database extends common.GrpcServiceObject { method: 'updateDatabase', reqOpts, gaxOpts, - headers: this.resourceHeader_, + headers: this.commonHeaders_, }, callback! ); @@ -688,7 +691,7 @@ class Database extends common.GrpcServiceObject { sessionCount: count, }; - const headers = this.resourceHeader_; + const headers = this.commonHeaders_; if (this._getSpanner().routeToLeaderEnabled) { addLeaderAwareRoutingHeader(headers); } @@ -989,7 +992,7 @@ class Database extends common.GrpcServiceObject { reqOpts.session.creatorRole = options.databaseRole || this.databaseRole || null; - const headers = this.resourceHeader_; + const headers = this.commonHeaders_; if (this._getSpanner().routeToLeaderEnabled) { addLeaderAwareRoutingHeader(headers); } @@ -1215,7 +1218,7 @@ class Database extends common.GrpcServiceObject { method: 'dropDatabase', reqOpts, gaxOpts, - headers: this.resourceHeader_, + headers: this.commonHeaders_, }, callback! ); @@ -1447,7 +1450,7 @@ class Database extends common.GrpcServiceObject { method: 'getDatabase', reqOpts, gaxOpts, - headers: this.resourceHeader_, + headers: this.commonHeaders_, }, (err, resp) => { if (resp) { @@ -1701,7 +1704,7 @@ class Database extends common.GrpcServiceObject { method: 'getDatabaseDdl', reqOpts, gaxOpts, - headers: this.resourceHeader_, + headers: this.commonHeaders_, }, // eslint-disable-next-line @typescript-eslint/no-explicit-any (err, statements, ...args: any[]) => { @@ -1781,7 +1784,7 @@ class Database extends common.GrpcServiceObject { method: 'getIamPolicy', reqOpts, gaxOpts: options.gaxOptions, - headers: this.resourceHeader_, + headers: this.commonHeaders_, }, (err, resp) => { callback!(err, resp); @@ -1920,7 +1923,7 @@ class Database extends common.GrpcServiceObject { method: 'listSessions', reqOpts, gaxOpts, - headers: this.resourceHeader_, + headers: this.commonHeaders_, }, (err, sessions, nextPageRequest, ...args) => { if (err) { @@ -2013,7 +2016,7 @@ class Database extends common.GrpcServiceObject { method: 'listSessionsStream', reqOpts, gaxOpts, - headers: this.resourceHeader_, + headers: this.commonHeaders_, }); } @@ -2405,7 +2408,7 @@ class Database extends common.GrpcServiceObject { method: 'listDatabaseRoles', reqOpts, gaxOpts, - headers: this.resourceHeader_, + headers: this.commonHeaders_, }, (err, roles, nextPageRequest, ...args) => { const nextQuery = nextPageRequest! @@ -2615,7 +2618,7 @@ class Database extends common.GrpcServiceObject { method: 'restoreDatabase', reqOpts, gaxOpts, - headers: this.resourceHeader_, + headers: this.commonHeaders_, }, (err, operation, resp) => { if (err) { @@ -3514,7 +3517,7 @@ class Database extends common.GrpcServiceObject { method: 'batchWrite', reqOpts, gaxOpts, - headers: this.resourceHeader_, + headers: this.commonHeaders_, }); dataStream .once('data', () => (dataReceived = true)) @@ -3796,7 +3799,7 @@ class Database extends common.GrpcServiceObject { method: 'setIamPolicy', reqOpts, gaxOpts: gaxOpts, - headers: this.resourceHeader_, + headers: this.commonHeaders_, }, (err, resp) => { callback!(err, resp); @@ -3926,7 +3929,7 @@ class Database extends common.GrpcServiceObject { method: 'updateDatabaseDdl', reqOpts, gaxOpts, - headers: this.resourceHeader_, + headers: this.commonHeaders_, }, callback! ); diff --git a/src/index.ts b/src/index.ts index 0bbdccd01..f0f88a801 100644 --- a/src/index.ts +++ b/src/index.ts @@ -39,6 +39,7 @@ import { IProtoMessageParams, IProtoEnumParams, } from './codec'; +import {context, propagation} from '@opentelemetry/api'; import {Backup} from './backup'; import {Database} from './database'; import { @@ -66,6 +67,7 @@ import { PagedOptionsWithFilter, CLOUD_RESOURCE_HEADER, NormalCallback, + getCommonHeaders, } from './common'; import {Session} from './session'; import {SessionPool} from './session-pool'; @@ -83,6 +85,7 @@ import * as v1 from './v1'; import { ObservabilityOptions, ensureInitialContextManagerSet, + ensureContextPropagation, } from './instrument'; // eslint-disable-next-line @typescript-eslint/no-var-requires @@ -242,7 +245,7 @@ class Spanner extends GrpcService { instanceConfigs_: Map; projectIdReplaced_: boolean; projectFormattedName_: string; - resourceHeader_: {[k: string]: string}; + commonHeaders_: {[k: string]: string}; routeToLeaderEnabled = true; directedReadOptions: google.spanner.v1.IDirectedReadOptions | null; _observabilityOptions: ObservabilityOptions | undefined; @@ -368,12 +371,14 @@ class Spanner extends GrpcService { this.instanceConfigs_ = new Map(); this.projectIdReplaced_ = false; this.projectFormattedName_ = 'projects/' + this.projectId; - this.resourceHeader_ = { - [CLOUD_RESOURCE_HEADER]: this.projectFormattedName_, - }; this.directedReadOptions = directedReadOptions; this._observabilityOptions = options.observabilityOptions; + this.commonHeaders_ = getCommonHeaders( + this.projectFormattedName_, + this._observabilityOptions?.enableEndToEndTracing + ); ensureInitialContextManagerSet(); + ensureContextPropagation(); } /** @@ -586,7 +591,7 @@ class Spanner extends GrpcService { method: 'createInstance', reqOpts, gaxOpts: config.gaxOptions, - headers: this.resourceHeader_, + headers: this.commonHeaders_, }, (err, operation, resp) => { if (err) { @@ -732,7 +737,7 @@ class Spanner extends GrpcService { method: 'listInstances', reqOpts, gaxOpts, - headers: this.resourceHeader_, + headers: this.commonHeaders_, }, (err, instances, nextPageRequest, ...args) => { let instanceInstances: Instance[] | null = null; @@ -816,7 +821,7 @@ class Spanner extends GrpcService { method: 'listInstancesStream', reqOpts, gaxOpts, - headers: this.resourceHeader_, + headers: this.commonHeaders_, }); } @@ -982,7 +987,7 @@ class Spanner extends GrpcService { method: 'createInstanceConfig', reqOpts, gaxOpts: config.gaxOptions, - headers: this.resourceHeader_, + headers: this.commonHeaders_, }, (err, operation, resp) => { if (err) { @@ -1125,7 +1130,7 @@ class Spanner extends GrpcService { method: 'listInstanceConfigs', reqOpts, gaxOpts, - headers: this.resourceHeader_, + headers: this.commonHeaders_, }, (err, instanceConfigs, nextPageRequest, ...args) => { const nextQuery = nextPageRequest! @@ -1201,7 +1206,7 @@ class Spanner extends GrpcService { method: 'listInstanceConfigsStream', reqOpts, gaxOpts, - headers: this.resourceHeader_, + headers: this.commonHeaders_, }); } @@ -1296,7 +1301,7 @@ class Spanner extends GrpcService { method: 'getInstanceConfig', reqOpts, gaxOpts, - headers: this.resourceHeader_, + headers: this.commonHeaders_, }, (err, instanceConfig) => { callback!(err, instanceConfig); @@ -1416,7 +1421,7 @@ class Spanner extends GrpcService { method: 'listInstanceConfigOperations', reqOpts, gaxOpts, - headers: this.resourceHeader_, + headers: this.commonHeaders_, }, (err, operations, nextPageRequest, ...args) => { const nextQuery = nextPageRequest! @@ -1532,6 +1537,12 @@ class Spanner extends GrpcService { config.headers[CLOUD_RESOURCE_HEADER], projectId! ); + // Do context propagation + propagation.inject(context.active(), config.headers, { + set: (carrier, key, value) => { + carrier[key] = value; // Set the span context (trace and span ID) + }, + }); const requestFn = gaxClient[config.method].bind( gaxClient, reqOpts, diff --git a/src/instance.ts b/src/instance.ts index 4986e3ecd..72257b24c 100644 --- a/src/instance.ts +++ b/src/instance.ts @@ -31,7 +31,7 @@ import { NormalCallback, ResourceCallback, PagedOptionsWithFilter, - CLOUD_RESOURCE_HEADER, + getCommonHeaders, } from './common'; import {Duplex} from 'stream'; import {SessionPoolOptions, SessionPool} from './session-pool'; @@ -164,7 +164,7 @@ class Instance extends common.GrpcServiceObject { requestStream: (config: RequestConfig) => Duplex; databases_: Map; metadata?: IInstance; - resourceHeader_: {[k: string]: string}; + commonHeaders_: {[k: string]: string}; _observabilityOptions?: ObservabilityOptions; constructor(spanner: Spanner, name: string) { const formattedName_ = Instance.formatName_(spanner.projectId, name); @@ -238,10 +238,11 @@ class Instance extends common.GrpcServiceObject { this.request = spanner.request.bind(spanner); this.requestStream = spanner.requestStream.bind(spanner); this.databases_ = new Map(); - this.resourceHeader_ = { - [CLOUD_RESOURCE_HEADER]: this.formattedName_, - }; this._observabilityOptions = spanner._observabilityOptions; + this.commonHeaders_ = getCommonHeaders( + this.formattedName_, + this._observabilityOptions?.enableEndToEndTracing + ); } /** @@ -432,7 +433,7 @@ class Instance extends common.GrpcServiceObject { method: 'listBackups', reqOpts, gaxOpts, - headers: this.resourceHeader_, + headers: this.commonHeaders_, }, (err, backups, nextPageRequest, ...args) => { let backupInstances: Backup[] | null = null; @@ -519,7 +520,7 @@ class Instance extends common.GrpcServiceObject { method: 'listBackupsStream', reqOpts, gaxOpts, - headers: this.resourceHeader_, + headers: this.commonHeaders_, }); } @@ -638,7 +639,7 @@ class Instance extends common.GrpcServiceObject { method: 'listBackupOperations', reqOpts, gaxOpts, - headers: this.resourceHeader_, + headers: this.commonHeaders_, }, (err, operations, nextPageRequest, ...args) => { const nextQuery = nextPageRequest! @@ -766,7 +767,7 @@ class Instance extends common.GrpcServiceObject { method: 'listDatabaseOperations', reqOpts, gaxOpts, - headers: this.resourceHeader_, + headers: this.commonHeaders_, }, (err, operations, nextPageRequest, ...args) => { const nextQuery = nextPageRequest! @@ -920,7 +921,7 @@ class Instance extends common.GrpcServiceObject { method: 'createDatabase', reqOpts, gaxOpts: options.gaxOptions, - headers: this.resourceHeader_, + headers: this.commonHeaders_, }, (err, operation, resp) => { if (err) { @@ -1059,7 +1060,7 @@ class Instance extends common.GrpcServiceObject { method: 'deleteInstance', reqOpts, gaxOpts, - headers: this.resourceHeader_, + headers: this.commonHeaders_, }, (err, resp) => { if (!err) { @@ -1355,7 +1356,7 @@ class Instance extends common.GrpcServiceObject { method: 'listDatabases', reqOpts, gaxOpts, - headers: this.resourceHeader_, + headers: this.commonHeaders_, }, (err, rowDatabases, nextPageRequest, ...args) => { let databases: Database[] | null = null; @@ -1443,7 +1444,7 @@ class Instance extends common.GrpcServiceObject { method: 'listDatabasesStream', reqOpts, gaxOpts, - headers: this.resourceHeader_, + headers: this.commonHeaders_, }); } @@ -1540,7 +1541,7 @@ class Instance extends common.GrpcServiceObject { method: 'getInstance', reqOpts, gaxOpts: options.gaxOptions, - headers: this.resourceHeader_, + headers: this.commonHeaders_, }, (err, resp) => { if (resp) { @@ -1636,7 +1637,7 @@ class Instance extends common.GrpcServiceObject { method: 'updateInstance', reqOpts, gaxOpts, - headers: this.resourceHeader_, + headers: this.commonHeaders_, }, callback! ); diff --git a/src/instrument.ts b/src/instrument.ts index 8ad123bf6..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, @@ -59,6 +60,7 @@ interface SQLStatement { interface ObservabilityOptions { tracerProvider: TracerProvider; enableExtendedTracing?: boolean; + enableEndToEndTracing?: boolean; } export type {ObservabilityOptions}; @@ -98,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 @@ -116,8 +120,15 @@ function ensureInitialContextManagerSet() { context.setGlobalContextManager(contextManager); } } + +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 diff --git a/src/session.ts b/src/session.ts index 2ece6b45d..32d79d352 100644 --- a/src/session.ts +++ b/src/session.ts @@ -38,8 +38,8 @@ import { import {ServiceObjectConfig} from '@google-cloud/common'; import { NormalCallback, - CLOUD_RESOURCE_HEADER, addLeaderAwareRoutingHeader, + getCommonHeaders, } from './common'; import {grpc, CallOptions} from 'google-gax'; import IRequestOptions = google.spanner.v1.IRequestOptions; @@ -117,7 +117,7 @@ export class Session extends common.GrpcServiceObject { txn?: Transaction; lastUsed?: number; lastError?: grpc.ServiceError; - resourceHeader_: {[k: string]: string}; + commonHeaders_: {[k: string]: string}; constructor(database: Database, name?: string) { const methods = { /** @@ -259,9 +259,10 @@ export class Session extends common.GrpcServiceObject { }, } as {} as ServiceObjectConfig); - this.resourceHeader_ = { - [CLOUD_RESOURCE_HEADER]: (this.parent as Database).formattedName_, - }; + this.commonHeaders_ = getCommonHeaders( + (this.parent as Database).formattedName_, + database._observabilityOptions?.enableEndToEndTracing + ); this.request = database.request; this.requestStream = database.requestStream; @@ -322,7 +323,7 @@ export class Session extends common.GrpcServiceObject { method: 'deleteSession', reqOpts, gaxOpts, - headers: this.resourceHeader_, + headers: this.commonHeaders_, }, callback! ); @@ -384,7 +385,7 @@ export class Session extends common.GrpcServiceObject { name: this.formattedName_, }; - const headers = this.resourceHeader_; + const headers = this.commonHeaders_; if (this._getSpanner().routeToLeaderEnabled) { addLeaderAwareRoutingHeader(headers); } @@ -446,7 +447,7 @@ export class Session extends common.GrpcServiceObject { method: 'executeSql', reqOpts, gaxOpts, - headers: this.resourceHeader_, + headers: this.commonHeaders_, }, callback! ); diff --git a/src/transaction.ts b/src/transaction.ts index fa1f10814..725ec3235 100644 --- a/src/transaction.ts +++ b/src/transaction.ts @@ -37,8 +37,8 @@ import {getActiveOrNoopSpan} from './instrument'; import {google as spannerClient} from '../protos/protos'; import { NormalCallback, - CLOUD_RESOURCE_HEADER, addLeaderAwareRoutingHeader, + getCommonHeaders, } from './common'; import {google} from '../protos/protos'; import IAny = google.protobuf.IAny; @@ -289,7 +289,7 @@ export class Snapshot extends EventEmitter { requestStream: (config: {}) => Readable; session: Session; queryOptions?: IQueryOptions; - resourceHeader_: {[k: string]: string}; + commonHeaders_: {[k: string]: string}; requestOptions?: Pick; _observabilityOptions?: ObservabilityOptions; protected _dbName?: string; @@ -353,12 +353,13 @@ export class Snapshot extends EventEmitter { const readOnly = Snapshot.encodeTimestampBounds(options || {}); this._options = {readOnly}; this._dbName = (this.session.parent as Database).formattedName_; - this.resourceHeader_ = { - [CLOUD_RESOURCE_HEADER]: this._dbName, - }; this._waitingRequests = []; this._inlineBeginStarted = false; this._observabilityOptions = session._observabilityOptions; + this.commonHeaders_ = getCommonHeaders( + this._dbName, + this._observabilityOptions?.enableEndToEndTracing + ); } /** @@ -432,7 +433,7 @@ export class Snapshot extends EventEmitter { reqOpts.requestOptions = this.requestOptions; } - const headers = this.resourceHeader_; + const headers = this.commonHeaders_; if ( this._getSpanner().routeToLeaderEnabled && (this._options.readWrite !== undefined || @@ -697,7 +698,7 @@ export class Snapshot extends EventEmitter { } ); - const headers = this.resourceHeader_; + const headers = this.commonHeaders_; if ( this._getSpanner().routeToLeaderEnabled && (this._options.readWrite !== undefined || @@ -1281,7 +1282,7 @@ export class Snapshot extends EventEmitter { }); }; - const headers = this.resourceHeader_; + const headers = this.commonHeaders_; if ( this._getSpanner().routeToLeaderEnabled && (this._options.readWrite !== undefined || @@ -1956,7 +1957,7 @@ export class Transaction extends Dml { statements, } as spannerClient.spanner.v1.ExecuteBatchDmlRequest; - const headers = this.resourceHeader_; + const headers = this.commonHeaders_; if (this._getSpanner().routeToLeaderEnabled) { addLeaderAwareRoutingHeader(headers); } @@ -2182,7 +2183,7 @@ export class Transaction extends Dml { this.requestOptions ); - const headers = this.resourceHeader_; + const headers = this.commonHeaders_; if (this._getSpanner().routeToLeaderEnabled) { addLeaderAwareRoutingHeader(headers); } @@ -2532,7 +2533,7 @@ export class Transaction extends Dml { transactionId, }; - const headers = this.resourceHeader_; + const headers = this.commonHeaders_; if (this._getSpanner().routeToLeaderEnabled) { addLeaderAwareRoutingHeader(headers); } diff --git a/test/database.ts b/test/database.ts index 684b0c1d3..f60286d3d 100644 --- a/test/database.ts +++ b/test/database.ts @@ -358,8 +358,8 @@ describe('Database', () => { calledWith.createMethod(null, options, done); }); - it('should set the resourceHeader_', () => { - assert.deepStrictEqual(database.resourceHeader_, { + it('should set the commonHeaders_', () => { + assert.deepStrictEqual(database.commonHeaders_, { [CLOUD_RESOURCE_HEADER]: database.formattedName_, }); }); @@ -400,7 +400,7 @@ describe('Database', () => { headers, Object.assign( {[LEADER_AWARE_ROUTING_HEADER]: true}, - database.resourceHeader_ + database.commonHeaders_ ) ); }); @@ -531,7 +531,7 @@ describe('Database', () => { assert.deepStrictEqual(METADATA, ORIGINAL_METADATA); assert.deepStrictEqual(config.gaxOpts, {}); - assert.deepStrictEqual(config.headers, database.resourceHeader_); + assert.deepStrictEqual(config.headers, database.commonHeaders_); assert.strictEqual(callback_, callback); @@ -676,7 +676,7 @@ describe('Database', () => { assert.strictEqual(args.method, 'batchWrite'); assert.deepStrictEqual(args.reqOpts, expectedReqOpts); assert.deepStrictEqual(args.gaxOpts, expectedGaxOpts); - assert.deepStrictEqual(args.headers, database.resourceHeader_); + assert.deepStrictEqual(args.headers, database.commonHeaders_); }); it('should return error when passing an empty list of mutationGroups', done => { @@ -1114,7 +1114,7 @@ describe('Database', () => { database: database.formattedName_, }); assert.deepStrictEqual(config.gaxOpts, {}); - assert.deepStrictEqual(config.headers, database.resourceHeader_); + assert.deepStrictEqual(config.headers, database.commonHeaders_); assert.strictEqual(callback, assert.ifError); }; @@ -1386,7 +1386,7 @@ describe('Database', () => { name: database.formattedName_, }); assert.deepStrictEqual(config.gaxOpts, {}); - assert.deepStrictEqual(config.headers, database.resourceHeader_); + assert.deepStrictEqual(config.headers, database.commonHeaders_); return requestReturnValue; }; @@ -1413,7 +1413,7 @@ describe('Database', () => { database: database.formattedName_, }); assert.deepStrictEqual(config.gaxOpts, {}); - assert.deepStrictEqual(config.headers, database.resourceHeader_); + assert.deepStrictEqual(config.headers, database.commonHeaders_); done(); }; @@ -2038,7 +2038,7 @@ describe('Database', () => { statements: STATEMENTS, }); assert.deepStrictEqual(config.gaxOpts, {}); - assert.deepStrictEqual(config.headers, database.resourceHeader_); + assert.deepStrictEqual(config.headers, database.commonHeaders_); assert.strictEqual(callback, assert.ifError); return requestReturnValue; }; @@ -2104,7 +2104,7 @@ describe('Database', () => { config.headers, Object.assign( {[LEADER_AWARE_ROUTING_HEADER]: true}, - database.resourceHeader_ + database.commonHeaders_ ) ); @@ -2491,7 +2491,7 @@ describe('Database', () => { assert.strictEqual(config.method, 'listSessions'); assert.deepStrictEqual(config.reqOpts, expectedReqOpts); assert.deepStrictEqual(config.gaxOpts, gaxOpts); - assert.deepStrictEqual(config.headers, database.resourceHeader_); + assert.deepStrictEqual(config.headers, database.commonHeaders_); done(); }; @@ -2664,7 +2664,7 @@ describe('Database', () => { assert.notStrictEqual(config.reqOpts, OPTIONS); assert.deepStrictEqual(config.gaxOpts, OPTIONS.gaxOptions); - assert.deepStrictEqual(config.headers, database.resourceHeader_); + assert.deepStrictEqual(config.headers, database.commonHeaders_); return returnValue; }; @@ -3271,7 +3271,7 @@ describe('Database', () => { assert.notStrictEqual(config.reqOpts, QUERY); assert.deepStrictEqual(QUERY, ORIGINAL_QUERY); assert.deepStrictEqual(config.gaxOpts, {}); - assert.deepStrictEqual(config.headers, database.resourceHeader_); + assert.deepStrictEqual(config.headers, database.commonHeaders_); done(); }; diff --git a/test/index.ts b/test/index.ts index 9be38bb6f..5952c3dcd 100644 --- a/test/index.ts +++ b/test/index.ts @@ -334,8 +334,8 @@ describe('Spanner', () => { ); }); - it('should set the resourceHeader_', () => { - assert.deepStrictEqual(spanner.resourceHeader_, { + it('should set the commonHeaders_', () => { + assert.deepStrictEqual(spanner.commonHeaders_, { [CLOUD_RESOURCE_HEADER]: spanner.projectFormattedName_, }); }); @@ -753,7 +753,7 @@ describe('Spanner', () => { }, }); assert.strictEqual(config.gaxOpts, undefined); - assert.deepStrictEqual(config.headers, spanner.resourceHeader_); + assert.deepStrictEqual(config.headers, spanner.commonHeaders_); done(); }; spanner.createInstance(NAME, CONFIG, assert.ifError); @@ -934,7 +934,7 @@ describe('Spanner', () => { assert.deepStrictEqual(OPTIONS, ORIGINAL_OPTIONS); assert.deepStrictEqual(config.gaxOpts, {}); - assert.deepStrictEqual(config.headers, spanner.resourceHeader_); + assert.deepStrictEqual(config.headers, spanner.commonHeaders_); done(); }; @@ -962,7 +962,7 @@ describe('Spanner', () => { assert.notStrictEqual(config.gaxOpts, gaxOptions); assert.notDeepStrictEqual(config.gaxOpts, gaxOptions); assert.deepStrictEqual(config.gaxOpts, expectedGaxOpts); - assert.deepStrictEqual(config.headers, spanner.resourceHeader_); + assert.deepStrictEqual(config.headers, spanner.commonHeaders_); done(); }; @@ -998,7 +998,7 @@ describe('Spanner', () => { assert.notStrictEqual(config.gaxOpts, gaxOptions); assert.notDeepStrictEqual(config.gaxOpts, gaxOptions); assert.deepStrictEqual(config.gaxOpts, expectedGaxOpts); - assert.deepStrictEqual(config.headers, spanner.resourceHeader_); + assert.deepStrictEqual(config.headers, spanner.commonHeaders_); done(); }; @@ -1126,7 +1126,7 @@ describe('Spanner', () => { assert.deepStrictEqual(OPTIONS, ORIGINAL_OPTIONS); assert.deepStrictEqual(config.gaxOpts, {}); - assert.deepStrictEqual(config.headers, spanner.resourceHeader_); + assert.deepStrictEqual(config.headers, spanner.commonHeaders_); return returnValue as Duplex; }; @@ -1153,7 +1153,7 @@ describe('Spanner', () => { assert.notStrictEqual(config.gaxOpts, gaxOptions); assert.notDeepStrictEqual(config.gaxOpts, gaxOptions); assert.deepStrictEqual(config.gaxOpts, expectedGaxOpts); - assert.deepStrictEqual(config.headers, spanner.resourceHeader_); + assert.deepStrictEqual(config.headers, spanner.commonHeaders_); return returnValue; }; @@ -1188,7 +1188,7 @@ describe('Spanner', () => { assert.notStrictEqual(config.gaxOpts, gaxOptions); assert.notDeepStrictEqual(config.gaxOpts, gaxOptions); assert.deepStrictEqual(config.gaxOpts, expectedGaxOpts); - assert.deepStrictEqual(config.headers, spanner.resourceHeader_); + assert.deepStrictEqual(config.headers, spanner.commonHeaders_); return returnValue; }; @@ -1273,7 +1273,7 @@ describe('Spanner', () => { }, }); assert.strictEqual(config.gaxOpts, undefined); - assert.deepStrictEqual(config.headers, spanner.resourceHeader_); + assert.deepStrictEqual(config.headers, spanner.commonHeaders_); done(); }; spanner.createInstanceConfig(NAME, CONFIG, assert.ifError); @@ -1402,7 +1402,7 @@ describe('Spanner', () => { const gaxOpts = config.gaxOpts; assert.deepStrictEqual(gaxOpts, options.gaxOptions); - assert.deepStrictEqual(config.headers, spanner.resourceHeader_); + assert.deepStrictEqual(config.headers, spanner.commonHeaders_); return returnValue; }; @@ -1431,7 +1431,7 @@ describe('Spanner', () => { assert.notStrictEqual(config.gaxOpts, gaxOptions); assert.notDeepStrictEqual(config.gaxOpts, gaxOptions); assert.deepStrictEqual(config.gaxOpts, expectedGaxOpts); - assert.deepStrictEqual(config.headers, spanner.resourceHeader_); + assert.deepStrictEqual(config.headers, spanner.commonHeaders_); done(); }; @@ -1466,7 +1466,7 @@ describe('Spanner', () => { assert.notStrictEqual(config.gaxOpts, gaxOptions); assert.notDeepStrictEqual(config.gaxOpts, gaxOptions); assert.deepStrictEqual(config.gaxOpts, expectedGaxOpts); - assert.deepStrictEqual(config.headers, spanner.resourceHeader_); + assert.deepStrictEqual(config.headers, spanner.commonHeaders_); done(); }; @@ -1686,7 +1686,7 @@ describe('Spanner', () => { assert.notStrictEqual(reqOpts, OPTIONS); assert.deepStrictEqual(config.gaxOpts, OPTIONS.gaxOptions); - assert.deepStrictEqual(config.headers, spanner.resourceHeader_); + assert.deepStrictEqual(config.headers, spanner.commonHeaders_); return returnValue; }; @@ -1740,7 +1740,7 @@ describe('Spanner', () => { assert.notStrictEqual(config.gaxOpts, gaxOptions); assert.notDeepStrictEqual(config.gaxOpts, gaxOptions); assert.deepStrictEqual(config.gaxOpts, expectedGaxOpts); - assert.deepStrictEqual(config.headers, spanner.resourceHeader_); + assert.deepStrictEqual(config.headers, spanner.commonHeaders_); return returnValue; }; @@ -1780,7 +1780,7 @@ describe('Spanner', () => { assert.notStrictEqual(config.gaxOpts, gaxOptions); assert.notDeepStrictEqual(config.gaxOpts, gaxOptions); assert.deepStrictEqual(config.gaxOpts, expectedGaxOpts); - assert.deepStrictEqual(config.headers, spanner.resourceHeader_); + assert.deepStrictEqual(config.headers, spanner.commonHeaders_); return returnValue; }; @@ -1817,7 +1817,7 @@ describe('Spanner', () => { const gaxOpts = config.gaxOpts; assert.deepStrictEqual(gaxOpts, options.gaxOptions); - assert.deepStrictEqual(config.headers, spanner.resourceHeader_); + assert.deepStrictEqual(config.headers, spanner.commonHeaders_); return returnValue; }; diff --git a/test/instance.ts b/test/instance.ts index 30c3d3b4d..7d6462dfb 100644 --- a/test/instance.ts +++ b/test/instance.ts @@ -174,8 +174,8 @@ describe('Instance', () => { calledWith.createMethod(null, options, done); }); - it('should set the resourceHeader_', () => { - assert.deepStrictEqual(instance.resourceHeader_, { + it('should set the commonHeaders_', () => { + assert.deepStrictEqual(instance.commonHeaders_, { [CLOUD_RESOURCE_HEADER]: instance.formattedName_, }); }); @@ -218,7 +218,7 @@ describe('Instance', () => { createStatement: 'CREATE DATABASE `' + NAME + '`', }); assert.strictEqual(config.gaxOpts, undefined); - assert.deepStrictEqual(config.headers, instance.resourceHeader_); + assert.deepStrictEqual(config.headers, instance.commonHeaders_); done(); }; @@ -556,7 +556,7 @@ describe('Instance', () => { name: instance.formattedName_, }); assert.deepStrictEqual(config.gaxOpts, {}); - assert.deepStrictEqual(config.headers, instance.resourceHeader_); + assert.deepStrictEqual(config.headers, instance.commonHeaders_); callback(); // done() }; @@ -930,7 +930,7 @@ describe('Instance', () => { assert.deepStrictEqual(OPTIONS, ORIGINAL_OPTIONS); assert.deepStrictEqual(config.gaxOpts, OPTIONS.gaxOptions); - assert.deepStrictEqual(config.headers, instance.resourceHeader_); + assert.deepStrictEqual(config.headers, instance.commonHeaders_); done(); }; @@ -1120,7 +1120,7 @@ describe('Instance', () => { assert.notStrictEqual(config.reqOpts, OPTIONS); assert.deepStrictEqual(config.gaxOpts, OPTIONS.gaxOptions); - assert.deepStrictEqual(config.headers, instance.resourceHeader_); + assert.deepStrictEqual(config.headers, instance.commonHeaders_); return returnValue; }; @@ -1221,7 +1221,7 @@ describe('Instance', () => { name: instance.formattedName_, }); assert.strictEqual(config.gaxOpts, undefined); - assert.deepStrictEqual(config.headers, instance.resourceHeader_); + assert.deepStrictEqual(config.headers, instance.commonHeaders_); return requestReturnValue; }; @@ -1317,7 +1317,7 @@ describe('Instance', () => { assert.deepStrictEqual(METADATA, ORIGINAL_METADATA); assert.deepStrictEqual(config.gaxOpts, {}); - assert.deepStrictEqual(config.headers, instance.resourceHeader_); + assert.deepStrictEqual(config.headers, instance.commonHeaders_); assert.strictEqual(callback_, callback); @@ -1369,7 +1369,7 @@ describe('Instance', () => { assert.deepStrictEqual(OPTIONS, ORIGINAL_OPTIONS); assert.deepStrictEqual(config.gaxOpts, options.gaxOptions); - assert.deepStrictEqual(config.headers, instance.resourceHeader_); + assert.deepStrictEqual(config.headers, instance.commonHeaders_); done(); }; @@ -1555,7 +1555,7 @@ describe('Instance', () => { assert.notStrictEqual(config.reqOpts, OPTIONS); assert.deepStrictEqual(config.gaxOpts, OPTIONS.gaxOptions); - assert.deepStrictEqual(config.headers, instance.resourceHeader_); + assert.deepStrictEqual(config.headers, instance.commonHeaders_); return returnValue; }; @@ -1822,7 +1822,7 @@ describe('Instance', () => { assert.deepStrictEqual(OPTIONS, ORIGINAL_OPTIONS); assert.deepStrictEqual(config.gaxOpts, options.gaxOptions); - assert.deepStrictEqual(config.headers, instance.resourceHeader_); + assert.deepStrictEqual(config.headers, instance.commonHeaders_); done(); }; diff --git a/test/session.ts b/test/session.ts index 31db117bd..c5598de22 100644 --- a/test/session.ts +++ b/test/session.ts @@ -147,8 +147,8 @@ describe('Session', () => { }); }); - it('should set the resourceHeader_', () => { - assert.deepStrictEqual(session.resourceHeader_, { + it('should set the commonHeaders_', () => { + assert.deepStrictEqual(session.commonHeaders_, { [CLOUD_RESOURCE_HEADER]: session.parent.formattedName_, }); }); @@ -262,7 +262,7 @@ describe('Session', () => { name: session.formattedName_, }); assert.deepStrictEqual(config.gaxOpts, {}); - assert.deepStrictEqual(config.headers, session.resourceHeader_); + assert.deepStrictEqual(config.headers, session.commonHeaders_); assert.strictEqual(callback_, callback); return requestReturnValue; @@ -298,7 +298,7 @@ describe('Session', () => { config.headers, Object.assign( {[LEADER_AWARE_ROUTING_HEADER]: true}, - session.resourceHeader_ + session.commonHeaders_ ) ); callback(null, requestReturnValue); @@ -325,7 +325,7 @@ describe('Session', () => { config.headers, Object.assign( {[LEADER_AWARE_ROUTING_HEADER]: true}, - session.resourceHeader_ + session.commonHeaders_ ) ); return new Promise(resolve => resolve(requestReturnValue)); @@ -348,7 +348,7 @@ describe('Session', () => { name: session.formattedName_, }); assert.deepStrictEqual(config.gaxOpts, {}); - assert.deepStrictEqual(config.headers, session.resourceHeader_); + assert.deepStrictEqual(config.headers, session.commonHeaders_); return requestReturnValue; }; @@ -403,7 +403,7 @@ describe('Session', () => { sql: 'SELECT 1', }); assert.deepStrictEqual(config.gaxOpts, {}); - assert.deepStrictEqual(config.headers, session.resourceHeader_); + assert.deepStrictEqual(config.headers, session.commonHeaders_); assert.strictEqual(callback_, callback); return requestReturnValue; }; diff --git a/test/transaction.ts b/test/transaction.ts index 28be1543f..590eff11e 100644 --- a/test/transaction.ts +++ b/test/transaction.ts @@ -148,8 +148,8 @@ describe('Transaction', () => { assert.strictEqual(REQUEST_STREAM.callCount, 1); }); - it('should set the resourceHeader_', () => { - assert.deepStrictEqual(snapshot.resourceHeader_, { + it('should set the commonHeaders_', () => { + assert.deepStrictEqual(snapshot.commonHeaders_, { [CLOUD_RESOURCE_HEADER]: snapshot.session.parent.formattedName_, }); }); @@ -170,7 +170,7 @@ describe('Transaction', () => { assert.strictEqual(method, 'beginTransaction'); assert.strictEqual(reqOpts.session, SESSION_NAME); assert.deepStrictEqual(gaxOpts, {}); - assert.deepStrictEqual(headers, snapshot.resourceHeader_); + assert.deepStrictEqual(headers, snapshot.commonHeaders_); }); it('should accept gaxOptions', done => { @@ -273,7 +273,7 @@ describe('Transaction', () => { assert.strictEqual(client, 'SpannerClient'); assert.strictEqual(method, 'streamingRead'); - assert.deepStrictEqual(headers, snapshot.resourceHeader_); + assert.deepStrictEqual(headers, snapshot.commonHeaders_); }); it('should use the transaction id if present', () => { @@ -624,7 +624,7 @@ describe('Transaction', () => { assert.strictEqual(client, 'SpannerClient'); assert.strictEqual(method, 'executeStreamingSql'); - assert.deepStrictEqual(headers, snapshot.resourceHeader_); + assert.deepStrictEqual(headers, snapshot.commonHeaders_); }); it('should use the transaction id if present', () => { @@ -1417,7 +1417,7 @@ describe('Transaction', () => { headers, Object.assign( {[LEADER_AWARE_ROUTING_HEADER]: true}, - transaction.resourceHeader_ + transaction.commonHeaders_ ) ); }); @@ -1553,7 +1553,7 @@ describe('Transaction', () => { headers, Object.assign( {[LEADER_AWARE_ROUTING_HEADER]: true}, - transaction.resourceHeader_ + transaction.commonHeaders_ ) ); }); @@ -1600,7 +1600,7 @@ describe('Transaction', () => { headers, Object.assign( {[LEADER_AWARE_ROUTING_HEADER]: true}, - transaction.resourceHeader_ + transaction.commonHeaders_ ) ); }); @@ -1624,7 +1624,7 @@ describe('Transaction', () => { headers, Object.assign( {[LEADER_AWARE_ROUTING_HEADER]: true}, - transaction.resourceHeader_ + transaction.commonHeaders_ ) ); }); @@ -2006,7 +2006,7 @@ describe('Transaction', () => { headers, Object.assign( {[LEADER_AWARE_ROUTING_HEADER]: true}, - transaction.resourceHeader_ + transaction.commonHeaders_ ) ); }); @@ -2180,7 +2180,7 @@ describe('Transaction', () => { config.headers, Object.assign( {[LEADER_AWARE_ROUTING_HEADER]: true}, - transaction.resourceHeader_ + transaction.commonHeaders_ ) ); done(); @@ -2228,7 +2228,7 @@ describe('Transaction', () => { headers, Object.assign( {[LEADER_AWARE_ROUTING_HEADER]: true}, - transaction.resourceHeader_ + transaction.commonHeaders_ ) ); }); @@ -2297,7 +2297,7 @@ describe('Transaction', () => { headers, Object.assign( {[LEADER_AWARE_ROUTING_HEADER]: true}, - pdml.resourceHeader_ + pdml.commonHeaders_ ) ); });