diff --git a/README.md b/README.md index 6d1ca42d1fc..1e3f5dd5652 100644 --- a/README.md +++ b/README.md @@ -19,7 +19,7 @@ If you are using Maven with [BOM][libraries-bom], add this to your pom.xml file: com.google.cloud libraries-bom - 26.48.0 + 26.49.0 pom import 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 a3d4e5bd072..9678ebfec74 100644 --- a/samples/snippets/src/main/java/com/example/spanner/TracingSample.java +++ b/samples/snippets/src/main/java/com/example/spanner/TracingSample.java @@ -16,6 +16,8 @@ package com.example.spanner; +import com.google.api.MonitoredResource; +import com.google.cloud.MetadataConfig; import com.google.cloud.spanner.DatabaseClient; import com.google.cloud.spanner.DatabaseId; import com.google.cloud.spanner.ResultSet; @@ -23,6 +25,7 @@ import com.google.cloud.spanner.SpannerOptions; import com.google.cloud.spanner.Statement; import com.google.cloud.spanner.spi.v1.SpannerRpcViews; +import io.opencensus.common.Duration; import io.opencensus.common.Scope; import io.opencensus.contrib.grpc.metrics.RpcViews; import io.opencensus.contrib.zpages.ZPageHandlers; @@ -32,11 +35,15 @@ import io.opencensus.trace.samplers.Samplers; import java.util.Arrays; -/** This sample demonstrates how to enable opencensus tracing and stats in cloud spanner client. +/** + * 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. -*/ + * @deprecated The OpenCensus project is deprecated. Use OpenTelemetry to enable metrics and stats + * with cloud spanner client. + *

Note: This sample uses System.exit(0) to ensure clean termination because the + * ZPageHandlers HTTP server (localhost:8080/tracez) uses non-daemon threads and does not + * provide a public stop() method. + */ public class TracingSample { private static final String SAMPLE_SPAN = "CloudSpannerSample"; @@ -58,7 +65,13 @@ public static void main(String[] args) throws Exception { .registerSpanNamesForCollection(Arrays.asList(SAMPLE_SPAN)); // Installs an exporter for stack driver stats. - StackdriverStatsExporter.createAndRegister(); + MonitoredResource.Builder builder = MonitoredResource.newBuilder(); + if (MetadataConfig.getProjectId() != null) { + builder.putLabels("project_id", options.getProjectId()); + } + builder.setType("global"); + StackdriverStatsExporter.createAndRegisterWithProjectIdAndMonitoredResource( + options.getProjectId(), Duration.create(60L, 0), builder.build()); RpcViews.registerAllGrpcViews(); // Capture GFE Latency and GFE Header missing count. SpannerRpcViews.registerGfeLatencyAndHeaderMissingCountViews(); @@ -85,8 +98,19 @@ public static void main(String[] args) throws Exception { } } } finally { - // Closes the client which will free up the resources used + // 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 ZPageHandlers.startHttpServerAndRegisterAll(8080) + // starts a non-daemon HTTP server thread that cannot be stopped gracefully + System.exit(0); } } }