Skip to content

Commit ddb65b1

Browse files
docs: fix tracing sample to exit when completed, and use custom monitored resource for export (#3287)
* docs: fix tracing sample to exit when completed, and use custom monitored resource for export * add comments and reorder ops * chore: generate libraries at Fri Oct 25 05:15:57 UTC 2024 * update comment as per findings --------- Co-authored-by: cloud-java-bot <[email protected]>
1 parent e5a3bad commit ddb65b1

File tree

2 files changed

+31
-7
lines changed

2 files changed

+31
-7
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ If you are using Maven with [BOM][libraries-bom], add this to your pom.xml file:
1919
<dependency>
2020
<groupId>com.google.cloud</groupId>
2121
<artifactId>libraries-bom</artifactId>
22-
<version>26.48.0</version>
22+
<version>26.49.0</version>
2323
<type>pom</type>
2424
<scope>import</scope>
2525
</dependency>

samples/snippets/src/main/java/com/example/spanner/TracingSample.java

+30-6
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,16 @@
1616

1717
package com.example.spanner;
1818

19+
import com.google.api.MonitoredResource;
20+
import com.google.cloud.MetadataConfig;
1921
import com.google.cloud.spanner.DatabaseClient;
2022
import com.google.cloud.spanner.DatabaseId;
2123
import com.google.cloud.spanner.ResultSet;
2224
import com.google.cloud.spanner.Spanner;
2325
import com.google.cloud.spanner.SpannerOptions;
2426
import com.google.cloud.spanner.Statement;
2527
import com.google.cloud.spanner.spi.v1.SpannerRpcViews;
28+
import io.opencensus.common.Duration;
2629
import io.opencensus.common.Scope;
2730
import io.opencensus.contrib.grpc.metrics.RpcViews;
2831
import io.opencensus.contrib.zpages.ZPageHandlers;
@@ -32,11 +35,15 @@
3235
import io.opencensus.trace.samplers.Samplers;
3336
import 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+
*/
4047
public 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

Comments
 (0)