Visitar URL original
fix: move resource detection to the first export to avoid slow start · googleapis/java-bigtable@3ceab08 · GitHub
Skip to content
This repository was archived by the owner on Aug 30, 2026. It is now read-only.

Commit 3ceab08

Browse files
committed
fix: move resource detection to the first export to avoid slow start
1 parent f98dca1 commit 3ceab08

1 file changed

Lines changed: 25 additions & 16 deletions

File tree

‎google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/stub/metrics/BigtableCloudMonitoringExporter.java‎

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,9 @@ public final class BigtableCloudMonitoringExporter implements MetricExporter {
9898
private final String taskId;
9999

100100
// The resource the client application is running on
101-
private final MonitoredResource applicationResource;
101+
private MonitoredResource applicationResource;
102102

103+
private final AtomicBoolean initializedAppResource = new AtomicBoolean(false);
103104
private final AtomicBoolean isShutdown = new AtomicBoolean(false);
104105

105106
private CompletableResultCode lastExportCode;
@@ -149,22 +150,9 @@ public static BigtableCloudMonitoringExporter create(
149150
// it as not retried for now.
150151
settingsBuilder.createServiceTimeSeriesSettings().setSimpleTimeoutNoRetries(timeout);
151152

152-
// Detect the resource that the client application is running on. For example,
153-
// this could be a GCE instance or a GKE pod. Currently, we only support GCE instance and
154-
// GKE pod. This method will return null for everything else.
155-
MonitoredResource applicationResource = null;
156-
try {
157-
applicationResource = BigtableExporterUtils.detectResource();
158-
} catch (Exception e) {
159-
logger.log(
160-
Level.WARNING,
161-
"Failed to detect resource, will skip exporting application level metrics ",
162-
e);
163-
}
164-
165153
return new BigtableCloudMonitoringExporter(
166154
MetricServiceClient.create(settingsBuilder.build()),
167-
applicationResource,
155+
null,
168156
BigtableExporterUtils.getDefaultTaskValue());
169157
}
170158

@@ -173,7 +161,11 @@ public static BigtableCloudMonitoringExporter create(
173161
MetricServiceClient client, @Nullable MonitoredResource applicationResource, String taskId) {
174162
this.client = client;
175163
this.taskId = taskId;
176-
this.applicationResource = applicationResource;
164+
if (applicationResource != null) {
165+
// for test to set a fake resource
166+
initializedAppResource.set(true);
167+
this.applicationResource = applicationResource;
168+
}
177169
}
178170

179171
@Override
@@ -258,6 +250,23 @@ public void onSuccess(List<Empty> emptyList) {
258250
/** Export metrics associated with the resource the Application is running on. */
259251
private CompletableResultCode exportApplicationResourceMetrics(
260252
Collection<MetricData> collection) {
253+
// Initialize the application resource on the first export which runs on a background thread
254+
// to avoid slowness when starting the client.
255+
if (initializedAppResource.compareAndSet(false, true)) {
256+
// Detect the resource that the client application is running on. For example,
257+
// this could be a GCE instance or a GKE pod. Currently, we only support GCE instance and
258+
// GKE pod. This method will return null for everything else.
259+
applicationResource = null;
260+
try {
261+
applicationResource = BigtableExporterUtils.detectResource();
262+
} catch (Exception e) {
263+
logger.log(
264+
Level.WARNING,
265+
"Failed to detect resource, will skip exporting application level metrics ",
266+
e);
267+
}
268+
}
269+
261270
if (applicationResource == null) {
262271
return CompletableResultCode.ofSuccess();
263272
}

0 commit comments

Comments
 (0)