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

Commit ffd2176

Browse files
Bogdan Drutusongy23
authored andcommitted
Remove min/max from Distribution. (#1542)
* Remove min/max from Distribution. * Add changelog entry.
1 parent dbcc97b commit ffd2176

File tree

8 files changed

+89
-110
lines changed

8 files changed

+89
-110
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
- The histogram bucket boundaries (`BucketBoundaries`) and values (`Count` and `Sum`) are no longer
1818
supported for negative values. The Record API drops the negative `value` and logs the warning.
1919
This could be a breaking change if you are recording negative value for any `measure`.
20+
- Remove support for min/max in the stats Distribution to make it compatible with Metrics.
2021

2122
## 0.16.1 - 2018-09-18
2223
- Fix ClassCastException in Log4j log correlation

api/src/main/java/io/opencensus/stats/AggregationData.java

Lines changed: 58 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,10 @@ public abstract static class DistributionData extends AggregationData {
275275
* @param exemplars the exemplars associated with histogram buckets.
276276
* @return a {@code DistributionData}.
277277
* @since 0.16
278+
* @deprecated since 0.17. Use {@link #create(double, long, double, List, List)}
278279
*/
280+
@Deprecated
281+
@SuppressWarnings("InconsistentOverloads")
279282
public static DistributionData create(
280283
double mean,
281284
long count,
@@ -284,26 +287,41 @@ public static DistributionData create(
284287
double sumOfSquaredDeviations,
285288
List<Long> bucketCounts,
286289
List<Exemplar> exemplars) {
287-
if (min != Double.POSITIVE_INFINITY || max != Double.NEGATIVE_INFINITY) {
288-
Utils.checkArgument(min <= max, "max should be greater or equal to min.");
289-
}
290+
return create(mean, count, sumOfSquaredDeviations, bucketCounts, exemplars);
291+
}
290292

291-
Utils.checkNotNull(bucketCounts, "bucketCounts");
292-
List<Long> bucketCountsCopy = Collections.unmodifiableList(new ArrayList<Long>(bucketCounts));
293-
for (Long bucket : bucketCountsCopy) {
294-
Utils.checkNotNull(bucket, "bucket");
293+
/**
294+
* Creates a {@code DistributionData}.
295+
*
296+
* @param mean mean value.
297+
* @param count count value.
298+
* @param sumOfSquaredDeviations sum of squared deviations.
299+
* @param bucketCounts histogram bucket counts.
300+
* @param exemplars the exemplars associated with histogram buckets.
301+
* @return a {@code DistributionData}.
302+
* @since 0.17
303+
*/
304+
public static DistributionData create(
305+
double mean,
306+
long count,
307+
double sumOfSquaredDeviations,
308+
List<Long> bucketCounts,
309+
List<Exemplar> exemplars) {
310+
List<Long> bucketCountsCopy =
311+
Collections.unmodifiableList(
312+
new ArrayList<Long>(Utils.checkNotNull(bucketCounts, "bucketCounts")));
313+
for (Long bucketCount : bucketCountsCopy) {
314+
Utils.checkNotNull(bucketCount, "bucketCount");
295315
}
296316

297-
Utils.checkNotNull(exemplars, "exemplar list should not be null.");
317+
Utils.checkNotNull(exemplars, "exemplars");
298318
for (Exemplar exemplar : exemplars) {
299319
Utils.checkNotNull(exemplar, "exemplar");
300320
}
301321

302322
return new AutoValue_AggregationData_DistributionData(
303323
mean,
304324
count,
305-
min,
306-
max,
307325
sumOfSquaredDeviations,
308326
bucketCountsCopy,
309327
Collections.<Exemplar>unmodifiableList(new ArrayList<Exemplar>(exemplars)));
@@ -320,7 +338,10 @@ public static DistributionData create(
320338
* @param bucketCounts histogram bucket counts.
321339
* @return a {@code DistributionData}.
322340
* @since 0.8
341+
* @deprecated since 0.17. Use {@link #create(double, long, double, List)}.
323342
*/
343+
@Deprecated
344+
@SuppressWarnings("InconsistentOverloads")
324345
public static DistributionData create(
325346
double mean,
326347
long count,
@@ -329,13 +350,23 @@ public static DistributionData create(
329350
double sumOfSquaredDeviations,
330351
List<Long> bucketCounts) {
331352
return create(
332-
mean,
333-
count,
334-
min,
335-
max,
336-
sumOfSquaredDeviations,
337-
bucketCounts,
338-
Collections.<Exemplar>emptyList());
353+
mean, count, sumOfSquaredDeviations, bucketCounts, Collections.<Exemplar>emptyList());
354+
}
355+
356+
/**
357+
* Creates a {@code DistributionData}.
358+
*
359+
* @param mean mean value.
360+
* @param count count value.
361+
* @param sumOfSquaredDeviations sum of squared deviations.
362+
* @param bucketCounts histogram bucket counts.
363+
* @return a {@code DistributionData}.
364+
* @since 0.17
365+
*/
366+
public static DistributionData create(
367+
double mean, long count, double sumOfSquaredDeviations, List<Long> bucketCounts) {
368+
return create(
369+
mean, count, sumOfSquaredDeviations, bucketCounts, Collections.<Exemplar>emptyList());
339370
}
340371

341372
/**
@@ -359,16 +390,24 @@ public static DistributionData create(
359390
*
360391
* @return the minimum of the population values.
361392
* @since 0.8
393+
* @deprecated since 0.17. Returns {@code 0}.
362394
*/
363-
public abstract double getMin();
395+
@Deprecated
396+
public double getMin() {
397+
return 0;
398+
}
364399

365400
/**
366401
* Returns the maximum of the population values.
367402
*
368403
* @return the maximum of the population values.
369404
* @since 0.8
405+
* @deprecated since 0.17. Returns {@code 0}.
370406
*/
371-
public abstract double getMax();
407+
@Deprecated
408+
public double getMax() {
409+
return 0;
410+
}
372411

373412
/**
374413
* Returns the aggregated sum of squared deviations.

api/src/test/java/io/opencensus/stats/AggregationDataTest.java

Lines changed: 17 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,9 @@ public class AggregationDataTest {
5555
@Test
5656
public void testCreateDistributionData() {
5757
DistributionData distributionData =
58-
DistributionData.create(7.7, 10, 1.1, 9.9, 32.2, Arrays.asList(4L, 1L, 5L));
58+
DistributionData.create(7.7, 10, 32.2, Arrays.asList(4L, 1L, 5L));
5959
assertThat(distributionData.getMean()).isWithin(TOLERANCE).of(7.7);
6060
assertThat(distributionData.getCount()).isEqualTo(10);
61-
assertThat(distributionData.getMin()).isWithin(TOLERANCE).of(1.1);
62-
assertThat(distributionData.getMax()).isWithin(TOLERANCE).of(9.9);
6361
assertThat(distributionData.getSumOfSquaredDeviations()).isWithin(TOLERANCE).of(32.2);
6462
assertThat(distributionData.getBucketCounts()).containsExactly(4L, 1L, 5L).inOrder();
6563
}
@@ -70,14 +68,14 @@ public void testCreateDistributionDataWithExemplar() {
7068
Exemplar exemplar2 = Exemplar.create(1, TIMESTAMP_1, ATTACHMENTS);
7169
DistributionData distributionData =
7270
DistributionData.create(
73-
7.7, 10, 1.1, 9.9, 32.2, Arrays.asList(4L, 1L), Arrays.asList(exemplar1, exemplar2));
71+
7.7, 10, 32.2, Arrays.asList(4L, 1L), Arrays.asList(exemplar1, exemplar2));
7472
assertThat(distributionData.getExemplars()).containsExactly(exemplar1, exemplar2).inOrder();
7573
}
7674

7775
@Test
7876
public void testExemplar() {
7977
Exemplar exemplar = Exemplar.create(15.0, TIMESTAMP_1, ATTACHMENTS);
80-
assertThat(exemplar.getValue()).isEqualTo(15.0);
78+
assertThat(exemplar.getValue()).isWithin(TOLERANCE).of(15.0);
8179
assertThat(exemplar.getTimestamp()).isEqualTo(TIMESTAMP_1);
8280
assertThat(exemplar.getAttachments()).isEqualTo(ATTACHMENTS);
8381
}
@@ -109,36 +107,29 @@ public void testExemplar_PreventNullAttachmentValue() {
109107
public void preventNullBucketCountList() {
110108
thrown.expect(NullPointerException.class);
111109
thrown.expectMessage("bucketCounts");
112-
DistributionData.create(1, 1, 1, 1, 0, null);
110+
DistributionData.create(1, 1, 0, null);
113111
}
114112

115113
@Test
116114
public void preventNullBucket() {
117115
thrown.expect(NullPointerException.class);
118-
thrown.expectMessage("bucket");
119-
DistributionData.create(1, 1, 1, 1, 0, Arrays.asList(0L, 1L, null));
116+
thrown.expectMessage("bucketCount");
117+
DistributionData.create(1, 1, 0, Arrays.asList(0L, 1L, null));
120118
}
121119

122120
@Test
123121
public void preventNullExemplarList() {
124122
thrown.expect(NullPointerException.class);
125-
thrown.expectMessage("exemplar list should not be null.");
126-
DistributionData.create(1, 1, 1, 1, 0, Arrays.asList(0L, 1L, 1L), null);
123+
thrown.expectMessage("exemplars");
124+
DistributionData.create(1, 1, 0, Arrays.asList(0L, 1L, 1L), null);
127125
}
128126

129127
@Test
130128
public void preventNullExemplar() {
131129
thrown.expect(NullPointerException.class);
132130
thrown.expectMessage("exemplar");
133131
DistributionData.create(
134-
1, 1, 1, 1, 0, Arrays.asList(0L, 1L, 1L), Collections.<Exemplar>singletonList(null));
135-
}
136-
137-
@Test
138-
public void preventMinIsGreaterThanMax() {
139-
thrown.expect(IllegalArgumentException.class);
140-
thrown.expectMessage("max should be greater or equal to min.");
141-
DistributionData.create(1, 1, 10, 1, 0, Arrays.asList(0L, 1L, 0L));
132+
1, 1, 0, Arrays.asList(0L, 1L, 1L), Collections.<Exemplar>singletonList(null));
142133
}
143134

144135
@Test
@@ -150,14 +141,13 @@ public void testEquals() {
150141
.addEqualityGroup(CountData.create(40), CountData.create(40))
151142
.addEqualityGroup(CountData.create(80), CountData.create(80))
152143
.addEqualityGroup(
153-
DistributionData.create(10, 10, 1, 1, 0, Arrays.asList(0L, 10L, 0L)),
154-
DistributionData.create(10, 10, 1, 1, 0, Arrays.asList(0L, 10L, 0L)))
155-
.addEqualityGroup(DistributionData.create(10, 10, 1, 1, 0, Arrays.asList(0L, 10L, 100L)))
156-
.addEqualityGroup(DistributionData.create(110, 10, 1, 1, 0, Arrays.asList(0L, 10L, 0L)))
157-
.addEqualityGroup(DistributionData.create(10, 110, 1, 1, 0, Arrays.asList(0L, 10L, 0L)))
158-
.addEqualityGroup(DistributionData.create(10, 10, -1, 1, 0, Arrays.asList(0L, 10L, 0L)))
159-
.addEqualityGroup(DistributionData.create(10, 10, 1, 5, 0, Arrays.asList(0L, 10L, 0L)))
160-
.addEqualityGroup(DistributionData.create(10, 10, 1, 1, 55.5, Arrays.asList(0L, 10L, 0L)))
144+
DistributionData.create(10, 10, 0, Arrays.asList(0L, 10L, 0L)),
145+
DistributionData.create(10, 10, 0, Arrays.asList(0L, 10L, 0L)))
146+
.addEqualityGroup(DistributionData.create(10, 10, 0, Arrays.asList(0L, 10L, 100L)))
147+
.addEqualityGroup(DistributionData.create(110, 10, 0, Arrays.asList(0L, 10L, 0L)))
148+
.addEqualityGroup(DistributionData.create(10, 110, 0, Arrays.asList(0L, 10L, 0L)))
149+
.addEqualityGroup(DistributionData.create(10, 10, 10, Arrays.asList(0L, 10L, 0L)))
150+
.addEqualityGroup(DistributionData.create(10, 10, 0, Arrays.asList(0L, 110L, 0L)))
161151
.addEqualityGroup(MeanData.create(5.0, 1), MeanData.create(5.0, 1))
162152
.addEqualityGroup(MeanData.create(-5.0, 1), MeanData.create(-5.0, 1))
163153
.addEqualityGroup(LastValueDataDouble.create(20.0), LastValueDataDouble.create(20.0))
@@ -172,7 +162,7 @@ public void testMatchAndGet() {
172162
SumDataDouble.create(10.0),
173163
SumDataLong.create(100000000),
174164
CountData.create(40),
175-
DistributionData.create(1, 1, 1, 1, 0, Arrays.asList(0L, 10L, 0L)),
165+
DistributionData.create(1, 1, 0, Arrays.asList(0L, 10L, 0L)),
176166
LastValueDataDouble.create(20.0),
177167
LastValueDataLong.create(200000000L));
178168

api/src/test/java/io/opencensus/stats/ViewDataTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -285,9 +285,9 @@ private void aggregationAndAggregationDataMismatch(
285285
private static final ImmutableMap<List<TagValue>, DistributionData> ENTRIES =
286286
ImmutableMap.of(
287287
Arrays.asList(V1, V2),
288-
DistributionData.create(1, 1, 1, 1, 0, Arrays.asList(0L, 1L, 0L)),
288+
DistributionData.create(1, 1, 0, Arrays.asList(0L, 1L, 0L)),
289289
Arrays.asList(V10, V20),
290-
DistributionData.create(-5, 6, -20, 5, 100.1, Arrays.asList(5L, 0L, 1L)));
290+
DistributionData.create(-5, 6, 100.1, Arrays.asList(5L, 0L, 1L)));
291291

292292
// name
293293
private static final View.Name NAME = View.Name.create("test-view");

impl_core/src/main/java/io/opencensus/implcore/stats/MutableAggregation.java

Lines changed: 1 addition & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -258,10 +258,6 @@ static final class MutableDistribution extends MutableAggregation {
258258
private long count = 0;
259259
private double sumOfSquaredDeviations = 0.0;
260260

261-
// Initial "impossible" values, that will get reset as soon as first value is added.
262-
private double min = Double.POSITIVE_INFINITY;
263-
private double max = Double.NEGATIVE_INFINITY;
264-
265261
private final BucketBoundaries bucketBoundaries;
266262
private final long[] bucketCounts;
267263

@@ -308,13 +304,6 @@ void add(double value, Map<String, String> attachments, Timestamp timestamp) {
308304
double deltaFromMean2 = value - mean;
309305
sumOfSquaredDeviations += deltaFromMean * deltaFromMean2;
310306

311-
if (value < min) {
312-
min = value;
313-
}
314-
if (value > max) {
315-
max = value;
316-
}
317-
318307
int bucket = 0;
319308
for (; bucket < bucketBoundaries.getBoundaries().size(); bucket++) {
320309
if (value < bucketBoundaries.getBoundaries().get(bucket)) {
@@ -360,13 +349,6 @@ void combine(MutableAggregation other, double fraction) {
360349
this.sum += mutableDistribution.sum;
361350
this.mean = this.sum / this.count;
362351

363-
if (mutableDistribution.min < this.min) {
364-
this.min = mutableDistribution.min;
365-
}
366-
if (mutableDistribution.max > this.max) {
367-
this.max = mutableDistribution.max;
368-
}
369-
370352
long[] bucketCounts = mutableDistribution.getBucketCounts();
371353
for (int i = 0; i < bucketCounts.length; i++) {
372354
this.bucketCounts[i] += bucketCounts[i];
@@ -401,7 +383,7 @@ AggregationData toAggregationData() {
401383
}
402384
}
403385
return DistributionData.create(
404-
mean, count, min, max, sumOfSquaredDeviations, boxedBucketCounts, exemplarList);
386+
mean, count, sumOfSquaredDeviations, boxedBucketCounts, exemplarList);
405387
}
406388

407389
@Override
@@ -446,14 +428,6 @@ long getCount() {
446428
return count;
447429
}
448430

449-
double getMin() {
450-
return min;
451-
}
452-
453-
double getMax() {
454-
return max;
455-
}
456-
457431
// Returns the aggregated sum of squared deviations.
458432
double getSumOfSquaredDeviations() {
459433
return sumOfSquaredDeviations;

0 commit comments

Comments
 (0)