Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: move resource detection to the first export to avoid slow start #2450

Merged
merged 6 commits into from
Dec 16, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -98,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;
private MonitoredResource applicationResource;

private final AtomicBoolean initializedAppResource = new AtomicBoolean(false);
mutianf marked this conversation as resolved.
Show resolved Hide resolved
private final AtomicBoolean isShutdown = new AtomicBoolean(false);

private CompletableResultCode lastExportCode;
Expand Down Expand Up @@ -149,22 +150,9 @@ public static BigtableCloudMonitoringExporter create(
// it as not retried for now.
settingsBuilder.createServiceTimeSeriesSettings().setSimpleTimeoutNoRetries(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,
null,
BigtableExporterUtils.getDefaultTaskValue());
}

Expand All @@ -173,7 +161,11 @@ public static BigtableCloudMonitoringExporter create(
MetricServiceClient client, @Nullable MonitoredResource applicationResource, String taskId) {
this.client = client;
this.taskId = taskId;
this.applicationResource = applicationResource;
if (applicationResource != null) {
// for test to set a fake resource
initializedAppResource.set(true);
this.applicationResource = applicationResource;
}
}

@Override
Expand Down Expand Up @@ -258,6 +250,23 @@ public void onSuccess(List<Empty> emptyList) {
/** Export metrics associated with the resource the Application is running on. */
private CompletableResultCode exportApplicationResourceMetrics(
Collection<MetricData> collection) {
// Initialize the application resource on the first export which runs on a background thread
// to avoid slowness when starting the client.
if (initializedAppResource.compareAndSet(false, true)) {
// 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.
applicationResource = null;
try {
applicationResource = BigtableExporterUtils.detectResource();
} catch (Exception e) {
logger.log(
Level.WARNING,
"Failed to detect resource, will skip exporting application level metrics ",
e);
}
}

if (applicationResource == null) {
return CompletableResultCode.ofSuccess();
}
Expand Down
Loading