|
| 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 | +} |
0 commit comments