Skip to content

Commit

Permalink
Trace and copy Promisify* from @google-cloud/promisify
Browse files Browse the repository at this point in the history
  • Loading branch information
odeke-em committed Jul 12, 2024
1 parent f033346 commit c16af37
Show file tree
Hide file tree
Showing 12 changed files with 190 additions and 31 deletions.
29 changes: 20 additions & 9 deletions samples/observability-traces.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,15 @@ function exportSpans(instanceId, databaseId, projectId) {
instanceId,
databaseId,
() => {
span.end();
console.log('main span.end');
setTimeout(() => {
exporter.forceFlush();
console.log('finished delete and creation of the database');
}, 18000);
insertUsingDml(tracer, database, () => {
console.log('main span.end');
span.end();
setTimeout(() => {
spanner.close();
exporter.forceFlush();
console.log('finished delete and creation of the database');
}, 8000);
});
}
);
});
Expand Down Expand Up @@ -265,7 +268,7 @@ function runMutations(tracer, database) {
});
}

function insertUsingDml(tracer, database) {
function insertUsingDml(tracer, database, callback) {
tracer.startActiveSpan('insertUsingDML', span => {
database.runTransaction(async (err, transaction) => {
if (err) {
Expand Down Expand Up @@ -293,12 +296,20 @@ function insertUsingDml(tracer, database) {
);

await transaction.commit();
span.end();
} catch (err) {
console.error('ERROR:', err);
} finally {
// Close the database when finished.
setTimeout(() => {}, 8000);
console.log('exiting insertUsingDml');
tracer.startActiveSpan('timingOutToExport-insertUsingDML', eSpan => {
setTimeout(() => {
eSpan.end();
span.end();
if (callback) {
callback();
}
}, 50);
});
}
});
});
Expand Down
2 changes: 1 addition & 1 deletion src/backup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
* limitations under the License.
*/

import {promisifyAll, callbackifyAll} from '@google-cloud/promisify';
import {Instance} from './instance';
import {
IOperation,
Expand All @@ -33,6 +32,7 @@ import {
import {DateStruct, PreciseDate} from '@google-cloud/precise-date';
import {google as databaseAdmin} from '../protos/protos';
import {common as p} from 'protobufjs';
import {promisifyAll, callbackifyAll} from './v1/instrument';

export type CreateBackupCallback = LongRunningCallback<Backup>;
export type CopyBackupCallback = LongRunningCallback<Backup>;
Expand Down
3 changes: 1 addition & 2 deletions src/batch-transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
*/

import {PreciseDate} from '@google-cloud/precise-date';
import {promisifyAll} from '@google-cloud/promisify';
import * as extend from 'extend';
import * as is from 'is';
import {Snapshot} from './transaction';
Expand All @@ -25,7 +24,7 @@ import {
CLOUD_RESOURCE_HEADER,
addLeaderAwareRoutingHeader,
} from '../src/common';
import {tracer, SPAN_CODE_ERROR} from './v1/instrument';
import {promisifyAll, tracer, SPAN_CODE_ERROR} from './v1/instrument';

export interface TransactionIdentifier {
session: string | Session;
Expand Down
22 changes: 18 additions & 4 deletions src/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import {
} from '@google-cloud/common';
// eslint-disable-next-line @typescript-eslint/no-var-requires
const common = require('./common-grpc/service-object');
import {promisify, promisifyAll} from '@google-cloud/promisify';
import * as extend from 'extend';
import * as r from 'teeny-request';
import * as streamEvents from 'stream-events';
Expand Down Expand Up @@ -99,7 +98,13 @@ import Policy = google.iam.v1.Policy;
import FieldMask = google.protobuf.FieldMask;
import IDatabase = google.spanner.admin.database.v1.IDatabase;
import snakeCase = require('lodash.snakecase');
import {tracer, SPAN_CODE_ERROR, callbackifyAll} from './v1/instrument';
import {
tracer,
SPAN_CODE_ERROR,
callbackifyAll,
promisify,
promisifyAll,
} from './v1/instrument';

export type GetDatabaseRolesCallback = RequestCallback<
IDatabaseRole,
Expand Down Expand Up @@ -3084,6 +3089,7 @@ class Database extends common.GrpcServiceObject {
optionsOrRunFn: RunTransactionOptions | RunTransactionCallback,
fn?: RunTransactionCallback
): void {
console.log('database.runTransaction', fn);
tracer.startActiveSpan(
'cloud.google.com/nodejs/spanner/Database.runTransaction',
span => {
Expand All @@ -3097,6 +3103,7 @@ class Database extends common.GrpcServiceObject {
: {};

this.pool_.getSession((err, session?, transaction?) => {
console.log('getSession', err);
if (err) {
span.setStatus({
code: SPAN_CODE_ERROR,
Expand Down Expand Up @@ -3131,12 +3138,19 @@ class Database extends common.GrpcServiceObject {
);

runner.run().then(release, err => {
console.log('runner.result', err);
if (err) {
span.setStatus({
code: SPAN_CODE_ERROR,
message: err.toString(),
});
}
span.end();

if (isSessionNotFoundError(err)) {
span.end();
release();
this.runTransaction(options, runFn!);
} else {
span.end();
setImmediate(runFn!, err);
release();
}
Expand Down
3 changes: 1 addition & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import {GrpcService, GrpcServiceConfig} from './common-grpc/service';
import {PreciseDate} from '@google-cloud/precise-date';
import {replaceProjectIdToken} from '@google-cloud/projectify';
import {promisifyAll} from '@google-cloud/promisify';
import * as extend from 'extend';
import {GoogleAuth, GoogleAuthOptions} from 'google-auth-library';
import * as path from 'path';
Expand Down Expand Up @@ -79,7 +78,7 @@ import {
import grpcGcpModule = require('grpc-gcp');
const grpcGcp = grpcGcpModule(grpc);
import * as v1 from './v1';
import {tracer, SPAN_CODE_ERROR} from './v1/instrument';
import {promisifyAll, tracer, SPAN_CODE_ERROR} from './v1/instrument';

// eslint-disable-next-line @typescript-eslint/no-var-requires
const gcpApiConfig = require('./spanner_grpc_config.json');
Expand Down
2 changes: 1 addition & 1 deletion src/instance-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ import {
RequestConfig,
Spanner,
} from './index';
import {promisifyAll} from '@google-cloud/promisify';
import {CallOptions, grpc} from 'google-gax';
import extend = require('extend');
import {promisifyAll} from './v1/instrument';

export type IOperation = instanceAdmin.longrunning.IOperation;

Expand Down
3 changes: 1 addition & 2 deletions src/instance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import arrify = require('arrify');
import {ServiceObjectConfig, GetConfig} from '@google-cloud/common';
// eslint-disable-next-line @typescript-eslint/no-var-requires
const common = require('./common-grpc/service-object');
import {promisifyAll} from '@google-cloud/promisify';
import * as extend from 'extend';
import snakeCase = require('lodash.snakecase');
import {Database, SessionPoolConstructor} from './database';
Expand Down Expand Up @@ -51,7 +50,7 @@ import {google as instanceAdmin} from '../protos/protos';
import {google as databaseAdmin} from '../protos/protos';
import {google as spannerClient} from '../protos/protos';
import {CreateInstanceRequest} from './index';
import {tracer, SPAN_CODE_ERROR} from './v1/instrument';
import {promisifyAll, tracer, SPAN_CODE_ERROR} from './v1/instrument';

export type IBackup = databaseAdmin.spanner.admin.database.v1.IBackup;
export type IDatabase = databaseAdmin.spanner.admin.database.v1.IDatabase;
Expand Down
3 changes: 1 addition & 2 deletions src/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@

// eslint-disable-next-line @typescript-eslint/no-var-requires
const common = require('./common-grpc/service-object');
import {promisifyAll} from '@google-cloud/promisify';
import * as extend from 'extend';
import * as r from 'teeny-request';
import {
Expand All @@ -44,7 +43,7 @@ import {
import {grpc, CallOptions} from 'google-gax';
import IRequestOptions = google.spanner.v1.IRequestOptions;
import {Spanner} from '.';
import {tracer, SPAN_CODE_ERROR} from './v1/instrument';
import {promisifyAll, tracer, SPAN_CODE_ERROR} from './v1/instrument';

export type GetSessionResponse = [Session, r.Response];

Expand Down
3 changes: 1 addition & 2 deletions src/table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
* limitations under the License.
*/

import {promisifyAll} from '@google-cloud/promisify';
import * as through from 'through2';
import {Operation as GaxOperation, CallOptions} from 'google-gax';
import {Database, UpdateSchemaCallback, UpdateSchemaResponse} from './database';
Expand All @@ -31,7 +30,7 @@ import {
import {google as databaseAdmin} from '../protos/protos';
import {Schema, LongRunningCallback} from './common';
import IRequestOptions = databaseAdmin.spanner.v1.IRequestOptions;
import {tracer, SPAN_CODE_ERROR} from './v1/instrument';
import {promisifyAll, tracer, SPAN_CODE_ERROR} from './v1/instrument';

export type Key = string | string[];

Expand Down
2 changes: 1 addition & 1 deletion src/transaction-runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
* limitations under the License.
*/

import {promisify} from '@google-cloud/promisify';
import {grpc} from 'google-gax';
import {Root} from 'protobufjs';
import * as through from 'through2';
Expand All @@ -26,6 +25,7 @@ import {isSessionNotFoundError} from './session-pool';
import {Database} from './database';
import {google} from '../protos/protos';
import IRequestOptions = google.spanner.v1.IRequestOptions;
import {promisify} from './v1/instrument';

// eslint-disable-next-line @typescript-eslint/no-var-requires
const jsonProtos = require('../protos/protos.json');
Expand Down
3 changes: 1 addition & 2 deletions src/transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
*/

import {DateStruct, PreciseDate} from '@google-cloud/precise-date';
import {promisifyAll} from '@google-cloud/promisify';
import arrify = require('arrify');
import Long = require('long');
import {EventEmitter} from 'events';
Expand Down Expand Up @@ -45,7 +44,7 @@ import IQueryOptions = google.spanner.v1.ExecuteSqlRequest.IQueryOptions;
import IRequestOptions = google.spanner.v1.IRequestOptions;
import {Database, Spanner} from '.';
import ReadLockMode = google.spanner.v1.TransactionOptions.ReadWrite.ReadLockMode;
import {tracer, SPAN_CODE_ERROR} from './v1/instrument';
import {promisifyAll, tracer, SPAN_CODE_ERROR} from './v1/instrument';

export type Rows = Array<Row | Json>;
const RETRY_INFO_TYPE = 'type.googleapis.com/google.rpc.retryinfo';
Expand Down
Loading

0 comments on commit c16af37

Please sign in to comment.