-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmetrics.go
159 lines (138 loc) · 3.9 KB
/
metrics.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
/*
Copyright 2017 Daniel Nichter
Copyright 2014-2016 Percona LLC and/or its affiliates
*/
package slowlog
import (
"sort"
)
// Metrics encapsulate the metrics of an event like Query_time and Rows_sent.
type Metrics struct {
TimeMetrics map[string]*TimeStats `json:",omitempty"`
NumberMetrics map[string]*NumberStats `json:",omitempty"`
BoolMetrics map[string]*BoolStats `json:",omitempty"`
}
// TimeStats are microsecond-based metrics like Query_time and Lock_time.
type TimeStats struct {
vals []float64
Sum float64
Min float64 `json:",omitempty"`
Avg float64 `json:",omitempty"`
Med float64 `json:",omitempty"` // median
P95 float64 `json:",omitempty"` // 95th percentile
Max float64 `json:",omitempty"`
outlierSum float64
}
// NumberStats are integer-based metrics like Rows_sent and Merge_passes.
type NumberStats struct {
vals []uint64
Sum uint64
Min uint64 `json:",omitempty"`
Avg uint64 `json:",omitempty"`
Med uint64 `json:",omitempty"` // median
P95 uint64 `json:",omitempty"` // 95th percentile
Max uint64 `json:",omitempty"`
outlierSum uint64
}
// BoolStats are boolean-based metrics like QC_Hit and Filesort.
type BoolStats struct {
Sum uint64 // %true = Sum/Cnt
outlierSum uint64
}
// NewMetrics returns a pointer to an initialized Metrics structure.
func NewMetrics() Metrics {
return Metrics{
TimeMetrics: map[string]*TimeStats{},
NumberMetrics: map[string]*NumberStats{},
BoolMetrics: map[string]*BoolStats{},
}
}
// AddEvent saves all the metrics of the event.
func (m *Metrics) AddEvent(e Event, outlier bool) {
for metric, val := range e.TimeMetrics {
stats, seenMetric := m.TimeMetrics[metric]
if !seenMetric {
m.TimeMetrics[metric] = &TimeStats{
vals: []float64{},
}
stats = m.TimeMetrics[metric]
}
if outlier {
stats.outlierSum += val
} else {
stats.Sum += val
}
stats.vals = append(stats.vals, float64(val))
}
for metric, val := range e.NumberMetrics {
stats, seenMetric := m.NumberMetrics[metric]
if !seenMetric {
m.NumberMetrics[metric] = &NumberStats{
vals: []uint64{},
}
stats = m.NumberMetrics[metric]
}
if outlier {
stats.outlierSum += val
} else {
stats.Sum += val
}
stats.vals = append(stats.vals, val)
}
for metric, val := range e.BoolMetrics {
stats, seenMetric := m.BoolMetrics[metric]
if !seenMetric {
m.BoolMetrics[metric] = &BoolStats{}
stats = m.BoolMetrics[metric]
}
if val {
if outlier {
stats.outlierSum += 1
} else {
stats.Sum += 1
}
}
}
}
type byUint64 []uint64
func (a byUint64) Len() int { return len(a) }
func (a byUint64) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a byUint64) Less(i, j int) bool {
return a[i] < a[j] // ascending order
}
// Finalize calculates the statistics of the added metrics. Call this function
// when done adding events.
func (m *Metrics) Finalize(rateLimit uint) {
if rateLimit == 0 {
rateLimit = 1
}
for _, s := range m.TimeMetrics {
sort.Float64s(s.vals)
cnt := len(s.vals)
s.Min = s.vals[0]
s.Avg = (s.Sum + s.outlierSum) / float64(cnt)
s.Med = s.vals[(50*cnt)/100] // median = 50th percentile
s.P95 = s.vals[(95*cnt)/100]
s.Max = s.vals[cnt-1]
// Update sum last because avg ^ needs the original value.
s.Sum = (s.Sum * float64(rateLimit)) + s.outlierSum
}
for _, s := range m.NumberMetrics {
sort.Sort(byUint64(s.vals))
cnt := len(s.vals)
s.Min = s.vals[0]
s.Avg = (s.Sum + s.outlierSum) / uint64(cnt)
s.Med = s.vals[(50*cnt)/100] // median = 50th percentile
s.P95 = s.vals[(95*cnt)/100]
s.Max = s.vals[cnt-1]
// Update sum last because avg ^ needs the original value.
s.Sum = (s.Sum * uint64(rateLimit)) + s.outlierSum
}
if len(m.BoolMetrics) > 0 {
for _, s := range m.BoolMetrics {
s.Sum = (s.Sum * uint64(rateLimit)) + s.outlierSum
}
} else {
m.BoolMetrics = nil
}
}