Skip to content
This repository has been archived by the owner on Dec 23, 2023. It is now read-only.

Exporter/Metrics/OcAgent: Optimization for already-sent metrics. #1637

Closed
Show file tree
Hide file tree
Changes from all commits
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 @@ -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));
Expand Down Expand Up @@ -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()));
Expand All @@ -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();
}
Expand All @@ -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
Expand All @@ -151,7 +156,7 @@ public Void apply(Long arg) {
new Function<Distribution, Void>() {
@Override
public Void apply(Distribution arg) {
builder.setDistributionValue(toDistributionProto(arg));
builder.setDistributionValue(toDistributionProto(arg, alreadySent));
return null;
}
},
Expand All @@ -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()) {
Expand All @@ -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 =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public class MetricsProtoUtilsTests {
Resource.create("env", Collections.<String, String>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()
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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);
}
}