-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
Changes from 7 commits
f65f72b
d576ca0
d7e9520
1d1738f
4d755ab
2f86625
74050b6
6635352
794561c
29568ba
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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> | ||
|
||
|
@@ -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> | ||
</dependency> | ||
<dependency> | ||
<groupId>io.opencensus</groupId> | ||
<artifactId>opencensus-impl</artifactId> | ||
<version>0.11.0</version> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
</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> | ||
|
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")); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(); | ||
} | ||
} | ||
|
||
} |
There was a problem hiding this comment.
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?