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 7 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
33 changes: 31 additions & 2 deletions spanner/cloud-client/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,16 @@ limitations under the License.
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-spanner</artifactId>
<version>0.32.0-beta</version>
<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 @@ -66,6 +70,31 @@ limitations under the License.
<artifactId>guava</artifactId>
<version>20.0</version>
</dependency>
<dependency>
<groupId>io.opencensus</groupId>
<artifactId>opencensus-api</artifactId>
<version>0.11.0</version>

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make it a variable because it is used in few places?

</dependency>
<dependency>
<groupId>io.opencensus</groupId>
<artifactId>opencensus-impl</artifactId>
<version>0.11.0</version>

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

<scope>runtime</scope>

</dependency>
<dependency>
<groupId>io.opencensus</groupId>
<artifactId>opencensus-contrib-zpages</artifactId>
<version>0.11.0</version>
</dependency>
<dependency>
<groupId>io.opencensus</groupId>
<artifactId>opencensus-exporter-trace-logging</artifactId>
<version>0.11.0</version>
</dependency>
<dependency>
<groupId>io.opencensus</groupId>
<artifactId>opencensus-exporter-trace-stackdriver</artifactId>
<version>0.11.0</version>
</dependency>

<!-- Test dependencies -->
<dependency>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package com.example.spanner;

import java.util.Arrays;

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.zpages.ZPageHandlers;
import io.opencensus.exporter.trace.stackdriver.StackdriverExporter;
import io.opencensus.trace.Tracing;
import io.opencensus.trace.samplers.Samplers;

public class TracingSample {

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.
StackdriverExporter.createAndRegister();
Tracing.getExportComponent().getSampledSpanStore().registerSpanNamesForCollection(Arrays.asList("CloudSpannerSample"));

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

private static final String CLOUD_SPANNER_SAMPLE_SPAN_NAME = "CloudSpannerSample";

Not necessary the long suggested variable name but that will ensure that the string is the same here and 10 lines below.


// 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("CloudSpannerSample", 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();
}
}

}