aggregation.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. // Copyright 2017, OpenCensus Authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. //
  15. package view
  16. import "time"
  17. // AggType represents the type of aggregation function used on a View.
  18. type AggType int
  19. // All available aggregation types.
  20. const (
  21. AggTypeNone AggType = iota // no aggregation; reserved for future use.
  22. AggTypeCount // the count aggregation, see Count.
  23. AggTypeSum // the sum aggregation, see Sum.
  24. AggTypeDistribution // the distribution aggregation, see Distribution.
  25. AggTypeLastValue // the last value aggregation, see LastValue.
  26. )
  27. func (t AggType) String() string {
  28. return aggTypeName[t]
  29. }
  30. var aggTypeName = map[AggType]string{
  31. AggTypeNone: "None",
  32. AggTypeCount: "Count",
  33. AggTypeSum: "Sum",
  34. AggTypeDistribution: "Distribution",
  35. AggTypeLastValue: "LastValue",
  36. }
  37. // Aggregation represents a data aggregation method. Use one of the functions:
  38. // Count, Sum, or Distribution to construct an Aggregation.
  39. type Aggregation struct {
  40. Type AggType // Type is the AggType of this Aggregation.
  41. Buckets []float64 // Buckets are the bucket endpoints if this Aggregation represents a distribution, see Distribution.
  42. newData func(time.Time) AggregationData
  43. }
  44. var (
  45. aggCount = &Aggregation{
  46. Type: AggTypeCount,
  47. newData: func(t time.Time) AggregationData {
  48. return &CountData{Start: t}
  49. },
  50. }
  51. aggSum = &Aggregation{
  52. Type: AggTypeSum,
  53. newData: func(t time.Time) AggregationData {
  54. return &SumData{Start: t}
  55. },
  56. }
  57. )
  58. // Count indicates that data collected and aggregated
  59. // with this method will be turned into a count value.
  60. // For example, total number of accepted requests can be
  61. // aggregated by using Count.
  62. func Count() *Aggregation {
  63. return aggCount
  64. }
  65. // Sum indicates that data collected and aggregated
  66. // with this method will be summed up.
  67. // For example, accumulated request bytes can be aggregated by using
  68. // Sum.
  69. func Sum() *Aggregation {
  70. return aggSum
  71. }
  72. // Distribution indicates that the desired aggregation is
  73. // a histogram distribution.
  74. //
  75. // A distribution aggregation may contain a histogram of the values in the
  76. // population. The bucket boundaries for that histogram are described
  77. // by the bounds. This defines len(bounds)+1 buckets.
  78. //
  79. // If len(bounds) >= 2 then the boundaries for bucket index i are:
  80. //
  81. // [-infinity, bounds[i]) for i = 0
  82. // [bounds[i-1], bounds[i]) for 0 < i < length
  83. // [bounds[i-1], +infinity) for i = length
  84. //
  85. // If len(bounds) is 0 then there is no histogram associated with the
  86. // distribution. There will be a single bucket with boundaries
  87. // (-infinity, +infinity).
  88. //
  89. // If len(bounds) is 1 then there is no finite buckets, and that single
  90. // element is the common boundary of the overflow and underflow buckets.
  91. func Distribution(bounds ...float64) *Aggregation {
  92. agg := &Aggregation{
  93. Type: AggTypeDistribution,
  94. Buckets: bounds,
  95. }
  96. agg.newData = func(t time.Time) AggregationData {
  97. return newDistributionData(agg, t)
  98. }
  99. return agg
  100. }
  101. // LastValue only reports the last value recorded using this
  102. // aggregation. All other measurements will be dropped.
  103. func LastValue() *Aggregation {
  104. return &Aggregation{
  105. Type: AggTypeLastValue,
  106. newData: func(_ time.Time) AggregationData {
  107. return &LastValueData{}
  108. },
  109. }
  110. }