mirror of
https://github.com/hibiken/asynqmon.git
synced 2025-01-19 03:05:53 +08:00
Add API endpoint to fetch redis info
This commit is contained in:
parent
9ac455cc7b
commit
094e23f736
2
go.mod
2
go.mod
@ -3,7 +3,7 @@ module asynqmon
|
|||||||
go 1.14
|
go 1.14
|
||||||
|
|
||||||
require (
|
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/gorilla/mux v1.8.0
|
||||||
github.com/hibiken/asynq v0.13.1
|
github.com/hibiken/asynq v0.13.1
|
||||||
github.com/rs/cors v1.7.0
|
github.com/rs/cors v1.7.0
|
||||||
|
1
go.sum
1
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-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-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-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/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.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||||
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
|
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
|
||||||
|
9
main.go
9
main.go
@ -8,6 +8,7 @@ import (
|
|||||||
"path/filepath"
|
"path/filepath"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/go-redis/redis/v8"
|
||||||
"github.com/gorilla/mux"
|
"github.com/gorilla/mux"
|
||||||
"github.com/hibiken/asynq"
|
"github.com/hibiken/asynq"
|
||||||
"github.com/rs/cors"
|
"github.com/rs/cors"
|
||||||
@ -64,6 +65,11 @@ func main() {
|
|||||||
})
|
})
|
||||||
defer inspector.Close()
|
defer inspector.Close()
|
||||||
|
|
||||||
|
rdb := redis.NewClient(&redis.Options{
|
||||||
|
Addr: "localhost:6379",
|
||||||
|
})
|
||||||
|
defer rdb.Close()
|
||||||
|
|
||||||
router := mux.NewRouter()
|
router := mux.NewRouter()
|
||||||
router.Use(loggingMiddleware)
|
router.Use(loggingMiddleware)
|
||||||
|
|
||||||
@ -119,6 +125,9 @@ func main() {
|
|||||||
api.HandleFunc("/scheduler_entries", newListSchedulerEntriesHandlerFunc(inspector)).Methods("GET")
|
api.HandleFunc("/scheduler_entries", newListSchedulerEntriesHandlerFunc(inspector)).Methods("GET")
|
||||||
api.HandleFunc("/scheduler_entries/{entry_id}/enqueue_events", newListSchedulerEnqueueEventsHandlerFunc(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"}
|
fs := &staticFileServer{staticPath: "ui/build", indexPath: "index.html"}
|
||||||
router.PathPrefix("/").Handler(fs)
|
router.PathPrefix("/").Handler(fs)
|
||||||
|
|
||||||
|
46
redis_info_handlers.go
Normal file
46
redis_info_handlers.go
Normal file
@ -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
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user