mirror of
https://github.com/prometheus/prometheus
synced 2025-01-12 18:01:36 +00:00
api: Consolidate web API contexts
This is based on the common/route changes in https://github.com/prometheus/common/pull/61.
This commit is contained in:
parent
766074568e
commit
f92532f254
@ -85,7 +85,6 @@ type apiFunc func(r *http.Request) (interface{}, *apiError)
|
||||
// API can register a set of endpoints in a router and handle
|
||||
// them using the provided storage and query engine.
|
||||
type API struct {
|
||||
Context context.Context
|
||||
Storage local.Storage
|
||||
QueryEngine *promql.Engine
|
||||
|
||||
@ -94,9 +93,8 @@ type API struct {
|
||||
}
|
||||
|
||||
// NewAPI returns an initialized API type.
|
||||
func NewAPI(ctx context.Context, qe *promql.Engine, st local.Storage) *API {
|
||||
func NewAPI(qe *promql.Engine, st local.Storage) *API {
|
||||
return &API{
|
||||
Context: ctx,
|
||||
QueryEngine: qe,
|
||||
Storage: st,
|
||||
context: route.Context,
|
||||
@ -159,7 +157,7 @@ func (api *API) query(r *http.Request) (interface{}, *apiError) {
|
||||
return nil, &apiError{errorBadData, err}
|
||||
}
|
||||
|
||||
res := qry.Exec(api.Context)
|
||||
res := qry.Exec(api.context(r))
|
||||
if res.Err != nil {
|
||||
switch res.Err.(type) {
|
||||
case promql.ErrQueryCanceled:
|
||||
@ -206,7 +204,7 @@ func (api *API) queryRange(r *http.Request) (interface{}, *apiError) {
|
||||
return nil, &apiError{errorBadData, err}
|
||||
}
|
||||
|
||||
res := qry.Exec(api.Context)
|
||||
res := qry.Exec(api.context(r))
|
||||
if res.Err != nil {
|
||||
switch res.Err.(type) {
|
||||
case promql.ErrQueryCanceled:
|
||||
@ -228,7 +226,7 @@ func (api *API) labelValues(r *http.Request) (interface{}, *apiError) {
|
||||
if !model.LabelNameRE.MatchString(name) {
|
||||
return nil, &apiError{errorBadData, fmt.Errorf("invalid label name: %q", name)}
|
||||
}
|
||||
vals, err := api.Storage.LabelValuesForLabelName(api.Context, model.LabelName(name))
|
||||
vals, err := api.Storage.LabelValuesForLabelName(api.context(r), model.LabelName(name))
|
||||
if err != nil {
|
||||
return nil, &apiError{errorExec, err}
|
||||
}
|
||||
@ -274,7 +272,7 @@ func (api *API) series(r *http.Request) (interface{}, *apiError) {
|
||||
matcherSets = append(matcherSets, matchers)
|
||||
}
|
||||
|
||||
res, err := api.Storage.MetricsForLabelMatchers(api.Context, start, end, matcherSets...)
|
||||
res, err := api.Storage.MetricsForLabelMatchers(api.context(r), start, end, matcherSets...)
|
||||
if err != nil {
|
||||
return nil, &apiError{errorExec, err}
|
||||
}
|
||||
|
@ -52,7 +52,6 @@ func TestEndpoints(t *testing.T) {
|
||||
api := &API{
|
||||
Storage: suite.Storage(),
|
||||
QueryEngine: suite.QueryEngine(),
|
||||
Context: suite.Context(),
|
||||
now: func() model.Time { return now },
|
||||
}
|
||||
|
||||
@ -610,7 +609,7 @@ func TestParseDuration(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestOptionsMethod(t *testing.T) {
|
||||
r := route.New()
|
||||
r := route.New(nil)
|
||||
api := &API{}
|
||||
api.Register(r)
|
||||
|
||||
|
@ -120,7 +120,9 @@ type Options struct {
|
||||
|
||||
// New initializes a new web Handler.
|
||||
func New(o *Options) *Handler {
|
||||
router := route.New()
|
||||
router := route.New(func(r *http.Request) context.Context {
|
||||
return o.Context
|
||||
})
|
||||
|
||||
h := &Handler{
|
||||
router: router,
|
||||
@ -138,7 +140,7 @@ func New(o *Options) *Handler {
|
||||
queryEngine: o.QueryEngine,
|
||||
storage: o.Storage,
|
||||
|
||||
apiV1: api_v1.NewAPI(o.Context, o.QueryEngine, o.Storage),
|
||||
apiV1: api_v1.NewAPI(o.QueryEngine, o.Storage),
|
||||
now: model.Now,
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user