You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

121 lines
3.7 KiB

  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. // AggType represents the type of aggregation function used on a View.
  17. type AggType int
  18. // All available aggregation types.
  19. const (
  20. AggTypeNone AggType = iota // no aggregation; reserved for future use.
  21. AggTypeCount // the count aggregation, see Count.
  22. AggTypeSum // the sum aggregation, see Sum.
  23. AggTypeDistribution // the distribution aggregation, see Distribution.
  24. AggTypeLastValue // the last value aggregation, see LastValue.
  25. )
  26. func (t AggType) String() string {
  27. return aggTypeName[t]
  28. }
  29. var aggTypeName = map[AggType]string{
  30. AggTypeNone: "None",
  31. AggTypeCount: "Count",
  32. AggTypeSum: "Sum",
  33. AggTypeDistribution: "Distribution",
  34. AggTypeLastValue: "LastValue",
  35. }
  36. // Aggregation represents a data aggregation method. Use one of the functions:
  37. // Count, Sum, or Distribution to construct an Aggregation.
  38. type Aggregation struct {
  39. Type AggType // Type is the AggType of this Aggregation.
  40. Buckets []float64 // Buckets are the bucket endpoints if this Aggregation represents a distribution, see Distribution.
  41. newData func() AggregationData
  42. }
  43. var (
  44. aggCount = &Aggregation{
  45. Type: AggTypeCount,
  46. newData: func() AggregationData {
  47. return &CountData{}
  48. },
  49. }
  50. aggSum = &Aggregation{
  51. Type: AggTypeSum,
  52. newData: func() AggregationData {
  53. return &SumData{}
  54. },
  55. }
  56. )
  57. // Count indicates that data collected and aggregated
  58. // with this method will be turned into a count value.
  59. // For example, total number of accepted requests can be
  60. // aggregated by using Count.
  61. func Count() *Aggregation {
  62. return aggCount
  63. }
  64. // Sum indicates that data collected and aggregated
  65. // with this method will be summed up.
  66. // For example, accumulated request bytes can be aggregated by using
  67. // Sum.
  68. func Sum() *Aggregation {
  69. return aggSum
  70. }
  71. // Distribution indicates that the desired aggregation is
  72. // a histogram distribution.
  73. //
  74. // An distribution aggregation may contain a histogram of the values in the
  75. // population. The bucket boundaries for that histogram are described
  76. // by the bounds. This defines len(bounds)+1 buckets.
  77. //
  78. // If len(bounds) >= 2 then the boundaries for bucket index i are:
  79. //
  80. // [-infinity, bounds[i]) for i = 0
  81. // [bounds[i-1], bounds[i]) for 0 < i < length
  82. // [bounds[i-1], +infinity) for i = length
  83. //
  84. // If len(bounds) is 0 then there is no histogram associated with the
  85. // distribution. There will be a single bucket with boundaries
  86. // (-infinity, +infinity).
  87. //
  88. // If len(bounds) is 1 then there is no finite buckets, and that single
  89. // element is the common boundary of the overflow and underflow buckets.
  90. func Distribution(bounds ...float64) *Aggregation {
  91. return &Aggregation{
  92. Type: AggTypeDistribution,
  93. Buckets: bounds,
  94. newData: func() AggregationData {
  95. return newDistributionData(bounds)
  96. },
  97. }
  98. }
  99. // LastValue only reports the last value recorded using this
  100. // aggregation. All other measurements will be dropped.
  101. func LastValue() *Aggregation {
  102. return &Aggregation{
  103. Type: AggTypeLastValue,
  104. newData: func() AggregationData {
  105. return &LastValueData{}
  106. },
  107. }
  108. }