diff --git a/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/stub/metrics/BigtableCloudMonitoringExporter.java b/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/stub/metrics/BigtableCloudMonitoringExporter.java index 28dc981730..a829c3f719 100644 --- a/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/stub/metrics/BigtableCloudMonitoringExporter.java +++ b/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/stub/metrics/BigtableCloudMonitoringExporter.java @@ -40,6 +40,8 @@ import com.google.cloud.monitoring.v3.MetricServiceClient; import com.google.cloud.monitoring.v3.MetricServiceSettings; import com.google.common.annotations.VisibleForTesting; +import com.google.common.base.Supplier; +import com.google.common.base.Suppliers; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableSet; import com.google.common.collect.Iterables; @@ -96,8 +98,9 @@ public final class BigtableCloudMonitoringExporter implements MetricExporter { private final String taskId; - // The resource the client application is running on - private final MonitoredResource applicationResource; + // Application resource is initialized on the first export, which runs on a background thread + // to avoid slowness when starting the client. + private final Supplier applicationResource; private final AtomicBoolean isShutdown = new AtomicBoolean(false); @@ -148,28 +151,15 @@ public static BigtableCloudMonitoringExporter create( // it as not retried for now. settingsBuilder.createServiceTimeSeriesSettings().setSimpleTimeoutNoRetriesDuration(timeout); - // Detect the resource that the client application is running on. For example, - // this could be a GCE instance or a GKE pod. Currently, we only support GCE instance and - // GKE pod. This method will return null for everything else. - MonitoredResource applicationResource = null; - try { - applicationResource = BigtableExporterUtils.detectResource(); - } catch (Exception e) { - logger.log( - Level.WARNING, - "Failed to detect resource, will skip exporting application level metrics ", - e); - } - return new BigtableCloudMonitoringExporter( MetricServiceClient.create(settingsBuilder.build()), - applicationResource, + Suppliers.memoize(BigtableExporterUtils::detectResourceSafe), BigtableExporterUtils.getDefaultTaskValue()); } @VisibleForTesting BigtableCloudMonitoringExporter( - MetricServiceClient client, @Nullable MonitoredResource applicationResource, String taskId) { + MetricServiceClient client, Supplier applicationResource, String taskId) { this.client = client; this.taskId = taskId; this.applicationResource = applicationResource; @@ -257,7 +247,7 @@ public void onSuccess(List emptyList) { /** Export metrics associated with the resource the Application is running on. */ private CompletableResultCode exportApplicationResourceMetrics( Collection collection) { - if (applicationResource == null) { + if (applicationResource.get() == null) { return CompletableResultCode.ofSuccess(); } @@ -276,7 +266,7 @@ private CompletableResultCode exportApplicationResourceMetrics( try { timeSeries = BigtableExporterUtils.convertToApplicationResourceTimeSeries( - metricData, taskId, applicationResource); + metricData, taskId, applicationResource.get()); } catch (Throwable e) { logger.log( Level.WARNING, @@ -291,7 +281,8 @@ private CompletableResultCode exportApplicationResourceMetrics( CompletableResultCode exportCode = new CompletableResultCode(); try { ProjectName projectName = - ProjectName.of(applicationResource.getLabelsOrThrow(APPLICATION_RESOURCE_PROJECT_ID)); + ProjectName.of( + applicationResource.get().getLabelsOrThrow(APPLICATION_RESOURCE_PROJECT_ID)); gceOrGkeFuture = exportTimeSeries(projectName, timeSeries); diff --git a/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/stub/metrics/BigtableExporterUtils.java b/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/stub/metrics/BigtableExporterUtils.java index 821c2295e0..95df887f0d 100644 --- a/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/stub/metrics/BigtableExporterUtils.java +++ b/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/stub/metrics/BigtableExporterUtils.java @@ -156,7 +156,20 @@ static List convertToApplicationResourceTimeSeries( } @Nullable - static MonitoredResource detectResource() { + static MonitoredResource detectResourceSafe() { + try { + return detectResource(); + } catch (Exception e) { + logger.log( + Level.WARNING, + "Failed to detect resource, will skip exporting application level metrics ", + e); + return null; + } + } + + @Nullable + private static MonitoredResource detectResource() { GCPPlatformDetector detector = GCPPlatformDetector.DEFAULT_INSTANCE; DetectedPlatform detectedPlatform = detector.detectPlatform(); MonitoredResource monitoredResource = null; diff --git a/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/data/v2/stub/metrics/BigtableCloudMonitoringExporterTest.java b/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/data/v2/stub/metrics/BigtableCloudMonitoringExporterTest.java index 657db7d8ae..e471b19a20 100644 --- a/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/data/v2/stub/metrics/BigtableCloudMonitoringExporterTest.java +++ b/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/data/v2/stub/metrics/BigtableCloudMonitoringExporterTest.java @@ -37,6 +37,7 @@ import com.google.api.gax.rpc.UnaryCallable; import com.google.cloud.monitoring.v3.MetricServiceClient; import com.google.cloud.monitoring.v3.stub.MetricServiceStub; +import com.google.common.base.Suppliers; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; import com.google.monitoring.v3.CreateTimeSeriesRequest; @@ -95,7 +96,7 @@ public void setUp() { exporter = new BigtableCloudMonitoringExporter( - fakeMetricServiceClient, /* applicationResource= */ null, taskId); + fakeMetricServiceClient, /* applicationResource= */ Suppliers.ofInstance(null), taskId); attributes = Attributes.builder() @@ -308,11 +309,12 @@ public void testTimeSeriesForMetricWithGceOrGkeResource() { BigtableCloudMonitoringExporter exporter = new BigtableCloudMonitoringExporter( fakeMetricServiceClient, - MonitoredResource.newBuilder() - .setType("gce-instance") - .putLabels("some-gce-key", "some-gce-value") - .putLabels("project_id", gceProjectId) - .build(), + Suppliers.ofInstance( + MonitoredResource.newBuilder() + .setType("gce-instance") + .putLabels("some-gce-key", "some-gce-value") + .putLabels("project_id", gceProjectId) + .build()), taskId); ArgumentCaptor argumentCaptor = ArgumentCaptor.forClass(CreateTimeSeriesRequest.class);