Commit 368e847d by Torkel Ödegaard

feat: added api health endpoint that does not require auth and never creates…

feat: added api health endpoint that does not require auth and never creates sessions, returns db status as well. #3302
parent fe64ed42
......@@ -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")
......
package models
type GetDBHealthQuery struct{}
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()
}
......@@ -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
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment