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
Prev Previous commit
Next Next commit
use supplier correctly
mutianf committed Dec 13, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
commit fe25319ea01a5e566d4e182bee3e3cca76e48edd
Original file line number Diff line number Diff line change
@@ -160,12 +160,15 @@ public static BigtableCloudMonitoringExporter create(

@VisibleForTesting
BigtableCloudMonitoringExporter(
MetricServiceClient client, @Nullable MonitoredResource applicationResource, String taskId) {
MetricServiceClient client,
@Nullable Supplier<MonitoredResource> applicationResource,
String taskId) {
this.client = client;
this.taskId = taskId;
if (applicationResource != null) {
this.applicationResource = Suppliers.ofInstance(applicationResource);
}
this.applicationResource =
applicationResource != null
? applicationResource
: Suppliers.memoize(BigtableExporterUtils::detectResourceSafe);
}

@Override
@@ -250,14 +253,6 @@ public void onSuccess(List<Empty> emptyList) {
/** Export metrics associated with the resource the Application is running on. */
private CompletableResultCode exportApplicationResourceMetrics(
Collection<MetricData> collection) {
// applicationResource will only not be null when this class is initialized by test
if (applicationResource == null) {
// 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 = Suppliers.memoize(BigtableExporterUtils::detectResourceSafe);
}

if (applicationResource.get() == null) {
return CompletableResultCode.ofSuccess();
}
Original file line number Diff line number Diff line change
@@ -155,6 +155,7 @@ static List<TimeSeries> convertToApplicationResourceTimeSeries(
return allTimeSeries;
}

@Nullable
static MonitoredResource detectResourceSafe() {
try {
return detectResource();
@@ -168,7 +169,7 @@ static MonitoredResource detectResourceSafe() {
}

@Nullable
static MonitoredResource detectResource() {
private static MonitoredResource detectResource() {
GCPPlatformDetector detector = GCPPlatformDetector.DEFAULT_INSTANCE;
DetectedPlatform detectedPlatform = detector.detectPlatform();
MonitoredResource monitoredResource = null;
Original file line number Diff line number Diff line change
@@ -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;
@@ -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<CreateTimeSeriesRequest> argumentCaptor =
ArgumentCaptor.forClass(CreateTimeSeriesRequest.class);