16
16
17
17
package com .example .spanner ;
18
18
19
+ import com .google .api .MonitoredResource ;
20
+ import com .google .cloud .MetadataConfig ;
19
21
import com .google .cloud .spanner .DatabaseClient ;
20
22
import com .google .cloud .spanner .DatabaseId ;
21
23
import com .google .cloud .spanner .ResultSet ;
22
24
import com .google .cloud .spanner .Spanner ;
23
25
import com .google .cloud .spanner .SpannerOptions ;
24
26
import com .google .cloud .spanner .Statement ;
25
27
import com .google .cloud .spanner .spi .v1 .SpannerRpcViews ;
28
+ import io .opencensus .common .Duration ;
26
29
import io .opencensus .common .Scope ;
27
30
import io .opencensus .contrib .grpc .metrics .RpcViews ;
28
31
import io .opencensus .contrib .zpages .ZPageHandlers ;
32
35
import io .opencensus .trace .samplers .Samplers ;
33
36
import java .util .Arrays ;
34
37
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.
36
40
*
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
+ */
40
47
public class TracingSample {
41
48
42
49
private static final String SAMPLE_SPAN = "CloudSpannerSample" ;
@@ -58,7 +65,13 @@ public static void main(String[] args) throws Exception {
58
65
.registerSpanNamesForCollection (Arrays .asList (SAMPLE_SPAN ));
59
66
60
67
// 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 ());
62
75
RpcViews .registerAllGrpcViews ();
63
76
// Capture GFE Latency and GFE Header missing count.
64
77
SpannerRpcViews .registerGfeLatencyAndHeaderMissingCountViews ();
@@ -85,8 +98,19 @@ public static void main(String[] args) throws Exception {
85
98
}
86
99
}
87
100
} 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
89
109
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 );
90
114
}
91
115
}
92
116
}
0 commit comments