...

Source file src/go.opencensus.io/stats/view/aggregation.go

Documentation: go.opencensus.io/stats/view

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

View as plain text