mirror of
https://github.com/kata-containers/kata-containers.git
synced 2025-06-25 15:02:45 +00:00
kata-monitor: add index page
Add an index page to the kata-monitor endpoint. Porting of https://github.com/liubin/kata-containers/commit/a45aa0696d55 Fixes: #2292 Signed-off-by: Francesco Giudici <fgiudici@redhat.com>
This commit is contained in:
parent
8739a73dd3
commit
f7f6bd0142
@ -7,6 +7,7 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"flag"
|
"flag"
|
||||||
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
goruntime "runtime"
|
goruntime "runtime"
|
||||||
@ -54,6 +55,15 @@ func printVersion(ver versionInfo) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type endpoint struct {
|
||||||
|
handler http.HandlerFunc
|
||||||
|
path string
|
||||||
|
desc string
|
||||||
|
}
|
||||||
|
|
||||||
|
// global variable endpoints contains all available endpoints
|
||||||
|
var endpoints []endpoint
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
ver := versionInfo{
|
ver := versionInfo{
|
||||||
AppName: appName,
|
AppName: appName,
|
||||||
@ -97,19 +107,62 @@ func main() {
|
|||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// setup handlers, now only metrics is supported
|
// setup handlers, currently only metrics are supported
|
||||||
m := http.NewServeMux()
|
m := http.NewServeMux()
|
||||||
m.Handle("/metrics", http.HandlerFunc(km.ProcessMetricsRequest))
|
endpoints = []endpoint{
|
||||||
m.Handle("/sandboxes", http.HandlerFunc(km.ListSandboxes))
|
{
|
||||||
m.Handle("/agent-url", http.HandlerFunc(km.GetAgentURL))
|
path: "/metrics",
|
||||||
|
desc: "Get metrics from sandboxes.",
|
||||||
|
handler: km.ProcessMetricsRequest,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
path: "/sandboxes",
|
||||||
|
desc: "List all Kata Containers sandboxes.",
|
||||||
|
handler: km.ListSandboxes,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
path: "/agent-url",
|
||||||
|
desc: "Get sandbox agent URL.",
|
||||||
|
handler: km.GetAgentURL,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
path: "/debug/vars",
|
||||||
|
desc: "Golang pprof `/debug/vars` endpoint for kata runtime shim process.",
|
||||||
|
handler: km.ExpvarHandler,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
path: "/debug/pprof/",
|
||||||
|
desc: "Golang pprof `/debug/pprof/` endpoint for kata runtime shim process.",
|
||||||
|
handler: km.PprofIndex,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
path: "/debug/pprof/cmdline",
|
||||||
|
desc: "Golang pprof `/debug/pprof/cmdline` endpoint for kata runtime shim process.",
|
||||||
|
handler: km.PprofCmdline,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
path: "/debug/pprof/profile",
|
||||||
|
desc: "Golang pprof `/debug/pprof/profile` endpoint for kata runtime shim process.",
|
||||||
|
handler: km.PprofProfile,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
path: "/debug/pprof/symbol",
|
||||||
|
desc: "Golang pprof `/debug/pprof/symbol` endpoint for kata runtime shim process.",
|
||||||
|
handler: km.PprofSymbol,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
path: "/debug/pprof/trace",
|
||||||
|
desc: "Golang pprof `/debug/pprof/trace` endpoint for kata runtime shim process.",
|
||||||
|
handler: km.PprofTrace,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
// for debug shim process
|
for _, endpoint := range endpoints {
|
||||||
m.Handle("/debug/vars", http.HandlerFunc(km.ExpvarHandler))
|
m.Handle(endpoint.path, endpoint.handler)
|
||||||
m.Handle("/debug/pprof/", http.HandlerFunc(km.PprofIndex))
|
}
|
||||||
m.Handle("/debug/pprof/cmdline", http.HandlerFunc(km.PprofCmdline))
|
|
||||||
m.Handle("/debug/pprof/profile", http.HandlerFunc(km.PprofProfile))
|
// root index page to show all endpoints in kata-monitor
|
||||||
m.Handle("/debug/pprof/symbol", http.HandlerFunc(km.PprofSymbol))
|
m.Handle("/", http.HandlerFunc(indexPage))
|
||||||
m.Handle("/debug/pprof/trace", http.HandlerFunc(km.PprofTrace))
|
|
||||||
|
|
||||||
// listening on the server
|
// listening on the server
|
||||||
svr := &http.Server{
|
svr := &http.Server{
|
||||||
@ -119,6 +172,23 @@ func main() {
|
|||||||
logrus.Fatal(svr.ListenAndServe())
|
logrus.Fatal(svr.ListenAndServe())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func indexPage(w http.ResponseWriter, r *http.Request) {
|
||||||
|
w.Write([]byte("Available HTTP endpoints:\n"))
|
||||||
|
|
||||||
|
spacing := 0
|
||||||
|
for _, endpoint := range endpoints {
|
||||||
|
if len(endpoint.path) > spacing {
|
||||||
|
spacing = len(endpoint.path)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
spacing = spacing + 3
|
||||||
|
|
||||||
|
formattedString := fmt.Sprintf("%%-%ds: %%s\n", spacing)
|
||||||
|
for _, endpoint := range endpoints {
|
||||||
|
w.Write([]byte(fmt.Sprintf(formattedString, endpoint.path, endpoint.desc)))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// initLog setup logger
|
// initLog setup logger
|
||||||
func initLog() {
|
func initLog() {
|
||||||
kataMonitorLog := logrus.WithFields(logrus.Fields{
|
kataMonitorLog := logrus.WithFields(logrus.Fields{
|
||||||
|
Loading…
Reference in New Issue
Block a user