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

Commit 17470b3

Browse files
authored
Add SumDataDouble and SumDataLong (#647)
* Add SumDataDouble and SumDataLong
1 parent e153ff0 commit 17470b3

6 files changed

Lines changed: 229 additions & 92 deletions

File tree

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

Lines changed: 75 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,10 @@
3030
* {@link AggregationData} is the result of applying a given {@link Aggregation} to a set of
3131
* {@code MeasureValue}s.
3232
*
33-
* <p>{@link AggregationData} currently supports 4 types of basic aggregation values:
33+
* <p>{@link AggregationData} currently supports 5 types of basic aggregation values:
3434
* <ul>
35-
* <li>SumData
35+
* <li>SumDataDouble
36+
* <li>SumDataLong
3637
* <li>CountData
3738
* <li>MeanData
3839
* <li>DistributionData
@@ -51,28 +52,29 @@ private AggregationData() {
5152
* Applies the given match function to the underlying data type.
5253
*/
5354
public abstract <T> T match(
54-
Function<? super SumData, T> p0,
55-
Function<? super CountData, T> p1,
56-
Function<? super MeanData, T> p2,
57-
Function<? super DistributionData, T> p3,
55+
Function<? super SumDataDouble, T> p0,
56+
Function<? super SumDataLong, T> p1,
57+
Function<? super CountData, T> p2,
58+
Function<? super MeanData, T> p3,
59+
Function<? super DistributionData, T> p4,
5860
Function<? super AggregationData, T> defaultFunction);
5961

60-
/** The sum value of aggregated {@code MeasureValue}s. */
62+
/** The sum value of aggregated {@code MeasureValueDouble}s. */
6163
@Immutable
6264
@AutoValue
63-
public abstract static class SumData extends AggregationData {
65+
public abstract static class SumDataDouble extends AggregationData {
6466

65-
SumData() {
67+
SumDataDouble() {
6668
}
6769

6870
/**
69-
* Creates a {@code SumData}.
71+
* Creates a {@code SumDataDouble}.
7072
*
7173
* @param sum the aggregated sum.
72-
* @return a {@code SumData}.
74+
* @return a {@code SumDataDouble}.
7375
*/
74-
public static SumData create(double sum) {
75-
return new AutoValue_AggregationData_SumData(sum);
76+
public static SumDataDouble create(double sum) {
77+
return new AutoValue_AggregationData_SumDataDouble(sum);
7678
}
7779

7880
/**
@@ -84,15 +86,53 @@ public static SumData create(double sum) {
8486

8587
@Override
8688
public final <T> T match(
87-
Function<? super SumData, T> p0,
88-
Function<? super CountData, T> p1,
89-
Function<? super MeanData, T> p2,
90-
Function<? super DistributionData, T> p3,
89+
Function<? super SumDataDouble, T> p0,
90+
Function<? super SumDataLong, T> p1,
91+
Function<? super CountData, T> p2,
92+
Function<? super MeanData, T> p3,
93+
Function<? super DistributionData, T> p4,
9194
Function<? super AggregationData, T> defaultFunction) {
9295
return p0.apply(this);
9396
}
9497
}
9598

99+
/** The sum value of aggregated {@code MeasureValueLong}s. */
100+
@Immutable
101+
@AutoValue
102+
public abstract static class SumDataLong extends AggregationData {
103+
104+
SumDataLong() {
105+
}
106+
107+
/**
108+
* Creates a {@code SumDataLong}.
109+
*
110+
* @param sum the aggregated sum.
111+
* @return a {@code SumDataLong}.
112+
*/
113+
public static SumDataLong create(long sum) {
114+
return new AutoValue_AggregationData_SumDataLong(sum);
115+
}
116+
117+
/**
118+
* Returns the aggregated sum.
119+
*
120+
* @return the aggregated sum.
121+
*/
122+
public abstract long getSum();
123+
124+
@Override
125+
public final <T> T match(
126+
Function<? super SumDataDouble, T> p0,
127+
Function<? super SumDataLong, T> p1,
128+
Function<? super CountData, T> p2,
129+
Function<? super MeanData, T> p3,
130+
Function<? super DistributionData, T> p4,
131+
Function<? super AggregationData, T> defaultFunction) {
132+
return p1.apply(this);
133+
}
134+
}
135+
96136
/** The count value of aggregated {@code MeasureValue}s. */
97137
@Immutable
98138
@AutoValue
@@ -120,12 +160,13 @@ public static CountData create(long count) {
120160

121161
@Override
122162
public final <T> T match(
123-
Function<? super SumData, T> p0,
124-
Function<? super CountData, T> p1,
125-
Function<? super MeanData, T> p2,
126-
Function<? super DistributionData, T> p3,
163+
Function<? super SumDataDouble, T> p0,
164+
Function<? super SumDataLong, T> p1,
165+
Function<? super CountData, T> p2,
166+
Function<? super MeanData, T> p3,
167+
Function<? super DistributionData, T> p4,
127168
Function<? super AggregationData, T> defaultFunction) {
128-
return p1.apply(this);
169+
return p2.apply(this);
129170
}
130171
}
131172

@@ -164,12 +205,13 @@ public static MeanData create(double mean, long count) {
164205

165206
@Override
166207
public final <T> T match(
167-
Function<? super SumData, T> p0,
168-
Function<? super CountData, T> p1,
169-
Function<? super MeanData, T> p2,
170-
Function<? super DistributionData, T> p3,
208+
Function<? super SumDataDouble, T> p0,
209+
Function<? super SumDataLong, T> p1,
210+
Function<? super CountData, T> p2,
211+
Function<? super MeanData, T> p3,
212+
Function<? super DistributionData, T> p4,
171213
Function<? super AggregationData, T> defaultFunction) {
172-
return p2.apply(this);
214+
return p3.apply(this);
173215
}
174216
}
175217

@@ -258,12 +300,13 @@ public static DistributionData create(
258300

259301
@Override
260302
public final <T> T match(
261-
Function<? super SumData, T> p0,
262-
Function<? super CountData, T> p1,
263-
Function<? super MeanData, T> p2,
264-
Function<? super DistributionData, T> p3,
303+
Function<? super SumDataDouble, T> p0,
304+
Function<? super SumDataLong, T> p1,
305+
Function<? super CountData, T> p2,
306+
Function<? super MeanData, T> p3,
307+
Function<? super DistributionData, T> p4,
265308
Function<? super AggregationData, T> defaultFunction) {
266-
return p3.apply(this);
309+
return p4.apply(this);
267310
}
268311
}
269312
}

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

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@
2424
import io.opencensus.stats.AggregationData.CountData;
2525
import io.opencensus.stats.AggregationData.DistributionData;
2626
import io.opencensus.stats.AggregationData.MeanData;
27-
import io.opencensus.stats.AggregationData.SumData;
27+
import io.opencensus.stats.AggregationData.SumDataDouble;
28+
import io.opencensus.stats.AggregationData.SumDataLong;
2829
import java.util.ArrayList;
2930
import java.util.Arrays;
3031
import java.util.List;
@@ -73,11 +74,14 @@ public void preventMinIsGreaterThanMax() {
7374
public void testEquals() {
7475
new EqualsTester()
7576
.addEqualityGroup(
76-
SumData.create(10.0),
77-
SumData.create(10.0))
77+
SumDataDouble.create(10.0),
78+
SumDataDouble.create(10.0))
7879
.addEqualityGroup(
79-
SumData.create(20.0),
80-
SumData.create(20.0))
80+
SumDataDouble.create(20.0),
81+
SumDataDouble.create(20.0))
82+
.addEqualityGroup(
83+
SumDataLong.create(20),
84+
SumDataLong.create(20))
8185
.addEqualityGroup(
8286
CountData.create(40),
8387
CountData.create(40))
@@ -111,17 +115,25 @@ public void testEquals() {
111115
@Test
112116
public void testMatchAndGet() {
113117
List<AggregationData> aggregations = Arrays.asList(
114-
SumData.create(10.0),
118+
SumDataDouble.create(10.0),
119+
SumDataLong.create(100000000),
115120
CountData.create(40),
116121
MeanData.create(5.0, 1),
117122
DistributionData.create(1, 1, 1, 1, 0, new long[]{0, 10, 0}));
118123

119124
final List<Object> actual = new ArrayList<Object>();
120125
for (AggregationData aggregation : aggregations) {
121126
aggregation.match(
122-
new Function<SumData, Void>() {
127+
new Function<SumDataDouble, Void>() {
128+
@Override
129+
public Void apply(SumDataDouble arg) {
130+
actual.add(arg.getSum());
131+
return null;
132+
}
133+
},
134+
new Function<SumDataLong, Void>() {
123135
@Override
124-
public Void apply(SumData arg) {
136+
public Void apply(SumDataLong arg) {
125137
actual.add(arg.getSum());
126138
return null;
127139
}
@@ -151,6 +163,6 @@ public Void apply(DistributionData arg) {
151163
}
152164

153165
assertThat(actual).isEqualTo(
154-
Arrays.asList(10.0, 40L, 5.0, Arrays.asList(0L, 10L, 0L)));
166+
Arrays.asList(10.0, 100000000L, 40L, 5.0, Arrays.asList(0L, 10L, 0L)));
155167
}
156168
}

core_impl/src/main/java/io/opencensus/implcore/stats/MutableViewData.java

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,9 @@
4141
import io.opencensus.stats.AggregationData.CountData;
4242
import io.opencensus.stats.AggregationData.DistributionData;
4343
import io.opencensus.stats.AggregationData.MeanData;
44-
import io.opencensus.stats.AggregationData.SumData;
44+
import io.opencensus.stats.AggregationData.SumDataDouble;
45+
import io.opencensus.stats.AggregationData.SumDataLong;
46+
import io.opencensus.stats.Measure;
4547
import io.opencensus.stats.View;
4648
import io.opencensus.stats.View.AggregationWindow.Cumulative;
4749
import io.opencensus.stats.View.AggregationWindow.Interval;
@@ -202,12 +204,13 @@ static MutableAggregation createMutableAggregation(Aggregation aggregation) {
202204
* Create an {@link AggregationData} snapshot based on the given {@link MutableAggregation}.
203205
*
204206
* @param aggregation {@code MutableAggregation}
207+
* @param measure {@code Measure}
205208
* @return an {@code AggregationData} which is the snapshot of current summary statistics.
206209
*/
207210
@VisibleForTesting
208-
static AggregationData createAggregationData(MutableAggregation aggregation) {
211+
static AggregationData createAggregationData(MutableAggregation aggregation, Measure measure) {
209212
return aggregation.match(
210-
CreateSumData.INSTANCE,
213+
new CreateSumData(measure),
211214
CreateCountData.INSTANCE,
212215
CreateMeanData.INSTANCE,
213216
CreateDistributionData.INSTANCE);
@@ -216,11 +219,11 @@ static AggregationData createAggregationData(MutableAggregation aggregation) {
216219
// Covert a mapping from TagValues to MutableAggregation, to a mapping from TagValues to
217220
// AggregationData.
218221
private static <T> Map<T, AggregationData> createAggregationMap(
219-
Map<T, MutableAggregation> tagValueAggregationMap) {
222+
Map<T, MutableAggregation> tagValueAggregationMap, Measure measure) {
220223
Map<T, AggregationData> map = Maps.newHashMap();
221224
for (Entry<T, MutableAggregation> entry :
222225
tagValueAggregationMap.entrySet()) {
223-
map.put(entry.getKey(), createAggregationData(entry.getValue()));
226+
map.put(entry.getKey(), createAggregationData(entry.getValue(), measure));
224227
}
225228
return map;
226229
}
@@ -249,7 +252,7 @@ void record(TagContext context, double value, Timestamp timestamp) {
249252
@Override
250253
ViewData toViewData(Clock clock) {
251254
return ViewData.create(
252-
super.view, createAggregationMap(tagValueAggregationMap),
255+
super.view, createAggregationMap(tagValueAggregationMap, super.view.getMeasure()),
253256
CumulativeData.create(start, clock.now()));
254257
}
255258
}
@@ -379,7 +382,7 @@ private Map<List<TagValue>, AggregationData> combineBucketsAndGetAggregationMap(
379382
putBucketsIntoMultiMap(shallowCopy, multimap, aggregation, now);
380383
Map<List<TagValue>, MutableAggregation> singleMap =
381384
aggregateOnEachTagValueList(multimap, aggregation);
382-
return createAggregationMap(singleMap);
385+
return createAggregationMap(singleMap, super.getView().getMeasure());
383386
}
384387

385388
// Put stats within each bucket to a multimap. Each tag value list (map key) could have multiple
@@ -491,12 +494,20 @@ public MutableAggregation apply(Distribution arg) {
491494
}
492495

493496
private static final class CreateSumData implements Function<MutableSum, AggregationData> {
494-
@Override
495-
public AggregationData apply(MutableSum arg) {
496-
return SumData.create(arg.getSum());
497+
498+
private final Measure measure;
499+
500+
private CreateSumData(Measure measure) {
501+
this.measure = measure;
497502
}
498503

499-
private static final CreateSumData INSTANCE = new CreateSumData();
504+
@Override
505+
public AggregationData apply(final MutableSum arg) {
506+
return measure.match(
507+
Functions.<AggregationData>returnConstant(SumDataDouble.create(arg.getSum())),
508+
Functions.<AggregationData>returnConstant(SumDataLong.create(Math.round(arg.getSum()))),
509+
Functions.<AggregationData>throwAssertionError());
510+
}
500511
}
501512

502513
private static final class CreateCountData implements Function<MutableCount, AggregationData> {

core_impl/src/test/java/io/opencensus/implcore/stats/MutableViewDataTest.java

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,11 @@
3333
import io.opencensus.stats.AggregationData.CountData;
3434
import io.opencensus.stats.AggregationData.DistributionData;
3535
import io.opencensus.stats.AggregationData.MeanData;
36-
import io.opencensus.stats.AggregationData.SumData;
36+
import io.opencensus.stats.AggregationData.SumDataDouble;
37+
import io.opencensus.stats.AggregationData.SumDataLong;
3738
import io.opencensus.stats.BucketBoundaries;
39+
import io.opencensus.stats.Measure.MeasureDouble;
40+
import io.opencensus.stats.Measure.MeasureLong;
3841
import io.opencensus.tags.TagKey.TagKeyString;
3942
import io.opencensus.tags.TagValue.TagValueString;
4043
import java.util.ArrayList;
@@ -56,6 +59,10 @@ public class MutableViewDataTest {
5659
private static final TagKeyString METHOD = TagKeyString.create("method");
5760
private static final TagValueString CALLER_V = TagValueString.create("some caller");
5861
private static final TagValueString METHOD_V = TagValueString.create("some method");
62+
private static final MeasureDouble MEASURE_DOUBLE =
63+
MeasureDouble.create("measure1", "description", "1");
64+
private static final MeasureLong MEASURE_LONG =
65+
MeasureLong.create("measure2", "description", "1");
5966

6067
@Test
6168
public void testConstants() {
@@ -100,16 +107,20 @@ public void createMutableAggregation() {
100107
public void createAggregationData() {
101108
BucketBoundaries bucketBoundaries = BucketBoundaries.create(Arrays.asList(-1.0, 0.0, 1.0));
102109
List<MutableAggregation> mutableAggregations = Arrays.asList(
103-
MutableSum.create(),
104110
MutableCount.create(),
105111
MutableMean.create(),
106112
MutableDistribution.create(bucketBoundaries));
107113
List<AggregationData> aggregates = new ArrayList<AggregationData>();
114+
115+
aggregates.add(MutableViewData.createAggregationData(MutableSum.create(), MEASURE_DOUBLE));
116+
aggregates.add(MutableViewData.createAggregationData(MutableSum.create(), MEASURE_LONG));
108117
for (MutableAggregation mutableAggregation : mutableAggregations) {
109-
aggregates.add(MutableViewData.createAggregationData(mutableAggregation));
118+
aggregates.add(MutableViewData.createAggregationData(mutableAggregation, MEASURE_DOUBLE));
110119
}
120+
111121
assertThat(aggregates).containsExactly(
112-
SumData.create(0),
122+
SumDataDouble.create(0),
123+
SumDataLong.create(0),
113124
CountData.create(0),
114125
MeanData.create(0, 0),
115126
DistributionData.create(

0 commit comments

Comments
 (0)