@@ -227,14 +227,12 @@ private boolean dfsContainsCallStack(long spanId, List<String> expectedCallStack
227227
228228 private static final int NUM_SPAN_ID_BYTES = 16 ;
229229
230- private static final int GET_TRACE_RETRY_COUNT = 60 ;
230+ private static final int GET_TRACE_RETRY_COUNT = 120 ;
231231
232232 private static final int GET_TRACE_RETRY_BACKOFF_MILLIS = 1000 ;
233233
234234 private static final int TRACE_FORCE_FLUSH_MILLIS = 5000 ;
235235
236- private static final int TRACE_PROVIDER_SHUTDOWN_MILLIS = 1000 ;
237-
238236 private static Key KEY1 ;
239237
240238 private static Key KEY2 ;
@@ -246,22 +244,24 @@ private boolean dfsContainsCallStack(long spanId, List<String> expectedCallStack
246244 // Random int generator for trace ID and span ID
247245 private static Random random ;
248246
249- private static TraceExporter traceExporter ;
247+ private static Credentials credentials ;
248+
249+ private TraceExporter traceExporter ;
250250
251251 // Required for reading back traces from Cloud Trace for validation
252252 private static TraceServiceClient traceClient ;
253253
254254 // Custom SpanContext for each test, required for TraceID injection
255- private static SpanContext customSpanContext ;
255+ private SpanContext customSpanContext ;
256256
257257 // Trace read back from Cloud Trace using traceClient for verification
258- private static Trace retrievedTrace ;
258+ private Trace retrievedTrace ;
259259
260- private static String rootSpanName ;
261- private static Tracer tracer ;
260+ private String rootSpanName ;
261+ private Tracer tracer ;
262262
263263 // Required to set custom-root span
264- private static OpenTelemetrySdk openTelemetrySdk ;
264+ private OpenTelemetrySdk openTelemetrySdk ;
265265
266266 private static String projectId ;
267267
@@ -287,14 +287,7 @@ public static void setup() throws IOException {
287287 // Share the same credentials used by Datastore client with the TraceExporter and
288288 // TraceServiceClient to ensure consistency and avoid auth issues in environments
289289 // 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 ());
290+ credentials = DatastoreOptions .getDefaultInstance ().getCredentials ();
298291
299292 TraceServiceSettings .Builder clientBuilder = TraceServiceSettings .newBuilder ();
300293 if (credentials != null ) {
@@ -310,6 +303,13 @@ public void before() throws Exception {
310303 Resource resource =
311304 Resource .getDefault ().merge (Resource .builder ().put (SERVICE_NAME , "Sparky" ).build ());
312305
306+ TraceConfiguration .Builder traceConfigurationBuilder =
307+ TraceConfiguration .builder ().setProjectId (projectId );
308+ if (credentials != null ) {
309+ traceConfigurationBuilder .setCredentials (credentials );
310+ }
311+ traceExporter = TraceExporter .createWithConfiguration (traceConfigurationBuilder .build ());
312+
313313 if (isUsingGlobalOpenTelemetrySDK ()) {
314314 openTelemetrySdk =
315315 OpenTelemetrySdk .builder ()
@@ -393,7 +393,22 @@ public void after() throws Exception {
393393 tracer = null ;
394394 retrievedTrace = null ;
395395 customSpanContext = null ;
396+ try {
397+ if (openTelemetrySdk != null ) {
398+ openTelemetrySdk .close ();
399+ }
400+ } finally {
401+ if (traceExporter != null ) {
402+ try {
403+ // Attempt to shut down traceExporter.
404+ traceExporter .shutdown ();
405+ } catch (Exception e ) {
406+ logger .log (Level .WARNING , "Failed to shut down traceExporter" , e );
407+ }
408+ }
409+ }
396410 openTelemetrySdk = null ;
411+ traceExporter = null ;
397412 }
398413
399414 @ AfterClass
@@ -437,10 +452,17 @@ protected Span getNewRootSpanWithContext() {
437452 }
438453
439454 protected void waitForTracesToComplete () throws Exception {
455+ if (openTelemetrySdk == null ) {
456+ logger .warning ("OpenTelemetrySdk is null, cannot flush traces" );
457+ return ;
458+ }
440459 logger .info ("Flushing traces..." );
441460 CompletableResultCode completableResultCode =
442461 openTelemetrySdk .getSdkTracerProvider ().forceFlush ();
443462 completableResultCode .join (TRACE_FORCE_FLUSH_MILLIS , TimeUnit .MILLISECONDS );
463+ if (!completableResultCode .isSuccess ()) {
464+ logger .warning ("Force flush did not complete successfully" );
465+ }
444466 }
445467
446468 // Validates `retrievedTrace`. Cloud Trace indexes traces w/ eventual consistency, even when
0 commit comments