|
| 1 | +/*! |
| 2 | + * Copyright 2024 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 |
| 19 | +// usage: node observability-traces.js <PROJECT-ID> <INSTANCE-ID> <DATABASE-ID> |
| 20 | + |
| 21 | +'use strict'; |
| 22 | + |
| 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 | +// Uncomment following line to register global tracerProvider instead |
| 45 | +// of passing it into SpannerOptions.observabilityOptions. |
| 46 | +// provider.register(); |
| 47 | + |
| 48 | +// Set `enableGrpcInstrumentation` to `true` to enable gRPC instrumentation. |
| 49 | +const enableGrpcInstrumentation = false; |
| 50 | +if (enableGrpcInstrumentation) { |
| 51 | + const {registerInstrumentations} = require('@opentelemetry/instrumentation'); |
| 52 | + const {GrpcInstrumentation} = require('@opentelemetry/instrumentation-grpc'); |
| 53 | + registerInstrumentations({ |
| 54 | + tracerProvider: provider, |
| 55 | + instrumentations: [new GrpcInstrumentation()], |
| 56 | + }); |
| 57 | +} |
| 58 | + |
| 59 | +async function main( |
| 60 | + projectId = 'my-project-id', |
| 61 | + instanceId = 'my-instance-id', |
| 62 | + databaseId = 'my-project-id' |
| 63 | +) { |
| 64 | + // Create the Cloud Spanner Client. |
| 65 | + const {Spanner} = require('@google-cloud/spanner'); |
| 66 | + |
| 67 | + /** |
| 68 | + * TODO(developer): Uncomment these variables before running the sample. |
| 69 | + */ |
| 70 | + // const projectId = 'my-project-id'; |
| 71 | + // const instanceId = 'my-instance-id'; |
| 72 | + // const databaseId = 'my-database-id'; |
| 73 | + |
| 74 | + const spanner = new Spanner({ |
| 75 | + projectId: projectId, |
| 76 | + observabilityOptions: { |
| 77 | + tracerProvider: provider, |
| 78 | + enableExtendedTracing: true, |
| 79 | + }, |
| 80 | + }); |
| 81 | + |
| 82 | + // Acquire the database handle. |
| 83 | + const instance = spanner.instance(instanceId); |
| 84 | + const database = instance.database(databaseId); |
| 85 | + |
| 86 | + try { |
| 87 | + const query = { |
| 88 | + sql: 'SELECT 1', |
| 89 | + }; |
| 90 | + const [rows] = await database.run(query); |
| 91 | + console.log(`Query: ${rows.length} found.`); |
| 92 | + rows.forEach(row => console.log(row)); |
| 93 | + } finally { |
| 94 | + spanner.close(); |
| 95 | + } |
| 96 | + |
| 97 | + provider.forceFlush(); |
| 98 | + |
| 99 | + // This sleep gives ample time for the trace |
| 100 | + // spans to be exported to Google Cloud Trace. |
| 101 | + await new Promise(resolve => { |
| 102 | + setTimeout(() => { |
| 103 | + resolve(); |
| 104 | + }, 8800); |
| 105 | + }); |
| 106 | +} |
| 107 | + |
| 108 | +process.on('unhandledRejection', err => { |
| 109 | + console.error(err.message); |
| 110 | + process.exitCode = 1; |
| 111 | +}); |
| 112 | +main(...process.argv.slice(2)); |
0 commit comments