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
+ */
40
44
public class TracingSample {
41
45
42
46
private static final String SAMPLE_SPAN = "CloudSpannerSample" ;
@@ -58,7 +62,13 @@ public static void main(String[] args) throws Exception {
58
62
.registerSpanNamesForCollection (Arrays .asList (SAMPLE_SPAN ));
59
63
60
64
// Installs an exporter for stack driver stats.
61
- StackdriverStatsExporter .createAndRegister ();
65
+ MonitoredResource .Builder builder = MonitoredResource .newBuilder ();
66
+ if (MetadataConfig .getProjectId () != null ) {
67
+ builder .putLabels ("project_id" , options .getProjectId ());
68
+ }
69
+ builder .setType ("global" );
70
+ StackdriverStatsExporter .createAndRegisterWithProjectIdAndMonitoredResource (
71
+ options .getProjectId (), Duration .create (60L , 0 ), builder .build ());
62
72
RpcViews .registerAllGrpcViews ();
63
73
// Capture GFE Latency and GFE Header missing count.
64
74
SpannerRpcViews .registerGfeLatencyAndHeaderMissingCountViews ();
@@ -87,6 +97,9 @@ public static void main(String[] args) throws Exception {
87
97
} finally {
88
98
// Closes the client which will free up the resources used
89
99
spanner .close ();
100
+ StackdriverExporter .unregister ();
101
+ StackdriverStatsExporter .unregister ();
102
+ System .exit (0 );
90
103
}
91
104
}
92
105
}
0 commit comments