point.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. // Copyright 2018, 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. package metricdata
  15. import (
  16. "time"
  17. )
  18. // Point is a single data point of a time series.
  19. type Point struct {
  20. // Time is the point in time that this point represents in a time series.
  21. Time time.Time
  22. // Value is the value of this point. Prefer using ReadValue to switching on
  23. // the value type, since new value types might be added.
  24. Value interface{}
  25. }
  26. //go:generate stringer -type ValueType
  27. // NewFloat64Point creates a new Point holding a float64 value.
  28. func NewFloat64Point(t time.Time, val float64) Point {
  29. return Point{
  30. Value: val,
  31. Time: t,
  32. }
  33. }
  34. // NewInt64Point creates a new Point holding an int64 value.
  35. func NewInt64Point(t time.Time, val int64) Point {
  36. return Point{
  37. Value: val,
  38. Time: t,
  39. }
  40. }
  41. // NewDistributionPoint creates a new Point holding a Distribution value.
  42. func NewDistributionPoint(t time.Time, val *Distribution) Point {
  43. return Point{
  44. Value: val,
  45. Time: t,
  46. }
  47. }
  48. // NewSummaryPoint creates a new Point holding a Summary value.
  49. func NewSummaryPoint(t time.Time, val *Summary) Point {
  50. return Point{
  51. Value: val,
  52. Time: t,
  53. }
  54. }
  55. // ValueVisitor allows reading the value of a point.
  56. type ValueVisitor interface {
  57. VisitFloat64Value(float64)
  58. VisitInt64Value(int64)
  59. VisitDistributionValue(*Distribution)
  60. VisitSummaryValue(*Summary)
  61. }
  62. // ReadValue accepts a ValueVisitor and calls the appropriate method with the
  63. // value of this point.
  64. // Consumers of Point should use this in preference to switching on the type
  65. // of the value directly, since new value types may be added.
  66. func (p Point) ReadValue(vv ValueVisitor) {
  67. switch v := p.Value.(type) {
  68. case int64:
  69. vv.VisitInt64Value(v)
  70. case float64:
  71. vv.VisitFloat64Value(v)
  72. case *Distribution:
  73. vv.VisitDistributionValue(v)
  74. case *Summary:
  75. vv.VisitSummaryValue(v)
  76. default:
  77. panic("unexpected value type")
  78. }
  79. }
  80. // Distribution contains summary statistics for a population of values. It
  81. // optionally contains a histogram representing the distribution of those
  82. // values across a set of buckets.
  83. type Distribution struct {
  84. // Count is the number of values in the population. Must be non-negative. This value
  85. // must equal the sum of the values in bucket_counts if a histogram is
  86. // provided.
  87. Count int64
  88. // Sum is the sum of the values in the population. If count is zero then this field
  89. // must be zero.
  90. Sum float64
  91. // SumOfSquaredDeviation is the sum of squared deviations from the mean of the values in the
  92. // population. For values x_i this is:
  93. //
  94. // Sum[i=1..n]((x_i - mean)^2)
  95. //
  96. // Knuth, "The Art of Computer Programming", Vol. 2, page 323, 3rd edition
  97. // describes Welford's method for accumulating this sum in one pass.
  98. //
  99. // If count is zero then this field must be zero.
  100. SumOfSquaredDeviation float64
  101. // BucketOptions describes the bounds of the histogram buckets in this
  102. // distribution.
  103. //
  104. // A Distribution may optionally contain a histogram of the values in the
  105. // population.
  106. //
  107. // If nil, there is no associated histogram.
  108. BucketOptions *BucketOptions
  109. // Bucket If the distribution does not have a histogram, then omit this field.
  110. // If there is a histogram, then the sum of the values in the Bucket counts
  111. // must equal the value in the count field of the distribution.
  112. Buckets []Bucket
  113. }
  114. // BucketOptions describes the bounds of the histogram buckets in this
  115. // distribution.
  116. type BucketOptions struct {
  117. // Bounds specifies a set of bucket upper bounds.
  118. // This defines len(bounds) + 1 (= N) buckets. The boundaries for bucket
  119. // index i are:
  120. //
  121. // [0, Bounds[i]) for i == 0
  122. // [Bounds[i-1], Bounds[i]) for 0 < i < N-1
  123. // [Bounds[i-1], +infinity) for i == N-1
  124. Bounds []float64
  125. }
  126. // Bucket represents a single bucket (value range) in a distribution.
  127. type Bucket struct {
  128. // Count is the number of values in each bucket of the histogram, as described in
  129. // bucket_bounds.
  130. Count int64
  131. // Exemplar associated with this bucket (if any).
  132. Exemplar *Exemplar
  133. }
  134. // Summary is a representation of percentiles.
  135. type Summary struct {
  136. // Count is the cumulative count (if available).
  137. Count int64
  138. // Sum is the cumulative sum of values (if available).
  139. Sum float64
  140. // HasCountAndSum is true if Count and Sum are available.
  141. HasCountAndSum bool
  142. // Snapshot represents percentiles calculated over an arbitrary time window.
  143. // The values in this struct can be reset at arbitrary unknown times, with
  144. // the requirement that all of them are reset at the same time.
  145. Snapshot Snapshot
  146. }
  147. // Snapshot represents percentiles over an arbitrary time.
  148. // The values in this struct can be reset at arbitrary unknown times, with
  149. // the requirement that all of them are reset at the same time.
  150. type Snapshot struct {
  151. // Count is the number of values in the snapshot. Optional since some systems don't
  152. // expose this. Set to 0 if not available.
  153. Count int64
  154. // Sum is the sum of values in the snapshot. Optional since some systems don't
  155. // expose this. If count is 0 then this field must be zero.
  156. Sum float64
  157. // Percentiles is a map from percentile (range (0-100.0]) to the value of
  158. // the percentile.
  159. Percentiles map[float64]float64
  160. }
  161. //go:generate stringer -type Type
  162. // Type is the overall type of metric, including its value type and whether it
  163. // represents a cumulative total (since the start time) or if it represents a
  164. // gauge value.
  165. type Type int
  166. // Metric types.
  167. const (
  168. TypeGaugeInt64 Type = iota
  169. TypeGaugeFloat64
  170. TypeGaugeDistribution
  171. TypeCumulativeInt64
  172. TypeCumulativeFloat64
  173. TypeCumulativeDistribution
  174. TypeSummary
  175. )