Skip to content

Commit d281a62

Browse files
authored
fix(datastore): Configure TraceExporter with Datastore credentials in E2E tests (#13627)
Fixes #13618 Configure `TraceExporter` and `TraceServiceClient` with default Datastore credentials in `ITE2ETracingTest`. This fixes authentication issues when running E2E tracing tests in environments where Application Default Credentials (ADC) might not be automatically picked up by the exporter or client. Similar to the fix implemented for Spanner in ef76d87. TAG=agy CONV=5d4f613d-13e3-4d5f-aa1f-5a913b05c507
1 parent 20999d3 commit d281a62

1 file changed

Lines changed: 28 additions & 11 deletions

File tree

java-datastore/google-cloud-datastore/src/test/java/com/google/cloud/datastore/it/ITE2ETracingTest.java

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,10 @@
3737
import static org.junit.Assert.assertNull;
3838
import static org.junit.Assert.assertTrue;
3939

40+
import com.google.api.gax.core.FixedCredentialsProvider;
4041
import com.google.api.gax.rpc.DeadlineExceededException;
4142
import com.google.api.gax.rpc.NotFoundException;
43+
import com.google.auth.Credentials;
4244
import com.google.cloud.datastore.AggregationQuery;
4345
import com.google.cloud.datastore.AggregationResult;
4446
import com.google.cloud.datastore.AggregationResults;
@@ -58,6 +60,7 @@
5860
import com.google.cloud.opentelemetry.trace.TraceConfiguration;
5961
import com.google.cloud.opentelemetry.trace.TraceExporter;
6062
import com.google.cloud.trace.v1.TraceServiceClient;
63+
import com.google.cloud.trace.v1.TraceServiceSettings;
6164
import com.google.common.base.Preconditions;
6265
import com.google.devtools.cloudtrace.v1.Trace;
6366
import 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

Comments
 (0)