| 1 | package exporter |
| 2 | |
| 3 | import ( |
| 4 | "os" |
| 5 | "strings" |
| 6 | "testing" |
| 7 | "time" |
| 8 | |
| 9 | "github.com/prometheus/client_golang/prometheus" |
| 10 | ) |
| 11 | |
| 12 | func TestDurationFieldToTimestamp(t *testing.T) { |
| 13 | nowTs := time.Now().Unix() |
| 14 | for _, tst := range []struct { |
| 15 | in string |
| 16 | expectedOk bool |
| 17 | expectedVal int64 |
| 18 | }{ |
| 19 | { |
| 20 | in: "123", |
| 21 | expectedOk: true, |
| 22 | expectedVal: nowTs - 123, |
| 23 | }, |
| 24 | { |
| 25 | in: "0", |
| 26 | expectedOk: true, |
| 27 | expectedVal: nowTs - 0, |
| 28 | }, |
| 29 | { |
| 30 | in: "abc", |
| 31 | expectedOk: false, |
| 32 | }, |
| 33 | } { |
| 34 | res, err := durationFieldToTimestamp(tst.in) |
| 35 | if err == nil && !tst.expectedOk { |
| 36 | t.Fatalf("expected not ok, but got no error, input: [%s]", tst.in) |
| 37 | } else if err != nil && tst.expectedOk { |
| 38 | t.Fatalf("expected ok, but got error: %s, input: [%s]", err, tst.in) |
| 39 | } |
| 40 | if tst.expectedOk { |
| 41 | if res != tst.expectedVal { |
| 42 | t.Fatalf("expected %d, but got: %d", tst.expectedVal, res) |
| 43 | } |
| 44 | } |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | func TestParseClientListString(t *testing.T) { |
| 49 | convertDurationToTimestampInt64 := func(duration string) int64 { |
| 50 | ts, err := durationFieldToTimestamp(duration) |
| 51 | if err != nil { |
| 52 | panic(err) |
| 53 | } |
| 54 | return ts |
| 55 | } |
| 56 | |
| 57 | tsts := []struct { |
| 58 | in string |
| 59 | expectedOk bool |
| 60 | expectedInfo ClientInfo |
| 61 | }{ |
| 62 | { |
| 63 | in: "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=3 oll=8 omem=0 tot-mem=0 events=r cmd=setex", |
| 64 | expectedOk: true, |
| 65 | expectedInfo: ClientInfo{Id: "11", CreatedAt: convertDurationToTimestampInt64("6321"), IdleSince: convertDurationToTimestampInt64("6320"), Flags: "N", Db: "0", Ssub: -1, Watch: -1, Obl: 3, Oll: 8, OMem: 0, TotMem: 0, Host: "127.0.0.1", Port: "63508"}, |
| 66 | }, { |
| 67 | in: "id=14 addr=127.0.0.1:64958 fd=9 name=foo age=5 idle=0 flags=N db=1 sub=0 psub=0 multi=-1 qbuf=26 qbuf-free=32742 obl=0 oll=0 omem=0 tot-mem=0 events=r cmd=client", |
| 68 | expectedOk: true, |
| 69 | expectedInfo: ClientInfo{Id: "14", Name: "foo", CreatedAt: convertDurationToTimestampInt64("5"), IdleSince: convertDurationToTimestampInt64("0"), Flags: "N", Db: "1", Ssub: -1, Watch: -1, Qbuf: 26, QbufFree: 32742, OMem: 0, TotMem: 0, Host: "127.0.0.1", Port: "64958"}, |
| 70 | }, { |
| 71 | in: "id=14 addr=127.0.0.1:64959 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 tot-mem=0 events=r cmd=client user=default resp=3", |
| 72 | expectedOk: true, |
| 73 | expectedInfo: ClientInfo{Id: "14", CreatedAt: convertDurationToTimestampInt64("5"), IdleSince: convertDurationToTimestampInt64("0"), Flags: "N", Db: "0", Ssub: -1, Watch: -1, Qbuf: 26, QbufFree: 32742, OMem: 0, TotMem: 0, Host: "127.0.0.1", Port: "64959", User: "default", Resp: "3"}, |
| 74 | }, { |
| 75 | in: "id=40253233 addr=fd40:1481:21:dbe0:7021:300:a03:1a06:44426 fd=19 name= age=782 idle=0 flags=N db=0 sub=896 psub=18 ssub=17 watch=3 multi=-1 qbuf=26 qbuf-free=32742 argv-mem=10 obl=0 oll=555 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", |
| 76 | expectedOk: true, |
| 77 | expectedInfo: ClientInfo{Id: "40253233", CreatedAt: convertDurationToTimestampInt64("782"), IdleSince: convertDurationToTimestampInt64("0"), Flags: "N", Db: "0", Sub: 896, Psub: 18, Ssub: 17, Watch: 3, Qbuf: 26, QbufFree: 32742, Oll: 555, OMem: 0, TotMem: 61466, Host: "fd40:1481:21:dbe0:7021:300:a03:1a06", Port: "44426", User: "default"}, |
| 78 | }, { |
| 79 | in: "id=14 addr=127.0.0.1:64958 fd=9 name=foo age=ABCDE idle=0 flags=N db=1 sub=0 psub=0 multi=-1 qbuf=26 qbuf-free=32742 obl=0 oll=0 omem=0 tot-mem=0 events=r cmd=client", |
| 80 | expectedOk: false, |
| 81 | }, { |
| 82 | in: "id=14 addr=127.0.0.1:64958 fd=9 name=foo age=5 idle=NOPE flags=N db=1 sub=0 psub=0 multi=-1 qbuf=26 qbuf-free=32742 obl=0 oll=0 omem=0 tot-mem=0 events=r cmd=client", |
| 83 | expectedOk: false, |
| 84 | }, { |
| 85 | in: "id=14 addr=127.0.0.1:64958 sub=ERR", |
| 86 | expectedOk: true, |
| 87 | expectedInfo: ClientInfo{Id: "14", Sub: 0, Ssub: -1, Watch: -1, Host: "127.0.0.1", Port: "64958"}, |
| 88 | }, { |
| 89 | in: "id=14 addr=127.0.0.1:64958 psub=ERR", |
| 90 | expectedOk: true, |
| 91 | expectedInfo: ClientInfo{Id: "14", Psub: 0, Ssub: -1, Watch: -1, Host: "127.0.0.1", Port: "64958"}, |
| 92 | }, { |
| 93 | in: "id=14 addr=127.0.0.1:64958 ssub=ERR", |
| 94 | expectedOk: true, |
| 95 | expectedInfo: ClientInfo{Id: "14", Ssub: 0, Watch: -1, Host: "127.0.0.1", Port: "64958"}, |
| 96 | }, { |
| 97 | in: "id=14 addr=127.0.0.1:64958 watch=ERR", |
| 98 | expectedOk: true, |
| 99 | expectedInfo: ClientInfo{Id: "14", Ssub: -1, Watch: 0, Host: "127.0.0.1", Port: "64958"}, |
| 100 | }, { |
| 101 | in: "id=14 addr=127.0.0.1:64958 qbuf=ERR", |
| 102 | expectedOk: true, |
| 103 | expectedInfo: ClientInfo{Id: "14", Ssub: -1, Watch: -1, Qbuf: 0, Host: "127.0.0.1", Port: "64958"}, |
| 104 | }, { |
| 105 | in: "id=14 addr=127.0.0.1:64958 qbuf-free=ERR", |
| 106 | expectedOk: true, |
| 107 | expectedInfo: ClientInfo{Id: "14", Ssub: -1, Watch: -1, QbufFree: 0, Host: "127.0.0.1", Port: "64958"}, |
| 108 | }, { |
| 109 | in: "id=14 addr=127.0.0.1:64958 obl=ERR", |
| 110 | expectedOk: true, |
| 111 | expectedInfo: ClientInfo{Id: "14", Ssub: -1, Watch: -1, Obl: 0, Host: "127.0.0.1", Port: "64958"}, |
| 112 | }, { |
| 113 | in: "id=14 addr=127.0.0.1:64958 oll=ERR", |
| 114 | expectedOk: true, |
| 115 | expectedInfo: ClientInfo{Id: "14", Ssub: -1, Watch: -1, Oll: 0, Host: "127.0.0.1", Port: "64958"}, |
| 116 | }, { |
| 117 | in: "id=14 addr=127.0.0.1:64958 omem=ERR", |
| 118 | expectedOk: true, |
| 119 | expectedInfo: ClientInfo{Id: "14", Ssub: -1, Watch: -1, OMem: 0, Host: "127.0.0.1", Port: "64958"}, |
| 120 | }, { |
| 121 | in: "id=14 addr=127.0.0.1:64958 tot-mem=ERR", |
| 122 | expectedOk: true, |
| 123 | expectedInfo: ClientInfo{Id: "14", Ssub: -1, Watch: -1, TotMem: 0, Host: "127.0.0.1", Port: "64958"}, |
| 124 | }, { |
| 125 | in: "", |
| 126 | expectedOk: false, |
| 127 | }, |
| 128 | } |
| 129 | |
| 130 | for _, tst := range tsts { |
| 131 | info, ok := parseClientListString(tst.in) |
| 132 | if !tst.expectedOk { |
| 133 | if ok { |
| 134 | t.Errorf("expected NOT ok, but got ok, input: %s", tst.in) |
| 135 | } |
| 136 | continue |
| 137 | } |
| 138 | |
| 139 | if *info != tst.expectedInfo { |
| 140 | t.Errorf("TestParseClientListString( %s ) error. Given: %#v Wanted: %#v", tst.in, info, tst.expectedInfo) |
| 141 | } |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | func TestExportClientList(t *testing.T) { |
| 146 | for _, isExportClientList := range []bool{true, false} { |
| 147 | e := getTestExporterWithOptions(Options{ |
| 148 | Namespace: "test", |
| 149 | ExportClientList: isExportClientList, |
| 150 | }) |
| 151 | |
| 152 | chM := make(chan prometheus.Metric) |
| 153 | go func() { |
| 154 | e.Collect(chM) |
| 155 | close(chM) |
| 156 | }() |
| 157 | |
| 158 | tsts := []struct { |
| 159 | in string |
| 160 | found bool |
| 161 | }{ |
| 162 | {in: "connected_client_info"}, |
| 163 | {in: "connected_client_output_buffer_memory_usage_bytes"}, |
| 164 | {in: "connected_client_total_memory_consumed_bytes"}, |
| 165 | {in: "connected_client_created_at_timestamp"}, |
| 166 | {in: "connected_client_idle_since_timestamp"}, |
| 167 | {in: "connected_client_channel_subscriptions_count"}, |
| 168 | {in: "connected_client_pattern_matching_subscriptions_count"}, |
| 169 | {in: "connected_client_query_buffer_length_bytes"}, |
| 170 | {in: "connected_client_query_buffer_free_space_bytes"}, |
| 171 | {in: "connected_client_output_buffer_length_bytes"}, |
| 172 | {in: "connected_client_output_list_length"}, |
| 173 | {in: "connected_client_shard_channel_subscriptions_count"}, |
| 174 | {in: "connected_client_info"}, |
| 175 | } |
| 176 | for m := range chM { |
| 177 | desc := m.Desc().String() |
| 178 | for i := range tsts { |
| 179 | if strings.Contains(desc, tsts[i].in) { |
| 180 | tsts[i].found = true |
| 181 | } |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | for _, tst := range tsts { |
| 186 | if isExportClientList && !tst.found { |
| 187 | t.Errorf("%s was *not* found in isExportClientList metrics but expected", tst.in) |
| 188 | } else if !isExportClientList && tst.found { |
| 189 | t.Errorf("%s was *found* in isExportClientList metrics but *not* expected", tst.in) |
| 190 | } |
| 191 | } |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | /* |
| 196 | some metrics are only in redis 7 but not yet in valkey 7.2 |
| 197 | like "connected_client_shard_channel_watched_keys" |
| 198 | */ |
| 199 | func TestExportClientListRedis7(t *testing.T) { |
| 200 | redisSevenAddr := os.Getenv("TEST_REDIS7_URI") |
| 201 | if redisSevenAddr == "" { |
| 202 | t.Skipf("Skipping TestExportClientListRedis7, env var TEST_REDIS7_URI not set") |
| 203 | } |
| 204 | |
| 205 | e := getTestExporterWithAddrAndOptions(redisSevenAddr, Options{ |
| 206 | Namespace: "test", |
| 207 | ExportClientList: true, |
| 208 | }) |
| 209 | |
| 210 | chM := make(chan prometheus.Metric) |
| 211 | go func() { |
| 212 | e.Collect(chM) |
| 213 | close(chM) |
| 214 | }() |
| 215 | |
| 216 | tsts := []struct { |
| 217 | in string |
| 218 | found bool |
| 219 | }{ |
| 220 | { |
| 221 | in: "connected_client_shard_channel_subscriptions_count", |
| 222 | }, { |
| 223 | in: "connected_client_shard_channel_watched_keys", |
| 224 | }, |
| 225 | } |
| 226 | for m := range chM { |
| 227 | desc := m.Desc().String() |
| 228 | for i := range tsts { |
| 229 | if strings.Contains(desc, tsts[i].in) { |
| 230 | tsts[i].found = true |
| 231 | } |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | for _, tst := range tsts { |
| 236 | if !tst.found { |
| 237 | t.Errorf(`%s was *not* found in isExportClientList metrics but expected`, tst.in) |
| 238 | } |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | func TestExportClientListInclPort(t *testing.T) { |
| 243 | for _, inclPort := range []bool{true, false} { |
| 244 | e := getTestExporterWithOptions(Options{ |
| 245 | Namespace: "test", |
| 246 | ExportClientList: true, |
| 247 | ExportClientsInclPort: inclPort, |
| 248 | }) |
| 249 | |
| 250 | chM := make(chan prometheus.Metric) |
| 251 | go func() { |
| 252 | e.Collect(chM) |
| 253 | close(chM) |
| 254 | }() |
| 255 | |
| 256 | found := false |
| 257 | for m := range chM { |
| 258 | desc := m.Desc().String() |
| 259 | if strings.Contains(desc, "connected_client_info") { |
| 260 | if strings.Contains(desc, "port") { |
| 261 | found = true |
| 262 | } |
| 263 | } |
| 264 | } |
| 265 | |
| 266 | if inclPort && !found { |
| 267 | t.Errorf(`connected_client_info did *not* include "port" in isExportClientList metrics but was expected`) |
| 268 | } else if !inclPort && found { |
| 269 | t.Errorf(`connected_client_info did *include* "port" in isExportClientList metrics but was *not* expected`) |
| 270 | } |
| 271 | } |
| 272 | } |