From b3fc345abafe40ddf48cf8f65959838dd2f307ec Mon Sep 17 00:00:00 2001 From: Anton Tolchanov Date: Wed, 31 Jul 2024 09:03:14 +0100 Subject: [PATCH] cmd/derpprobe: use a status page from the prober library Updates tailscale/corp#20583 Signed-off-by: Anton Tolchanov --- cmd/derpprobe/derpprobe.go | 34 +++++++--------------------------- 1 file changed, 7 insertions(+), 27 deletions(-) diff --git a/cmd/derpprobe/derpprobe.go b/cmd/derpprobe/derpprobe.go index 117d73582..1d0ec32c3 100644 --- a/cmd/derpprobe/derpprobe.go +++ b/cmd/derpprobe/derpprobe.go @@ -7,8 +7,6 @@ package main import ( "flag" "fmt" - "html" - "io" "log" "net/http" "sort" @@ -70,8 +68,13 @@ func main() { } mux := http.NewServeMux() - tsweb.Debugger(mux) - mux.HandleFunc("/", http.HandlerFunc(serveFunc(p))) + d := tsweb.Debugger(mux) + d.Handle("probe-run", "Run a probe", tsweb.StdHandler(tsweb.ReturnHandlerFunc(p.RunHandler), tsweb.HandlerOptions{Logf: log.Printf})) + mux.Handle("/", tsweb.StdHandler(p.StatusHandler( + prober.WithTitle("DERP Prober"), + prober.WithPageLink("Prober metrics", "/debug/varz"), + prober.WithProbeLink("Run Probe", "/debug/probe-run?name={{.Name}}"), + ), tsweb.HandlerOptions{Logf: log.Printf})) log.Printf("Listening on %s", *listen) log.Fatal(http.ListenAndServe(*listen, mux)) } @@ -105,26 +108,3 @@ func getOverallStatus(p *prober.Prober) (o overallStatus) { sort.Strings(o.good) return } - -func serveFunc(p *prober.Prober) func(w http.ResponseWriter, r *http.Request) { - return func(w http.ResponseWriter, r *http.Request) { - st := getOverallStatus(p) - summary := "All good" - if (float64(len(st.bad)) / float64(len(st.bad)+len(st.good))) > 0.25 { - // Returning a 500 allows monitoring this server externally and configuring - // an alert on HTTP response code. - w.WriteHeader(500) - summary = fmt.Sprintf("%d problems", len(st.bad)) - } - - io.WriteString(w, "\n") - fmt.Fprintf(w, "

derp probe

\n%s:\n") - } -}