Merge pull request #3259 from prometheus/beorn7/web

Only respond to API requests once the server is ready
This commit is contained in:
Björn Rabenstein 2017-10-06 18:41:11 +02:00 committed by GitHub
commit 6bfc8f094c
3 changed files with 16 additions and 5 deletions

View file

@ -107,10 +107,18 @@ type API struct {
now func() model.Time now func() model.Time
config func() config.Config config func() config.Config
ready func(http.HandlerFunc) http.HandlerFunc
} }
// NewAPI returns an initialized API type. // NewAPI returns an initialized API type.
func NewAPI(qe *promql.Engine, st local.Storage, tr targetRetriever, ar alertmanagerRetriever, configFunc func() config.Config) *API { func NewAPI(
qe *promql.Engine,
st local.Storage,
tr targetRetriever,
ar alertmanagerRetriever,
configFunc func() config.Config,
readyFunc func(http.HandlerFunc) http.HandlerFunc,
) *API {
return &API{ return &API{
QueryEngine: qe, QueryEngine: qe,
Storage: st, Storage: st,
@ -118,6 +126,7 @@ func NewAPI(qe *promql.Engine, st local.Storage, tr targetRetriever, ar alertman
alertmanagerRetriever: ar, alertmanagerRetriever: ar,
now: model.Now, now: model.Now,
config: configFunc, config: configFunc,
ready: readyFunc,
} }
} }
@ -134,9 +143,9 @@ func (api *API) Register(r *route.Router) {
w.WriteHeader(http.StatusNoContent) w.WriteHeader(http.StatusNoContent)
} }
}) })
return prometheus.InstrumentHandler(name, httputil.CompressionHandler{ return api.ready(prometheus.InstrumentHandler(name, httputil.CompressionHandler{
Handler: hf, Handler: hf,
}) }))
} }
r.Options("/*path", instr("options", api.options)) r.Options("/*path", instr("options", api.options))
@ -153,7 +162,7 @@ func (api *API) Register(r *route.Router) {
r.Get("/alertmanagers", instr("alertmanagers", api.alertmanagers)) r.Get("/alertmanagers", instr("alertmanagers", api.alertmanagers))
r.Get("/status/config", instr("config", api.serveConfig)) r.Get("/status/config", instr("config", api.serveConfig))
r.Post("/read", prometheus.InstrumentHandler("read", http.HandlerFunc(api.remoteRead))) r.Post("/read", api.ready(prometheus.InstrumentHandler("read", http.HandlerFunc(api.remoteRead))))
} }
type queryData struct { type queryData struct {

View file

@ -104,6 +104,7 @@ func TestEndpoints(t *testing.T) {
config: func() config.Config { config: func() config.Config {
return samplePrometheusCfg return samplePrometheusCfg
}, },
ready: func(f http.HandlerFunc) http.HandlerFunc { return f },
} }
start := model.Time(0) start := model.Time(0)
@ -723,7 +724,7 @@ func TestParseDuration(t *testing.T) {
func TestOptionsMethod(t *testing.T) { func TestOptionsMethod(t *testing.T) {
r := route.New() r := route.New()
api := &API{} api := &API{ready: func(f http.HandlerFunc) http.HandlerFunc { return f }}
api.Register(r) api.Register(r)
s := httptest.NewServer(r) s := httptest.NewServer(r)

View file

@ -172,6 +172,7 @@ func New(o *Options) *Handler {
defer h.mtx.RUnlock() defer h.mtx.RUnlock()
return *h.config return *h.config
}, },
h.testReady,
) )
if o.RoutePrefix != "/" { if o.RoutePrefix != "/" {