Debian Codegraph @

event/metrics.go

raw symbols SCIP
1 /*
2 Copyright (c) 2019, Percona LLC.
3 All rights reserved.
4
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
7
8 * Redistributions of source code must retain the above copyright notice, this
9 list of conditions and the following disclaimer.
10
11 * Redistributions in binary form must reproduce the above copyright notice,
12 this list of conditions and the following disclaimer in the documentation
13 and/or other materials provided with the distribution.
14
15 * Neither the name of the copyright holder nor the names of its
16 contributors may be used to endorse or promote products derived from
17 this software without specific prior written permission.
18
19 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
23 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31 package event
32
33 import (
34 "sort"
35
36 "github.com/percona/go-mysql/log"
37 )
38
39 // Metrics encapsulate the metrics of an event like Query_time and Rows_sent.
40 type Metrics struct {
41 TimeMetrics map[string]*TimeStats `json:",omitempty"`
42 NumberMetrics map[string]*NumberStats `json:",omitempty"`
43 BoolMetrics map[string]*BoolStats `json:",omitempty"`
44 }
45
46 // TimeStats are microsecond-based metrics like Query_time and Lock_time.
47 type TimeStats struct {
48 vals []float64 `json:"-"`
49 Cnt uint64
50 Sum float64
51 Min *float64 `json:",omitempty"`
52 P99 *float64 `json:",omitempty"` // 99th percentile
53 Max *float64 `json:",omitempty"`
54 outlierSum float64
55 }
56
57 // NumberStats are integer-based metrics like Rows_sent and Merge_passes.
58 type NumberStats struct {
59 vals []uint64 `json:"-"`
60 Cnt uint64
61 Sum uint64
62 Min *uint64 `json:",omitempty"`
63 P99 *uint64 `json:",omitempty"` // 99th percentile
64 Max *uint64 `json:",omitempty"`
65 outlierSum uint64
66 }
67
68 // BoolStats are boolean-based metrics like QC_Hit and Filesort.
69 type BoolStats struct {
70 vals []bool `json:"-"`
71 Cnt uint64
72 Sum uint64 // %true = Sum/Cnt
73 outlierSum uint64
74 }
75
76 // NewMetrics returns a pointer to an initialized Metrics structure.
77 func NewMetrics() *Metrics {
78 m := &Metrics{
79 TimeMetrics: make(map[string]*TimeStats),
80 NumberMetrics: make(map[string]*NumberStats),
81 BoolMetrics: make(map[string]*BoolStats),
82 }
83 return m
84 }
85
86 // AddEvent saves all the metrics of the event.
87 func (m *Metrics) AddEvent(e *log.Event, outlier bool) {
88 for metric, val := range e.TimeMetrics {
89 stats, seenMetric := m.TimeMetrics[metric]
90 if !seenMetric {
91 m.TimeMetrics[metric] = &TimeStats{
92 vals: []float64{},
93 }
94 stats = m.TimeMetrics[metric]
95 }
96 if outlier {
97 stats.outlierSum += val
98 } else {
99 stats.Sum += val
100 }
101 stats.vals = append(stats.vals, float64(val))
102 }
103
104 for metric, val := range e.NumberMetrics {
105 stats, seenMetric := m.NumberMetrics[metric]
106 if !seenMetric {
107 m.NumberMetrics[metric] = &NumberStats{
108 vals: []uint64{},
109 }
110 stats = m.NumberMetrics[metric]
111 }
112 if outlier {
113 stats.outlierSum += val
114 } else {
115 stats.Sum += val
116 }
117 stats.vals = append(stats.vals, val)
118 }
119
120 for metric, val := range e.BoolMetrics {
121 stats, seenMetric := m.BoolMetrics[metric]
122 if !seenMetric {
123 stats = &BoolStats{}
124 m.BoolMetrics[metric] = stats
125 }
126 if val {
127 if outlier {
128 stats.outlierSum += 1
129 } else {
130 stats.Sum += 1
131 }
132 }
133 stats.vals = append(stats.vals, val)
134 }
135 }
136
137 type byUint64 []uint64
138
139 func (a byUint64) Len() int { return len(a) }
140 func (a byUint64) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
141 func (a byUint64) Less(i, j int) bool {
142 return a[i] < a[j] // ascending order
143 }
144
145 // Finalize calculates the statistics of the added metrics. Call this function
146 // when done adding events.
147 func (m *Metrics) Finalize(rateLimit uint, totalQueries uint) {
148 if rateLimit == 0 {
149 rateLimit = 1
150 }
151
152 for _, s := range m.TimeMetrics {
153 sort.Float64s(s.vals)
154 cnt := len(s.vals)
155
156 s.Cnt = uint64(cnt)
157 s.Min = Float64(s.vals[0])
158 s.P99 = Float64(s.vals[(99*cnt)/100])
159 s.Max = Float64(s.vals[cnt-1])
160 s.Sum = (s.Sum * float64(rateLimit)) + s.outlierSum
161 }
162
163 for _, s := range m.NumberMetrics {
164 sort.Sort(byUint64(s.vals))
165 cnt := len(s.vals)
166
167 s.Cnt = uint64(cnt)
168 s.Min = Uint64(s.vals[0])
169 s.P99 = Uint64(s.vals[(99*cnt)/100])
170 s.Max = Uint64(s.vals[cnt-1])
171 s.Sum = (s.Sum * uint64(rateLimit)) + s.outlierSum
172 }
173
174 for _, s := range m.BoolMetrics {
175 cnt := len(s.vals)
176 s.Cnt = uint64(cnt)
177 s.Sum = (s.Sum * uint64(rateLimit)) + s.outlierSum
178 }
179 }
180
181 // Float64 returns a pointer to the float64 value passed in.
182 func Float64(v float64) *float64 {
183 return &v
184 }
185
186 // Float64Value returns the value of the float64 pointer passed in or
187 // 0 if the pointer is nil.
188 func Float64Value(v *float64) float64 {
189 if v != nil {
190 return *v
191 }
192 return 0
193 }
194
195 // Uint64 returns a pointer to the uint64 value passed in.
196 func Uint64(v uint64) *uint64 {
197 return &v
198 }
199
200 // Uint64Value returns the value of the uint64 pointer passed in or
201 // 0 if the pointer is nil.
202 func Uint64Value(v *uint64) uint64 {
203 if v != nil {
204 return *v
205 }
206 return 0
207 }