File tree

8 files changed

+314
-80
lines changed

8 files changed

+314
-80
lines changed
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@
2121
<maven.compiler.target>1.8</maven.compiler.target>
2222
<maven.compiler.source>1.8</maven.compiler.source>
2323
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
24+
<opencensus.version>0.26.0</opencensus.version>
25+
<trace.version>1.1.0</trace.version>
26+
<cloudmonitoring.version>1.100.1</cloudmonitoring.version>
2427
</properties>
2528

2629

@@ -32,6 +35,60 @@
3235
<version>2.0.1</version>
3336
</dependency>
3437
<!-- [END spanner_install_without_bom] -->
38+
39+
<dependency>
40+
<groupId>io.opencensus</groupId>
41+
<artifactId>opencensus-api</artifactId>
42+
<version>${opencensus.version}</version>
43+
</dependency>
44+
<dependency>
45+
<groupId>io.opencensus</groupId>
46+
<artifactId>opencensus-impl</artifactId>
47+
<version>${opencensus.version}</version>
48+
<scope>runtime</scope>
49+
</dependency>
50+
<dependency>
51+
<groupId>io.opencensus</groupId>
52+
<artifactId>opencensus-contrib-zpages</artifactId>
53+
<version>${opencensus.version}</version>
54+
</dependency>
55+
<dependency>
56+
<groupId>io.opencensus</groupId>
57+
<artifactId>opencensus-exporter-trace-stackdriver</artifactId>
58+
<version>${opencensus.version}</version>
59+
<exclusions>
60+
<exclusion>
61+
<groupId>com.google.cloud</groupId>
62+
<artifactId>google-cloud-trace</artifactId>
63+
</exclusion>
64+
</exclusions>
65+
</dependency>
66+
<dependency>
67+
<groupId>io.opencensus</groupId>
68+
<artifactId>opencensus-exporter-stats-stackdriver</artifactId>
69+
<version>${opencensus.version}</version>
70+
<exclusions>
71+
<exclusion>
72+
<groupId>com.google.cloud</groupId>
73+
<artifactId>google-cloud-monitoring</artifactId>
74+
</exclusion>
75+
</exclusions>
76+
</dependency>
77+
<dependency>
78+
<groupId>io.opencensus</groupId>
79+
<artifactId>opencensus-contrib-grpc-metrics</artifactId>
80+
<version>${opencensus.version}</version>
81+
</dependency>
82+
<dependency>
83+
<groupId>com.google.cloud</groupId>
84+
<artifactId>google-cloud-trace</artifactId>
85+
<version>${trace.version}</version>
86+
</dependency>
87+
<dependency>
88+
<groupId>com.google.cloud</groupId>
89+
<artifactId>google-cloud-monitoring</artifactId>
90+
<version>${cloudmonitoring.version}</version>
91+
</dependency>
3592

3693
<dependency>
3794
<groupId>junit</groupId>
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@
2121
<maven.compiler.target>1.8</maven.compiler.target>
2222
<maven.compiler.source>1.8</maven.compiler.source>
2323
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
24+
<opencensus.version>0.26.0</opencensus.version>
25+
<trace.version>1.1.0</trace.version>
26+
<cloudmonitoring.version>1.100.1</cloudmonitoring.version>
2427
</properties>
2528

2629
<dependencies>
@@ -31,6 +34,60 @@
3134
<version>2.0.3-SNAPSHOT</version>
3235
</dependency>
3336
<!-- {x-version-update-end} -->
37+
38+
<dependency>
39+
<groupId>io.opencensus</groupId>
40+
<artifactId>opencensus-api</artifactId>
41+
<version>${opencensus.version}</version>
42+
</dependency>
43+
<dependency>
44+
<groupId>io.opencensus</groupId>
45+
<artifactId>opencensus-impl</artifactId>
46+
<version>${opencensus.version}</version>
47+
<scope>runtime</scope>
48+
</dependency>
49+
<dependency>
50+
<groupId>io.opencensus</groupId>
51+
<artifactId>opencensus-contrib-zpages</artifactId>
52+
<version>${opencensus.version}</version>
53+
</dependency>
54+
<dependency>
55+
<groupId>io.opencensus</groupId>
56+
<artifactId>opencensus-exporter-trace-stackdriver</artifactId>
57+
<version>${opencensus.version}</version>
58+
<exclusions>
59+
<exclusion>
60+
<groupId>com.google.cloud</groupId>
61+
<artifactId>google-cloud-trace</artifactId>
62+
</exclusion>
63+
</exclusions>
64+
</dependency>
65+
<dependency>
66+
<groupId>io.opencensus</groupId>
67+
<artifactId>opencensus-exporter-stats-stackdriver</artifactId>
68+
<version>${opencensus.version}</version>
69+
<exclusions>
70+
<exclusion>
71+
<groupId>com.google.cloud</groupId>
72+
<artifactId>google-cloud-monitoring</artifactId>
73+
</exclusion>
74+
</exclusions>
75+
</dependency>
76+
<dependency>
77+
<groupId>io.opencensus</groupId>
78+
<artifactId>opencensus-contrib-grpc-metrics</artifactId>
79+
<version>${opencensus.version}</version>
80+
</dependency>
81+
<dependency>
82+
<groupId>com.google.cloud</groupId>
83+
<artifactId>google-cloud-trace</artifactId>
84+
<version>${trace.version}</version>
85+
</dependency>
86+
<dependency>
87+
<groupId>com.google.cloud</groupId>
88+
<artifactId>google-cloud-monitoring</artifactId>
89+
<version>${cloudmonitoring.version}</version>
90+
</dependency>
3491

3592
<dependency>
3693
<groupId>junit</groupId>
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
<maven.compiler.target>1.8</maven.compiler.target>
2222
<maven.compiler.source>1.8</maven.compiler.source>
2323
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
24+
<opencensus.version>0.26.0</opencensus.version>
2425
</properties>
2526

2627

@@ -43,7 +44,61 @@
4344
<artifactId>google-cloud-spanner</artifactId>
4445
</dependency>
4546
<!-- [END spanner_install_with_bom] -->
46-
47+
48+
<dependency>
49+
<groupId>io.opencensus</groupId>
50+
<artifactId>opencensus-api</artifactId>
51+
<version>${opencensus.version}</version>
52+
</dependency>
53+
<dependency>
54+
<groupId>io.opencensus</groupId>
55+
<artifactId>opencensus-impl</artifactId>
56+
<version>${opencensus.version}</version>
57+
<scope>runtime</scope>
58+
</dependency>
59+
<dependency>
60+
<groupId>io.opencensus</groupId>
61+
<artifactId>opencensus-contrib-zpages</artifactId>
62+
<version>${opencensus.version}</version>
63+
</dependency>
64+
<dependency>
65+
<groupId>io.opencensus</groupId>
66+
<artifactId>opencensus-exporter-trace-stackdriver</artifactId>
67+
<version>${opencensus.version}</version>
68+
<exclusions>
69+
<exclusion>
70+
<groupId>com.google.cloud</groupId>
71+
<artifactId>google-cloud-trace</artifactId>
72+
</exclusion>
73+
</exclusions>
74+
</dependency>
75+
<dependency>
76+
<groupId>io.opencensus</groupId>
77+
<artifactId>opencensus-exporter-stats-stackdriver</artifactId>
78+
<version>${opencensus.version}</version>
79+
<exclusions>
80+
<exclusion>
81+
<groupId>com.google.cloud</groupId>
82+
<artifactId>google-cloud-monitoring</artifactId>
83+
</exclusion>
84+
</exclusions>
85+
</dependency>
86+
<dependency>
87+
<groupId>io.opencensus</groupId>
88+
<artifactId>opencensus-contrib-grpc-metrics</artifactId>
89+
<version>${opencensus.version}</version>
90+
</dependency>
91+
<!-- Version auto-managed by BOM -->
92+
<dependency>
93+
<groupId>com.google.cloud</groupId>
94+
<artifactId>google-cloud-trace</artifactId>
95+
</dependency>
96+
<!-- Version auto-managed by BOM -->
97+
<dependency>
98+
<groupId>com.google.cloud</groupId>
99+
<artifactId>google-cloud-monitoring</artifactId>
100+
</dependency>
101+
47102
<dependency>
48103
<groupId>junit</groupId>
49104
<artifactId>junit</artifactId>
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,15 @@
1717
package com.example.spanner;
1818

1919
// [START spanner_async_query_to_list]
20+
2021
import com.google.api.core.ApiFuture;
2122
import com.google.cloud.spanner.AsyncResultSet;
2223
import com.google.cloud.spanner.DatabaseClient;
2324
import com.google.cloud.spanner.DatabaseId;
2425
import com.google.cloud.spanner.Spanner;
2526
import com.google.cloud.spanner.SpannerOptions;
2627
import com.google.cloud.spanner.Statement;
27-
import com.google.common.collect.ImmutableList;
28+
import java.util.List;
2829
import java.util.concurrent.ExecutionException;
2930
import java.util.concurrent.ExecutorService;
3031
import java.util.concurrent.Executors;
@@ -62,7 +63,7 @@ static void asyncQueryToList() throws InterruptedException, ExecutionException,
6263
static void asyncQueryToList(DatabaseClient client)
6364
throws InterruptedException, ExecutionException, TimeoutException {
6465
ExecutorService executor = Executors.newSingleThreadExecutor();
65-
ApiFuture<ImmutableList<Album>> albums;
66+
ApiFuture<List<Album>> albums;
6667
try (AsyncResultSet resultSet =
6768
client
6869
.singleUse()
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
package com.example.spanner;
1818

19-
//[START spanner_create_instance]
19+
// [START spanner_create_instance]
2020
import com.google.api.gax.longrunning.OperationFuture;
2121
import com.google.cloud.spanner.Instance;
2222
import com.google.cloud.spanner.InstanceAdminClient;
@@ -38,34 +38,36 @@ static void createInstance() {
3838
}
3939

4040
static void createInstance(String projectId, String instanceId) {
41-
Spanner spanner = SpannerOptions.newBuilder().setProjectId(projectId).build().getService();
42-
InstanceAdminClient instanceAdminClient = spanner.getInstanceAdminClient();
41+
try (Spanner spanner =
42+
SpannerOptions.newBuilder().setProjectId(projectId).build().getService()) {
43+
InstanceAdminClient instanceAdminClient = spanner.getInstanceAdminClient();
4344

44-
// Set Instance configuration.
45-
String configId = "regional-us-central1";
46-
int nodeCount = 2;
47-
String displayName = "Descriptive name";
45+
// Set Instance configuration.
46+
String configId = "regional-us-central1";
47+
int nodeCount = 2;
48+
String displayName = "Descriptive name";
4849

49-
// Create an InstanceInfo object that will be used to create the instance.
50-
InstanceInfo instanceInfo =
51-
InstanceInfo.newBuilder(InstanceId.of(projectId, instanceId))
52-
.setInstanceConfigId(InstanceConfigId.of(projectId, configId))
53-
.setNodeCount(nodeCount)
54-
.setDisplayName(displayName)
55-
.build();
56-
OperationFuture<Instance, CreateInstanceMetadata> operation =
57-
instanceAdminClient.createInstance(instanceInfo);
58-
try {
59-
// Wait for the createInstance operation to finish.
60-
Instance instance = operation.get();
61-
System.out.printf("Instance %s was successfully created%n", instance.getId());
62-
} catch (ExecutionException e) {
63-
System.out.printf(
64-
"Error: Creating instance %s failed with error message %s%n",
65-
instanceInfo.getId(), e.getMessage());
66-
} catch (InterruptedException e) {
67-
System.out.println("Error: Waiting for createInstance operation to finish was interrupted");
50+
// Create an InstanceInfo object that will be used to create the instance.
51+
InstanceInfo instanceInfo =
52+
InstanceInfo.newBuilder(InstanceId.of(projectId, instanceId))
53+
.setInstanceConfigId(InstanceConfigId.of(projectId, configId))
54+
.setNodeCount(nodeCount)
55+
.setDisplayName(displayName)
56+
.build();
57+
OperationFuture<Instance, CreateInstanceMetadata> operation =
58+
instanceAdminClient.createInstance(instanceInfo);
59+
try {
60+
// Wait for the createInstance operation to finish.
61+
Instance instance = operation.get();
62+
System.out.printf("Instance %s was successfully created%n", instance.getId());
63+
} catch (ExecutionException e) {
64+
System.out.printf(
65+
"Error: Creating instance %s failed with error message %s%n",
66+
instanceInfo.getId(), e.getMessage());
67+
} catch (InterruptedException e) {
68+
System.out.println("Error: Waiting for createInstance operation to finish was interrupted");
69+
}
6870
}
6971
}
7072
}
71-
//[END spanner_create_instance]
73+
// [END spanner_create_instance]
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,9 @@ static String createRestoredSampleDbId(DatabaseId database) {
177177
if (restoredDbId.length() > 30) {
178178
restoredDbId = restoredDbId.substring(0, 30);
179179
}
180+
if (restoredDbId.endsWith("-")) {
181+
restoredDbId = restoredDbId.substring(0, restoredDbId.length() - 1);
182+
}
180183
return restoredDbId;
181184
}
182185

0 commit comments

Comments
 (0)