diff --git a/samples/snippets/src/main/java/com/example/spanner/TracingSample.java b/samples/snippets/src/main/java/com/example/spanner/TracingSample.java index 2e0cc7f6557..dc2315d833e 100644 --- a/samples/snippets/src/main/java/com/example/spanner/TracingSample.java +++ b/samples/snippets/src/main/java/com/example/spanner/TracingSample.java @@ -39,7 +39,9 @@ * This sample demonstrates how to enable opencensus tracing and stats in cloud spanner client. * * @deprecated The OpenCensus project is deprecated. Use OpenTelemetry to enable metrics and stats - * with cloud spanner client. + * with cloud spanner client. + *
Note: This sample uses System.exit(0) to ensure clean termination due to OpenCensus + * background threads. */ public class TracingSample { @@ -95,10 +97,18 @@ public static void main(String[] args) throws Exception { } } } finally { - // Closes the client which will free up the resources used - spanner.close(); - StackdriverExporter.unregister(); + // First, shutdown the stats/metrics exporters StackdriverStatsExporter.unregister(); + + // Shutdown tracing components + StackdriverExporter.unregister(); + Tracing.getExportComponent().shutdown(); + + // Close the spanner client + spanner.close(); + + // Force immediate exit since this is a sample application and OpenCensus + // background threads might prevent clean shutdown System.exit(0); } }