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

Adds tracing sample for cloud spanner #1002

Merged
merged 10 commits into from
Jan 23, 2018
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
6 changes: 6 additions & 0 deletions spanner/cloud-client/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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="<command> 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=<instance id> -Dspanner.sample.database=<new database id> -Dspanner.quickstart.database=<existing database id>
38 changes: 37 additions & 1 deletion spanner/cloud-client/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ limitations under the License.
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.source>1.8</maven.compiler.source>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<opencensus.version>0.11.0</opencensus.version>
</properties>

<dependencies>
Expand All @@ -41,10 +42,14 @@ limitations under the License.
<artifactId>google-cloud-spanner</artifactId>
<version>0.33.0-beta</version>
<exclusions>
<exclusion> <!-- exclude an old version of Guava -->
<exclusion>
<groupId>com.google.guava</groupId>
<artifactId>guava-jdk5</artifactId>
</exclusion>
<exclusion>
<groupId>io.opencensus</groupId>
<artifactId>opencensus-api</artifactId>
</exclusion>
</exclusions>
</dependency>

Expand All @@ -53,6 +58,37 @@ limitations under the License.
<artifactId>guava</artifactId>
<version>20.0</version>
</dependency>
<dependency>
<groupId>io.opencensus</groupId>
<artifactId>opencensus-api</artifactId>
<version>${opencensus.version}</version>
</dependency>
<dependency>
<groupId>io.opencensus</groupId>
<artifactId>opencensus-impl</artifactId>
<version>${opencensus.version}</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>io.opencensus</groupId>
<artifactId>opencensus-contrib-zpages</artifactId>
<version>${opencensus.version}</version>
</dependency>
<dependency>
<groupId>io.opencensus</groupId>
<artifactId>opencensus-exporter-trace-stackdriver</artifactId>
<version>${opencensus.version}</version>
</dependency>
<dependency>
<groupId>io.opencensus</groupId>
<artifactId>opencensus-exporter-stats-stackdriver</artifactId>
<version>${opencensus.version}</version>
</dependency>
<dependency>
<groupId>io.opencensus</groupId>
<artifactId>opencensus-contrib-grpc-metrics</artifactId>
<version>${opencensus.version}</version>
</dependency>

<!-- Test dependencies -->
<dependency>
Expand Down
Original file line number Diff line number Diff line change
@@ -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 <instance_id> <database_id>");
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();
}
}

}