From 6dc73a67120699ac06debf375f50cf1f6ffb5115 Mon Sep 17 00:00:00 2001 From: Carl Bergquist Date: Fri, 18 Sep 2020 13:03:18 +0200 Subject: [PATCH] add /healthz endpoint (#27536) kuberentes (and Im sure other orchastrators does as well) support two kind of checks. readiness checks and liveness checks. Grafanas current `/api/health` endpoint requires database access which might not always be required for the instance to be considered active. --- pkg/api/api.go | 3 ++- pkg/api/http_server.go | 15 +++++++++++---- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/pkg/api/api.go b/pkg/api/api.go index bd0a771f731..923c9c05156 100644 --- a/pkg/api/api.go +++ b/pkg/api/api.go @@ -437,7 +437,8 @@ func (hs *HTTPServer) registerRoutes() { r.Delete("/api/snapshots/:key", reqEditorRole, Wrap(DeleteDashboardSnapshot)) // Health check - r.Get("/api/health", hs.healthHandler) + r.Get("/api/health", hs.apiHealthHandler) + r.Get("/healthz", hs.healthzHandler) r.Get("/*", reqSignedIn, hs.Index) } diff --git a/pkg/api/http_server.go b/pkg/api/http_server.go index b6d82125879..19f416831f1 100644 --- a/pkg/api/http_server.go +++ b/pkg/api/http_server.go @@ -375,12 +375,19 @@ func (hs *HTTPServer) metricsEndpoint(ctx *macaron.Context) { ServeHTTP(ctx.Resp, ctx.Req.Request) } -func (hs *HTTPServer) healthHandler(ctx *macaron.Context) { - notHeadOrGet := ctx.Req.Method != http.MethodGet && ctx.Req.Method != http.MethodHead - if notHeadOrGet || ctx.Req.URL.Path != "/api/health" { - return +// healthzHandler always return 200 - Ok if Grafana's web server is running +func (hs *HTTPServer) healthzHandler(ctx *macaron.Context) { + ctx.WriteHeader(200) + _, err := ctx.Resp.Write([]byte("Ok")) + if err != nil { + hs.log.Error("could not write to response", "err", err) } +} +// apiHealthHandler will return ok if Grafana's web server is running and it +// can access the database. If the database cannot be access it will return +// http status code 503. +func (hs *HTTPServer) apiHealthHandler(ctx *macaron.Context) { data := simplejson.New() data.Set("database", "ok") if !hs.Cfg.AnonymousHideVersion {