mirror of
https://github.com/grafana/grafana.git
synced 2024-12-27 09:21:35 -06:00
feat: added api health endpoint that does not require auth and never creates sessions, returns db status as well. #3302
This commit is contained in:
parent
fe64ed424e
commit
368e847d12
@ -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")
|
||||
|
3
pkg/models/health.go
Normal file
3
pkg/models/health.go
Normal file
@ -0,0 +1,3 @@
|
||||
package models
|
||||
|
||||
type GetDBHealthQuery struct{}
|
14
pkg/services/sqlstore/health.go
Normal file
14
pkg/services/sqlstore/health.go
Normal file
@ -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()
|
||||
}
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user