Skip to content

Commit 41df694

Browse files
vkediakurtisvg
authored andcommitted
Adds tracing sample for cloud spanner (GoogleCloudPlatform#1002)
* Add tracing * Fixes * tracing * Add tracing sample * Revert Quickstart * Revert Spanner sample * Address review comments * Fix style violations
1 parent e0918ef commit 41df694

File tree

3 files changed

+131
-1
lines changed

3 files changed

+131
-1
lines changed

spanner/cloud-client/README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,11 @@ You can then run a given `ClassName` via:
3434
### Running the tutorial
3535
mvn exec:java -Dexec.mainClass=com.example.spanner.SpannerSample -Dexec.args="<command> my-instance my-database"
3636

37+
## Tracing sample
38+
TracingSample.java demonstrates how to export traces generated by client library to StackDriver and to /tracez page.
39+
40+
### Running the tracing sample
41+
mvn exec:java -Dexec.mainClass=com.example.spanner.TracingSample -Dexec.args="my-instance my-database"
42+
3743
## Test
3844
mvn verify -Dspanner.test.instance=<instance id> -Dspanner.sample.database=<new database id> -Dspanner.quickstart.database=<existing database id>

spanner/cloud-client/pom.xml

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ limitations under the License.
3333
<maven.compiler.target>1.8</maven.compiler.target>
3434
<maven.compiler.source>1.8</maven.compiler.source>
3535
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
36+
<opencensus.version>0.11.0</opencensus.version>
3637
</properties>
3738

3839
<dependencies>
@@ -41,10 +42,14 @@ limitations under the License.
4142
<artifactId>google-cloud-spanner</artifactId>
4243
<version>0.33.0-beta</version>
4344
<exclusions>
44-
<exclusion> <!-- exclude an old version of Guava -->
45+
<exclusion>
4546
<groupId>com.google.guava</groupId>
4647
<artifactId>guava-jdk5</artifactId>
4748
</exclusion>
49+
<exclusion>
50+
<groupId>io.opencensus</groupId>
51+
<artifactId>opencensus-api</artifactId>
52+
</exclusion>
4853
</exclusions>
4954
</dependency>
5055

@@ -53,6 +58,37 @@ limitations under the License.
5358
<artifactId>guava</artifactId>
5459
<version>20.0</version>
5560
</dependency>
61+
<dependency>
62+
<groupId>io.opencensus</groupId>
63+
<artifactId>opencensus-api</artifactId>
64+
<version>${opencensus.version}</version>
65+
</dependency>
66+
<dependency>
67+
<groupId>io.opencensus</groupId>
68+
<artifactId>opencensus-impl</artifactId>
69+
<version>${opencensus.version}</version>
70+
<scope>runtime</scope>
71+
</dependency>
72+
<dependency>
73+
<groupId>io.opencensus</groupId>
74+
<artifactId>opencensus-contrib-zpages</artifactId>
75+
<version>${opencensus.version}</version>
76+
</dependency>
77+
<dependency>
78+
<groupId>io.opencensus</groupId>
79+
<artifactId>opencensus-exporter-trace-stackdriver</artifactId>
80+
<version>${opencensus.version}</version>
81+
</dependency>
82+
<dependency>
83+
<groupId>io.opencensus</groupId>
84+
<artifactId>opencensus-exporter-stats-stackdriver</artifactId>
85+
<version>${opencensus.version}</version>
86+
</dependency>
87+
<dependency>
88+
<groupId>io.opencensus</groupId>
89+
<artifactId>opencensus-contrib-grpc-metrics</artifactId>
90+
<version>${opencensus.version}</version>
91+
</dependency>
5692

5793
<!-- Test dependencies -->
5894
<dependency>
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/*
2+
* Copyright 2018 Google Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.spanner;
18+
19+
import com.google.cloud.spanner.DatabaseClient;
20+
import com.google.cloud.spanner.DatabaseId;
21+
import com.google.cloud.spanner.ResultSet;
22+
import com.google.cloud.spanner.Spanner;
23+
import com.google.cloud.spanner.SpannerOptions;
24+
import com.google.cloud.spanner.Statement;
25+
26+
import io.opencensus.common.Scope;
27+
import io.opencensus.contrib.grpc.metrics.RpcViews;
28+
import io.opencensus.contrib.zpages.ZPageHandlers;
29+
import io.opencensus.exporter.stats.stackdriver.StackdriverStatsExporter;
30+
import io.opencensus.exporter.trace.stackdriver.StackdriverExporter;
31+
import io.opencensus.trace.Tracing;
32+
import io.opencensus.trace.samplers.Samplers;
33+
34+
import java.util.Arrays;
35+
36+
/**
37+
* This sample demonstrates how to enable opencensus tracing and stats in cloud spanner client.
38+
*/
39+
public class TracingSample {
40+
41+
private static final String SAMPLE_SPAN = "CloudSpannerSample";
42+
43+
public static void main(String[] args) throws Exception {
44+
if (args.length != 2) {
45+
System.err.println("Usage: TracingSample <instance_id> <database_id>");
46+
return;
47+
}
48+
SpannerOptions options = SpannerOptions.newBuilder().build();
49+
Spanner spanner = options.getService();
50+
51+
// Installs a handler for /tracez page.
52+
ZPageHandlers.startHttpServerAndRegisterAll(8080);
53+
// Installs an exporter for stack driver traces.
54+
StackdriverExporter.createAndRegister();
55+
Tracing.getExportComponent().getSampledSpanStore().registerSpanNamesForCollection(
56+
Arrays.asList(SAMPLE_SPAN));
57+
58+
// Installs an exporter for stack driver stats.
59+
StackdriverStatsExporter.createAndRegister();
60+
RpcViews.registerAllCumulativeViews();
61+
62+
// Name of your instance & database.
63+
String instanceId = args[0];
64+
String databaseId = args[1];
65+
try {
66+
// Creates a database client
67+
DatabaseClient dbClient = spanner.getDatabaseClient(DatabaseId.of(
68+
options.getProjectId(), instanceId, databaseId));
69+
// Queries the database
70+
try (Scope ss = Tracing.getTracer()
71+
.spanBuilderWithExplicitParent(SAMPLE_SPAN, null)
72+
.setSampler(Samplers.alwaysSample())
73+
.startScopedSpan()) {
74+
ResultSet resultSet = dbClient.singleUse().executeQuery(Statement.of("SELECT 1"));
75+
76+
System.out.println("\n\nResults:");
77+
// Prints the results
78+
while (resultSet.next()) {
79+
System.out.printf("%d\n\n", resultSet.getLong(0));
80+
}
81+
}
82+
} finally {
83+
// Closes the client which will free up the resources used
84+
spanner.close();
85+
}
86+
}
87+
88+
}

0 commit comments

Comments
 (0)