• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2017, OpenCensus Authors
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package io.opencensus.stats;
18 
19 import com.google.auto.value.AutoValue;
20 import io.opencensus.common.Function;
21 import io.opencensus.internal.Utils;
22 import io.opencensus.metrics.data.Exemplar;
23 import java.util.ArrayList;
24 import java.util.Collections;
25 import java.util.List;
26 import javax.annotation.concurrent.Immutable;
27 
28 /**
29  * {@link AggregationData} is the result of applying a given {@link Aggregation} to a set of {@code
30  * MeasureValue}s.
31  *
32  * <p>{@link AggregationData} currently supports 6 types of basic aggregation values:
33  *
34  * <ul>
35  *   <li>SumDataDouble
36  *   <li>SumDataLong
37  *   <li>CountData
38  *   <li>DistributionData
39  *   <li>LastValueDataDouble
40  *   <li>LastValueDataLong
41  * </ul>
42  *
43  * <p>{@link ViewData} will contain one {@link AggregationData}, corresponding to its {@link
44  * Aggregation} definition in {@link View}.
45  *
46  * @since 0.8
47  */
48 @Immutable
49 public abstract class AggregationData {
50 
AggregationData()51   private AggregationData() {}
52 
53   /**
54    * Applies the given match function to the underlying data type.
55    *
56    * @since 0.13
57    */
match( Function<? super SumDataDouble, T> p0, Function<? super SumDataLong, T> p1, Function<? super CountData, T> p2, Function<? super DistributionData, T> p3, Function<? super LastValueDataDouble, T> p4, Function<? super LastValueDataLong, T> p5, Function<? super AggregationData, T> defaultFunction)58   public abstract <T> T match(
59       Function<? super SumDataDouble, T> p0,
60       Function<? super SumDataLong, T> p1,
61       Function<? super CountData, T> p2,
62       Function<? super DistributionData, T> p3,
63       Function<? super LastValueDataDouble, T> p4,
64       Function<? super LastValueDataLong, T> p5,
65       Function<? super AggregationData, T> defaultFunction);
66 
67   /**
68    * The sum value of aggregated {@code MeasureValueDouble}s.
69    *
70    * @since 0.8
71    */
72   @Immutable
73   @AutoValue
74   public abstract static class SumDataDouble extends AggregationData {
75 
SumDataDouble()76     SumDataDouble() {}
77 
78     /**
79      * Creates a {@code SumDataDouble}.
80      *
81      * @param sum the aggregated sum.
82      * @return a {@code SumDataDouble}.
83      * @since 0.8
84      */
create(double sum)85     public static SumDataDouble create(double sum) {
86       return new AutoValue_AggregationData_SumDataDouble(sum);
87     }
88 
89     /**
90      * Returns the aggregated sum.
91      *
92      * @return the aggregated sum.
93      * @since 0.8
94      */
getSum()95     public abstract double getSum();
96 
97     @Override
match( Function<? super SumDataDouble, T> p0, Function<? super SumDataLong, T> p1, Function<? super CountData, T> p2, Function<? super DistributionData, T> p3, Function<? super LastValueDataDouble, T> p4, Function<? super LastValueDataLong, T> p5, Function<? super AggregationData, T> defaultFunction)98     public final <T> T match(
99         Function<? super SumDataDouble, T> p0,
100         Function<? super SumDataLong, T> p1,
101         Function<? super CountData, T> p2,
102         Function<? super DistributionData, T> p3,
103         Function<? super LastValueDataDouble, T> p4,
104         Function<? super LastValueDataLong, T> p5,
105         Function<? super AggregationData, T> defaultFunction) {
106       return p0.apply(this);
107     }
108   }
109 
110   /**
111    * The sum value of aggregated {@code MeasureValueLong}s.
112    *
113    * @since 0.8
114    */
115   @Immutable
116   @AutoValue
117   public abstract static class SumDataLong extends AggregationData {
118 
SumDataLong()119     SumDataLong() {}
120 
121     /**
122      * Creates a {@code SumDataLong}.
123      *
124      * @param sum the aggregated sum.
125      * @return a {@code SumDataLong}.
126      * @since 0.8
127      */
create(long sum)128     public static SumDataLong create(long sum) {
129       return new AutoValue_AggregationData_SumDataLong(sum);
130     }
131 
132     /**
133      * Returns the aggregated sum.
134      *
135      * @return the aggregated sum.
136      * @since 0.8
137      */
getSum()138     public abstract long getSum();
139 
140     @Override
match( Function<? super SumDataDouble, T> p0, Function<? super SumDataLong, T> p1, Function<? super CountData, T> p2, Function<? super DistributionData, T> p3, Function<? super LastValueDataDouble, T> p4, Function<? super LastValueDataLong, T> p5, Function<? super AggregationData, T> defaultFunction)141     public final <T> T match(
142         Function<? super SumDataDouble, T> p0,
143         Function<? super SumDataLong, T> p1,
144         Function<? super CountData, T> p2,
145         Function<? super DistributionData, T> p3,
146         Function<? super LastValueDataDouble, T> p4,
147         Function<? super LastValueDataLong, T> p5,
148         Function<? super AggregationData, T> defaultFunction) {
149       return p1.apply(this);
150     }
151   }
152 
153   /**
154    * The count value of aggregated {@code MeasureValue}s.
155    *
156    * @since 0.8
157    */
158   @Immutable
159   @AutoValue
160   public abstract static class CountData extends AggregationData {
161 
CountData()162     CountData() {}
163 
164     /**
165      * Creates a {@code CountData}.
166      *
167      * @param count the aggregated count.
168      * @return a {@code CountData}.
169      * @since 0.8
170      */
create(long count)171     public static CountData create(long count) {
172       return new AutoValue_AggregationData_CountData(count);
173     }
174 
175     /**
176      * Returns the aggregated count.
177      *
178      * @return the aggregated count.
179      * @since 0.8
180      */
getCount()181     public abstract long getCount();
182 
183     @Override
match( Function<? super SumDataDouble, T> p0, Function<? super SumDataLong, T> p1, Function<? super CountData, T> p2, Function<? super DistributionData, T> p3, Function<? super LastValueDataDouble, T> p4, Function<? super LastValueDataLong, T> p5, Function<? super AggregationData, T> defaultFunction)184     public final <T> T match(
185         Function<? super SumDataDouble, T> p0,
186         Function<? super SumDataLong, T> p1,
187         Function<? super CountData, T> p2,
188         Function<? super DistributionData, T> p3,
189         Function<? super LastValueDataDouble, T> p4,
190         Function<? super LastValueDataLong, T> p5,
191         Function<? super AggregationData, T> defaultFunction) {
192       return p2.apply(this);
193     }
194   }
195 
196   /**
197    * The mean value of aggregated {@code MeasureValue}s.
198    *
199    * @since 0.8
200    * @deprecated since 0.13, use {@link DistributionData} instead.
201    */
202   @Immutable
203   @AutoValue
204   @Deprecated
205   @AutoValue.CopyAnnotations
206   public abstract static class MeanData extends AggregationData {
207 
MeanData()208     MeanData() {}
209 
210     /**
211      * Creates a {@code MeanData}.
212      *
213      * @param mean the aggregated mean.
214      * @param count the aggregated count.
215      * @return a {@code MeanData}.
216      * @since 0.8
217      */
create(double mean, long count)218     public static MeanData create(double mean, long count) {
219       return new AutoValue_AggregationData_MeanData(mean, count);
220     }
221 
222     /**
223      * Returns the aggregated mean.
224      *
225      * @return the aggregated mean.
226      * @since 0.8
227      */
getMean()228     public abstract double getMean();
229 
230     /**
231      * Returns the aggregated count.
232      *
233      * @return the aggregated count.
234      * @since 0.8
235      */
getCount()236     public abstract long getCount();
237 
238     @Override
match( Function<? super SumDataDouble, T> p0, Function<? super SumDataLong, T> p1, Function<? super CountData, T> p2, Function<? super DistributionData, T> p3, Function<? super LastValueDataDouble, T> p4, Function<? super LastValueDataLong, T> p5, Function<? super AggregationData, T> defaultFunction)239     public final <T> T match(
240         Function<? super SumDataDouble, T> p0,
241         Function<? super SumDataLong, T> p1,
242         Function<? super CountData, T> p2,
243         Function<? super DistributionData, T> p3,
244         Function<? super LastValueDataDouble, T> p4,
245         Function<? super LastValueDataLong, T> p5,
246         Function<? super AggregationData, T> defaultFunction) {
247       return defaultFunction.apply(this);
248     }
249   }
250 
251   /**
252    * The distribution stats of aggregated {@code MeasureValue}s. Distribution stats include mean,
253    * count, histogram, min, max and sum of squared deviations.
254    *
255    * @since 0.8
256    */
257   @Immutable
258   @AutoValue
259   public abstract static class DistributionData extends AggregationData {
260 
DistributionData()261     DistributionData() {}
262 
263     /**
264      * Creates a {@code DistributionData}.
265      *
266      * @param mean mean value.
267      * @param count count value.
268      * @param min min value.
269      * @param max max value.
270      * @param sumOfSquaredDeviations sum of squared deviations.
271      * @param bucketCounts histogram bucket counts.
272      * @param exemplars the exemplars associated with histogram buckets.
273      * @return a {@code DistributionData}.
274      * @since 0.16
275      * @deprecated since 0.17. Use {@link #create(double, long, double, List, List)}
276      */
277     @Deprecated
278     @SuppressWarnings("InconsistentOverloads")
create( double mean, long count, double min, double max, double sumOfSquaredDeviations, List<Long> bucketCounts, List<Exemplar> exemplars)279     public static DistributionData create(
280         double mean,
281         long count,
282         double min,
283         double max,
284         double sumOfSquaredDeviations,
285         List<Long> bucketCounts,
286         List<Exemplar> exemplars) {
287       return create(mean, count, sumOfSquaredDeviations, bucketCounts, exemplars);
288     }
289 
290     /**
291      * Creates a {@code DistributionData}.
292      *
293      * @param mean mean value.
294      * @param count count value.
295      * @param sumOfSquaredDeviations sum of squared deviations.
296      * @param bucketCounts histogram bucket counts.
297      * @param exemplars the exemplars associated with histogram buckets.
298      * @return a {@code DistributionData}.
299      * @since 0.17
300      */
create( double mean, long count, double sumOfSquaredDeviations, List<Long> bucketCounts, List<Exemplar> exemplars)301     public static DistributionData create(
302         double mean,
303         long count,
304         double sumOfSquaredDeviations,
305         List<Long> bucketCounts,
306         List<Exemplar> exemplars) {
307       List<Long> bucketCountsCopy =
308           Collections.unmodifiableList(
309               new ArrayList<Long>(Utils.checkNotNull(bucketCounts, "bucketCounts")));
310       for (Long bucketCount : bucketCountsCopy) {
311         Utils.checkNotNull(bucketCount, "bucketCount");
312       }
313 
314       Utils.checkNotNull(exemplars, "exemplars");
315       for (Exemplar exemplar : exemplars) {
316         Utils.checkNotNull(exemplar, "exemplar");
317       }
318 
319       return new AutoValue_AggregationData_DistributionData(
320           mean,
321           count,
322           sumOfSquaredDeviations,
323           bucketCountsCopy,
324           Collections.<Exemplar>unmodifiableList(new ArrayList<Exemplar>(exemplars)));
325     }
326 
327     /**
328      * Creates a {@code DistributionData}.
329      *
330      * @param mean mean value.
331      * @param count count value.
332      * @param min min value.
333      * @param max max value.
334      * @param sumOfSquaredDeviations sum of squared deviations.
335      * @param bucketCounts histogram bucket counts.
336      * @return a {@code DistributionData}.
337      * @since 0.8
338      * @deprecated since 0.17. Use {@link #create(double, long, double, List)}.
339      */
340     @Deprecated
341     @SuppressWarnings("InconsistentOverloads")
create( double mean, long count, double min, double max, double sumOfSquaredDeviations, List<Long> bucketCounts)342     public static DistributionData create(
343         double mean,
344         long count,
345         double min,
346         double max,
347         double sumOfSquaredDeviations,
348         List<Long> bucketCounts) {
349       return create(
350           mean, count, sumOfSquaredDeviations, bucketCounts, Collections.<Exemplar>emptyList());
351     }
352 
353     /**
354      * Creates a {@code DistributionData}.
355      *
356      * @param mean mean value.
357      * @param count count value.
358      * @param sumOfSquaredDeviations sum of squared deviations.
359      * @param bucketCounts histogram bucket counts.
360      * @return a {@code DistributionData}.
361      * @since 0.17
362      */
create( double mean, long count, double sumOfSquaredDeviations, List<Long> bucketCounts)363     public static DistributionData create(
364         double mean, long count, double sumOfSquaredDeviations, List<Long> bucketCounts) {
365       return create(
366           mean, count, sumOfSquaredDeviations, bucketCounts, Collections.<Exemplar>emptyList());
367     }
368 
369     /**
370      * Returns the aggregated mean.
371      *
372      * @return the aggregated mean.
373      * @since 0.8
374      */
getMean()375     public abstract double getMean();
376 
377     /**
378      * Returns the aggregated count.
379      *
380      * @return the aggregated count.
381      * @since 0.8
382      */
getCount()383     public abstract long getCount();
384 
385     /**
386      * Returns the minimum of the population values.
387      *
388      * @return the minimum of the population values.
389      * @since 0.8
390      * @deprecated since 0.17. Returns {@code 0}.
391      */
392     @Deprecated
getMin()393     public double getMin() {
394       return 0;
395     }
396 
397     /**
398      * Returns the maximum of the population values.
399      *
400      * @return the maximum of the population values.
401      * @since 0.8
402      * @deprecated since 0.17. Returns {@code 0}.
403      */
404     @Deprecated
getMax()405     public double getMax() {
406       return 0;
407     }
408 
409     /**
410      * Returns the aggregated sum of squared deviations.
411      *
412      * @return the aggregated sum of squared deviations.
413      * @since 0.8
414      */
getSumOfSquaredDeviations()415     public abstract double getSumOfSquaredDeviations();
416 
417     /**
418      * Returns the aggregated bucket counts. The returned list is immutable, trying to update it
419      * will throw an {@code UnsupportedOperationException}.
420      *
421      * @return the aggregated bucket counts.
422      * @since 0.8
423      */
getBucketCounts()424     public abstract List<Long> getBucketCounts();
425 
426     /**
427      * Returns the {@link Exemplar}s associated with histogram buckets.
428      *
429      * @return the {@code Exemplar}s associated with histogram buckets.
430      * @since 0.16
431      */
getExemplars()432     public abstract List<Exemplar> getExemplars();
433 
434     @Override
match( Function<? super SumDataDouble, T> p0, Function<? super SumDataLong, T> p1, Function<? super CountData, T> p2, Function<? super DistributionData, T> p3, Function<? super LastValueDataDouble, T> p4, Function<? super LastValueDataLong, T> p5, Function<? super AggregationData, T> defaultFunction)435     public final <T> T match(
436         Function<? super SumDataDouble, T> p0,
437         Function<? super SumDataLong, T> p1,
438         Function<? super CountData, T> p2,
439         Function<? super DistributionData, T> p3,
440         Function<? super LastValueDataDouble, T> p4,
441         Function<? super LastValueDataLong, T> p5,
442         Function<? super AggregationData, T> defaultFunction) {
443       return p3.apply(this);
444     }
445   }
446 
447   /**
448    * The last value of aggregated {@code MeasureValueDouble}s.
449    *
450    * @since 0.13
451    */
452   @Immutable
453   @AutoValue
454   public abstract static class LastValueDataDouble extends AggregationData {
455 
LastValueDataDouble()456     LastValueDataDouble() {}
457 
458     /**
459      * Creates a {@code LastValueDataDouble}.
460      *
461      * @param lastValue the last value.
462      * @return a {@code LastValueDataDouble}.
463      * @since 0.13
464      */
create(double lastValue)465     public static LastValueDataDouble create(double lastValue) {
466       return new AutoValue_AggregationData_LastValueDataDouble(lastValue);
467     }
468 
469     /**
470      * Returns the last value.
471      *
472      * @return the last value.
473      * @since 0.13
474      */
getLastValue()475     public abstract double getLastValue();
476 
477     @Override
match( Function<? super SumDataDouble, T> p0, Function<? super SumDataLong, T> p1, Function<? super CountData, T> p2, Function<? super DistributionData, T> p3, Function<? super LastValueDataDouble, T> p4, Function<? super LastValueDataLong, T> p5, Function<? super AggregationData, T> defaultFunction)478     public final <T> T match(
479         Function<? super SumDataDouble, T> p0,
480         Function<? super SumDataLong, T> p1,
481         Function<? super CountData, T> p2,
482         Function<? super DistributionData, T> p3,
483         Function<? super LastValueDataDouble, T> p4,
484         Function<? super LastValueDataLong, T> p5,
485         Function<? super AggregationData, T> defaultFunction) {
486       return p4.apply(this);
487     }
488   }
489 
490   /**
491    * The last value of aggregated {@code MeasureValueLong}s.
492    *
493    * @since 0.13
494    */
495   @Immutable
496   @AutoValue
497   public abstract static class LastValueDataLong extends AggregationData {
498 
LastValueDataLong()499     LastValueDataLong() {}
500 
501     /**
502      * Creates a {@code LastValueDataLong}.
503      *
504      * @param lastValue the last value.
505      * @return a {@code LastValueDataLong}.
506      * @since 0.13
507      */
create(long lastValue)508     public static LastValueDataLong create(long lastValue) {
509       return new AutoValue_AggregationData_LastValueDataLong(lastValue);
510     }
511 
512     /**
513      * Returns the last value.
514      *
515      * @return the last value.
516      * @since 0.13
517      */
getLastValue()518     public abstract long getLastValue();
519 
520     @Override
match( Function<? super SumDataDouble, T> p0, Function<? super SumDataLong, T> p1, Function<? super CountData, T> p2, Function<? super DistributionData, T> p3, Function<? super LastValueDataDouble, T> p4, Function<? super LastValueDataLong, T> p5, Function<? super AggregationData, T> defaultFunction)521     public final <T> T match(
522         Function<? super SumDataDouble, T> p0,
523         Function<? super SumDataLong, T> p1,
524         Function<? super CountData, T> p2,
525         Function<? super DistributionData, T> p3,
526         Function<? super LastValueDataDouble, T> p4,
527         Function<? super LastValueDataLong, T> p5,
528         Function<? super AggregationData, T> defaultFunction) {
529       return p5.apply(this);
530     }
531   }
532 }
533