Skip to content

Commit

Permalink
docs: fix tracing sample to exit when completed, and use custom monit…
Browse files Browse the repository at this point in the history
…ored 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]>
  • Loading branch information
rahul2393 and cloud-java-bot authored Oct 25, 2024
1 parent e5a3bad commit ddb65b1
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 7 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ If you are using Maven with [BOM][libraries-bom], add this to your pom.xml file:
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>libraries-bom</artifactId>
<version>26.48.0</version>
<version>26.49.0</version>
<type>pom</type>
<scope>import</scope>
</dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,16 @@

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;
import com.google.cloud.spanner.Spanner;
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;
Expand All @@ -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.
* <p>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";
Expand All @@ -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();
Expand All @@ -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);
}
}
}

0 comments on commit ddb65b1

Please sign in to comment.