Skip to content

Commit b9e5dc0

Browse files
committed
fix(datastore): filter out non-datastore metrics in Cloud Monitoring exporter
1 parent cd36523 commit b9e5dc0

3 files changed

Lines changed: 37 additions & 0 deletions

File tree

java-datastore/google-cloud-datastore/src/main/java/com/google/cloud/datastore/telemetry/DatastoreCloudMonitoringExporter.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,10 @@ public CompletableResultCode export(@Nonnull Collection<MetricData> collection)
235235
return CompletableResultCode.ofFailure();
236236
}
237237

238+
if (datastoreTimeSeries.isEmpty()) {
239+
return CompletableResultCode.ofSuccess();
240+
}
241+
238242
ProjectName projectName = ProjectName.of(projectId);
239243

240244
// Perform the actual network call to Cloud Monitoring.

java-datastore/google-cloud-datastore/src/main/java/com/google/cloud/datastore/telemetry/DatastoreCloudMonitoringExporterUtils.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,9 @@ static List<TimeSeries> convertToDatastoreTimeSeries(
8484

8585
// Metrics should already been filtered for Gax and Datastore related ones
8686
for (MetricData metricData : collection) {
87+
if (!metricData.getName().startsWith(TelemetryConstants.METRIC_PREFIX)) {
88+
continue;
89+
}
8790
// TODO(b/405457573): The monitored resource is currently written to `global` because the
8891
// Firestore namespace in Cloud Monitoring has not been deployed yet. Once the namespace
8992
// is available, database_id and location labels should be added here using

java-datastore/google-cloud-datastore/src/test/java/com/google/cloud/datastore/telemetry/DatastoreCloudMonitoringExporterTest.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
import com.google.monitoring.v3.TimeSeries;
4343
import com.google.protobuf.Empty;
4444
import io.opentelemetry.api.common.Attributes;
45+
import io.opentelemetry.sdk.common.CompletableResultCode;
4546
import io.opentelemetry.sdk.common.InstrumentationScopeInfo;
4647
import io.opentelemetry.sdk.metrics.data.AggregationTemporality;
4748
import io.opentelemetry.sdk.metrics.data.LongPointData;
@@ -190,6 +191,35 @@ public void testClientCacheReferenceCounting() {
190191
verify(mockClient);
191192
}
192193

194+
@Test
195+
public void testExportingIgnoredMetrics() {
196+
// No expectations on mockMetricServiceStub means we expect NO calls to it.
197+
replay(mockMetricServiceStub);
198+
199+
long fakeValue = 11L;
200+
long startEpoch = 10;
201+
long endEpoch = 15;
202+
LongPointData longPointData =
203+
ImmutableLongPointData.create(startEpoch, endEpoch, attributes, fakeValue);
204+
205+
String ignoredMetricName = "otel.sdk.metric_reader.collection.duration";
206+
MetricData ignoredData =
207+
ImmutableMetricData.createLongSum(
208+
resource,
209+
scope,
210+
ignoredMetricName,
211+
"description",
212+
"1",
213+
ImmutableSumData.create(
214+
true, AggregationTemporality.CUMULATIVE, ImmutableList.of(longPointData)));
215+
216+
CompletableResultCode result = exporter.export(Collections.singletonList(ignoredData));
217+
218+
assertThat(result.isSuccess()).isTrue();
219+
220+
verify(mockMetricServiceStub);
221+
}
222+
193223
private static class FakeMetricServiceClient extends MetricServiceClient {
194224
protected FakeMetricServiceClient(MetricServiceStub stub) {
195225
super(stub);

0 commit comments

Comments
 (0)