-
Notifications
You must be signed in to change notification settings - Fork 115
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
Changes from all commits
fb30837
37f88d0
15c4881
9082cca
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
@@ -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"; | ||
|
@@ -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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The 'getting stuck' problem is actually caused by 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. |
||
} | ||
} | ||
} |
There was a problem hiding this comment.
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