diff --git a/exporters/metrics/ocagent/src/main/java/io/opencensus/exporter/metrics/ocagent/MetricsProtoUtils.java b/exporters/metrics/ocagent/src/main/java/io/opencensus/exporter/metrics/ocagent/MetricsProtoUtils.java index ecfba54243..a2b9aa929b 100644 --- a/exporters/metrics/ocagent/src/main/java/io/opencensus/exporter/metrics/ocagent/MetricsProtoUtils.java +++ b/exporters/metrics/ocagent/src/main/java/io/opencensus/exporter/metrics/ocagent/MetricsProtoUtils.java @@ -37,15 +37,18 @@ /** Utilities for converting Metrics APIs in OpenCensus Java to OpenCensus Metrics Proto. */ final class MetricsProtoUtils { - // TODO(songya): determine if we should make the optimization on not sending already-existed - // MetricDescriptors. static Metric toMetricProto( io.opencensus.metrics.export.Metric metric, - @Nullable io.opencensus.resource.Resource resource) { + @Nullable io.opencensus.resource.Resource resource, + boolean alreadySent) { Metric.Builder builder = Metric.newBuilder(); - builder.setMetricDescriptor(toMetricDescriptorProto(metric.getMetricDescriptor())); + if (alreadySent) { + builder.setName(metric.getMetricDescriptor().getName()); + } else { + builder.setMetricDescriptor(toMetricDescriptorProto(metric.getMetricDescriptor())); + } for (io.opencensus.metrics.export.TimeSeries timeSeries : metric.getTimeSeriesList()) { - builder.addTimeseries(toTimeSeriesProto(timeSeries)); + builder.addTimeseries(toTimeSeriesProto(timeSeries, alreadySent)); } if (resource != null) { builder.setResource(toResourceProto(resource)); @@ -104,7 +107,8 @@ private static Resource toResourceProto(io.opencensus.resource.Resource resource return builder.build(); } - private static TimeSeries toTimeSeriesProto(io.opencensus.metrics.export.TimeSeries timeSeries) { + private static TimeSeries toTimeSeriesProto( + io.opencensus.metrics.export.TimeSeries timeSeries, boolean alreadySent) { TimeSeries.Builder builder = TimeSeries.newBuilder(); if (timeSeries.getStartTimestamp() != null) { builder.setStartTimestamp(toTimestampProto(timeSeries.getStartTimestamp())); @@ -113,7 +117,7 @@ private static TimeSeries toTimeSeriesProto(io.opencensus.metrics.export.TimeSer builder.addLabelValues(toLabelValueProto(labelValue)); } for (io.opencensus.metrics.export.Point point : timeSeries.getPoints()) { - builder.addPoints(toPointProto(point)); + builder.addPoints(toPointProto(point, alreadySent)); } return builder.build(); } @@ -128,7 +132,8 @@ private static LabelValue toLabelValueProto(io.opencensus.metrics.LabelValue lab return builder.build(); } - private static Point toPointProto(io.opencensus.metrics.export.Point point) { + private static Point toPointProto( + io.opencensus.metrics.export.Point point, final boolean alreadySent) { final Point.Builder builder = Point.newBuilder(); builder.setTimestamp(toTimestampProto(point.getTimestamp())); point @@ -151,7 +156,7 @@ public Void apply(Long arg) { new Function() { @Override public Void apply(Distribution arg) { - builder.setDistributionValue(toDistributionProto(arg)); + builder.setDistributionValue(toDistributionProto(arg, alreadySent)); return null; } }, @@ -167,13 +172,13 @@ public Void apply(Summary arg) { } private static DistributionValue toDistributionProto( - io.opencensus.metrics.export.Distribution distribution) { + io.opencensus.metrics.export.Distribution distribution, boolean alreadySent) { DistributionValue.Builder builder = DistributionValue.newBuilder(); builder .setSum(distribution.getSum()) .setCount(distribution.getCount()) .setSumOfSquaredDeviation(distribution.getSumOfSquaredDeviations()); - if (distribution.getBucketOptions() != null) { + if (!alreadySent && distribution.getBucketOptions() != null) { builder.setBucketOptions(toBucketOptionsProto(distribution.getBucketOptions())); } for (io.opencensus.metrics.export.Distribution.Bucket bucket : distribution.getBuckets()) { @@ -182,8 +187,6 @@ private static DistributionValue toDistributionProto( return builder.build(); } - // TODO(songya): determine if we should make the optimization on not sending already-existed - // BucketOptions. private static DistributionValue.BucketOptions toBucketOptionsProto( Distribution.BucketOptions bucketOptions) { final DistributionValue.BucketOptions.Builder builder = diff --git a/exporters/metrics/ocagent/src/test/java/io/opencensus/exporter/metrics/ocagent/MetricsProtoUtilsTests.java b/exporters/metrics/ocagent/src/test/java/io/opencensus/exporter/metrics/ocagent/MetricsProtoUtilsTests.java index cbf0121526..c74820bb8b 100644 --- a/exporters/metrics/ocagent/src/test/java/io/opencensus/exporter/metrics/ocagent/MetricsProtoUtilsTests.java +++ b/exporters/metrics/ocagent/src/test/java/io/opencensus/exporter/metrics/ocagent/MetricsProtoUtilsTests.java @@ -109,7 +109,7 @@ public class MetricsProtoUtilsTests { Resource.create("env", Collections.singletonMap("env_key", "env_val")); @Test - public void toMetricProto_Distribution() { + public void toMetricProto_Distribution_NotSent() { Metric metric = Metric.create(DESCRIPTOR_1, Collections.singletonList(TIME_SERIES_1)); io.opencensus.proto.metrics.v1.Metric expected = io.opencensus.proto.metrics.v1.Metric.newBuilder() @@ -178,12 +178,61 @@ public void toMetricProto_Distribution() { .build()) .build(); io.opencensus.proto.metrics.v1.Metric actual = - MetricsProtoUtils.toMetricProto(metric, RESOURCE); + MetricsProtoUtils.toMetricProto(metric, RESOURCE, false); assertThat(actual).isEqualTo(expected); } @Test - public void toMetricProto_Summary() { + public void toMetricProto_Distribution_AlreadySent() { + Metric metric = Metric.create(DESCRIPTOR_1, Collections.singletonList(TIME_SERIES_1)); + io.opencensus.proto.metrics.v1.Metric expected = + io.opencensus.proto.metrics.v1.Metric.newBuilder() + .setName(METRIC_NAME_1) // Send metric name instead of whole metric descriptor. + .addTimeseries( + io.opencensus.proto.metrics.v1.TimeSeries.newBuilder() + .setStartTimestamp(MetricsProtoUtils.toTimestampProto(TIMESTAMP_4)) + .addLabelValues( + io.opencensus.proto.metrics.v1.LabelValue.newBuilder() + .setHasValue(true) + .setValue(VALUE_1.getValue()) + .build()) + .addPoints( + io.opencensus.proto.metrics.v1.Point.newBuilder() + .setTimestamp(MetricsProtoUtils.toTimestampProto(TIMESTAMP_2)) + .setDistributionValue( + DistributionValue.newBuilder() + .setCount(5) + .setSum(24.0) + .setSumOfSquaredDeviation(321.5) + // No need to send BucketOptions again. + .addBuckets( + DistributionValue.Bucket.newBuilder().setCount(2).build()) + .addBuckets( + DistributionValue.Bucket.newBuilder().setCount(1).build()) + .addBuckets( + DistributionValue.Bucket.newBuilder() + .setCount(2) + .setExemplar( + DistributionValue.Exemplar.newBuilder() + .setTimestamp( + MetricsProtoUtils.toTimestampProto( + TIMESTAMP_1)) + .setValue(11) + .build()) + .build()) + .addBuckets( + DistributionValue.Bucket.newBuilder().setCount(0).build()) + .build()) + .build()) + .build()) + .build(); + io.opencensus.proto.metrics.v1.Metric actual = + MetricsProtoUtils.toMetricProto(metric, null, true); + assertThat(actual).isEqualTo(expected); + } + + @Test + public void toMetricProto_Summary_NotSent() { Metric metric = Metric.create(DESCRIPTOR_2, Collections.singletonList(TIME_SERIES_2)); io.opencensus.proto.metrics.v1.Metric expected = io.opencensus.proto.metrics.v1.Metric.newBuilder() @@ -237,7 +286,8 @@ public void toMetricProto_Summary() { .build()) .build()) .build(); - io.opencensus.proto.metrics.v1.Metric actual = MetricsProtoUtils.toMetricProto(metric, null); + io.opencensus.proto.metrics.v1.Metric actual = + MetricsProtoUtils.toMetricProto(metric, null, false); assertThat(actual).isEqualTo(expected); } }