Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: fix tracing sample to exit when completed, and use custom monitored resource for export #3287

Merged
merged 4 commits into from
Oct 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
Copy link
Contributor Author

@rahul2393 rahul2393 Oct 25, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is being added because without this an exception is thrown because StackdriverStatsExporter tries to use different project from metadata API

Caused by: io.grpc.StatusRuntimeException: INVALID_ARGUMENT: Field timeSeries[0].resource.labels.project_id had an invalid value of "google.com:xxxxx: if present, must be the project number or ID in the request name.                                                      
        at io.grpc.Status.asRuntimeException(Status.java:533) 
        ```

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);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems overly harsh. What is the reason that we need to call System.exit(0) to stop the application here? Is the Stackdriver exporter using non-daemon threads that are not stopped? If so, does the StackdriverExporter have a close/shutdown method?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

StackdriverExporter does not export any close/shutdown method they only have unregister, without system.exit I see exceptions and sample getting stuck https://pastebin.com/drh6qubH

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The 'getting stuck' problem is actually caused by ZPageHandlers.startHttpServerAndRegisterAll(8080);. That starts an HTTP server that can be accessed on http://localhost:8080/tracez to view the traces that were collected. That component does not have a public stop() method, and keeps the JVM alive, as it uses non-daemon threads. Can we update the comment here to reflect this?

The errors regarding closed channels could in theory still happen. I think that there is a race condition in the OpenCensus stats exporter that tries to flush any remaining stats, while at the same time also closing the underlying exporter client. Those can be ignored.

}
}
}
Loading