From 41df69423fb97740e7d69d02b323d74e573e5177 Mon Sep 17 00:00:00 2001 From: Vikas Kedia Date: Tue, 23 Jan 2018 13:13:31 -0800 Subject: [PATCH] Adds tracing sample for cloud spanner (#1002) * Add tracing * Fixes * tracing * Add tracing sample * Revert Quickstart * Revert Spanner sample * Address review comments * Fix style violations --- spanner/cloud-client/README.md | 6 ++ spanner/cloud-client/pom.xml | 38 +++++++- .../com/example/spanner/TracingSample.java | 88 +++++++++++++++++++ 3 files changed, 131 insertions(+), 1 deletion(-) create mode 100644 spanner/cloud-client/src/main/java/com/example/spanner/TracingSample.java diff --git a/spanner/cloud-client/README.md b/spanner/cloud-client/README.md index 9029e54dea1..7c2fe580e24 100644 --- a/spanner/cloud-client/README.md +++ b/spanner/cloud-client/README.md @@ -34,5 +34,11 @@ You can then run a given `ClassName` via: ### Running the tutorial mvn exec:java -Dexec.mainClass=com.example.spanner.SpannerSample -Dexec.args=" my-instance my-database" +## Tracing sample +TracingSample.java demonstrates how to export traces generated by client library to StackDriver and to /tracez page. + +### Running the tracing sample + mvn exec:java -Dexec.mainClass=com.example.spanner.TracingSample -Dexec.args="my-instance my-database" + ## Test mvn verify -Dspanner.test.instance= -Dspanner.sample.database= -Dspanner.quickstart.database= diff --git a/spanner/cloud-client/pom.xml b/spanner/cloud-client/pom.xml index c4f48e70242..6cd5f91e944 100644 --- a/spanner/cloud-client/pom.xml +++ b/spanner/cloud-client/pom.xml @@ -33,6 +33,7 @@ limitations under the License. 1.8 1.8 UTF-8 + 0.11.0 @@ -41,10 +42,14 @@ limitations under the License. google-cloud-spanner 0.33.0-beta - + com.google.guava guava-jdk5 + + io.opencensus + opencensus-api + @@ -53,6 +58,37 @@ limitations under the License. guava 20.0 + + io.opencensus + opencensus-api + ${opencensus.version} + + + io.opencensus + opencensus-impl + ${opencensus.version} + runtime + + + io.opencensus + opencensus-contrib-zpages + ${opencensus.version} + + + io.opencensus + opencensus-exporter-trace-stackdriver + ${opencensus.version} + + + io.opencensus + opencensus-exporter-stats-stackdriver + ${opencensus.version} + + + io.opencensus + opencensus-contrib-grpc-metrics + ${opencensus.version} + diff --git a/spanner/cloud-client/src/main/java/com/example/spanner/TracingSample.java b/spanner/cloud-client/src/main/java/com/example/spanner/TracingSample.java new file mode 100644 index 00000000000..4dfd5a1cb15 --- /dev/null +++ b/spanner/cloud-client/src/main/java/com/example/spanner/TracingSample.java @@ -0,0 +1,88 @@ +/* + * Copyright 2018 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.spanner; + +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 io.opencensus.common.Scope; +import io.opencensus.contrib.grpc.metrics.RpcViews; +import io.opencensus.contrib.zpages.ZPageHandlers; +import io.opencensus.exporter.stats.stackdriver.StackdriverStatsExporter; +import io.opencensus.exporter.trace.stackdriver.StackdriverExporter; +import io.opencensus.trace.Tracing; +import io.opencensus.trace.samplers.Samplers; + +import java.util.Arrays; + +/** + * This sample demonstrates how to enable opencensus tracing and stats in cloud spanner client. + */ +public class TracingSample { + + private static final String SAMPLE_SPAN = "CloudSpannerSample"; + + public static void main(String[] args) throws Exception { + if (args.length != 2) { + System.err.println("Usage: TracingSample "); + return; + } + SpannerOptions options = SpannerOptions.newBuilder().build(); + Spanner spanner = options.getService(); + + // Installs a handler for /tracez page. + ZPageHandlers.startHttpServerAndRegisterAll(8080); + // Installs an exporter for stack driver traces. + StackdriverExporter.createAndRegister(); + Tracing.getExportComponent().getSampledSpanStore().registerSpanNamesForCollection( + Arrays.asList(SAMPLE_SPAN)); + + // Installs an exporter for stack driver stats. + StackdriverStatsExporter.createAndRegister(); + RpcViews.registerAllCumulativeViews(); + + // Name of your instance & database. + String instanceId = args[0]; + String databaseId = args[1]; + try { + // Creates a database client + DatabaseClient dbClient = spanner.getDatabaseClient(DatabaseId.of( + options.getProjectId(), instanceId, databaseId)); + // Queries the database + try (Scope ss = Tracing.getTracer() + .spanBuilderWithExplicitParent(SAMPLE_SPAN, null) + .setSampler(Samplers.alwaysSample()) + .startScopedSpan()) { + ResultSet resultSet = dbClient.singleUse().executeQuery(Statement.of("SELECT 1")); + + System.out.println("\n\nResults:"); + // Prints the results + while (resultSet.next()) { + System.out.printf("%d\n\n", resultSet.getLong(0)); + } + } + } finally { + // Closes the client which will free up the resources used + spanner.close(); + } + } + +}