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
  • Loading branch information
rahul2393 committed Aug 21, 2024
1 parent b44104f commit 7be099f
Showing 1 changed file with 18 additions and 5 deletions.
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,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";
Expand All @@ -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();
Expand Down Expand Up @@ -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);
}
}
}

0 comments on commit 7be099f

Please sign in to comment.