1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304
|
// Copyright 2019 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package metric
import (
"fmt"
"sort"
"time"
"golang.org/x/tools/internal/event/keys"
"golang.org/x/tools/internal/event/label"
)
// Data represents a single point in the time series of a metric.
// This provides the common interface to all metrics no matter their data
// format.
// To get the actual values for the metric you must type assert to a concrete
// metric type.
type Data interface {
// Handle returns the metric handle this data is for.
//TODO: rethink the concept of metric handles
Handle() string
// Groups reports the rows that currently exist for this metric.
Groups() [][]label.Label
}
// Int64Data is a concrete implementation of Data for int64 scalar metrics.
type Int64Data struct {
// Info holds the original construction information.
Info *Scalar
// IsGauge is true for metrics that track values, rather than increasing over time.
IsGauge bool
// Rows holds the per group values for the metric.
Rows []int64
// End is the last time this metric was updated.
EndTime time.Time
groups [][]label.Label
key *keys.Int64
}
// Float64Data is a concrete implementation of Data for float64 scalar metrics.
type Float64Data struct {
// Info holds the original construction information.
Info *Scalar
// IsGauge is true for metrics that track values, rather than increasing over time.
IsGauge bool
// Rows holds the per group values for the metric.
Rows []float64
// End is the last time this metric was updated.
EndTime time.Time
groups [][]label.Label
key *keys.Float64
}
// HistogramInt64Data is a concrete implementation of Data for int64 histogram metrics.
type HistogramInt64Data struct {
// Info holds the original construction information.
Info *HistogramInt64
// Rows holds the per group values for the metric.
Rows []*HistogramInt64Row
// End is the last time this metric was updated.
EndTime time.Time
groups [][]label.Label
key *keys.Int64
}
// HistogramInt64Row holds the values for a single row of a HistogramInt64Data.
type HistogramInt64Row struct {
// Values is the counts per bucket.
Values []int64
// Count is the total count.
Count int64
// Sum is the sum of all the values recorded.
Sum int64
// Min is the smallest recorded value.
Min int64
// Max is the largest recorded value.
Max int64
}
// HistogramFloat64Data is a concrete implementation of Data for float64 histogram metrics.
type HistogramFloat64Data struct {
// Info holds the original construction information.
Info *HistogramFloat64
// Rows holds the per group values for the metric.
Rows []*HistogramFloat64Row
// End is the last time this metric was updated.
EndTime time.Time
groups [][]label.Label
key *keys.Float64
}
// HistogramFloat64Row holds the values for a single row of a HistogramFloat64Data.
type HistogramFloat64Row struct {
// Values is the counts per bucket.
Values []int64
// Count is the total count.
Count int64
// Sum is the sum of all the values recorded.
Sum float64
// Min is the smallest recorded value.
Min float64
// Max is the largest recorded value.
Max float64
}
func labelListEqual(a, b []label.Label) bool {
//TODO: make this more efficient
return fmt.Sprint(a) == fmt.Sprint(b)
}
func labelListLess(a, b []label.Label) bool {
//TODO: make this more efficient
return fmt.Sprint(a) < fmt.Sprint(b)
}
func getGroup(lm label.Map, g *[][]label.Label, keys []label.Key) (int, bool) {
group := make([]label.Label, len(keys))
for i, key := range keys {
l := lm.Find(key)
if l.Valid() {
group[i] = l
}
}
old := *g
index := sort.Search(len(old), func(i int) bool {
return !labelListLess(old[i], group)
})
if index < len(old) && labelListEqual(group, old[index]) {
// not a new group
return index, false
}
*g = make([][]label.Label, len(old)+1)
copy(*g, old[:index])
copy((*g)[index+1:], old[index:])
(*g)[index] = group
return index, true
}
func (data *Int64Data) Handle() string { return data.Info.Name }
func (data *Int64Data) Groups() [][]label.Label { return data.groups }
func (data *Int64Data) modify(at time.Time, lm label.Map, f func(v int64) int64) Data {
index, insert := getGroup(lm, &data.groups, data.Info.Keys)
old := data.Rows
if insert {
data.Rows = make([]int64, len(old)+1)
copy(data.Rows, old[:index])
copy(data.Rows[index+1:], old[index:])
} else {
data.Rows = make([]int64, len(old))
copy(data.Rows, old)
}
data.Rows[index] = f(data.Rows[index])
data.EndTime = at
frozen := *data
return &frozen
}
func (data *Int64Data) count(at time.Time, lm label.Map, l label.Label) Data {
return data.modify(at, lm, func(v int64) int64 {
return v + 1
})
}
func (data *Int64Data) sum(at time.Time, lm label.Map, l label.Label) Data {
return data.modify(at, lm, func(v int64) int64 {
return v + data.key.From(l)
})
}
func (data *Int64Data) latest(at time.Time, lm label.Map, l label.Label) Data {
return data.modify(at, lm, func(v int64) int64 {
return data.key.From(l)
})
}
func (data *Float64Data) Handle() string { return data.Info.Name }
func (data *Float64Data) Groups() [][]label.Label { return data.groups }
func (data *Float64Data) modify(at time.Time, lm label.Map, f func(v float64) float64) Data {
index, insert := getGroup(lm, &data.groups, data.Info.Keys)
old := data.Rows
if insert {
data.Rows = make([]float64, len(old)+1)
copy(data.Rows, old[:index])
copy(data.Rows[index+1:], old[index:])
} else {
data.Rows = make([]float64, len(old))
copy(data.Rows, old)
}
data.Rows[index] = f(data.Rows[index])
data.EndTime = at
frozen := *data
return &frozen
}
func (data *Float64Data) sum(at time.Time, lm label.Map, l label.Label) Data {
return data.modify(at, lm, func(v float64) float64 {
return v + data.key.From(l)
})
}
func (data *Float64Data) latest(at time.Time, lm label.Map, l label.Label) Data {
return data.modify(at, lm, func(v float64) float64 {
return data.key.From(l)
})
}
func (data *HistogramInt64Data) Handle() string { return data.Info.Name }
func (data *HistogramInt64Data) Groups() [][]label.Label { return data.groups }
func (data *HistogramInt64Data) modify(at time.Time, lm label.Map, f func(v *HistogramInt64Row)) Data {
index, insert := getGroup(lm, &data.groups, data.Info.Keys)
old := data.Rows
var v HistogramInt64Row
if insert {
data.Rows = make([]*HistogramInt64Row, len(old)+1)
copy(data.Rows, old[:index])
copy(data.Rows[index+1:], old[index:])
} else {
data.Rows = make([]*HistogramInt64Row, len(old))
copy(data.Rows, old)
v = *data.Rows[index]
}
oldValues := v.Values
v.Values = make([]int64, len(data.Info.Buckets))
copy(v.Values, oldValues)
f(&v)
data.Rows[index] = &v
data.EndTime = at
frozen := *data
return &frozen
}
func (data *HistogramInt64Data) record(at time.Time, lm label.Map, l label.Label) Data {
return data.modify(at, lm, func(v *HistogramInt64Row) {
value := data.key.From(l)
v.Sum += value
if v.Min > value || v.Count == 0 {
v.Min = value
}
if v.Max < value || v.Count == 0 {
v.Max = value
}
v.Count++
for i, b := range data.Info.Buckets {
if value <= b {
v.Values[i]++
}
}
})
}
func (data *HistogramFloat64Data) Handle() string { return data.Info.Name }
func (data *HistogramFloat64Data) Groups() [][]label.Label { return data.groups }
func (data *HistogramFloat64Data) modify(at time.Time, lm label.Map, f func(v *HistogramFloat64Row)) Data {
index, insert := getGroup(lm, &data.groups, data.Info.Keys)
old := data.Rows
var v HistogramFloat64Row
if insert {
data.Rows = make([]*HistogramFloat64Row, len(old)+1)
copy(data.Rows, old[:index])
copy(data.Rows[index+1:], old[index:])
} else {
data.Rows = make([]*HistogramFloat64Row, len(old))
copy(data.Rows, old)
v = *data.Rows[index]
}
oldValues := v.Values
v.Values = make([]int64, len(data.Info.Buckets))
copy(v.Values, oldValues)
f(&v)
data.Rows[index] = &v
data.EndTime = at
frozen := *data
return &frozen
}
func (data *HistogramFloat64Data) record(at time.Time, lm label.Map, l label.Label) Data {
return data.modify(at, lm, func(v *HistogramFloat64Row) {
value := data.key.From(l)
v.Sum += value
if v.Min > value || v.Count == 0 {
v.Min = value
}
if v.Max < value || v.Count == 0 {
v.Max = value
}
v.Count++
for i, b := range data.Info.Buckets {
if value <= b {
v.Values[i]++
}
}
})
}
|