From 094e23f7369d95d968cbfd9671dbcd3d6ee1626e Mon Sep 17 00:00:00 2001 From: Ken Hibino Date: Sun, 3 Jan 2021 06:56:53 -0800 Subject: [PATCH] Add API endpoint to fetch redis info --- go.mod | 2 +- go.sum | 1 + main.go | 9 +++++++++ redis_info_handlers.go | 46 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 57 insertions(+), 1 deletion(-) create mode 100644 redis_info_handlers.go diff --git a/go.mod b/go.mod index 52ccf1d..2128743 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module asynqmon go 1.14 require ( - github.com/go-redis/redis/v8 v8.4.4 // indirect + github.com/go-redis/redis/v8 v8.4.4 github.com/gorilla/mux v1.8.0 github.com/hibiken/asynq v0.13.1 github.com/rs/cors v1.7.0 diff --git a/go.sum b/go.sum index 482cf69..b6a48e1 100644 --- a/go.sum +++ b/go.sum @@ -74,6 +74,7 @@ golang.org/x/sys v0.0.0-20191204072324-ce4227a45e2e h1:9vRrk9YW2BTzLP0VCB9ZDjU4c golang.org/x/sys v0.0.0-20191204072324-ce4227a45e2e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200519105757-fe76b779f299/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f h1:+Nyd8tzPX9R7BWHguqsrbFdRx3WQ/1ib8I44HXV5yTA= golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk= diff --git a/main.go b/main.go index a192d1a..10fb204 100644 --- a/main.go +++ b/main.go @@ -8,6 +8,7 @@ import ( "path/filepath" "time" + "github.com/go-redis/redis/v8" "github.com/gorilla/mux" "github.com/hibiken/asynq" "github.com/rs/cors" @@ -64,6 +65,11 @@ func main() { }) defer inspector.Close() + rdb := redis.NewClient(&redis.Options{ + Addr: "localhost:6379", + }) + defer rdb.Close() + router := mux.NewRouter() router.Use(loggingMiddleware) @@ -119,6 +125,9 @@ func main() { api.HandleFunc("/scheduler_entries", newListSchedulerEntriesHandlerFunc(inspector)).Methods("GET") api.HandleFunc("/scheduler_entries/{entry_id}/enqueue_events", newListSchedulerEnqueueEventsHandlerFunc(inspector)).Methods("GET") + // Redis info endpoint. + api.HandleFunc("/redis_info", newRedisInfoHandlerFunc(rdb)).Methods("GET") + fs := &staticFileServer{staticPath: "ui/build", indexPath: "index.html"} router.PathPrefix("/").Handler(fs) diff --git a/redis_info_handlers.go b/redis_info_handlers.go new file mode 100644 index 0000000..c32138a --- /dev/null +++ b/redis_info_handlers.go @@ -0,0 +1,46 @@ +package main + +import ( + "context" + "encoding/json" + "net/http" + "strings" + + "github.com/go-redis/redis/v8" +) + +// **************************************************************************** +// This file defines: +// - http.Handler(s) for redis info related endpoints +// **************************************************************************** + +func newRedisInfoHandlerFunc(rdb *redis.Client) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + ctx := context.Background() + res, err := rdb.Info(ctx).Result() + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + info := parseRedisInfo(res) + if err := json.NewEncoder(w).Encode(info); err != nil { + 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 + +}