-
Notifications
You must be signed in to change notification settings - Fork 0
/
metric.go
249 lines (213 loc) · 7.29 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
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
package main
import (
"time"
"github.com/rach/pome/Godeps/_workspace/src/github.com/jmoiron/sqlx"
_ "github.com/rach/pome/Godeps/_workspace/src/github.com/lib/pq"
)
type MetricList struct {
TableBloat map[string]Metric `json:"table_bloat"`
IndexBloat map[string]Metric `json:"index_bloat"`
TopBloatIndexRatio []Metric `json:"top_index_bloat"`
TopBloatTableRatio []Metric `json:"top_table_bloat"`
TotalTableBloatBytes []Metric `json:"total_table_bloat_bytes"`
TotalIndexBloatBytes []Metric `json:"total_index_bloat_bytes"`
DatabaseSize []Metric `json:"database_size"`
NumberOfConnection []Metric `json:"number_of_connection"`
Version string `json:"version"`
TransactionPerSec []Metric `json:"transaction_per_sec"`
LastTransactionNumber TransactionNumber `json:"-"`
}
type TransactionNumber struct {
Timestamp int64
Count int64
}
func initMetricList(version string) MetricList {
return MetricList{
Version: version,
TableBloat: make(map[string]Metric),
IndexBloat: make(map[string]Metric),
TopBloatIndexRatio: []Metric{},
TopBloatTableRatio: []Metric{},
TotalTableBloatBytes: []Metric{},
TotalIndexBloatBytes: []Metric{},
DatabaseSize: []Metric{},
NumberOfConnection: []Metric{},
TransactionPerSec: []Metric{},
}
}
type metricFct func(db *sqlx.DB, metrics *MetricList, datafct databaseResultFct, limit int)
type Metric interface {
}
type Metrics []Metric
type transactionPerSecMetric struct {
Timestamp int64 `json:"timestamp"`
Tps int `json:"tps"`
}
type numberConnectionMetric struct {
Timestamp int64 `json:"timestamp"`
Count int `json:"count"`
}
type databaseSizeMetric struct {
Timestamp int64 `json:"timestamp"`
TableSize int `json:"table_size"`
IndexSize int `json:"index_size"`
TotalSize int `json:"total_size"`
IndexRatio float64 `json:"index_ratio"`
}
type topTableBloatRatioMetric struct {
Timestamp int64 `json:"timestamp"`
BloatRatio float64 `json:"bloat_ratio"`
TableSchema string `json:"table_schema"`
TableName string `json:"table_name"`
}
type topIndexBloatRatioMetric struct {
Timestamp int64 `json:"timestamp"`
BloatRatio float64 `json:"bloat_ratio"`
TableSchema string `json:"table_schema"`
TableName string `json:"table_name"`
IndexName string `json:"index_name"`
}
type totalBloatBytesMetric struct {
Timestamp int64 `json:"timestamp"`
BloatBytes int64 `json:"bloat_bytes"`
}
type bloatMetric struct {
Timestamp int64 `json:"timestamp"`
BloatBytes int64 `json:"bloat_bytes"`
BloatRatio float64 `json:"bloat_ratio"`
}
type tableBloatMetric struct {
TableSchema string `json:"table_schema"`
TableName string `json:"table_name"`
Bloat []Metric `json:"data"`
}
type indexBloatMetric struct {
TableSchema string `json:"table_schema"`
TableName string `json:"table_name"`
IndexName string `json:"index_name"`
Bloat []Metric `json:"data"`
}
func GetTimestamp() int64 {
return time.Now().Unix()
}
func appendAndFilter(list []Metric, m Metric, limit int) []Metric {
r := append(list, m)
if len(r) > limit {
r = r[len(r)-limit:]
}
return r
}
func initMapMetric(key string, vm *map[string]Metric, metric Metric) {
if *vm == nil {
*vm = make(map[string]Metric)
}
if _, ok := (*vm)[key]; !ok {
(*vm)[key] = metric
}
}
func indexBloatUpdate(db *sqlx.DB, metrics *MetricList, datafct databaseResultFct, limit int) {
timestamp := GetTimestamp()
results := (datafct(db)).([]IndexBloatDatabaseResult)
var totalBytes int64 = 0
var topBloatRatio float64 = 0
var topBloatRatioMetric topIndexBloatRatioMetric
// iterate over each row
for _, v := range results {
if v.Schema == "information_schema" {
continue
}
totalBytes += v.BloatBytes
if v.BloatRatio > topBloatRatio {
topBloatRatio = v.BloatRatio
topBloatRatioMetric = topIndexBloatRatioMetric{
timestamp,
topBloatRatio,
v.Schema,
v.Table,
v.Index,
}
}
initMapMetric(
v.Key,
&((*metrics).IndexBloat),
indexBloatMetric{
TableSchema: v.Schema,
TableName: v.Table,
IndexName: v.Index})
m := bloatMetric{Timestamp: timestamp, BloatBytes: v.BloatBytes, BloatRatio: v.BloatRatio}
current_val := ((*metrics).IndexBloat[v.Key]).(indexBloatMetric)
tmp_metrics := appendAndFilter(current_val.Bloat, m, limit)
current_val.Bloat = tmp_metrics
(*metrics).IndexBloat[v.Key] = current_val
}
(*metrics).TopBloatIndexRatio = appendAndFilter(
(*metrics).TopBloatIndexRatio,
topBloatRatioMetric,
limit,
)
tbbm := totalBloatBytesMetric{timestamp, totalBytes}
(*metrics).TotalIndexBloatBytes = appendAndFilter((*metrics).TotalIndexBloatBytes, tbbm, limit)
}
func tableBloatUpdate(db *sqlx.DB, metrics *MetricList, datafct databaseResultFct, limit int) {
timestamp := GetTimestamp()
results := (datafct(db)).([]TableBloatDatabaseResult)
var total_bytes int64 = 0
var topBloatRatio float64 = 0
var topBloatRatioMetric topTableBloatRatioMetric
// iterate over each row
for _, v := range results {
if v.Schema == "information_schema" {
continue
}
total_bytes += v.BloatBytes
if v.BloatRatio > topBloatRatio {
topBloatRatio = v.BloatRatio
topBloatRatioMetric = topTableBloatRatioMetric{
timestamp,
topBloatRatio,
v.Schema,
v.Table,
}
}
initMapMetric(
v.Key,
&((*metrics).TableBloat),
tableBloatMetric{
TableSchema: v.Schema,
TableName: v.Table})
m := bloatMetric{Timestamp: timestamp, BloatBytes: v.BloatBytes, BloatRatio: v.BloatRatio}
current_val := ((*metrics).TableBloat[v.Key]).(tableBloatMetric)
tmp_metrics := appendAndFilter(current_val.Bloat, m, limit)
current_val.Bloat = tmp_metrics
(*metrics).TableBloat[v.Key] = current_val
}
(*metrics).TopBloatTableRatio = appendAndFilter(
(*metrics).TopBloatTableRatio,
topBloatRatioMetric,
limit,
)
tbbm := totalBloatBytesMetric{timestamp, total_bytes}
(*metrics).TotalTableBloatBytes = appendAndFilter((*metrics).TotalTableBloatBytes, tbbm, limit)
}
func databaseSizeUpdate(db *sqlx.DB, metrics *MetricList, datafct databaseResultFct, limit int) {
timestamp := GetTimestamp()
res := (datafct(db)).(DatabaseSizeResult)
met := databaseSizeMetric{timestamp, res.TableSize, res.IndexSize, res.TotalSize, res.IndexRatio}
(*metrics).DatabaseSize = appendAndFilter((*metrics).DatabaseSize, met, limit)
}
func numberOfConnectionUpdate(db *sqlx.DB, metrics *MetricList, datafct databaseResultFct, limit int) {
timestamp := GetTimestamp()
res := (datafct(db)).(NumberOfConnectionResult)
met := numberConnectionMetric{timestamp, res.Count}
(*metrics).NumberOfConnection = appendAndFilter((*metrics).NumberOfConnection, met, limit)
}
func transactionPerSecUpdate(db *sqlx.DB, metrics *MetricList, datafct databaseResultFct, limit int) {
timestamp := GetTimestamp()
res := (datafct(db)).(TransactionNumberResult)
if &metrics.LastTransactionNumber != nil {
tps := (res.Count - metrics.LastTransactionNumber.Count) / (timestamp - metrics.LastTransactionNumber.Timestamp)
met := transactionPerSecMetric{timestamp, int(tps)}
(*metrics).TransactionPerSec = appendAndFilter((*metrics).TransactionPerSec, met, limit)
}
metrics.LastTransactionNumber = TransactionNumber{Timestamp: timestamp, Count: res.Count}
}