1616
1717package com .example .spanner ;
1818
19+ import com .google .api .MonitoredResource ;
20+ import com .google .cloud .MetadataConfig ;
1921import com .google .cloud .spanner .DatabaseClient ;
2022import com .google .cloud .spanner .DatabaseId ;
2123import com .google .cloud .spanner .ResultSet ;
2224import com .google .cloud .spanner .Spanner ;
2325import com .google .cloud .spanner .SpannerOptions ;
2426import com .google .cloud .spanner .Statement ;
2527import com .google .cloud .spanner .spi .v1 .SpannerRpcViews ;
28+ import io .opencensus .common .Duration ;
2629import io .opencensus .common .Scope ;
2730import io .opencensus .contrib .grpc .metrics .RpcViews ;
2831import io .opencensus .contrib .zpages .ZPageHandlers ;
3235import io .opencensus .trace .samplers .Samplers ;
3336import java .util .Arrays ;
3437
35- /** This sample demonstrates how to enable opencensus tracing and stats in cloud spanner client.
38+ /**
39+ * This sample demonstrates how to enable opencensus tracing and stats in cloud spanner client.
3640 *
37- * @deprecated The OpenCensus project is deprecated. Use OpenTelemetry to enable metrics
38- * and stats with cloud spanner client.
39- */
41+ * @deprecated The OpenCensus project is deprecated. Use OpenTelemetry to enable metrics and stats
42+ * with cloud spanner client.
43+ * <p>Note: This sample uses System.exit(0) to ensure clean termination because the
44+ * ZPageHandlers HTTP server (localhost:8080/tracez) uses non-daemon threads and does not
45+ * provide a public stop() method.
46+ */
4047public class TracingSample {
4148
4249 private static final String SAMPLE_SPAN = "CloudSpannerSample" ;
@@ -58,7 +65,13 @@ public static void main(String[] args) throws Exception {
5865 .registerSpanNamesForCollection (Arrays .asList (SAMPLE_SPAN ));
5966
6067 // Installs an exporter for stack driver stats.
61- StackdriverStatsExporter .createAndRegister ();
68+ MonitoredResource .Builder builder = MonitoredResource .newBuilder ();
69+ if (MetadataConfig .getProjectId () != null ) {
70+ builder .putLabels ("project_id" , options .getProjectId ());
71+ }
72+ builder .setType ("global" );
73+ StackdriverStatsExporter .createAndRegisterWithProjectIdAndMonitoredResource (
74+ options .getProjectId (), Duration .create (60L , 0 ), builder .build ());
6275 RpcViews .registerAllGrpcViews ();
6376 // Capture GFE Latency and GFE Header missing count.
6477 SpannerRpcViews .registerGfeLatencyAndHeaderMissingCountViews ();
@@ -85,8 +98,19 @@ public static void main(String[] args) throws Exception {
8598 }
8699 }
87100 } finally {
88- // Closes the client which will free up the resources used
101+ // First, shutdown the stats/metrics exporters
102+ StackdriverStatsExporter .unregister ();
103+
104+ // Shutdown tracing components
105+ StackdriverExporter .unregister ();
106+ Tracing .getExportComponent ().shutdown ();
107+
108+ // Close the spanner client
89109 spanner .close ();
110+
111+ // Force immediate exit since ZPageHandlers.startHttpServerAndRegisterAll(8080)
112+ // starts a non-daemon HTTP server thread that cannot be stopped gracefully
113+ System .exit (0 );
90114 }
91115 }
92116}
0 commit comments