| 1 | package exporter |
| 2 | |
| 3 | import ( |
| 4 | "strconv" |
| 5 | "strings" |
| 6 | |
| 7 | "github.com/gomodule/redigo/redis" |
| 8 | "github.com/prometheus/client_golang/prometheus" |
| 9 | log "github.com/sirupsen/logrus" |
| 10 | ) |
| 11 | |
| 12 | // All fields of the streamInfo struct must be exported |
| 13 | // because of redis.ScanStruct (reflect) limitations |
| 14 | type streamInfo struct { |
| 15 | Length int64 `redis:"length"` |
| 16 | RadixTreeKeys int64 `redis:"radix-tree-keys"` |
| 17 | RadixTreeNodes int64 `redis:"radix-tree-nodes"` |
| 18 | LastGeneratedId string `redis:"last-generated-id"` |
| 19 | Groups int64 `redis:"groups"` |
| 20 | MaxDeletedEntryId string `redis:"max-deleted-entry-id"` |
| 21 | FirstEntryId string |
| 22 | LastEntryId string |
| 23 | StreamGroupsInfo []streamGroupsInfo |
| 24 | } |
| 25 | |
| 26 | type streamGroupsInfo struct { |
| 27 | Name string `redis:"name"` |
| 28 | Consumers int64 `redis:"consumers"` |
| 29 | Pending int64 `redis:"pending"` |
| 30 | LastDeliveredId string `redis:"last-delivered-id"` |
| 31 | EntriesRead int64 `redis:"entries-read"` |
| 32 | Lag int64 `redis:"lag"` |
| 33 | StreamGroupConsumersInfo []streamGroupConsumersInfo |
| 34 | } |
| 35 | |
| 36 | type streamGroupConsumersInfo struct { |
| 37 | Name string `redis:"name"` |
| 38 | Pending int64 `redis:"pending"` |
| 39 | Idle int64 `redis:"idle"` |
| 40 | } |
| 41 | |
| 42 | func getStreamInfo(c redis.Conn, key string) (*streamInfo, error) { |
| 43 | values, err := redis.Values(doRedisCmd(c, "XINFO", "STREAM", key)) |
| 44 | if err != nil { |
| 45 | return nil, err |
| 46 | } |
| 47 | |
| 48 | // Scan slice to struct |
| 49 | var stream streamInfo |
| 50 | if err := redis.ScanStruct(values, &stream); err != nil { |
| 51 | return nil, err |
| 52 | } |
| 53 | |
| 54 | // Extract first and last id from slice |
| 55 | for idx, v := range values { |
| 56 | vbytes, ok := v.([]byte) |
| 57 | if !ok { |
| 58 | continue |
| 59 | } |
| 60 | if string(vbytes) == "first-entry" { |
| 61 | stream.FirstEntryId = getStreamEntryId(values, idx+1) |
| 62 | } |
| 63 | if string(vbytes) == "last-entry" { |
| 64 | stream.LastEntryId = getStreamEntryId(values, idx+1) |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | stream.StreamGroupsInfo, err = scanStreamGroups(c, key) |
| 69 | if err != nil { |
| 70 | return nil, err |
| 71 | } |
| 72 | |
| 73 | log.Debugf("getStreamInfo() stream: %#v", &stream) |
| 74 | return &stream, nil |
| 75 | } |
| 76 | |
| 77 | func getStreamEntryId(redisValue []any, index int) string { |
| 78 | if index >= len(redisValue) || redisValue[index] == nil { |
| 79 | log.Debugf("Failed to parse StreamEntryId") |
| 80 | return "" |
| 81 | } |
| 82 | |
| 83 | values, ok := redisValue[index].([]any) |
| 84 | if !ok || len(values) < 1 { |
| 85 | log.Debugf("Failed to parse StreamEntryId") |
| 86 | return "" |
| 87 | } |
| 88 | |
| 89 | entryId, ok := values[0].([]byte) |
| 90 | if !ok { |
| 91 | log.Debugf("Failed to parse StreamEntryId") |
| 92 | return "" |
| 93 | } |
| 94 | return string(entryId) |
| 95 | } |
| 96 | |
| 97 | func scanStreamGroups(c redis.Conn, stream string) ([]streamGroupsInfo, error) { |
| 98 | groups, err := redis.Values(doRedisCmd(c, "XINFO", "GROUPS", stream)) |
| 99 | if err != nil { |
| 100 | return nil, err |
| 101 | } |
| 102 | |
| 103 | var result []streamGroupsInfo |
| 104 | for _, g := range groups { |
| 105 | v, err := redis.Values(g, nil) |
| 106 | if err != nil { |
| 107 | log.Errorf("Couldn't convert group values for stream '%s': %s", stream, err) |
| 108 | continue |
| 109 | } |
| 110 | log.Debugf("streamGroupsInfo value: %#v", v) |
| 111 | |
| 112 | var group streamGroupsInfo |
| 113 | if err := redis.ScanStruct(v, &group); err != nil { |
| 114 | log.Errorf("Couldn't scan group in stream '%s': %s", stream, err) |
| 115 | continue |
| 116 | } |
| 117 | |
| 118 | group.StreamGroupConsumersInfo, err = scanStreamGroupConsumers(c, stream, group.Name) |
| 119 | if err != nil { |
| 120 | return nil, err |
| 121 | } |
| 122 | |
| 123 | result = append(result, group) |
| 124 | } |
| 125 | |
| 126 | log.Debugf("groups: %v", result) |
| 127 | return result, nil |
| 128 | } |
| 129 | |
| 130 | func scanStreamGroupConsumers(c redis.Conn, stream string, group string) ([]streamGroupConsumersInfo, error) { |
| 131 | consumers, err := redis.Values(doRedisCmd(c, "XINFO", "CONSUMERS", stream, group)) |
| 132 | if err != nil { |
| 133 | return nil, err |
| 134 | } |
| 135 | |
| 136 | var result []streamGroupConsumersInfo |
| 137 | for _, c := range consumers { |
| 138 | |
| 139 | v, err := redis.Values(c, nil) |
| 140 | if err != nil { |
| 141 | log.Errorf("Couldn't convert consumer values for group '%s' in stream '%s': %s", group, stream, err) |
| 142 | continue |
| 143 | } |
| 144 | log.Debugf("streamGroupConsumersInfo value: %#v", v) |
| 145 | |
| 146 | var consumer streamGroupConsumersInfo |
| 147 | if err := redis.ScanStruct(v, &consumer); err != nil { |
| 148 | log.Errorf("Couldn't scan consumers for group '%s' in stream '%s': %s", group, stream, err) |
| 149 | continue |
| 150 | } |
| 151 | |
| 152 | result = append(result, consumer) |
| 153 | } |
| 154 | |
| 155 | log.Debugf("consumers: %v", result) |
| 156 | return result, nil |
| 157 | } |
| 158 | |
| 159 | func parseStreamItemId(id string) float64 { |
| 160 | if strings.TrimSpace(id) == "" { |
| 161 | return 0 |
| 162 | } |
| 163 | frags := strings.Split(id, "-") |
| 164 | if len(frags) == 0 { |
| 165 | log.Errorf("Couldn't parse StreamItemId: %s", id) |
| 166 | return 0 |
| 167 | } |
| 168 | parsedId, err := strconv.ParseFloat(strings.Split(id, "-")[0], 64) |
| 169 | if err != nil { |
| 170 | log.Errorf("Couldn't parse given StreamItemId: [%s] err: %s", id, err) |
| 171 | } |
| 172 | return parsedId |
| 173 | } |
| 174 | |
| 175 | func (e *Exporter) extractStreamMetrics(ch chan<- prometheus.Metric, c redis.Conn) { |
| 176 | streams, err := parseKeyArg(e.options.CheckStreams) |
| 177 | if err != nil { |
| 178 | log.Errorf("Couldn't parse given stream keys: %s", err) |
| 179 | return |
| 180 | } |
| 181 | |
| 182 | singleStreams, err := parseKeyArg(e.options.CheckSingleStreams) |
| 183 | if err != nil { |
| 184 | log.Errorf("Couldn't parse check-single-streams: %s", err) |
| 185 | return |
| 186 | } |
| 187 | allStreams := append([]dbKeyPair{}, singleStreams...) |
| 188 | |
| 189 | scannedStreams, err := getKeysFromPatterns(c, streams, e.options.CheckKeysBatchSize) |
| 190 | if err != nil { |
| 191 | log.Errorf("Error expanding key patterns: %s", err) |
| 192 | } else { |
| 193 | allStreams = append(allStreams, scannedStreams...) |
| 194 | } |
| 195 | |
| 196 | log.Debugf("allStreams: %#v", allStreams) |
| 197 | for _, k := range allStreams { |
| 198 | if _, err := doRedisCmd(c, "SELECT", k.db); err != nil { |
| 199 | log.Debugf("Couldn't select database '%s' when getting stream info", k.db) |
| 200 | continue |
| 201 | } |
| 202 | info, err := getStreamInfo(c, k.key) |
| 203 | if err != nil { |
| 204 | log.Errorf("couldn't get info for stream '%s', err: %s", k.key, err) |
| 205 | continue |
| 206 | } |
| 207 | dbLabel := "db" + k.db |
| 208 | |
| 209 | e.registerConstMetricGauge(ch, "stream_length", float64(info.Length), dbLabel, k.key) |
| 210 | e.registerConstMetricGauge(ch, "stream_radix_tree_keys", float64(info.RadixTreeKeys), dbLabel, k.key) |
| 211 | e.registerConstMetricGauge(ch, "stream_radix_tree_nodes", float64(info.RadixTreeNodes), dbLabel, k.key) |
| 212 | e.registerConstMetricGauge(ch, "stream_last_generated_id", parseStreamItemId(info.LastGeneratedId), dbLabel, k.key) |
| 213 | e.registerConstMetricGauge(ch, "stream_groups", float64(info.Groups), dbLabel, k.key) |
| 214 | e.registerConstMetricGauge(ch, "stream_max_deleted_entry_id", parseStreamItemId(info.MaxDeletedEntryId), dbLabel, k.key) |
| 215 | e.registerConstMetricGauge(ch, "stream_first_entry_id", parseStreamItemId(info.FirstEntryId), dbLabel, k.key) |
| 216 | e.registerConstMetricGauge(ch, "stream_last_entry_id", parseStreamItemId(info.LastEntryId), dbLabel, k.key) |
| 217 | |
| 218 | for _, g := range info.StreamGroupsInfo { |
| 219 | e.registerConstMetricGauge(ch, "stream_group_consumers", float64(g.Consumers), dbLabel, k.key, g.Name) |
| 220 | e.registerConstMetricGauge(ch, "stream_group_messages_pending", float64(g.Pending), dbLabel, k.key, g.Name) |
| 221 | e.registerConstMetricGauge(ch, "stream_group_last_delivered_id", parseStreamItemId(g.LastDeliveredId), dbLabel, k.key, g.Name) |
| 222 | e.registerConstMetricGauge(ch, "stream_group_entries_read", float64(g.EntriesRead), dbLabel, k.key, g.Name) |
| 223 | e.registerConstMetricGauge(ch, "stream_group_lag", float64(g.Lag), dbLabel, k.key, g.Name) |
| 224 | if !e.options.StreamsExcludeConsumerMetrics { |
| 225 | for _, c := range g.StreamGroupConsumersInfo { |
| 226 | e.registerConstMetricGauge(ch, "stream_group_consumer_messages_pending", float64(c.Pending), dbLabel, k.key, g.Name, c.Name) |
| 227 | e.registerConstMetricGauge(ch, "stream_group_consumer_idle_seconds", float64(c.Idle)/1e3, dbLabel, k.key, g.Name, c.Name) |
| 228 | } |
| 229 | } |
| 230 | } |
| 231 | } |
| 232 | } |