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:
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); } } }