From c33e6db1bccb99273618dd4f01d36409d8e7f9ac Mon Sep 17 00:00:00 2001 From: Mattie Fu Date: Fri, 19 Aug 2022 12:49:51 -0400 Subject: [PATCH] udpate --- .../BigtableCreateTimeSeriesExporter.java | 7 +++- google-cloud-bigtable/pom.xml | 32 +++++++++++++++++-- .../bigtable/data/v2/it/BuiltinMetricsIT.java | 26 +++++++++------ 3 files changed, 52 insertions(+), 13 deletions(-) diff --git a/google-cloud-bigtable-stats/src/main/java/com/google/cloud/bigtable/stats/BigtableCreateTimeSeriesExporter.java b/google-cloud-bigtable-stats/src/main/java/com/google/cloud/bigtable/stats/BigtableCreateTimeSeriesExporter.java index ad2e76867c..5ae9162c13 100644 --- a/google-cloud-bigtable-stats/src/main/java/com/google/cloud/bigtable/stats/BigtableCreateTimeSeriesExporter.java +++ b/google-cloud-bigtable-stats/src/main/java/com/google/cloud/bigtable/stats/BigtableCreateTimeSeriesExporter.java @@ -16,6 +16,7 @@ package com.google.cloud.bigtable.stats; import com.google.api.MonitoredResource; +import com.google.api.gax.grpc.GrpcResponseMetadata; import com.google.cloud.monitoring.v3.MetricServiceClient; import com.google.monitoring.v3.CreateTimeSeriesRequest; import com.google.monitoring.v3.ProjectName; @@ -72,12 +73,16 @@ public void export(Collection metrics) { for (Map.Entry> entry : projectToTimeSeries.entrySet()) { ProjectName projectName = ProjectName.of(entry.getKey()); + GrpcResponseMetadata metadata = new GrpcResponseMetadata(); CreateTimeSeriesRequest request = CreateTimeSeriesRequest.newBuilder() .setName(projectName.toString()) .addAllTimeSeries(entry.getValue()) .build(); - this.metricServiceClient.createServiceTimeSeries(request); + this.metricServiceClient + .createTimeSeriesCallable() + .call(request, metadata.createContextWithHandlers()); + // this.metricServiceClient.createServiceTimeSeries(request); } } catch (Throwable e) { logger.log(Level.WARNING, "Exception thrown when exporting TimeSeries.", e); diff --git a/google-cloud-bigtable/pom.xml b/google-cloud-bigtable/pom.xml index 9e00c9a699..1be4f68144 100644 --- a/google-cloud-bigtable/pom.xml +++ b/google-cloud-bigtable/pom.xml @@ -54,6 +54,14 @@ pom import + + com.google.cloud + google-cloud-monitoring-bom + 3.4.1 + pom + import + + @@ -63,8 +71,9 @@ com.google.cloud google-cloud-bigtable-stats - + io.opencensus @@ -254,6 +263,16 @@ + + com.google.cloud + google-cloud-monitoring + test + + + com.google.api.grpc + proto-google-cloud-monitoring-v3 + test + com.google.truth truth @@ -645,7 +664,16 @@ io.grpc:grpc-auth,io.grpc:grpc-grpclb io.opencensus:opencensus-impl-core + + + + + + + + + diff --git a/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/data/v2/it/BuiltinMetricsIT.java b/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/data/v2/it/BuiltinMetricsIT.java index 58df1d131b..b4a21e3084 100644 --- a/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/data/v2/it/BuiltinMetricsIT.java +++ b/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/data/v2/it/BuiltinMetricsIT.java @@ -16,6 +16,7 @@ package com.google.cloud.bigtable.data.v2.it; import static com.google.common.truth.TruthJUnit.assume; +import static com.google.common.truth.Truth.assertThat; import com.google.api.client.util.Lists; import com.google.cloud.bigtable.data.v2.BigtableDataSettings; @@ -26,19 +27,24 @@ import com.google.cloud.bigtable.test_helpers.env.TestEnvRule; import com.google.cloud.monitoring.v3.MetricServiceClient; import com.google.cloud.monitoring.v3.MetricServiceSettings; -import com.google.common.truth.Truth; import com.google.monitoring.v3.ListTimeSeriesRequest; import com.google.monitoring.v3.ListTimeSeriesResponse; import com.google.monitoring.v3.ProjectName; import com.google.monitoring.v3.TimeInterval; import com.google.protobuf.util.Timestamps; import java.io.IOException; +import java.time.Duration; import java.util.ArrayList; +import java.util.concurrent.TimeUnit; + import org.junit.AfterClass; import org.junit.BeforeClass; import org.junit.ClassRule; import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; +@RunWith(JUnit4.class) public class BuiltinMetricsIT { @ClassRule public static TestEnvRule testEnvRule = new TestEnvRule(); public static MetricServiceClient metricClient; @@ -70,6 +76,11 @@ public static void setUpClass() throws IOException { metricClient = MetricServiceClient.create(settings); } + @AfterClass + public static void tearDown() { + metricClient.close(); + } + @Test public void testBuiltinMetrics() throws Exception { // Send a MutateRow and ReadRows request @@ -87,12 +98,12 @@ public void testBuiltinMetrics() throws Exception { .readRows(Query.create(testEnvRule.env().getTableId()).limit(10))); // Sleep 5 minutes so the metrics could be published and precomputation is done - Thread.sleep(60 * 5 * 1000); + Thread.sleep(Duration.ofMinutes(5).toMillis()); ProjectName name = ProjectName.of(testEnvRule.env().getProjectId()); // Restrict time to last 10 minutes - long startMillis = System.currentTimeMillis() - ((60 * 10) * 1000); + long startMillis = System.currentTimeMillis() - Duration.ofMinutes (10).toMillis(); TimeInterval interval = TimeInterval.newBuilder() .setStartTime(Timestamps.fromMillis(startMillis)) @@ -115,7 +126,7 @@ public void testBuiltinMetrics() throws Exception { .setView(ListTimeSeriesRequest.TimeSeriesView.FULL); ListTimeSeriesResponse response = metricClient.listTimeSeriesCallable().call(requestBuilder.build()); - Truth.assertThat(response.getTimeSeriesCount()).isGreaterThan(0); + assertThat(response.getTimeSeriesCount()).isGreaterThan(0); // Verify that metrics are published for ReadRows request metricFilter = @@ -125,12 +136,7 @@ public void testBuiltinMetrics() throws Exception { testEnvRule.env().getInstanceId()); requestBuilder.setFilter(metricFilter); response = metricClient.listTimeSeriesCallable().call(requestBuilder.build()); - Truth.assertThat(response.getTimeSeriesCount()).isGreaterThan(0); + assertThat(response.getTimeSeriesCount()).isGreaterThan(0); } } - - @AfterClass - public static void tearDown() { - metricClient.close(); - } }