Skip to content

Commit

Permalink
Merge pull request #2775 from fgiudici/kata-monitor_issue2292
Browse files Browse the repository at this point in the history
kata-monitor: add index page
  • Loading branch information
liubin authored Oct 14, 2021
2 parents 5c7e1b4 + f7f6bd0 commit 8be85fd
Showing 1 changed file with 82 additions and 12 deletions.
94 changes: 82 additions & 12 deletions src/runtime/cmd/kata-monitor/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package main

import (
"flag"
"fmt"
"net/http"
"os"
goruntime "runtime"
Expand Down Expand Up @@ -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() {
ver := versionInfo{
AppName: appName,
Expand Down Expand Up @@ -97,19 +107,62 @@ func main() {
panic(err)
}

// setup handlers, now only metrics is supported
// setup handlers, currently only metrics are supported
m := http.NewServeMux()
m.Handle("/metrics", http.HandlerFunc(km.ProcessMetricsRequest))
m.Handle("/sandboxes", http.HandlerFunc(km.ListSandboxes))
m.Handle("/agent-url", http.HandlerFunc(km.GetAgentURL))

// for debug shim process
m.Handle("/debug/vars", http.HandlerFunc(km.ExpvarHandler))
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))
m.Handle("/debug/pprof/symbol", http.HandlerFunc(km.PprofSymbol))
m.Handle("/debug/pprof/trace", http.HandlerFunc(km.PprofTrace))
endpoints = []endpoint{
{
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 _, endpoint := range endpoints {
m.Handle(endpoint.path, endpoint.handler)
}

// root index page to show all endpoints in kata-monitor
m.Handle("/", http.HandlerFunc(indexPage))

// listening on the server
svr := &http.Server{
Expand All @@ -119,6 +172,23 @@ func main() {
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
func initLog() {
kataMonitorLog := logrus.WithFields(logrus.Fields{
Expand Down

0 comments on commit 8be85fd

Please sign in to comment.