Context: Add context to /api/health calls (#40031)

Signed-off-by: bergquist <carl.bergquist@gmail.com>
This commit is contained in:
Carl Bergquist 2021-10-11 14:35:03 +02:00 committed by GitHub
parent 57fcfd578d
commit c9f25cf0a5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 22 additions and 13 deletions

View File

@ -1,20 +1,21 @@
package api package api
import ( import (
"context"
"time" "time"
"github.com/grafana/grafana/pkg/bus" "github.com/grafana/grafana/pkg/bus"
"github.com/grafana/grafana/pkg/models" "github.com/grafana/grafana/pkg/models"
) )
func (hs *HTTPServer) databaseHealthy() bool { func (hs *HTTPServer) databaseHealthy(ctx context.Context) bool {
const cacheKey = "db-healthy" const cacheKey = "db-healthy"
if cached, found := hs.CacheService.Get(cacheKey); found { if cached, found := hs.CacheService.Get(cacheKey); found {
return cached.(bool) return cached.(bool)
} }
healthy := bus.Dispatch(&models.GetDBHealthQuery{}) == nil healthy := bus.DispatchCtx(ctx, &models.GetDBHealthQuery{}) == nil
hs.CacheService.Set(cacheKey, healthy, time.Second*5) hs.CacheService.Set(cacheKey, healthy, time.Second*5)
return healthy return healthy

View File

@ -1,6 +1,7 @@
package api package api
import ( import (
"context"
"errors" "errors"
"net/http" "net/http"
"net/http/httptest" "net/http/httptest"
@ -21,7 +22,7 @@ func TestHealthAPI_Version(t *testing.T) {
cfg.BuildCommit = "59906ab1bf" cfg.BuildCommit = "59906ab1bf"
}) })
bus.AddHandler("test", func(query *models.GetDBHealthQuery) error { bus.AddHandlerCtx("test", func(ctx context.Context, query *models.GetDBHealthQuery) error {
return nil return nil
}) })
@ -44,7 +45,7 @@ func TestHealthAPI_AnonymousHideVersion(t *testing.T) {
m, hs := setupHealthAPITestEnvironment(t) m, hs := setupHealthAPITestEnvironment(t)
hs.Cfg.AnonymousHideVersion = true hs.Cfg.AnonymousHideVersion = true
bus.AddHandler("test", func(query *models.GetDBHealthQuery) error { bus.AddHandlerCtx("test", func(ctx context.Context, query *models.GetDBHealthQuery) error {
return nil return nil
}) })
@ -67,7 +68,7 @@ func TestHealthAPI_DatabaseHealthy(t *testing.T) {
m, hs := setupHealthAPITestEnvironment(t) m, hs := setupHealthAPITestEnvironment(t)
hs.Cfg.AnonymousHideVersion = true hs.Cfg.AnonymousHideVersion = true
bus.AddHandler("test", func(query *models.GetDBHealthQuery) error { bus.AddHandlerCtx("test", func(ctx context.Context, query *models.GetDBHealthQuery) error {
return nil return nil
}) })
@ -98,7 +99,7 @@ func TestHealthAPI_DatabaseUnhealthy(t *testing.T) {
m, hs := setupHealthAPITestEnvironment(t) m, hs := setupHealthAPITestEnvironment(t)
hs.Cfg.AnonymousHideVersion = true hs.Cfg.AnonymousHideVersion = true
bus.AddHandler("test", func(query *models.GetDBHealthQuery) error { bus.AddHandlerCtx("test", func(ctx context.Context, query *models.GetDBHealthQuery) error {
return errors.New("bad") return errors.New("bad")
}) })
@ -130,7 +131,7 @@ func TestHealthAPI_DatabaseHealthCached(t *testing.T) {
hs.Cfg.AnonymousHideVersion = true hs.Cfg.AnonymousHideVersion = true
// Database is healthy. // Database is healthy.
bus.AddHandler("test", func(query *models.GetDBHealthQuery) error { bus.AddHandlerCtx("test", func(ctx context.Context, query *models.GetDBHealthQuery) error {
return nil return nil
}) })

View File

@ -493,7 +493,7 @@ func (hs *HTTPServer) apiHealthHandler(ctx *web.Context) {
data.Set("commit", hs.Cfg.BuildCommit) data.Set("commit", hs.Cfg.BuildCommit)
} }
if !hs.databaseHealthy() { if !hs.databaseHealthy(ctx.Req.Context()) {
data.Set("database", "failing") data.Set("database", "failing")
ctx.Resp.Header().Set("Content-Type", "application/json; charset=UTF-8") ctx.Resp.Header().Set("Content-Type", "application/json; charset=UTF-8")
ctx.Resp.WriteHeader(503) ctx.Resp.WriteHeader(503)

View File

@ -1,15 +1,21 @@
package sqlstore package sqlstore
import ( import (
"context"
"github.com/grafana/grafana/pkg/bus" "github.com/grafana/grafana/pkg/bus"
"github.com/grafana/grafana/pkg/models" "github.com/grafana/grafana/pkg/models"
) )
func init() { func init() {
bus.AddHandler("sql", GetDBHealthQuery) bus.AddHandlerCtx("sql", GetDBHealthQuery)
} }
func GetDBHealthQuery(query *models.GetDBHealthQuery) error { // GetDBHealthQuery executes a query to check
_, err := x.Exec("SELECT 1") // the availability of the database.
return err func GetDBHealthQuery(ctx context.Context, query *models.GetDBHealthQuery) error {
return withDbSession(ctx, x, func(session *DBSession) error {
_, err := session.Exec("SELECT 1")
return err
})
} }

View File

@ -4,6 +4,7 @@
package sqlstore package sqlstore
import ( import (
"context"
"testing" "testing"
"github.com/grafana/grafana/pkg/models" "github.com/grafana/grafana/pkg/models"
@ -14,6 +15,6 @@ func TestGetDBHealthQuery(t *testing.T) {
InitTestDB(t) InitTestDB(t)
query := models.GetDBHealthQuery{} query := models.GetDBHealthQuery{}
err := GetDBHealthQuery(&query) err := GetDBHealthQuery(context.Background(), &query)
require.NoError(t, err) require.NoError(t, err)
} }