-
Notifications
You must be signed in to change notification settings - Fork 8
/
global.go
281 lines (234 loc) · 7.41 KB
/
global.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
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
// Copyright 2022 Block, Inc.
package varglobal
import (
"context"
"database/sql"
"fmt"
"strings"
"github.com/cashapp/blip"
"github.com/cashapp/blip/sqlutil"
)
const (
DOMAIN = "var.global"
OPT_SOURCE = "source"
OPT_ALL = "all"
SOURCE_SELECT = "select"
SOURCE_PFS = "pfs"
SOURCE_SHOW = "show"
)
// Global collects global system variables for the var.global domain.
type Global struct {
db *sql.DB
// --
metrics map[string][]string // keyed on level
queryIn map[string]string // keyed on level
sourceIn map[string]string // keyed on level
}
var _ blip.Collector = &Global{}
func NewGlobal(db *sql.DB) *Global {
return &Global{
db: db,
metrics: map[string][]string{},
queryIn: make(map[string]string),
sourceIn: make(map[string]string),
}
}
func (c *Global) Domain() string {
return DOMAIN
}
func (c *Global) Help() blip.CollectorHelp {
return blip.CollectorHelp{
Domain: DOMAIN,
Description: "Global system variables (sysvars) like 'innodb_log_file_size' and 'max_connections'",
Options: map[string]blip.CollectorHelpOption{
OPT_SOURCE: {
Name: OPT_SOURCE,
Desc: "Where to collect sysvars from",
Default: "auto",
Values: map[string]string{
"auto": "Auto-determine best source",
"select": "@@GLOBAL.metric_name",
"pfs": "performance_schema.global_variables",
"show": "SHOW GLOBAL VARIABLES",
},
},
OPT_ALL: {
Name: OPT_ALL,
Desc: "Collect all sysvars",
Default: "no",
Values: map[string]string{
"yes": "Collect all (CAUTION: there are >500 sysvars)",
"no": "Collect only sysvars listed in metrics",
},
},
},
}
}
// Prepares queries for all levels in the plan that contain the "var.global" domain
func (c *Global) Prepare(ctx context.Context, plan blip.Plan) (func(), error) {
LEVEL:
for levelName, level := range plan.Levels {
dom, ok := level.Collect[DOMAIN]
if !ok {
continue LEVEL // not collected in this level
}
err := c.prepareLevel(levelName, dom.Metrics, dom.Options)
if err != nil {
return nil, err
}
}
return nil, nil
}
func (c *Global) Collect(ctx context.Context, levelName string) ([]blip.MetricValue, error) {
switch c.sourceIn[levelName] {
case SOURCE_SELECT:
return c.collectSELECT(ctx, levelName)
case SOURCE_PFS:
return c.collectRows(ctx, levelName)
case SOURCE_SHOW:
return c.collectRows(ctx, levelName)
}
panic(fmt.Sprintf("invalid source in Collect %s", c.sourceIn[levelName]))
}
// //////////////////////////////////////////////////////////////////////////
// Internal methods
// //////////////////////////////////////////////////////////////////////////
// Prepares the query for given level based on it's metrics and source option
func (c *Global) prepareLevel(levelName string, metrics []string, options map[string]string) error {
// Reset in case because prepareLevel can be called multiple times
// if the LPA changes the plan
c.sourceIn[levelName] = ""
c.queryIn[levelName] = ""
c.metrics[levelName] = []string{}
// Save metrics to collect for this level
c.metrics[levelName] = append(c.metrics[levelName], metrics...)
// -------------------------------------------------------------------------
// Manual source
// -------------------------------------------------------------------------
// If user specified a method, use only that method, whether it works or not
if src, ok := options[OPT_SOURCE]; ok {
if len(src) > 0 && src != "auto" {
switch src {
case SOURCE_SELECT:
return c.prepareSELECT(levelName)
case SOURCE_PFS:
return c.preparePFS(levelName)
case SOURCE_SHOW:
return c.prepareSHOW(levelName, false)
default:
return fmt.Errorf("invalid source: %s; valid values: auto, select, pfs, show", src)
}
}
}
if all, ok := options[OPT_ALL]; ok && strings.ToLower(all) == "yes" {
return c.prepareSHOW(levelName, true)
}
// -------------------------------------------------------------------------
// Auto source (default)
// -------------------------------------------------------------------------
var err error
if err = c.prepareSELECT(levelName); err == nil {
return nil
}
if err = c.preparePFS(levelName); err == nil {
return nil
}
if err = c.prepareSHOW(levelName, false); err == nil {
return nil
}
return fmt.Errorf("auto source failed, last error: %s", err)
}
func (c *Global) prepareSELECT(levelName string) error {
var globalMetrics = make([]string, len(c.metrics[levelName]))
for i, str := range c.metrics[levelName] {
globalMetrics[i] = fmt.Sprintf("@@GLOBAL.%s", str)
}
globalMetricString := strings.Join(globalMetrics, ", ")
c.queryIn[levelName] = fmt.Sprintf("SELECT CONCAT_WS(',', %s) v", globalMetricString)
c.sourceIn[levelName] = SOURCE_SELECT
// Try collecting, discard metrics
_, err := c.collectSELECT(context.TODO(), levelName)
return err
}
func (c *Global) preparePFS(levelName string) error {
var metricString string
metricString = strings.Join(c.metrics[levelName], "', '")
query := fmt.Sprintf("SELECT variable_name, variable_value from performance_schema.global_variables WHERE variable_name in ('%s')",
metricString,
)
c.queryIn[levelName] = query
c.sourceIn[levelName] = SOURCE_PFS
// Try collecting, discard metrics
_, err := c.collectRows(context.TODO(), levelName)
return err
}
func (c *Global) prepareSHOW(levelName string, all bool) error {
var query string
if all {
query = "SHOW GLOBAL VARIABLES"
} else {
metricString := strings.Join(c.metrics[levelName], "', '")
query = fmt.Sprintf("SHOW GLOBAL VARIABLES WHERE variable_name in ('%s')", metricString)
}
c.queryIn[levelName] = query
c.sourceIn[levelName] = SOURCE_SHOW
// Try collecting, discard metrics
_, err := c.collectRows(context.TODO(), levelName)
return err
}
// --------------------------------------------------------------------------
func (c *Global) collectSELECT(ctx context.Context, levelName string) ([]blip.MetricValue, error) {
rows, err := c.db.QueryContext(ctx, c.queryIn[levelName])
if err != nil {
return nil, err
}
defer rows.Close()
metrics := make([]blip.MetricValue, len(c.metrics[levelName]))
for rows.Next() {
var val string
if err := rows.Scan(&val); err != nil {
return nil, err
}
values := strings.Split(val, ",")
for i, metric := range c.metrics[levelName] {
// Many sysvars are not numbers or convertible to numbers--that's ok.
// Ignore anything we can't convert, which is industry standard practice.
f, ok := sqlutil.Float64(values[i])
if !ok {
continue
}
metrics[i] = blip.MetricValue{
Name: metric,
Value: f,
Type: blip.GAUGE,
}
}
}
return metrics, nil
}
// Since both `show` and `pfs` queries return results in same format (ie; 2 columns, name and value)
// use the same logic for querying and retrieving metrics from the results
func (c *Global) collectRows(ctx context.Context, levelName string) ([]blip.MetricValue, error) {
rows, err := c.db.QueryContext(ctx, c.queryIn[levelName])
if err != nil {
return nil, err
}
defer rows.Close()
metrics := []blip.MetricValue{}
var val string
var ok bool
for rows.Next() {
m := blip.MetricValue{Type: blip.GAUGE}
if err = rows.Scan(&m.Name, &val); err != nil {
return nil, err
}
// Many sysvars are not numbers or convertible to numbers--that's ok.
// Ignore anything we can't convert, which is industry standard practice.
m.Value, ok = sqlutil.Float64(val)
if !ok {
continue
}
metrics = append(metrics, m)
}
return metrics, err
}