Skip to content

Commit c039a83

Browse files
test: add integration test for builtin metrics (#1360)
* test: add integration test for builtin metrics * add license * test on staging * udpate * address comments * remove debugging * fix dependency test * update comment * update integration test to only close client if it's not null * explain why we're including grpc-xds * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
1 parent 1ac859b commit c039a83

File tree

6 files changed

+185
-4
lines changed

6 files changed

+185
-4
lines changed

google-cloud-bigtable-bom/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
<groupId>com.google.cloud</groupId>
1010
<artifactId>google-cloud-shared-config</artifactId>
1111
<version>1.5.1</version>
12+
<relativePath/>
1213
</parent>
1314

1415
<name>Google Cloud Bigtable BOM</name>

google-cloud-bigtable-deps-bom/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
<groupId>com.google.cloud</groupId>
99
<artifactId>google-cloud-shared-config</artifactId>
1010
<version>1.5.1</version>
11+
<relativePath/>
1112
</parent>
1213

1314
<groupId>com.google.cloud</groupId>

google-cloud-bigtable-stats/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@
5959
<dependency>
6060
<groupId>com.google.cloud</groupId>
6161
<artifactId>google-cloud-monitoring</artifactId>
62-
<!-- Excluding http dependencies since they're not used and are causing conflict in google-cloud-bigtable -->
62+
<!-- Excluding unused transitive dependencies to avoid conflict in google-cloud-bigtable -->
6363
<exclusions>
6464
<exclusion>
6565
<groupId>com.google.http-client</groupId>

google-cloud-bigtable/pom.xml

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,17 +54,25 @@
5454
<type>pom</type>
5555
<scope>import</scope>
5656
</dependency>
57+
<dependency>
58+
<groupId>com.google.cloud</groupId>
59+
<artifactId>google-cloud-monitoring-bom</artifactId>
60+
<version>3.4.1</version> <!-- needs to be in sync with google-cloud-bigtable-stats -->
61+
<type>pom</type>
62+
<scope>import</scope>
63+
</dependency>
5764
</dependencies>
5865
</dependencyManagement>
5966

6067
<dependencies>
6168
<!-- NOTE: Dependencies are organized into two groups, production and test.
6269
Within a group, dependencies are sorted by (groupId, artifactId) -->
63-
6470
<dependency>
6571
<groupId>com.google.cloud</groupId>
6672
<artifactId>google-cloud-bigtable-stats</artifactId>
67-
<!-- Exclude all the shaded transitive dependencies -->
73+
<!-- Exclude all dependencies that have been shaded. This is to workaround maven's immutable project structure:
74+
after shading, the maven-shade-plugin tries to remove shaded dependencies, but it can't since the project
75+
structure is immutable. So we have to manually exclude the shaded transitive dependencies manually. -->
6876
<exclusions>
6977
<exclusion>
7078
<groupId>io.opencensus</groupId>
@@ -254,6 +262,24 @@
254262
</exclusion>
255263
</exclusions>
256264
</dependency>
265+
<!-- cloud-monitoring client depends on grpc-xds, which has transitive dependency on opencensus-proto. We need to
266+
use cloud-monitoring as a test dependency, which also marks the opencensus-proto as a test dependency. Promote
267+
grpc-xds to a runtime dependency so that opencensus-proto will still be a runtime dependency in the flattened pom. -->
268+
<dependency>
269+
<groupId>io.grpc</groupId>
270+
<artifactId>grpc-xds</artifactId>
271+
<scope>runtime</scope>
272+
</dependency>
273+
<dependency>
274+
<groupId>com.google.cloud</groupId>
275+
<artifactId>google-cloud-monitoring</artifactId>
276+
<scope>test</scope>
277+
</dependency>
278+
<dependency>
279+
<groupId>com.google.api.grpc</groupId>
280+
<artifactId>proto-google-cloud-monitoring-v3</artifactId>
281+
<scope>test</scope>
282+
</dependency>
257283
<dependency>
258284
<groupId>com.google.truth</groupId>
259285
<artifactId>truth</artifactId>
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
/*
2+
* Copyright 2022 Google LLC
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+
* https://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+
package com.google.cloud.bigtable.data.v2.it;
17+
18+
import static com.google.common.truth.Truth.assertThat;
19+
import static com.google.common.truth.TruthJUnit.assume;
20+
21+
import com.google.api.client.util.Lists;
22+
import com.google.cloud.bigtable.data.v2.BigtableDataSettings;
23+
import com.google.cloud.bigtable.data.v2.models.Query;
24+
import com.google.cloud.bigtable.data.v2.models.Row;
25+
import com.google.cloud.bigtable.data.v2.models.RowMutation;
26+
import com.google.cloud.bigtable.test_helpers.env.EmulatorEnv;
27+
import com.google.cloud.bigtable.test_helpers.env.TestEnvRule;
28+
import com.google.cloud.monitoring.v3.MetricServiceClient;
29+
import com.google.monitoring.v3.ListTimeSeriesRequest;
30+
import com.google.monitoring.v3.ListTimeSeriesResponse;
31+
import com.google.monitoring.v3.ProjectName;
32+
import com.google.monitoring.v3.TimeInterval;
33+
import com.google.protobuf.util.Timestamps;
34+
import java.io.IOException;
35+
import java.time.Duration;
36+
import java.util.ArrayList;
37+
import org.junit.AfterClass;
38+
import org.junit.BeforeClass;
39+
import org.junit.ClassRule;
40+
import org.junit.Test;
41+
import org.junit.runner.RunWith;
42+
import org.junit.runners.JUnit4;
43+
44+
@RunWith(JUnit4.class)
45+
public class BuiltinMetricsIT {
46+
@ClassRule public static TestEnvRule testEnvRule = new TestEnvRule();
47+
public static MetricServiceClient metricClient;
48+
49+
public static String[] VIEWS = {
50+
"operation_latencies",
51+
"retry_count",
52+
"attempt_latencies",
53+
"server_latencies",
54+
"connectivity_error_count",
55+
"application_latencies"
56+
};
57+
58+
@BeforeClass
59+
public static void setUpClass() throws IOException {
60+
assume()
61+
.withMessage("Builtin metrics integration test is not supported by emulator")
62+
.that(testEnvRule.env())
63+
.isNotInstanceOf(EmulatorEnv.class);
64+
65+
// Enable built in metrics
66+
BigtableDataSettings.enableBuiltinMetrics();
67+
68+
// Create a cloud monitoring client
69+
metricClient = MetricServiceClient.create();
70+
}
71+
72+
@AfterClass
73+
public static void tearDown() {
74+
if (metricClient != null) {
75+
metricClient.close();
76+
}
77+
}
78+
79+
@Test
80+
public void testBuiltinMetrics() throws Exception {
81+
// Send a MutateRow and ReadRows request
82+
testEnvRule
83+
.env()
84+
.getDataClient()
85+
.mutateRow(
86+
RowMutation.create(testEnvRule.env().getTableId(), "a-new-key")
87+
.setCell(testEnvRule.env().getFamilyId(), "q", "abc"));
88+
ArrayList<Row> rows =
89+
Lists.newArrayList(
90+
testEnvRule
91+
.env()
92+
.getDataClient()
93+
.readRows(Query.create(testEnvRule.env().getTableId()).limit(10)));
94+
95+
// Sleep 5 minutes so the metrics could be published and precomputation is done
96+
Thread.sleep(Duration.ofMinutes(5).toMillis());
97+
98+
ProjectName name = ProjectName.of(testEnvRule.env().getProjectId());
99+
100+
// Restrict time to last 10 minutes
101+
long startMillis = System.currentTimeMillis() - Duration.ofMinutes(10).toMillis();
102+
TimeInterval interval =
103+
TimeInterval.newBuilder()
104+
.setStartTime(Timestamps.fromMillis(startMillis))
105+
.setEndTime(Timestamps.fromMillis(System.currentTimeMillis()))
106+
.build();
107+
108+
for (String view : VIEWS) {
109+
// Filter on instance and method name
110+
// Verify that metrics are published for MutateRow request
111+
String metricFilter =
112+
String.format(
113+
"metric.type=\"bigtable.googleapis.com/client/%s\" "
114+
+ "AND resource.labels.instance=\"%s\" AND metric.labels.method=\"Bigtable.MutateRow\"",
115+
view, testEnvRule.env().getInstanceId());
116+
ListTimeSeriesRequest.Builder requestBuilder =
117+
ListTimeSeriesRequest.newBuilder()
118+
.setName(name.toString())
119+
.setFilter(metricFilter)
120+
.setInterval(interval)
121+
.setView(ListTimeSeriesRequest.TimeSeriesView.FULL);
122+
ListTimeSeriesResponse response =
123+
metricClient.listTimeSeriesCallable().call(requestBuilder.build());
124+
assertThat(response.getTimeSeriesCount()).isGreaterThan(0);
125+
126+
// Verify that metrics are published for ReadRows request
127+
metricFilter =
128+
String.format(
129+
"metric.type=\"bigtable.googleapis.com/client/operation_latencies\" "
130+
+ "AND resource.labels.instance=\"%s\" AND metric.labels.method=\"Bigtable.ReadRows\"",
131+
testEnvRule.env().getInstanceId());
132+
requestBuilder.setFilter(metricFilter);
133+
response = metricClient.listTimeSeriesCallable().call(requestBuilder.build());
134+
assertThat(response.getTimeSeriesCount()).isGreaterThan(0);
135+
}
136+
}
137+
}

pom.xml

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
<groupId>com.google.cloud</groupId>
1616
<artifactId>google-cloud-shared-config</artifactId>
1717
<version>1.5.3</version>
18+
<relativePath/>
1819
</parent>
1920

2021
<developers>
@@ -339,6 +340,22 @@
339340
</plugins>
340341
</build>
341342
</profile>
343+
344+
<profile>
345+
<!-- Shading workaround for IDEs: This project relocates some of its internal dependencies. Since IDEs can't
346+
resolve the relocated symbols on their own, a workaround is added: first mvn install the shaded module,
347+
then disable the with-shaded profile to force IDEs to resolve the relocated dependencies from the local
348+
repository -->
349+
<id>with-shaded</id>
350+
<activation>
351+
<property>
352+
<name>!skip-shaded</name>
353+
</property>
354+
</activation>
355+
<modules>
356+
<module>google-cloud-bigtable-stats</module>
357+
</modules>
358+
</profile>
342359
</profiles>
343360

344361
<modules>
@@ -347,7 +364,6 @@
347364
<module>grpc-google-cloud-bigtable-v2</module>
348365
<module>proto-google-cloud-bigtable-admin-v2</module>
349366
<module>proto-google-cloud-bigtable-v2</module>
350-
<module>google-cloud-bigtable-stats</module>
351367
<module>google-cloud-bigtable-emulator-core</module>
352368
<module>google-cloud-bigtable-emulator</module>
353369
<module>google-cloud-bigtable-bom</module>

0 commit comments

Comments
 (0)