|
| 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