| 1 | package exporter |
| 2 | |
| 3 | import ( |
| 4 | "strconv" |
| 5 | |
| 6 | "github.com/gomodule/redigo/redis" |
| 7 | "github.com/prometheus/client_golang/prometheus" |
| 8 | log "github.com/sirupsen/logrus" |
| 9 | ) |
| 10 | |
| 11 | func (e *Exporter) extractLuaScriptMetrics(ch chan<- prometheus.Metric, c redis.Conn, filename string, script []byte) error { |
| 12 | luaCMD := "EVAL" |
| 13 | if e.options.LuaScriptReadOnly { |
| 14 | luaCMD = "EVAL_RO" |
| 15 | } |
| 16 | |
| 17 | log.Debugf("Evaluating e.options.LuaScript: %s, cmd: %s", filename, luaCMD) |
| 18 | kv, err := redis.StringMap(doRedisCmd(c, luaCMD, script, 0, 0)) |
| 19 | if err != nil { |
| 20 | log.Errorf("LuaScript error: %v", err) |
| 21 | e.registerConstMetricGauge(ch, "script_result", 0, filename) |
| 22 | return err |
| 23 | } |
| 24 | |
| 25 | if len(kv) == 0 { |
| 26 | log.Debugf("Lua script returned no results") |
| 27 | e.registerConstMetricGauge(ch, "script_result", 2, filename) |
| 28 | return nil |
| 29 | } |
| 30 | |
| 31 | for key, stringVal := range kv { |
| 32 | val, err := strconv.ParseFloat(stringVal, 64) |
| 33 | if err != nil { |
| 34 | log.Errorf("Error parsing lua script results, err: %s", err) |
| 35 | e.registerConstMetricGauge(ch, "script_result", 0, filename) |
| 36 | return err |
| 37 | } |
| 38 | e.registerConstMetricGauge(ch, "script_values", val, key, filename) |
| 39 | } |
| 40 | e.registerConstMetricGauge(ch, "script_result", 1, filename) |
| 41 | return nil |
| 42 | } |