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.
 
 
 

196 lines
6.0 KiB

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