Skip to content

Commit

Permalink
all: update exporter to use Google Cloud Trace
Browse files Browse the repository at this point in the history
  • Loading branch information
odeke-em committed Jul 13, 2024
1 parent ce1681f commit a99db4b
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 22 deletions.
7 changes: 3 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ Please use a tracer named "nodejs-spanner".
`SPANNER_NODEJS_ANNOTATE_PII_SQL=1`, this is because SQL statements can be
sensitive personally-identifiable-information (PII).**

To test out trace examination, you can use the Zipkin tracing service like this.
To test out trace examination, you can use Google Cloud Trace like this.

```javascript
function exportSpans(instanceId, databaseId, projectId) {
Expand All @@ -121,9 +121,8 @@ function exportSpans(instanceId, databaseId, projectId) {
})
);

const {ZipkinExporter} = require('@opentelemetry/exporter-zipkin');
const options = {serviceName: 'nodejs-spanner'};
const exporter = new ZipkinExporter(options);
const {TraceExporter} = require('@google-cloud/opentelemetry-cloud-trace-exporter');
const exporter = new TraceExporter({});

const sdk = new NodeSDK({
resource: resource,
Expand Down
10 changes: 5 additions & 5 deletions samples/observability-traces.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,11 @@ function exportSpans(instanceId, databaseId, projectId) {
})
);

const {ZipkinExporter} = require('@opentelemetry/exporter-zipkin');
const options = {serviceName: 'nodejs-spanner'};
const exporter = new ZipkinExporter(options);
const {
TraceExporter,
} = require('@google-cloud/opentelemetry-cloud-trace-exporter');
const exporter = new TraceExporter({});

const sdk = new NodeSDK({
resource: resource,
Expand All @@ -55,11 +57,10 @@ function exportSpans(instanceId, databaseId, projectId) {
});
sdk.start();

const {OTTracePropagator} = require('@opentelemetry/propagator-ot-trace');
const provider = new NodeTracerProvider({resource: resource});
provider.addSpanProcessor(new BatchSpanProcessor(exporter));
provider.addSpanProcessor(new SimpleSpanProcessor(new ConsoleSpanExporter()));
provider.register({propagator: new OTTracePropagator()});
provider.register();

// OpenTelemetry MUST be imported much earlier than the cloud-spanner package.
const tracer = trace.getTracer('nodejs-spanner');
Expand Down Expand Up @@ -120,7 +121,6 @@ function exportSpans(instanceId, databaseId, projectId) {
span.end();
spanner.close();
setTimeout(() => {
exporter.forceFlush();
console.log('finished delete and creation of the database');
}, 8000);
});
Expand Down
2 changes: 1 addition & 1 deletion samples/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
},
"dependencies": {
"@google-cloud/kms": "^4.0.0",
"@google-cloud/opentelemetry-cloud-trace-exporter": "^2.2.0",
"@google-cloud/opentelemetry-cloud-trace-exporter": "^2.3.0",
"@google-cloud/precise-date": "^4.0.0",
"@google-cloud/spanner": "^7.9.1",
"@opentelemetry/api": "^1.9.0",
Expand Down
6 changes: 3 additions & 3 deletions src/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ export class Session extends common.GrpcServiceObject {
optionsOrCallback: CreateSessionOptions | CreateSessionCallback,
callback: CreateSessionCallback
) => {
const span = startTrace('cloud.google.com/nodejs/Session.create');
const span = startTrace('Session.create');
const options =
typeof optionsOrCallback === 'object' ? optionsOrCallback : {};
callback =
Expand Down Expand Up @@ -379,7 +379,7 @@ export class Session extends common.GrpcServiceObject {
optionsOrCallback?: CallOptions | GetSessionMetadataCallback,
cb?: GetSessionMetadataCallback
): void | Promise<GetSessionMetadataResponse> {
const span = startTrace('cloud.google.com/nodejs/Session.getMetadata');
const span = startTrace('Session.getMetadata');
const gaxOpts =
typeof optionsOrCallback === 'object' ? optionsOrCallback : {};
const callback =
Expand Down Expand Up @@ -441,7 +441,7 @@ export class Session extends common.GrpcServiceObject {
optionsOrCallback?: CallOptions | KeepAliveCallback,
cb?: KeepAliveCallback
): void | Promise<KeepAliveResponse> {
const span = startTrace('cloud.google.com/nodejs/Session.keepAlive');
const span = startTrace('Session.keepAlive');
const gaxOpts =
typeof optionsOrCallback === 'object' ? optionsOrCallback : {};
const callback =
Expand Down
17 changes: 8 additions & 9 deletions src/v1/instrument.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ registerInstrumentations({
instrumentations: [new GrpcInstrumentation(), new HttpInstrumentation()],
});

var optedInPII = process.env.SPANNER_NODEJS_ANNOTATE_PII_SQL === '1';
const optedInPII = process.env.SPANNER_NODEJS_ANNOTATE_PII_SQL === '1';

interface SQLStatement {
sql: string;
Expand All @@ -49,7 +49,10 @@ interface SQLStatement {
// scope change in which trying to use tracer.startActiveSpan
// would change the meaning of this, and also introduction of callbacks
// would radically change all the code structures making it more invasive.
export function startTrace(spanNameSuffix: string, sql?: string | SQLStatement): Span {
export function startTrace(
spanNameSuffix: string,
sql?: string | SQLStatement
): Span {
const span = tracer.startSpan(
'cloud.google.com/nodejs/spanner/' + spanNameSuffix
);
Expand Down Expand Up @@ -105,10 +108,7 @@ function callbackify(originalMethod: typeof CallbackMethod) {

const cb = Array.prototype.pop.call(arguments);

console.log('cb.name', cb.name);
const span = startTrace(
'cloud.google.com/nodejs/Spanner.' + cb.name + '.callbackify'
);
const span = startTrace('Spanner.' + cb.name);
originalMethod.apply(this, arguments).then(
// tslint:disable-next-line:no-any
(res: any) => {
Expand Down Expand Up @@ -186,9 +186,8 @@ export function promisify(
const slice = Array.prototype.slice;

const wrapper: any = function (this: typeof WithPromise) {
const span = startTrace(
'cloud.google.com/nodejs/Spanner.' + originalMethod.name + '.promisify'
);
const span = startTrace('Spanner.' + originalMethod.name);

// tslint:disable-next-line:no-any
let last;

Expand Down

0 comments on commit a99db4b

Please sign in to comment.