Skip to content

Commit 49fff5b

Browse files
authored
chore: samples for opentelemetry tracing (#2249)
1 parent a7bb3a6 commit 49fff5b

File tree

5 files changed

+157
-52
lines changed

5 files changed

+157
-52
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ Samples are in the [`samples/`](https://github.com/googleapis/nodejs-spanner/tre
157157
| Numeric-add-column | [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/numeric-add-column.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-spanner&page=editor&open_in_editor=samples/numeric-add-column.js,samples/README.md) |
158158
| Numeric-query-parameter | [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/numeric-query-parameter.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-spanner&page=editor&open_in_editor=samples/numeric-query-parameter.js,samples/README.md) |
159159
| Numeric-update-data | [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/numeric-update-data.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-spanner&page=editor&open_in_editor=samples/numeric-update-data.js,samples/README.md) |
160+
| Observability (Tracing) with OpenTelemetry using OTLP | [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/observability-traces-otlp.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-spanner&page=editor&open_in_editor=samples/observability-traces-otlp.js,samples/README.md) |
160161
| Observability (Tracing) with OpenTelemetry | [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/observability-traces.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-spanner&page=editor&open_in_editor=samples/observability-traces.js,samples/README.md) |
161162
| Adds a column to an existing table in a Spanner PostgreSQL database. | [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/pg-add-column.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-spanner&page=editor&open_in_editor=samples/pg-add-column.js,samples/README.md) |
162163
| Showcase the rules for case-sensitivity and case folding for a Spanner PostgreSQL database. | [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/pg-case-sensitivity.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-spanner&page=editor&open_in_editor=samples/pg-case-sensitivity.js,samples/README.md) |

samples/README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ and automatic, synchronous replication for high availability.
8282
* [Numeric-add-column](#numeric-add-column)
8383
* [Numeric-query-parameter](#numeric-query-parameter)
8484
* [Numeric-update-data](#numeric-update-data)
85+
* [Observability (Tracing) with OpenTelemetry using OTLP](#observability-tracing-with-opentelemetry-using-otlp)
8586
* [Observability (Tracing) with OpenTelemetry](#observability-tracing-with-opentelemetry)
8687
* [Adds a column to an existing table in a Spanner PostgreSQL database.](#adds-a-column-to-an-existing-table-in-a-spanner-postgresql-database.)
8788
* [Showcase the rules for case-sensitivity and case folding for a Spanner PostgreSQL database.](#showcase-the-rules-for-case-sensitivity-and-case-folding-for-a-spanner-postgresql-database.)
@@ -1306,6 +1307,23 @@ __Usage:__
13061307

13071308

13081309

1310+
### Observability (Tracing) with OpenTelemetry using OTLP
1311+
1312+
View the [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/observability-traces-otlp.js).
1313+
1314+
[![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-spanner&page=editor&open_in_editor=samples/observability-traces-otlp.js,samples/README.md)
1315+
1316+
__Usage:__
1317+
1318+
1319+
`node observability-traces-otlp.js <PROJECT-ID> <INSTANCE-ID> <DATABASE-ID>`
1320+
1321+
1322+
-----
1323+
1324+
1325+
1326+
13091327
### Observability (Tracing) with OpenTelemetry
13101328

13111329
View the [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/observability-traces.js).
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
/*!
2+
* Copyright 2025 Google LLC. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
// sample-metadata:
18+
// title: Observability (Tracing) with OpenTelemetry using OTLP
19+
// usage: node observability-traces-otlp.js <PROJECT-ID> <INSTANCE-ID> <DATABASE-ID>
20+
21+
'use strict';
22+
23+
async function main(
24+
projectId = 'my-project-id',
25+
instanceId = 'my-instance-id',
26+
databaseId = 'my-project-id'
27+
) {
28+
// [START spanner_opentelemetry_traces_otlp_usage]
29+
const {NodeTracerProvider} = require('@opentelemetry/sdk-trace-node');
30+
const {
31+
OTLPTraceExporter,
32+
} = require('@opentelemetry/exporter-trace-otlp-grpc');
33+
const {
34+
BatchSpanProcessor,
35+
TraceIdRatioBasedSampler,
36+
} = require('@opentelemetry/sdk-trace-base');
37+
const {Resource} = require('@opentelemetry/resources');
38+
const {Spanner} = require('@google-cloud/spanner');
39+
40+
// Define a Resource with service metadata
41+
const resource = new Resource({
42+
'service.name': 'my-service',
43+
'service.version': '1.0.0',
44+
});
45+
46+
// Create an OTLP gRPC trace exporter
47+
const traceExporter = new OTLPTraceExporter({
48+
url: 'http://localhost:4317', // Default OTLP gRPC endpoint
49+
});
50+
51+
// Create a provider with a custom sampler
52+
const provider = new NodeTracerProvider({
53+
sampler: new TraceIdRatioBasedSampler(1.0), // Sample 100% of traces
54+
resource,
55+
spanProcessors: [new BatchSpanProcessor(traceExporter)],
56+
});
57+
58+
// Uncomment following line to register tracerProvider globally or pass it in Spanner object
59+
// provider.register();
60+
61+
// Create the Cloud Spanner Client.
62+
const spanner = new Spanner({
63+
projectId: projectId,
64+
observabilityOptions: {
65+
tracerProvider: provider,
66+
enableExtendedTracing: true,
67+
enableEndToEndTracing: true,
68+
},
69+
});
70+
71+
// [END spanner_opentelemetry_traces_otlp_usage]
72+
73+
// Acquire the database handle.
74+
const instance = spanner.instance(instanceId);
75+
const database = instance.database(databaseId);
76+
77+
try {
78+
const query = {
79+
sql: 'SELECT 1',
80+
};
81+
const [rows] = await database.run(query);
82+
console.log(`Query: ${rows.length} found.`);
83+
rows.forEach(row => console.log(row));
84+
} finally {
85+
spanner.close();
86+
}
87+
88+
provider.forceFlush();
89+
90+
// This sleep gives ample time for the trace
91+
// spans to be exported to Google Cloud Trace.
92+
await new Promise(resolve => {
93+
setTimeout(() => {
94+
resolve();
95+
}, 8800);
96+
});
97+
}
98+
99+
process.on('unhandledRejection', err => {
100+
console.error(err.message);
101+
process.exitCode = 1;
102+
});
103+
main(...process.argv.slice(2));

samples/observability-traces.js

Lines changed: 30 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*!
2-
* Copyright 2024 Google LLC. All Rights Reserved.
2+
* Copyright 2025 Google LLC. All Rights Reserved.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -20,71 +20,52 @@
2020

2121
'use strict';
2222

23-
// Setup OpenTelemetry and the trace exporter.
24-
const {
25-
NodeTracerProvider,
26-
TraceIdRatioBasedSampler,
27-
} = require('@opentelemetry/sdk-trace-node');
28-
const {BatchSpanProcessor} = require('@opentelemetry/sdk-trace-base');
29-
30-
// Create the Google Cloud Trace exporter for OpenTelemetry.
31-
const {
32-
TraceExporter,
33-
} = require('@google-cloud/opentelemetry-cloud-trace-exporter');
34-
const exporter = new TraceExporter();
35-
36-
// Create the OpenTelemetry tracerProvider that the exporter shall be attached to.
37-
const provider = new NodeTracerProvider({
38-
// Modify the following line to adjust the sampling rate.
39-
// It is currently set to 1.0, meaning all requests will be traced.
40-
sampler: new TraceIdRatioBasedSampler(1.0),
41-
});
42-
provider.addSpanProcessor(new BatchSpanProcessor(exporter));
43-
44-
// Set global propagator to propogate the trace context for end to end tracing.
45-
const {propagation} = require('@opentelemetry/api');
46-
const {W3CTraceContextPropagator} = require('@opentelemetry/core');
47-
propagation.setGlobalPropagator(new W3CTraceContextPropagator());
48-
49-
// Uncomment following line to register global tracerProvider instead
50-
// of passing it into SpannerOptions.observabilityOptions.
51-
// provider.register();
52-
53-
// Set `enableGrpcInstrumentation` to `true` to enable gRPC instrumentation.
54-
const enableGrpcInstrumentation = false;
55-
if (enableGrpcInstrumentation) {
56-
const {registerInstrumentations} = require('@opentelemetry/instrumentation');
57-
const {GrpcInstrumentation} = require('@opentelemetry/instrumentation-grpc');
58-
registerInstrumentations({
59-
tracerProvider: provider,
60-
instrumentations: [new GrpcInstrumentation()],
61-
});
62-
}
63-
6423
async function main(
6524
projectId = 'my-project-id',
6625
instanceId = 'my-instance-id',
6726
databaseId = 'my-project-id'
6827
) {
69-
// Create the Cloud Spanner Client.
28+
// [START spanner_opentelemetry_traces_cloudtrace_usage]
29+
30+
const {NodeTracerProvider} = require('@opentelemetry/sdk-trace-node');
31+
const {
32+
TraceExporter,
33+
} = require('@google-cloud/opentelemetry-cloud-trace-exporter');
34+
const {
35+
BatchSpanProcessor,
36+
TraceIdRatioBasedSampler,
37+
} = require('@opentelemetry/sdk-trace-base');
7038
const {Spanner} = require('@google-cloud/spanner');
7139

72-
/**
73-
* TODO(developer): Uncomment these variables before running the sample.
74-
*/
75-
// const projectId = 'my-project-id';
76-
// const instanceId = 'my-instance-id';
77-
// const databaseId = 'my-database-id';
40+
const traceExporter = new TraceExporter();
41+
42+
// Create a provider with a custom sampler
43+
const provider = new NodeTracerProvider({
44+
sampler: new TraceIdRatioBasedSampler(1.0), // Sample 100% of traces
45+
spanProcessors: [new BatchSpanProcessor(traceExporter)],
46+
});
47+
48+
// Uncomment following line to register tracerProvider globally or pass it in Spanner object
49+
// provider.register();
50+
51+
// Set global propagator to propogate the trace context for end to end tracing.
52+
const {propagation} = require('@opentelemetry/api');
53+
const {W3CTraceContextPropagator} = require('@opentelemetry/core');
54+
propagation.setGlobalPropagator(new W3CTraceContextPropagator());
7855

7956
const spanner = new Spanner({
8057
projectId: projectId,
8158
observabilityOptions: {
8259
tracerProvider: provider,
60+
// Enable extended tracing to allow your SQL statements to be annotated.
8361
enableExtendedTracing: true,
62+
// Enable end to end tracing.
8463
enableEndToEndTracing: true,
8564
},
8665
});
8766

67+
// [END spanner_opentelemetry_traces_cloudtrace_usage]
68+
8869
// Acquire the database handle.
8970
const instance = spanner.instance(instanceId);
9071
const database = instance.database(databaseId);

samples/package.json

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,14 @@
2323
},
2424
"devDependencies": {
2525
"@google-cloud/opentelemetry-cloud-trace-exporter": "^2.4.1",
26+
"@opentelemetry/exporter-trace-otlp-grpc": "^0.57.0",
2627
"@opentelemetry/instrumentation": "^0.57.0",
2728
"@opentelemetry/instrumentation-grpc": "^0.57.0",
28-
"@opentelemetry/sdk-trace-base": "^1.26.0",
29-
"@opentelemetry/sdk-trace-node": "^1.26.0",
29+
"@opentelemetry/resources": "1.30.1",
30+
"@opentelemetry/sdk-trace-base":"~1.30.1",
31+
"@opentelemetry/sdk-trace-node":"^1.30.1",
3032
"@opentelemetry/api": "^1.9.0",
31-
"@opentelemetry/core": "^1.27.0",
33+
"@opentelemetry/core":"^1.30.1",
3234
"chai": "^4.2.0",
3335
"mocha": "^9.0.0",
3436
"p-limit": "^3.0.1"

0 commit comments

Comments
 (0)