3737import static org .junit .Assert .assertNull ;
3838import static org .junit .Assert .assertTrue ;
3939
40+ import com .google .api .gax .core .FixedCredentialsProvider ;
4041import com .google .api .gax .rpc .DeadlineExceededException ;
4142import com .google .api .gax .rpc .NotFoundException ;
43+ import com .google .auth .Credentials ;
4244import com .google .cloud .datastore .AggregationQuery ;
4345import com .google .cloud .datastore .AggregationResult ;
4446import com .google .cloud .datastore .AggregationResults ;
5860import com .google .cloud .opentelemetry .trace .TraceConfiguration ;
5961import com .google .cloud .opentelemetry .trace .TraceExporter ;
6062import com .google .cloud .trace .v1 .TraceServiceClient ;
63+ import com .google .cloud .trace .v1 .TraceServiceSettings ;
6164import com .google .common .base .Preconditions ;
6265import com .google .devtools .cloudtrace .v1 .Trace ;
6366import com .google .devtools .cloudtrace .v1 .TraceSpan ;
@@ -246,12 +249,12 @@ private boolean dfsContainsCallStack(long spanId, List<String> expectedCallStack
246249 private static TraceExporter traceExporter ;
247250
248251 // Required for reading back traces from Cloud Trace for validation
249- private static TraceServiceClient traceClient_v1 ;
252+ private static TraceServiceClient traceClient ;
250253
251254 // Custom SpanContext for each test, required for TraceID injection
252255 private static SpanContext customSpanContext ;
253256
254- // Trace read back from Cloud Trace using traceClient_v1 for verification
257+ // Trace read back from Cloud Trace using traceClient for verification
255258 private static Trace retrievedTrace ;
256259
257260 private static String rootSpanName ;
@@ -280,10 +283,24 @@ private boolean dfsContainsCallStack(long spanId, List<String> expectedCallStack
280283 @ BeforeClass
281284 public static void setup () throws IOException {
282285 projectId = DatastoreOptions .getDefaultProjectId ();
283- traceExporter =
284- TraceExporter .createWithConfiguration (
285- TraceConfiguration .builder ().setProjectId (projectId ).build ());
286- traceClient_v1 = TraceServiceClient .create ();
286+
287+ // Share the same credentials used by Datastore client with the TraceExporter and
288+ // TraceServiceClient to ensure consistency and avoid auth issues in environments
289+ // where default ADC resolution might fail for the exporter.
290+ Credentials credentials = DatastoreOptions .getDefaultInstance ().getCredentials ();
291+
292+ TraceConfiguration .Builder traceConfigurationBuilder =
293+ TraceConfiguration .builder ().setProjectId (projectId );
294+ if (credentials != null ) {
295+ traceConfigurationBuilder .setCredentials (credentials );
296+ }
297+ traceExporter = TraceExporter .createWithConfiguration (traceConfigurationBuilder .build ());
298+
299+ TraceServiceSettings .Builder clientBuilder = TraceServiceSettings .newBuilder ();
300+ if (credentials != null ) {
301+ clientBuilder .setCredentialsProvider (FixedCredentialsProvider .create (credentials ));
302+ }
303+ traceClient = TraceServiceClient .create (clientBuilder .build ());
287304 random = new Random ();
288305 }
289306
@@ -381,7 +398,7 @@ public void after() throws Exception {
381398
382399 @ AfterClass
383400 public static void teardown () throws Exception {
384- traceClient_v1 .close ();
401+ traceClient .close ();
385402 }
386403
387404 // Generates a random hex string of length `numBytes`
@@ -443,7 +460,7 @@ protected void fetchAndValidateTrace(
443460 // Fetch traces
444461 do {
445462 try {
446- retrievedTrace = traceClient_v1 .getTrace (projectId , traceId );
463+ retrievedTrace = traceClient .getTrace (projectId , traceId );
447464 assertEquals (traceId , retrievedTrace .getTraceId ());
448465
449466 logger .info (
@@ -530,7 +547,7 @@ public void traceContainerTest() throws Exception {
530547 int numRetries = GET_TRACE_RETRY_COUNT ;
531548 do {
532549 try {
533- traceResp = traceClient_v1 .getTrace (projectId , customSpanContext .getTraceId ());
550+ traceResp = traceClient .getTrace (projectId , customSpanContext .getTraceId ());
534551 if (traceResp .getSpansCount () == expectedSpanCount ) {
535552 logger .info ("Success: Got " + expectedSpanCount + " spans." );
536553 break ;
@@ -990,7 +1007,7 @@ public void runInTransactionQueryTest() throws Exception {
9901007 Query .newEntityQueryBuilder ().setKind (KEY1 .getKind ()).setFilter (filter ).build ();
9911008 Datastore .TransactionCallable <Boolean > callable =
9921009 transaction -> {
993- QueryResults <Entity > queryResults = datastore .run (query );
1010+ QueryResults <Entity > queryResults = transaction .run (query );
9941011 assertTrue (queryResults .hasNext ());
9951012 assertEquals (entity1 , queryResults .next ());
9961013 assertFalse (queryResults .hasNext ());
@@ -1007,7 +1024,7 @@ public void runInTransactionQueryTest() throws Exception {
10071024 /* numExpectedSpans= */ 4 ,
10081025 Arrays .asList (
10091026 Arrays .asList (SPAN_NAME_TRANSACTION_RUN , SPAN_NAME_BEGIN_TRANSACTION ),
1010- Arrays .asList (SPAN_NAME_TRANSACTION_RUN , SPAN_NAME_RUN_QUERY ),
1027+ Arrays .asList (SPAN_NAME_TRANSACTION_RUN , SPAN_NAME_TRANSACTION_RUN_QUERY ),
10111028 Arrays .asList (SPAN_NAME_TRANSACTION_RUN , SPAN_NAME_TRANSACTION_COMMIT )));
10121029 }
10131030}
0 commit comments