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 282 283 284 285 286 287 288 289 290 291 292 293
|
package main
import (
"fmt"
"strconv"
"strings"
"time"
"github.com/go-kit/log"
"github.com/go-kit/log/level"
"github.com/prometheus/client_golang/prometheus"
)
// Run executes a single Query on a single connection
func (q *Query) Run(conn *connection) error {
if q.log == nil {
q.log = log.NewNopLogger()
}
queryCounter.WithLabelValues(q.jobName, q.Name).Inc()
if q.desc == nil {
failedQueryCounter.WithLabelValues(q.jobName, q.Name).Inc()
return fmt.Errorf("metrics descriptor is nil")
}
if q.Query == "" {
failedQueryCounter.WithLabelValues(q.jobName, q.Name).Inc()
return fmt.Errorf("query is empty")
}
if conn == nil || conn.conn == nil {
failedQueryCounter.WithLabelValues(q.jobName, q.Name).Inc()
return fmt.Errorf("db connection not initialized (should not happen)")
}
// execute query
now := time.Now()
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)
failedQueryCounter.WithLabelValues(q.jobName, q.Name).Inc()
return err
}
defer rows.Close()
duration := time.Since(now)
queryDurationHistogram.WithLabelValues(q.jobName, q.Name).Observe(duration.Seconds())
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
}
m, err := q.updateMetrics(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 {
if q.AllowZeroRows {
failedScrapes.WithLabelValues(conn.driver, conn.host, conn.database, conn.user, q.jobName, q.Name).Set(0.0)
} else {
failedScrapes.WithLabelValues(conn.driver, conn.host, conn.database, conn.user, q.jobName, q.Name).Set(1.0)
failedQueryCounter.WithLabelValues(q.jobName, q.Name).Inc()
return fmt.Errorf("zero rows returned")
}
}
// update the metrics cache
q.Lock()
q.metrics[conn] = metrics
q.Unlock()
return nil
}
// RunIterator runs the query for each iterator value on a single connection
func (q *Query) RunIterator(conn *connection, ph string, ivs []string, il string) error {
if q.log == nil {
q.log = log.NewNopLogger()
}
queryCounter.WithLabelValues(q.jobName, q.Name).Inc()
if q.desc == nil {
failedQueryCounter.WithLabelValues(q.jobName, q.Name).Inc()
return fmt.Errorf("metrics descriptor is nil")
}
if q.Query == "" {
failedQueryCounter.WithLabelValues(q.jobName, q.Name).Inc()
return fmt.Errorf("query is empty")
}
if conn == nil || conn.conn == nil {
failedQueryCounter.WithLabelValues(q.jobName, q.Name).Inc()
return fmt.Errorf("db connection not initialized (should not happen)")
}
// execute query for each iterator value
now := time.Now()
metrics := make([]prometheus.Metric, 0, len(q.metrics))
updated := 0
for _, iv := range ivs {
rows, err := conn.conn.Queryx(q.ReplaceIterator(ph, iv))
if err != nil {
failedScrapes.WithLabelValues(conn.driver, conn.host, conn.database, conn.user, q.jobName, q.Name).Set(1.0)
failedQueryCounter.WithLabelValues(q.jobName, q.Name).Inc()
return err
}
defer rows.Close()
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
}
m, err := q.updateMetrics(conn, res, iv, il)
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)
}
}
duration := time.Since(now)
queryDurationHistogram.WithLabelValues(q.jobName, q.Name).Observe(duration.Seconds())
if updated < 1 {
if q.AllowZeroRows {
failedScrapes.WithLabelValues(conn.driver, conn.host, conn.database, conn.user, q.jobName, q.Name).Set(0.0)
} else {
return fmt.Errorf("zero rows returned")
}
}
// update the metrics cache
q.Lock()
q.metrics[conn] = metrics
q.Unlock()
return nil
}
// HasIterator returns true if the query contains the given placeholder
func (q *Query) HasIterator(ph string) bool {
return strings.Contains(q.Query, ph)
}
// ReplaceIterator replaces a given placeholder with an iterator value and returns a new query
func (q *Query) ReplaceIterator(ph string, iv string) string {
iteratorReplacer := strings.NewReplacer(fmt.Sprint("{{", ph, "}}"), iv)
return iteratorReplacer.Replace(q.Query)
}
// updateMetrics parses the result set and returns a slice of const metrics
func (q *Query) updateMetrics(conn *connection, res map[string]interface{}, iv string, il string) ([]prometheus.Metric, error) {
// if no value were defined to be parsed, return immediately
if len(q.Values) == 0 {
level.Debug(q.log).Log("msg", "No values defined in configuration, skipping metric update")
return nil, nil
}
updated := 0
metrics := make([]prometheus.Metric, 0, len(q.Values))
for _, valueName := range q.Values {
m, err := q.updateMetric(conn, res, valueName, iv, il)
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
}
// updateMetrics parses a single row and returns a const metric
func (q *Query) updateMetric(conn *connection, res map[string]interface{}, valueName string, iv string, il string) (prometheus.Metric, 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 nil, 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 nil, fmt.Errorf("column '%s' must be type float, is '%T' (val: %s)", valueName, i, f)
}
value = val
default:
return nil, fmt.Errorf("column '%s' must be type float, is '%T' (val: %s)", valueName, i, f)
}
} else {
level.Warn(q.log).Log(
"msg", "Column not found in query result",
"column", valueName,
"resultColumns", res,
)
}
// make space for all defined variable label columns and the "static" labels
// added below
labels := make([]string, 0, len(q.Labels)+5)
for _, label := range q.Labels {
// append iterator value to the labels
if label == il && iv != "" {
labels = append(labels, iv)
continue
}
// 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)
// create a new immutable const metric that can be cached and returned on
// every scrape. Remember that the order of the label values in the labels
// slice must match the order of the label names in the descriptor!
metric, err := prometheus.NewConstMetric(
q.desc, prometheus.GaugeValue, value, labels...,
)
if err != nil {
return nil, err
}
if q.Timestamp != "" {
if tsRaw, ok := res[q.Timestamp]; ok {
switch ts := tsRaw.(type) {
case time.Time:
return prometheus.NewMetricWithTimestamp(ts, metric), nil
default:
level.Warn(q.log).Log(
"msg", "timestamp label %q is of type %T, expected time.Time",
"column", tsRaw,
)
}
}
}
return metric, nil
}
|