From a28a2fba513c3ff4738dc945e16697222144d63c Mon Sep 17 00:00:00 2001 From: Carl Bergquist Date: Fri, 2 Oct 2020 07:14:26 +0200 Subject: [PATCH] healthchecks should work regardless domain (#27981) --- pkg/api/api.go | 4 ---- pkg/api/http_server.go | 15 +++++++++++++++ 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/pkg/api/api.go b/pkg/api/api.go index 923c9c05156..982f0b55603 100644 --- a/pkg/api/api.go +++ b/pkg/api/api.go @@ -436,9 +436,5 @@ func (hs *HTTPServer) registerRoutes() { r.Get("/api/snapshots-delete/:deleteKey", reqSnapshotPublicModeOrSignedIn, Wrap(DeleteDashboardSnapshotByDeleteKey)) r.Delete("/api/snapshots/:key", reqEditorRole, Wrap(DeleteDashboardSnapshot)) - // Health check - 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 b7c6665672f..13c6814e83c 100644 --- a/pkg/api/http_server.go +++ b/pkg/api/http_server.go @@ -332,7 +332,12 @@ func (hs *HTTPServer) addMiddlewaresAndStaticRoutes() { Delims: macaron.Delims{Left: "[[", Right: "]]"}, })) + // These endpoints are used for monitoring the Grafana instance + // and should not be redirect or rejected. + m.Use(hs.healthzHandler) + m.Use(hs.apiHealthHandler) m.Use(hs.metricsEndpoint) + m.Use(middleware.GetContextHandler( hs.AuthTokenService, hs.RemoteCacheService, @@ -373,6 +378,11 @@ func (hs *HTTPServer) metricsEndpoint(ctx *macaron.Context) { // healthzHandler always return 200 - Ok if Grafana's web server is running func (hs *HTTPServer) healthzHandler(ctx *macaron.Context) { + notHeadOrGet := ctx.Req.Method != http.MethodGet && ctx.Req.Method != http.MethodHead + if notHeadOrGet || ctx.Req.URL.Path != "/healthz" { + return + } + ctx.WriteHeader(200) _, err := ctx.Resp.Write([]byte("Ok")) if err != nil { @@ -384,6 +394,11 @@ func (hs *HTTPServer) healthzHandler(ctx *macaron.Context) { // can access the database. If the database cannot be access it will return // http status code 503. func (hs *HTTPServer) apiHealthHandler(ctx *macaron.Context) { + notHeadOrGet := ctx.Req.Method != http.MethodGet && ctx.Req.Method != http.MethodHead + if notHeadOrGet || ctx.Req.URL.Path != "/api/health" { + return + } + data := simplejson.New() data.Set("database", "ok") if !hs.Cfg.AnonymousHideVersion {