API: Query database from /api/health endpoint (#28349)

This commit is contained in:
Emil Hessman
2020-10-21 11:06:19 +02:00
committed by GitHub
parent 9fce64c6aa
commit 13e67660f5
5 changed files with 234 additions and 4 deletions
+3 -3
View File
@@ -338,7 +338,7 @@ func (hs *HTTPServer) addMiddlewaresAndStaticRoutes() {
}))
// These endpoints are used for monitoring the Grafana instance
// and should not be redirect or rejected.
// and should not be redirected or rejected.
m.Use(hs.healthzHandler)
m.Use(hs.apiHealthHandler)
m.Use(hs.metricsEndpoint)
@@ -396,7 +396,7 @@ func (hs *HTTPServer) healthzHandler(ctx *macaron.Context) {
}
// 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
// can access the database. If the database cannot be accessed 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
@@ -411,7 +411,7 @@ func (hs *HTTPServer) apiHealthHandler(ctx *macaron.Context) {
data.Set("commit", setting.BuildCommit)
}
if err := bus.Dispatch(&models.GetDBHealthQuery{}); err != nil {
if !hs.databaseHealthy() {
data.Set("database", "failing")
ctx.Resp.Header().Set("Content-Type", "application/json; charset=UTF-8")
ctx.Resp.WriteHeader(503)