| 1 | package exporter |
| 2 | |
| 3 | import ( |
| 4 | "regexp" |
| 5 | "strconv" |
| 6 | "strings" |
| 7 | "sync" |
| 8 | |
| 9 | "github.com/gomodule/redigo/redis" |
| 10 | "github.com/prometheus/client_golang/prometheus" |
| 11 | log "github.com/sirupsen/logrus" |
| 12 | ) |
| 13 | |
| 14 | var ( |
| 15 | logLatestErrOnce, logHistogramErrOnce sync.Once |
| 16 | |
| 17 | extractUsecRegexp = regexp.MustCompile(`(?m)^cmdstat_([a-zA-Z0-9\|]+):.*usec=([0-9]+).*$`) |
| 18 | ) |
| 19 | |
| 20 | func (e *Exporter) extractLatencyMetrics(ch chan<- prometheus.Metric, infoAll string, c redis.Conn) { |
| 21 | e.extractLatencyLatestMetrics(ch, c) |
| 22 | e.extractLatencyHistogramMetrics(ch, infoAll, c) |
| 23 | } |
| 24 | |
| 25 | func (e *Exporter) extractLatencyLatestMetrics(outChan chan<- prometheus.Metric, redisConn redis.Conn) { |
| 26 | reply, err := redis.Values(doRedisCmd(redisConn, "LATENCY", "LATEST")) |
| 27 | if err != nil { |
| 28 | /* |
| 29 | this can be a little too verbose, see e.g. https://github.com/oliver006/redis_exporter/issues/495 |
| 30 | we're logging this only once as an Error and always as Debugf() |
| 31 | */ |
| 32 | logLatestErrOnce.Do(func() { |
| 33 | log.Errorf("WARNING, LOGGED ONCE ONLY: cmd LATENCY LATEST, err: %s", err) |
| 34 | }) |
| 35 | log.Debugf("cmd LATENCY LATEST, err: %s", err) |
| 36 | return |
| 37 | } |
| 38 | |
| 39 | for _, l := range reply { |
| 40 | if latencyResult, err := redis.Values(l, nil); err == nil { |
| 41 | var eventName string |
| 42 | var spikeLast, spikeDuration, maxLatency int64 |
| 43 | if _, err := redis.Scan(latencyResult, &eventName, &spikeLast, &spikeDuration, &maxLatency); err == nil { |
| 44 | spikeDurationSeconds := float64(spikeDuration) / 1e3 |
| 45 | e.registerConstMetricGauge(outChan, "latency_spike_last", float64(spikeLast), eventName) |
| 46 | e.registerConstMetricGauge(outChan, "latency_spike_duration_seconds", spikeDurationSeconds, eventName) |
| 47 | } |
| 48 | } |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | /* |
| 53 | https://redis.io/docs/latest/commands/latency-histogram/ |
| 54 | */ |
| 55 | func (e *Exporter) extractLatencyHistogramMetrics(outChan chan<- prometheus.Metric, infoAll string, redisConn redis.Conn) { |
| 56 | reply, err := redis.Values(doRedisCmd(redisConn, "LATENCY", "HISTOGRAM")) |
| 57 | if err != nil { |
| 58 | logHistogramErrOnce.Do(func() { |
| 59 | log.Errorf("WARNING, LOGGED ONCE ONLY: cmd LATENCY HISTOGRAM, err: %s", err) |
| 60 | }) |
| 61 | log.Debugf("cmd LATENCY HISTOGRAM, err: %s", err) |
| 62 | return |
| 63 | } |
| 64 | |
| 65 | for i := 0; i+1 < len(reply); i += 2 { |
| 66 | cmd, _ := redis.String(reply[i], nil) |
| 67 | details, _ := redis.Values(reply[i+1], nil) |
| 68 | |
| 69 | var totalCalls uint64 |
| 70 | var bucketInfo []uint64 |
| 71 | |
| 72 | if _, err := redis.Scan(details, nil, &totalCalls, nil, &bucketInfo); err != nil { |
| 73 | break |
| 74 | } |
| 75 | |
| 76 | buckets := map[float64]uint64{} |
| 77 | |
| 78 | for j := 0; j+1 < len(bucketInfo); j += 2 { |
| 79 | usec := float64(bucketInfo[j]) |
| 80 | count := bucketInfo[j+1] |
| 81 | buckets[usec] = count |
| 82 | } |
| 83 | |
| 84 | totalUsecs := extractTotalUsecForCommand(infoAll, cmd) |
| 85 | |
| 86 | e.createMetricDescription("commands_latencies_usec", []string{"cmd"}) |
| 87 | e.registerConstHistogram(outChan, "commands_latencies_usec", totalCalls, float64(totalUsecs), buckets, cmd) |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | func extractTotalUsecForCommand(infoAll string, cmd string) uint64 { |
| 92 | total := uint64(0) |
| 93 | |
| 94 | matches := extractUsecRegexp.FindAllStringSubmatch(infoAll, -1) |
| 95 | for _, match := range matches { |
| 96 | if match[1] != cmd && !strings.HasPrefix(match[1], cmd+"|") { |
| 97 | continue |
| 98 | } |
| 99 | |
| 100 | usecs, err := strconv.ParseUint(match[2], 10, 0) |
| 101 | if err != nil { |
| 102 | log.Warnf("Unable to parse uint from string \"%s\": %v", match[2], err) |
| 103 | continue |
| 104 | } |
| 105 | |
| 106 | total += usecs |
| 107 | } |
| 108 | |
| 109 | return total |
| 110 | } |