diff --git a/pkg/api/http_server.go b/pkg/api/http_server.go index fe7ca21f402..2ad57281b78 100644 --- a/pkg/api/http_server.go +++ b/pkg/api/http_server.go @@ -13,9 +13,12 @@ import ( "github.com/grafana/grafana/pkg/api/live" httpstatic "github.com/grafana/grafana/pkg/api/static" + "github.com/grafana/grafana/pkg/bus" "github.com/grafana/grafana/pkg/cmd/grafana-cli/logger" + "github.com/grafana/grafana/pkg/components/simplejson" "github.com/grafana/grafana/pkg/log" "github.com/grafana/grafana/pkg/middleware" + "github.com/grafana/grafana/pkg/models" "github.com/grafana/grafana/pkg/plugins" "github.com/grafana/grafana/pkg/setting" ) @@ -147,6 +150,7 @@ func (hs *HttpServer) newMacaron() *macaron.Macaron { Delims: macaron.Delims{Left: "[[", Right: "]]"}, })) + m.Use(hs.healthHandler) m.Use(middleware.GetContextHandler()) m.Use(middleware.Sessioner(&setting.SessionOptions)) m.Use(middleware.RequestMetrics()) @@ -160,6 +164,28 @@ func (hs *HttpServer) newMacaron() *macaron.Macaron { return m } +func (hs *HttpServer) healthHandler(ctx *macaron.Context) { + if ctx.Req.Method != "GET" || ctx.Req.URL.Path != "/api/health" { + return + } + + data := simplejson.New() + data.Set("status", "ok") + data.Set("db_status", "ok") + data.Set("version", setting.BuildVersion) + data.Set("commit", setting.BuildCommit) + + if err := bus.Dispatch(&models.GetDBHealthQuery{}); err != nil { + data.Set("db_status", "failing") + } + + ctx.Resp.Header().Set("Content-Type", "application/json; charset=UTF-8") + ctx.Resp.WriteHeader(200) + + dataBytes, _ := data.EncodePretty() + ctx.Resp.Write(dataBytes) +} + func (hs *HttpServer) mapStatic(m *macaron.Macaron, rootDir string, dir string, prefix string) { headers := func(c *macaron.Context) { c.Resp.Header().Set("Cache-Control", "public, max-age=3600") diff --git a/pkg/models/health.go b/pkg/models/health.go new file mode 100644 index 00000000000..ee206ed2d6f --- /dev/null +++ b/pkg/models/health.go @@ -0,0 +1,3 @@ +package models + +type GetDBHealthQuery struct{} diff --git a/pkg/services/sqlstore/health.go b/pkg/services/sqlstore/health.go new file mode 100644 index 00000000000..ca5a8d2d085 --- /dev/null +++ b/pkg/services/sqlstore/health.go @@ -0,0 +1,14 @@ +package sqlstore + +import ( + "github.com/grafana/grafana/pkg/bus" + m "github.com/grafana/grafana/pkg/models" +) + +func init() { + bus.AddHandler("sql", GetDBHealthQuery) +} + +func GetDBHealthQuery(query *m.GetDBHealthQuery) error { + return x.Ping() +} diff --git a/pkg/services/sqlstore/sqlstore.go b/pkg/services/sqlstore/sqlstore.go index fe759ec8df9..848dd884323 100644 --- a/pkg/services/sqlstore/sqlstore.go +++ b/pkg/services/sqlstore/sqlstore.go @@ -158,6 +158,7 @@ func getEngine() (*xorm.Engine, error) { } else { engine.SetMaxOpenConns(DbCfg.MaxOpenConn) engine.SetMaxIdleConns(DbCfg.MaxIdleConn) + engine.SetLogger(&xorm.DiscardLogger{}) // engine.SetLogger(NewXormLogger(log.LvlInfo, log.New("sqlstore.xorm"))) // engine.ShowSQL = true // engine.ShowInfo = true