forked from justwatchcom/sql_exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquery.go
257 lines (237 loc) · 7.12 KB
/
query.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
package main
import (
"fmt"
"strconv"
"github.com/go-kit/kit/log"
"github.com/go-kit/kit/log/level"
"github.com/prometheus/client_golang/prometheus"
)
const (
metricTypeGauge = "gauge"
metricTypeHist = "histogram"
)
// Run executes a single Query on a single connection
func (q *Query) Run(conn *connection) error {
if q.log == nil {
q.log = log.NewNopLogger()
}
if q.desc == nil {
return fmt.Errorf("metrics descriptor is nil")
}
if q.Query == "" {
return fmt.Errorf("query is empty")
}
if conn == nil || conn.conn == nil {
return fmt.Errorf("db connection not initialized (should not happen)")
}
// execute query
rows, err := conn.conn.Queryx(q.Query)
if err != nil {
failedScrapes.WithLabelValues(conn.driver, conn.host, conn.database, conn.user, q.jobName, q.Name).Set(1.0)
return err
}
defer rows.Close()
updated := 0
metrics := make([]prometheus.Metric, 0, len(q.metrics))
for rows.Next() {
res := make(map[string]interface{})
err := rows.MapScan(res)
if err != nil {
level.Error(q.log).Log("msg", "Failed to scan", "err", err, "host", conn.host, "db", conn.database)
failedScrapes.WithLabelValues(conn.driver, conn.host, conn.database, conn.user, q.jobName, q.Name).Set(1.0)
continue
}
var m []prometheus.Metric
switch q.Type {
case metricTypeGauge:
m, err = q.updateConstMetrics(conn, res)
case metricTypeHist:
m, err = q.updateHistMetrics(conn, res)
default:
// backward compatible: default to const gauge metric
m, err = q.updateConstMetrics(conn, res)
}
if err != nil {
level.Error(q.log).Log("msg", "Failed to update metrics", "err", err, "host", conn.host, "db", conn.database)
failedScrapes.WithLabelValues(conn.driver, conn.host, conn.database, conn.user, q.jobName, q.Name).Set(1.0)
continue
}
metrics = append(metrics, m...)
updated++
failedScrapes.WithLabelValues(conn.driver, conn.host, conn.database, conn.user, q.jobName, q.Name).Set(0.0)
}
if updated < 1 {
return fmt.Errorf("zero rows returned")
}
// update the metrics cache
q.Lock()
q.metrics[conn] = metrics
q.Unlock()
return nil
}
// updateConstMetrics parses the result set and returns a slice of const metrics.
func (q *Query) updateConstMetrics(conn *connection, res map[string]interface{}) ([]prometheus.Metric, error) {
updated := 0
metrics := make([]prometheus.Metric, 0, len(q.Values))
for _, valueName := range q.Values {
m, err := q.updateConstMetric(conn, res, valueName)
if err != nil {
level.Error(q.log).Log(
"msg", "Failed to update metric",
"value", valueName,
"err", err,
"host", conn.host,
"db", conn.database,
)
continue
}
metrics = append(metrics, m)
updated++
}
if updated < 1 {
return nil, fmt.Errorf("zero values found")
}
return metrics, nil
}
// updateHistMetrics parses the result set and returns a slice of histogram metrics.
func (q *Query) updateHistMetrics(conn *connection, res map[string]interface{}) ([]prometheus.Metric, error) {
updated := 0
metrics := make([]prometheus.Metric, 0, len(q.Values))
for _, histValue := range q.HistValues {
m, err := q.updateHistogramMetric(conn, res, histValue)
if err != nil {
level.Error(q.log).Log(
"msg", "Failed to update metric",
"value", histValue.Name,
"err", err,
"host", conn.host,
"db", conn.database,
)
continue
}
metrics = append(metrics, m)
updated++
}
if updated < 1 {
return nil, fmt.Errorf("zero values found")
}
return metrics, nil
}
func parseValue(res map[string]interface{}, valueName string) (float64, error) {
var value float64
if i, ok := res[valueName]; ok {
switch f := i.(type) {
case int:
value = float64(f)
case int32:
value = float64(f)
case int64:
value = float64(f)
case uint:
value = float64(f)
case uint32:
value = float64(f)
case uint64:
value = float64(f)
case float32:
value = float64(f)
case float64:
value = float64(f)
case []uint8:
val, err := strconv.ParseFloat(string(f), 64)
if err != nil {
return 0.0, fmt.Errorf("Column '%s' must be type float, is '%T' (val: %s)", valueName, i, f)
}
value = val
case string:
val, err := strconv.ParseFloat(f, 64)
if err != nil {
return 0.0, fmt.Errorf("Column '%s' must be type float, is '%T' (val: %s)", valueName, i, f)
}
value = val
default:
return 0.0, fmt.Errorf("Column '%s' must be type float, is '%T' (val: %s)", valueName, i, f)
}
}
return value, nil
}
func buildLabels(conn *connection, res map[string]interface{}, valueName string, inLabels []string) ([]string, error) {
// make space for all defined variable label columns and the "static" labels
// added below
labels := make([]string, 0, len(inLabels)+5)
for _, label := range inLabels {
// we need to fill every spot in the slice or the key->value mapping
// won't match up in the end.
//
// ORDER MATTERS!
lv := ""
if i, ok := res[label]; ok {
switch str := i.(type) {
case string:
lv = str
case []uint8:
lv = string(str)
default:
return nil, fmt.Errorf("Column '%s' must be type text (string)", label)
}
}
labels = append(labels, lv)
}
labels = append(labels, conn.driver)
labels = append(labels, conn.host)
labels = append(labels, conn.database)
labels = append(labels, conn.user)
labels = append(labels, valueName)
return labels, nil
}
// updateMetrics parses a single row and returns a const metric.
func (q *Query) updateConstMetric(conn *connection, res map[string]interface{}, valueName string) (prometheus.Metric, error) {
// parse value from result
value, err := parseValue(res, valueName)
if err != nil {
return nil, err
}
// build user defined labels along with pre-defined "static" labels
labels, err := buildLabels(conn, res, valueName, q.Labels)
if err != nil {
return nil, err
}
// create a new immutable const metric that can be cached and returned on
// every scrape. Remember that the order of the lable values in the labels
// slice must match the order of the label names in the descriptor!
return prometheus.NewConstMetric(q.desc, prometheus.GaugeValue, value, labels...)
}
// updateHistogramMetric parses rows to return a histogram metric.
func (q *Query) updateHistogramMetric(conn *connection, res map[string]interface{}, histValue *HistValue) (prometheus.Metric, error) {
// parse hist count
countValue, err := parseValue(res, histValue.Count)
if err != nil {
return nil, err
}
// parse hist sum
sumVal, err := parseValue(res, histValue.Sum)
if err != nil {
return nil, err
}
// parse hist buckets
bucketVals := make(map[float64]uint64, len(histValue.Buckets))
for _, bucket := range histValue.Buckets {
b, err := strconv.ParseFloat(bucket.Value, 64)
if err != nil {
return nil, err
}
bVal, err := parseValue(res, bucket.Name)
if err != nil {
return nil, err
}
bucketVals[b] = uint64(bVal)
}
// build user defined labels along with pre-defined "static" labels
labels, err := buildLabels(conn, res, histValue.Name, q.Labels)
if err != nil {
return nil, err
}
// create a new immutable const histogram that can be cached and returned on
// every scrape
return prometheus.NewConstHistogram(q.desc, uint64(countValue), sumVal, bucketVals, labels...)
}