1616
1717package com .example .spanner ;
1818
19+ import com .google .auth .oauth2 .GoogleCredentials ;
1920import com .google .cloud .opentelemetry .trace .TraceConfiguration ;
2021import com .google .cloud .opentelemetry .trace .TraceExporter ;
2122import com .google .cloud .spanner .DatabaseClient ;
3334import io .opentelemetry .sdk .trace .export .SpanExporter ;
3435import io .opentelemetry .sdk .trace .samplers .Sampler ;
3536
37+ import java .io .IOException ;
38+ import java .util .Collections ;
39+ import java .util .HashMap ;
40+ import java .util .List ;
41+ import java .util .Map ;
42+
3643/**
3744 * This sample demonstrates how to configure OpenTelemetry and inject via Spanner Options.
3845 */
@@ -46,11 +53,10 @@ public class OpenTelemetryUsage {
4653 static String instanceId = "my-instance" ;
4754 static String databaseId = "my-database" ;
4855
49- // Replace these variables to use OTLP Exporter
50- static boolean useCloudTraceExporter = true ; // Replace to false for OTLP
51- static String otlpEndpoint = "http://localhost:4317" ; // Replace with your OTLP endpoint
56+ static boolean useCloudTraceExporter = false ; // Replace to true for Cloud Trace exporter
57+ static String otlpEndpoint = "https://telemetry.googleapis.com" ; // Replace with your OTLP endpoint
5258
53- public static void main (String [] args ) {
59+ public static void main (String [] args ) throws IOException {
5460
5561 if (useCloudTraceExporter ) {
5662 spanner = getSpannerWithCloudTraceExporter ();
@@ -64,25 +70,42 @@ public static void main(String[] args) {
6470 try (ResultSet resultSet =
6571 dbClient
6672 .singleUse () // Execute a single read or query against Cloud Spanner.
67- .executeQuery (Statement .of ("SELECT SingerId, AlbumId, AlbumTitle FROM Albums" ))) {
73+ .executeQuery (Statement .of ("SELECT SingerId, AlbumId, AlbumTitle FROM Albums" ))) {
6874 while (resultSet .next ()) {
6975 System .out .printf (
70- "%d %d %s" , resultSet .getLong (0 ), resultSet .getLong (1 ), resultSet .getString (2 ));
76+ "%d %d %s" , resultSet .getLong (0 ), resultSet .getLong (1 ), resultSet .getString (2 ));
7177 }
7278 }
7379
7480 sdkTracerProvider .forceFlush ();
7581 }
7682
77- public static Spanner getSpannerWithOtlpExporter () {
83+ public static Spanner getSpannerWithOtlpExporter () throws IOException {
7884 // [START spanner_opentelemetry_traces_otlp_usage]
7985 Resource resource = Resource
80- .getDefault ().merge (Resource .builder ().put ("service.name" , "My App" ).build ());
86+ .getDefault ().merge (Resource .builder ().put ("service.name" , "My App" ).put ( "gcp.project_id" , projectId ). build ());
8187
88+ GoogleCredentials credentials = GoogleCredentials .getApplicationDefault ()
89+ .createScoped (Collections .singleton ("https://www.googleapis.com/auth/trace.append" ));
8290 OtlpGrpcSpanExporter otlpGrpcSpanExporter =
8391 OtlpGrpcSpanExporter
8492 .builder ()
85- .setEndpoint (otlpEndpoint ) // Replace with your OTLP endpoint
93+ .setEndpoint (otlpEndpoint )
94+ .setHeaders (() -> {
95+ try {
96+ credentials .refreshIfExpired ();
97+ Map <String , List <String >> metadata = credentials .getRequestMetadata ();
98+ Map <String , String > headers = new HashMap <>();
99+ if (metadata != null ) {
100+ metadata .forEach ((key , values ) ->
101+ headers .put (key , String .join ("," , values )));
102+ }
103+ return headers ;
104+ } catch (Exception e ) {
105+ // Handle error fetching credentials
106+ return Collections .emptyMap ();
107+ }
108+ })// Replace with your OTLP endpoint
86109 .build ();
87110
88111 // Using a batch span processor
0 commit comments