-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
metric.go
190 lines (164 loc) · 4.2 KB
/
metric.go
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
// +build !js
// Copyright (c) 2009-2020 Misakai Ltd. and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for details.
package stats
import (
"math/rand"
"sync"
"sync/atomic"
"time"
)
// Measurer represents a monitoring contract.
type Measurer interface {
Snapshotter
Measure(name string, value int32)
MeasureElapsed(name string, start time.Time)
MeasureRuntime()
Tag(name, tag string)
}
// Metric maintains a combination of a gauge and a statistically-significant selection
// of the values from a stream. This is essentially a combination of a histogram, gauge
// and a counter.
type Metric struct {
sync.Mutex
data sample // The sample used to build a histogram
count int32 // The number of samples observed
create int64 // The first updated time
name string // The name of the metric
tag string // The tag of the metric (e.g.: IP Address)
}
const (
reservoirSize = 1024
)
// NewMetric creates a new metric.
func NewMetric(name string) *Metric {
return &Metric{
name: name,
data: make([]int32, reservoirSize, reservoirSize),
create: time.Now().Unix(),
}
}
// Reset clears all samples and resets the metric.
func (m *Metric) Reset() {
m.Lock()
defer m.Unlock()
atomic.StoreInt32(&m.count, 0)
m.create = time.Now().Unix()
}
// Name returns the name of the histogram.
func (m *Metric) Name() string {
return m.name
}
// Tag returns the associated tag of the metric.
func (m *Metric) Tag() string {
m.Lock()
defer m.Unlock()
return m.tag
}
// Window returns start and end time of the histogram.
func (m *Metric) Window() (time.Time, time.Time) {
return time.Unix(m.create, 0), time.Now()
}
// sample returns the usable sample
func (m *Metric) sample() sample {
count := m.count
if count > reservoirSize {
count = reservoirSize
}
return m.data[:count]
}
// Count returns the number of samples recorded, which may exceed the
// reservoir size.
func (m *Metric) Count() int {
m.Lock()
defer m.Unlock()
return int(m.count)
}
// Max returns the maximum value in the sample, which may not be the maximum
// value ever to be part of the sample.
func (m *Metric) Max() int {
m.Lock()
defer m.Unlock()
return m.sample().Max()
}
// Mean returns the mean of the values in the sample.
func (m *Metric) Mean() float64 {
m.Lock()
defer m.Unlock()
return m.sample().Mean()
}
// Min returns the minimum value in the sample, which may not be the minimum
// value ever to be part of the sample.
func (m *Metric) Min() int {
m.Lock()
defer m.Unlock()
return m.sample().Min()
}
// Quantile returns a slice of arbitrary quantiles of the sample.
func (m *Metric) Quantile(quantiles ...float64) []float64 {
m.Lock()
defer m.Unlock()
return m.sample().Quantile(quantiles...)
}
// Snapshot returns a read-only copy of the sample.
func (m *Metric) Snapshot() *Snapshot {
m.Lock()
defer m.Unlock()
// Snapshot the data
sample := m.sample()
dest := make([]int32, len(sample))
copy(dest, sample)
return &Snapshot{
Metric: m.name,
Label: m.tag,
T0: m.create,
T1: time.Now().Unix(),
Amount: m.count,
Sample: dest,
}
}
// StdDev returns the standard deviation of the values in the sample.
func (m *Metric) StdDev() float64 {
m.Lock()
defer m.Unlock()
return m.sample().StdDev()
}
// Variance returns the variance of the values in the sample.
func (m *Metric) Variance() float64 {
m.Lock()
defer m.Unlock()
return m.sample().Variance()
}
// Rate returns a operation per second rate since the creation of the metric.
func (m *Metric) Rate() float64 {
t0, t1 := m.Window()
return float64(m.Count()) / float64(t1.Sub(t0).Seconds())
}
// Update samples a new value into the metric.
func (m *Metric) Update(v int32) {
count := atomic.AddInt32(&m.count, 1)
if count <= reservoirSize {
m.Lock()
m.data[count-1] = v
m.Unlock()
return
}
if r := int(rand.Int31n(count)); r < reservoirSize {
m.Lock()
m.data[r] = v
m.Unlock()
return
}
}
// UpdateTag updates the associated metric tag.
func (m *Metric) UpdateTag(tag string) {
m.Lock()
m.tag = tag
m.Unlock()
}
// Histogram creates a histogram with the bins provided.
func (m *Metric) Histogram(bins ...int) []Bin {
m.Lock()
defer m.Unlock()
return m.sample().Histogram(bins)
}