From fb30837a4cfa6ffc096e5ceafb0871ad8c2f065a Mon Sep 17 00:00:00 2001 From: rahul yadav Date: Wed, 21 Aug 2024 15:35:48 +0530 Subject: [PATCH 1/4] docs: fix tracing sample to exit when completed, and use custom monitored resource for export --- .../com/example/spanner/TracingSample.java | 23 +++++++++++++++---- 1 file changed, 18 insertions(+), 5 deletions(-) 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..2e0cc7f6557 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,12 @@ 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. + */ public class TracingSample { private static final String SAMPLE_SPAN = "CloudSpannerSample"; @@ -58,7 +62,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(); @@ -87,6 +97,9 @@ public static void main(String[] args) throws Exception { } finally { // Closes the client which will free up the resources used spanner.close(); + StackdriverExporter.unregister(); + StackdriverStatsExporter.unregister(); + System.exit(0); } } } From 37f88d088ae0d25287cecf8a557c38ef85efe917 Mon Sep 17 00:00:00 2001 From: rahul yadav Date: Fri, 25 Oct 2024 10:39:09 +0530 Subject: [PATCH 2/4] add comments and reorder ops --- .../com/example/spanner/TracingSample.java | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) 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 2e0cc7f6557..dc2315d833e 100644 --- a/samples/snippets/src/main/java/com/example/spanner/TracingSample.java +++ b/samples/snippets/src/main/java/com/example/spanner/TracingSample.java @@ -39,7 +39,9 @@ * 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. + * with cloud spanner client. + *

Note: This sample uses System.exit(0) to ensure clean termination due to OpenCensus + * background threads. */ public class TracingSample { @@ -95,10 +97,18 @@ public static void main(String[] args) throws Exception { } } } finally { - // Closes the client which will free up the resources used - spanner.close(); - StackdriverExporter.unregister(); + // 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 this is a sample application and OpenCensus + // background threads might prevent clean shutdown System.exit(0); } } From 15c4881e7a072024c2eadda6b7f4bc7cafc6c700 Mon Sep 17 00:00:00 2001 From: cloud-java-bot Date: Fri, 25 Oct 2024 05:19:04 +0000 Subject: [PATCH 3/4] chore: generate libraries at Fri Oct 25 05:15:57 UTC 2024 --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 From 9082cca22a2ad9f0e8352279278a125f24a3913f Mon Sep 17 00:00:00 2001 From: rahul yadav Date: Fri, 25 Oct 2024 12:12:47 +0530 Subject: [PATCH 4/4] update comment as per findings --- .../src/main/java/com/example/spanner/TracingSample.java | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) 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 dc2315d833e..9678ebfec74 100644 --- a/samples/snippets/src/main/java/com/example/spanner/TracingSample.java +++ b/samples/snippets/src/main/java/com/example/spanner/TracingSample.java @@ -40,8 +40,9 @@ * * @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 due to OpenCensus - * background threads. + *

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 { @@ -107,8 +108,8 @@ public static void main(String[] args) throws Exception { // Close the spanner client spanner.close(); - // Force immediate exit since this is a sample application and OpenCensus - // background threads might prevent clean shutdown + // Force immediate exit since ZPageHandlers.startHttpServerAndRegisterAll(8080) + // starts a non-daemon HTTP server thread that cannot be stopped gracefully System.exit(0); } }