|
1 | 1 | /*! |
2 | | - * Copyright 2024 Google LLC. All Rights Reserved. |
| 2 | + * Copyright 2025 Google LLC. All Rights Reserved. |
3 | 3 | * |
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
5 | 5 | * you may not use this file except in compliance with the License. |
|
20 | 20 |
|
21 | 21 | 'use strict'; |
22 | 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 | | -// 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 | | - |
64 | 23 | async function main( |
65 | 24 | projectId = 'my-project-id', |
66 | 25 | instanceId = 'my-instance-id', |
67 | 26 | databaseId = 'my-project-id' |
68 | 27 | ) { |
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'); |
70 | 38 | const {Spanner} = require('@google-cloud/spanner'); |
71 | 39 |
|
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()); |
78 | 55 |
|
79 | 56 | const spanner = new Spanner({ |
80 | 57 | projectId: projectId, |
81 | 58 | observabilityOptions: { |
82 | 59 | tracerProvider: provider, |
| 60 | + // Enable extended tracing to allow your SQL statements to be annotated. |
83 | 61 | enableExtendedTracing: true, |
| 62 | + // Enable end to end tracing. |
84 | 63 | enableEndToEndTracing: true, |
85 | 64 | }, |
86 | 65 | }); |
87 | 66 |
|
| 67 | + // [END spanner_opentelemetry_traces_cloudtrace_usage] |
| 68 | + |
88 | 69 | // Acquire the database handle. |
89 | 70 | const instance = spanner.instance(instanceId); |
90 | 71 | const database = instance.database(databaseId); |
|
0 commit comments