asynqmon/redis_info_handlers.go

123 lines
3.5 KiB
Go
Raw Permalink Normal View History

package asynqmon
2021-01-03 22:56:53 +08:00
import (
2021-10-01 02:26:01 +08:00
"context"
2021-01-03 22:56:53 +08:00
"encoding/json"
"net/http"
"strings"
"github.com/hibiken/asynq"
2023-05-04 12:17:09 +08:00
"github.com/redis/go-redis/v9"
2021-01-03 22:56:53 +08:00
)
// ****************************************************************************
// This file defines:
// - http.Handler(s) for redis info related endpoints
// ****************************************************************************
2021-10-04 23:18:00 +08:00
type redisInfoResponse struct {
2021-01-05 04:05:37 +08:00
Addr string `json:"address"`
Info map[string]string `json:"info"`
RawInfo string `json:"raw_info"`
Cluster bool `json:"cluster"`
// Following fields are only set when connected to redis cluster.
RawClusterNodes string `json:"raw_cluster_nodes"`
2021-10-04 23:18:00 +08:00
QueueLocations []*queueLocationInfo `json:"queue_locations"`
}
2021-10-04 23:18:00 +08:00
type queueLocationInfo struct {
Queue string `json:"queue"` // queue name
KeySlot int64 `json:"keyslot"` // cluster key slot for the queue
Nodes []string `json:"nodes"` // list of cluster node addresses
}
func newRedisInfoHandlerFunc(client *redis.Client) http.HandlerFunc {
2021-01-03 22:56:53 +08:00
return func(w http.ResponseWriter, r *http.Request) {
2021-10-01 02:26:01 +08:00
res, err := client.Info(context.Background()).Result()
2021-01-03 22:56:53 +08:00
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
info := parseRedisInfo(res)
2021-10-04 23:18:00 +08:00
resp := redisInfoResponse{
Addr: client.Options().Addr,
2021-01-05 04:05:37 +08:00
Info: info,
RawInfo: res,
Cluster: false,
}
if err := json.NewEncoder(w).Encode(resp); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
}
func newRedisClusterInfoHandlerFunc(client *redis.ClusterClient, inspector *asynq.Inspector) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
2021-10-01 02:26:01 +08:00
ctx := context.Background()
rawClusterInfo, err := client.ClusterInfo(ctx).Result()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
info := parseRedisInfo(rawClusterInfo)
2021-10-01 02:26:01 +08:00
rawClusterNodes, err := client.ClusterNodes(ctx).Result()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
queues, err := inspector.Queues()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
2021-10-04 23:18:00 +08:00
var queueLocations []*queueLocationInfo
for _, qname := range queues {
2021-10-04 23:18:00 +08:00
q := queueLocationInfo{Queue: qname}
q.KeySlot, err = inspector.ClusterKeySlot(qname)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
nodes, err := inspector.ClusterNodes(qname)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
for _, n := range nodes {
q.Nodes = append(q.Nodes, n.Addr)
}
queueLocations = append(queueLocations, &q)
}
2021-10-04 23:18:00 +08:00
resp := redisInfoResponse{
Addr: strings.Join(client.Options().Addrs, ","),
Info: info,
RawInfo: rawClusterInfo,
Cluster: true,
RawClusterNodes: rawClusterNodes,
QueueLocations: queueLocations,
}
if err := json.NewEncoder(w).Encode(resp); err != nil {
2021-01-03 22:56:53 +08:00
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
}
// Parses the return value from the INFO command.
// See https://redis.io/commands/info#return-value.
func parseRedisInfo(infoStr string) map[string]string {
info := make(map[string]string)
lines := strings.Split(infoStr, "\r\n")
for _, l := range lines {
kv := strings.Split(l, ":")
if len(kv) == 2 {
info[kv[0]] = kv[1]
}
}
return info
}