Visitar URL original
fix(datastore): filter out non-datastore metrics in Cloud Monitoring … · googleapis/google-cloud-java@a71d22a · GitHub
Skip to content

Commit a71d22a

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

3 files changed

Lines changed: 39 additions & 1 deletion

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: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,12 @@ static List<TimeSeries> convertToDatastoreTimeSeries(
8282
List<MetricData> collection, Map<String, String> clientAttributes) {
8383
List<TimeSeries> allTimeSeries = new ArrayList<>();
8484

85-
// Metrics should already been filtered for Gax and Datastore related ones
85+
// Only convert metrics that belong to Datastore/GAX (starting with METRIC_PREFIX)
86+
// to filter out OpenTelemetry internal diagnostic metrics.
8687
for (MetricData metricData : collection) {
88+
if (!metricData.getName().startsWith(TelemetryConstants.METRIC_PREFIX)) {
89+
continue;
90+
}
8791
// TODO(b/405457573): The monitored resource is currently written to `global` because the
8892
// Firestore namespace in Cloud Monitoring has not been deployed yet. Once the namespace
8993
// 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)