feat(coderd): add simple healthcheck formatting option (#9864)

This commit is contained in:
Colin Adler
2023-09-25 17:55:50 -05:00
committed by GitHub
parent 6e6b808143
commit 89292264be
2 changed files with 65 additions and 3 deletions
+28 -2
View File
@@ -2,6 +2,7 @@ package coderd
import (
"context"
"fmt"
"net/http"
"time"
@@ -37,7 +38,7 @@ func (api *API) debugDeploymentHealth(rw http.ResponseWriter, r *http.Request) {
// Get cached report if it exists.
if report := api.healthCheckCache.Load(); report != nil {
if time.Since(report.Time) < api.HealthcheckRefresh {
httpapi.WriteIndent(ctx, rw, http.StatusOK, report)
formatHealthcheck(ctx, rw, r, report)
return
}
}
@@ -59,11 +60,36 @@ func (api *API) debugDeploymentHealth(rw http.ResponseWriter, r *http.Request) {
})
return
case res := <-resChan:
httpapi.WriteIndent(ctx, rw, http.StatusOK, res.Val)
formatHealthcheck(ctx, rw, r, res.Val)
return
}
}
func formatHealthcheck(ctx context.Context, rw http.ResponseWriter, r *http.Request, hc *healthcheck.Report) {
format := r.URL.Query().Get("format")
switch format {
case "text":
rw.Header().Set("Content-Type", "text/plain; charset=utf-8")
rw.WriteHeader(http.StatusOK)
_, _ = fmt.Fprintln(rw, "time:", hc.Time.Format(time.RFC3339))
_, _ = fmt.Fprintln(rw, "healthy:", hc.Healthy)
_, _ = fmt.Fprintln(rw, "derp:", hc.DERP.Healthy)
_, _ = fmt.Fprintln(rw, "access_url:", hc.AccessURL.Healthy)
_, _ = fmt.Fprintln(rw, "websocket:", hc.Websocket.Healthy)
_, _ = fmt.Fprintln(rw, "database:", hc.Database.Healthy)
case "", "json":
httpapi.WriteIndent(ctx, rw, http.StatusOK, hc)
default:
httpapi.Write(ctx, rw, http.StatusBadRequest, codersdk.Response{
Message: fmt.Sprintf("Invalid format option %q.", format),
Detail: "Allowed values are: \"json\", \"simple\".",
})
}
}
// For some reason the swagger docs need to be attached to a function.
//
// @Summary Debug Info Websocket Test