Skip to content

Commit a662a77

Browse files
authored
Exporter/Stats/Stackdriver: Add 0 count for underflow/first bucket (census-instrumentation#1525)
* Add 0 count for underflow/first bucket * Fix comments
1 parent a254d07 commit a662a77

2 files changed

Lines changed: 50 additions & 3 deletions

File tree

exporters/stats/stackdriver/src/main/java/io/opencensus/exporter/stats/stackdriver/StackdriverExportUtils.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,13 @@ public TypedValue apply(Summary arg) {
124124
@Override
125125
public BucketOptions apply(ExplicitOptions arg) {
126126
BucketOptions.Builder builder = BucketOptions.newBuilder();
127-
builder.setExplicitBuckets(Explicit.newBuilder().addAllBounds(arg.getBucketBoundaries()));
127+
Explicit.Builder explicitBuilder = Explicit.newBuilder();
128+
// The first bucket bound should be 0.0 because the Metrics first bucket is
129+
// [0, first_bound) but Stackdriver monitoring bucket bounds begin with -infinity
130+
// (first bucket is (-infinity, 0))
131+
explicitBuilder.addBounds(0.0);
132+
explicitBuilder.addAllBounds(arg.getBucketBoundaries());
133+
builder.setExplicitBuckets(explicitBuilder.build());
128134
return builder.build();
129135
}
130136
};
@@ -333,6 +339,9 @@ static BucketOptions createBucketOptions(
333339
// Convert a OpenCensus Buckets to a list of counts
334340
private static List<Long> createBucketCounts(List<Bucket> buckets) {
335341
List<Long> bucketCounts = new ArrayList<>();
342+
// The first bucket (underflow bucket) should always be 0 count because the Metrics first bucket
343+
// is [0, first_bound) but StackDriver distribution consists of an underflow bucket (number 0).
344+
bucketCounts.add(0L);
336345
for (Bucket bucket : buckets) {
337346
bucketCounts.add(bucket.getCount());
338347
}

exporters/stats/stackdriver/src/test/java/io/opencensus/exporter/stats/stackdriver/StackdriverExportUtilsTest.java

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,18 @@ public class StackdriverExportUtilsTest {
119119
private static final Value DISTRIBUTION_VALUE = Value.distributionValue(DISTRIBUTION);
120120
private static final Value SUMMARY_VALUE = Value.summaryValue(SUMMARY);
121121

122+
private static final io.opencensus.metrics.export.MetricDescriptor HISTOGRAM_METRIC_DESCRIPTOR =
123+
io.opencensus.metrics.export.MetricDescriptor.create(
124+
METRIC_NAME, METRIC_DESCRIPTION, METRIC_UNIT, Type.CUMULATIVE_DISTRIBUTION, LABEL_KEY);
125+
126+
private static final Point DISTRIBUTION_POINT = Point.create(DISTRIBUTION_VALUE, TIMESTAMP);
127+
private static final io.opencensus.metrics.export.TimeSeries DISTRIBUTION_TIME_SERIES =
128+
io.opencensus.metrics.export.TimeSeries.createWithOnePoint(
129+
LABEL_VALUE, DISTRIBUTION_POINT, null);
130+
private static final io.opencensus.metrics.export.Metric DISTRIBUTION_METRIC =
131+
io.opencensus.metrics.export.Metric.createWithOneTimeSeries(
132+
HISTOGRAM_METRIC_DESCRIPTOR, DISTRIBUTION_TIME_SERIES);
133+
122134
@Test
123135
public void createLabelDescriptor() {
124136
assertThat(StackdriverExportUtils.createLabelDescriptor(LabelKey.create("key", "desc")))
@@ -216,7 +228,7 @@ public void createBucketOptions() {
216228
.isEqualTo(
217229
BucketOptions.newBuilder()
218230
.setExplicitBuckets(
219-
Explicit.newBuilder().addAllBounds(Arrays.asList(1.0, 3.0, 5.0)))
231+
Explicit.newBuilder().addAllBounds(Arrays.asList(0.0, 1.0, 3.0, 5.0)))
220232
.build());
221233
}
222234

@@ -234,7 +246,7 @@ public void createDistribution() {
234246
.setCount(3)
235247
.setMean(0.6666666666666666)
236248
.setBucketOptions(StackdriverExportUtils.createBucketOptions(BUCKET_OPTIONS))
237-
.addAllBucketCounts(Arrays.asList(3L, 1L, 2L, 4L))
249+
.addAllBucketCounts(Arrays.asList(0L, 3L, 1L, 2L, 4L))
238250
.setSumOfSquaredDeviation(14)
239251
.build());
240252
}
@@ -363,6 +375,32 @@ public void createTimeSeriesList_Cumulative() {
363375
assertThat(timeSeriesList).containsExactly(expectedTimeSeries);
364376
}
365377

378+
@Test
379+
public void createTimeSeriesList_Distribution() {
380+
List<TimeSeries> timeSeriesList =
381+
StackdriverExportUtils.createTimeSeriesList(
382+
DISTRIBUTION_METRIC, DEFAULT_RESOURCE, CUSTOM_OPENCENSUS_DOMAIN);
383+
384+
assertThat(timeSeriesList.size()).isEqualTo(1);
385+
TimeSeries timeSeries = timeSeriesList.get(0);
386+
assertThat(timeSeries.getPointsCount()).isEqualTo(1);
387+
assertThat(timeSeries.getPoints(0).getValue().getDistributionValue())
388+
.isEqualTo(
389+
com.google.api.Distribution.newBuilder()
390+
.setCount(3)
391+
.setMean(0.6666666666666666)
392+
.setBucketOptions(
393+
BucketOptions.newBuilder()
394+
.setExplicitBuckets(
395+
Explicit.newBuilder()
396+
.addAllBounds(Arrays.asList(0.0, 1.0, 3.0, 5.0))
397+
.build())
398+
.build())
399+
.addAllBucketCounts(Arrays.asList(0L, 3L, 1L, 2L, 4L))
400+
.setSumOfSquaredDeviation(14)
401+
.build());
402+
}
403+
366404
@Test
367405
public void createTimeSeriesList_Gauge() {
368406
io.opencensus.metrics.export.Metric metric =

0 commit comments

Comments
 (0)