Debian Codegraph @

exporter/clients.go

raw symbols SCIP
1 package exporter
2
3 import (
4 "regexp"
5 "strconv"
6 "strings"
7 "time"
8
9 "github.com/gomodule/redigo/redis"
10 "github.com/prometheus/client_golang/prometheus"
11 log "github.com/sirupsen/logrus"
12 )
13
14 var reClientListId = regexp.MustCompile(`^id=\d+ addr=\S+`)
15
16 type ClientInfo struct {
17 Id,
18 Name,
19 User,
20 Flags,
21 Db,
22 Host,
23 Port,
24 Resp string
25 CreatedAt,
26 IdleSince,
27 Sub,
28 Psub,
29 Ssub,
30 Watch,
31 Qbuf,
32 QbufFree,
33 Obl,
34 Oll,
35 OMem,
36 TotMem int64
37 }
38
39 /*
40 Valid Examples
41 id=11 addr=127.0.0.1:63508 fd=8 name= age=6321 idle=6320 flags=N db=0 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=0 obl=0 oll=0 omem=0 events=r cmd=setex user=default resp=2
42 id=14 addr=127.0.0.1:64958 fd=9 name= age=5 idle=0 flags=N db=0 sub=0 psub=0 multi=-1 qbuf=26 qbuf-free=32742 obl=0 oll=0 omem=0 events=r cmd=client user=default resp=3
43 id=40253233 addr=fd40:1481:21:dbe0:7021:300:a03:1a06:44426 fd=19 name= age=782 idle=0 flags=N db=0 sub=0 psub=0 multi=-1 qbuf=26 qbuf-free=32742 argv-mem=10 obl=0 oll=0 omem=0 tot-mem=61466 ow=0 owmem=0 events=r cmd=client user=default lib-name=redis-py lib-ver=5.0.1 numops=9
44 */
45 func parseClientListString(clientInfo string) (*ClientInfo, bool) {
46 if !reClientListId.MatchString(clientInfo) {
47 return nil, false
48 }
49 connectedClient := ClientInfo{}
50 connectedClient.Ssub = -1 // mark it as missing - introduced in Redis 7.0.3
51 connectedClient.Watch = -1 // mark it as missing - introduced in Redis 7.4
52 for kvPart := range strings.SplitSeq(clientInfo, " ") {
53 vPart := strings.Split(kvPart, "=")
54 if len(vPart) != 2 {
55 log.Debugf("Invalid format for client list string, got: %s", kvPart)
56 return nil, false
57 }
58
59 switch vPart[0] {
60 case "id":
61 connectedClient.Id = vPart[1]
62 case "name":
63 connectedClient.Name = vPart[1]
64 case "user":
65 connectedClient.User = vPart[1]
66 case "age":
67 createdAt, err := durationFieldToTimestamp(vPart[1])
68 if err != nil {
69 log.Debugf("could not parse 'age' field(%s): %s", vPart[1], err.Error())
70 return nil, false
71 }
72 connectedClient.CreatedAt = createdAt
73 case "idle":
74 idleSinceTs, err := durationFieldToTimestamp(vPart[1])
75 if err != nil {
76 log.Debugf("could not parse 'idle' field(%s): %s", vPart[1], err.Error())
77 return nil, false
78 }
79 connectedClient.IdleSince = idleSinceTs
80 case "flags":
81 connectedClient.Flags = vPart[1]
82 case "db":
83 connectedClient.Db = vPart[1]
84 case "sub":
85 connectedClient.Sub, _ = strconv.ParseInt(vPart[1], 10, 64)
86 case "psub":
87 connectedClient.Psub, _ = strconv.ParseInt(vPart[1], 10, 64)
88 case "ssub":
89 connectedClient.Ssub, _ = strconv.ParseInt(vPart[1], 10, 64)
90 case "watch":
91 connectedClient.Watch, _ = strconv.ParseInt(vPart[1], 10, 64)
92 case "qbuf":
93 connectedClient.Qbuf, _ = strconv.ParseInt(vPart[1], 10, 64)
94 case "qbuf-free":
95 connectedClient.QbufFree, _ = strconv.ParseInt(vPart[1], 10, 64)
96 case "obl":
97 connectedClient.Obl, _ = strconv.ParseInt(vPart[1], 10, 64)
98 case "oll":
99 connectedClient.Oll, _ = strconv.ParseInt(vPart[1], 10, 64)
100 case "omem":
101 connectedClient.OMem, _ = strconv.ParseInt(vPart[1], 10, 64)
102 case "tot-mem":
103 connectedClient.TotMem, _ = strconv.ParseInt(vPart[1], 10, 64)
104 case "addr":
105 hostPortString := strings.Split(vPart[1], ":")
106 if len(hostPortString) < 2 {
107 log.Debug("Invalid value for 'addr' found in client info")
108 return nil, false
109 }
110 connectedClient.Host = strings.Join(hostPortString[:len(hostPortString)-1], ":")
111 connectedClient.Port = hostPortString[len(hostPortString)-1]
112 case "resp":
113 connectedClient.Resp = vPart[1]
114 }
115 }
116
117 return &connectedClient, true
118 }
119
120 func durationFieldToTimestamp(field string) (int64, error) {
121 parsed, err := strconv.ParseInt(field, 10, 64)
122 if err != nil {
123 return 0, err
124 }
125 return time.Now().Unix() - parsed, nil
126 }
127
128 func (e *Exporter) extractConnectedClientMetrics(ch chan<- prometheus.Metric, c redis.Conn) {
129 reply, err := redis.String(doRedisCmd(c, "CLIENT", "LIST"))
130 if err != nil {
131 log.Errorf("CLIENT LIST err: %s", err)
132 return
133 }
134 e.parseConnectedClientMetrics(reply, ch)
135 }
136
137 func (e *Exporter) parseConnectedClientMetrics(input string, ch chan<- prometheus.Metric) {
138
139 for s := range strings.SplitSeq(input, "\n") {
140 info, ok := parseClientListString(s)
141 if !ok {
142 log.Debugf("parseClientListString( %s ) - couldn';t parse input", s)
143 continue
144 }
145 clientInfoLabels := []string{"id", "name", "flags", "db", "host"}
146 clientInfoLabelValues := []string{info.Id, info.Name, info.Flags, info.Db, info.Host}
147
148 if e.options.ExportClientsInclPort {
149 clientInfoLabels = append(clientInfoLabels, "port")
150 clientInfoLabelValues = append(clientInfoLabelValues, info.Port)
151 }
152
153 if user := info.User; user != "" {
154 clientInfoLabels = append(clientInfoLabels, "user")
155 clientInfoLabelValues = append(clientInfoLabelValues, user)
156 }
157
158 // introduced in Redis 7.0
159 if resp := info.Resp; resp != "" {
160 clientInfoLabels = append(clientInfoLabels, "resp")
161 clientInfoLabelValues = append(clientInfoLabelValues, resp)
162 }
163
164 e.createMetricDescription("connected_client_info", clientInfoLabels)
165 e.registerConstMetricGauge(
166 ch, "connected_client_info", 1.0,
167 clientInfoLabelValues...,
168 )
169
170 clientBaseLabels := []string{"id", "name"}
171 clientBaseLabelsValues := []string{info.Id, info.Name}
172
173 for _, metricName := range []string{
174 "connected_client_output_buffer_memory_usage_bytes",
175 "connected_client_total_memory_consumed_bytes",
176 "connected_client_created_at_timestamp",
177 "connected_client_idle_since_timestamp",
178 "connected_client_channel_subscriptions_count",
179 "connected_client_pattern_matching_subscriptions_count",
180 "connected_client_query_buffer_length_bytes",
181 "connected_client_query_buffer_free_space_bytes",
182 "connected_client_output_buffer_length_bytes",
183 "connected_client_output_list_length",
184 } {
185 e.createMetricDescription(metricName, clientBaseLabels)
186 }
187
188 e.registerConstMetricGauge(
189 ch, "connected_client_output_buffer_memory_usage_bytes", float64(info.OMem),
190 clientBaseLabelsValues...,
191 )
192
193 e.registerConstMetricGauge(
194 ch, "connected_client_total_memory_consumed_bytes", float64(info.TotMem),
195 clientBaseLabelsValues...,
196 )
197
198 e.registerConstMetricGauge(
199 ch, "connected_client_created_at_timestamp", float64(info.CreatedAt),
200 clientBaseLabelsValues...,
201 )
202
203 e.registerConstMetricGauge(
204 ch, "connected_client_idle_since_timestamp", float64(info.IdleSince),
205 clientBaseLabelsValues...,
206 )
207
208 e.registerConstMetricGauge(
209 ch, "connected_client_channel_subscriptions_count", float64(info.Sub),
210 clientBaseLabelsValues...,
211 )
212
213 e.registerConstMetricGauge(
214 ch, "connected_client_pattern_matching_subscriptions_count", float64(info.Psub),
215 clientBaseLabelsValues...,
216 )
217
218 e.registerConstMetricGauge(
219 ch, "connected_client_query_buffer_length_bytes", float64(info.Qbuf),
220 clientBaseLabelsValues...,
221 )
222
223 e.registerConstMetricGauge(
224 ch, "connected_client_query_buffer_free_space_bytes", float64(info.QbufFree),
225 clientBaseLabelsValues...,
226 )
227
228 e.registerConstMetricGauge(
229 ch, "connected_client_output_buffer_length_bytes", float64(info.Obl),
230 clientBaseLabelsValues...,
231 )
232
233 e.registerConstMetricGauge(
234 ch, "connected_client_output_list_length", float64(info.Oll),
235 clientBaseLabelsValues...,
236 )
237
238 if info.Ssub != -1 {
239 e.createMetricDescription("connected_client_shard_channel_subscriptions_count", clientBaseLabels)
240 e.registerConstMetricGauge(
241 ch, "connected_client_shard_channel_subscriptions_count", float64(info.Ssub),
242 clientBaseLabelsValues...,
243 )
244 }
245 if info.Watch != -1 {
246 e.createMetricDescription("connected_client_shard_channel_watched_keys", clientBaseLabels)
247 e.registerConstMetricGauge(
248 ch, "connected_client_shard_channel_watched_keys", float64(info.Watch),
249 clientBaseLabelsValues...,
250 )
251 }
252 }
253 }