From 7291a3bd713fa8a5201ba058050f3e3c9a7cd64f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lucas=20Serv=C3=A9n=20Mar=C3=ADn?= Date: Fri, 22 Apr 2022 12:03:56 +0200 Subject: [PATCH] cmd/kg: add pprof endpoints MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This commit enhances the Kilo agent internal HTTP server to include pprof endpoints. For simplicity, this commit migrates the internal server creation to https://github.com/metalmatze/signal/internalserver, which allows for easy registration of common internal server observability endpoints. Signed-off-by: Lucas Servén Marín --- cmd/kg/main.go | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/cmd/kg/main.go b/cmd/kg/main.go index ee85fdb..7e84fd8 100644 --- a/cmd/kg/main.go +++ b/cmd/kg/main.go @@ -27,10 +27,10 @@ import ( "github.com/go-kit/kit/log" "github.com/go-kit/kit/log/level" + "github.com/metalmatze/signal/internalserver" "github.com/oklog/run" "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus/collectors" - "github.com/prometheus/client_golang/prometheus/promhttp" "github.com/spf13/cobra" apiextensions "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset" "k8s.io/client-go/kubernetes" @@ -251,18 +251,21 @@ func runRoot(_ *cobra.Command, _ []string) error { var g run.Group { + h := internalserver.NewHandler( + internalserver.WithName("Internal Kilo API"), + internalserver.WithPrometheusRegistry(registry), + internalserver.WithPProf(), + ) + h.AddEndpoint("/health", "Exposes health checks", healthHandler) + h.AddEndpoint("/graph", "Exposes Kilo mesh topology graph", (&graphHandler{m, gr, &hostname, s}).ServeHTTP) // Run the HTTP server. - mux := http.NewServeMux() - mux.HandleFunc("/health", healthHandler) - mux.Handle("/graph", &graphHandler{m, gr, &hostname, s}) - mux.Handle("/metrics", promhttp.HandlerFor(registry, promhttp.HandlerOpts{})) l, err := net.Listen("tcp", listen) if err != nil { return fmt.Errorf("failed to listen on %s: %v", listen, err) } g.Add(func() error { - if err := http.Serve(l, mux); err != nil && err != http.ErrServerClosed { + if err := http.Serve(l, h); err != nil && err != http.ErrServerClosed { return fmt.Errorf("error: server exited unexpectedly: %v", err) } return nil