Browse Source

Merge pull request #2474 from Gouthamve/custom-timeouts-1399

Support Custom Timeout for Queries
pull/2312/merge
Fabian Reinartz 8 years ago committed by GitHub
parent
commit
de1e4322d7
  1. 8
      promql/engine.go
  2. 32
      web/api/v1/api.go

8
promql/engine.go

@ -59,7 +59,7 @@ var (
Namespace: namespace, Namespace: namespace,
Subsystem: subsystem, Subsystem: subsystem,
Name: "query_duration_seconds", Name: "query_duration_seconds",
Help: "Query timmings", Help: "Query timings",
ConstLabels: prometheus.Labels{"slice": "prepare_time"}, ConstLabels: prometheus.Labels{"slice": "prepare_time"},
}, },
) )
@ -68,7 +68,7 @@ var (
Namespace: namespace, Namespace: namespace,
Subsystem: subsystem, Subsystem: subsystem,
Name: "query_duration_seconds", Name: "query_duration_seconds",
Help: "Query timmings", Help: "Query timings",
ConstLabels: prometheus.Labels{"slice": "inner_eval"}, ConstLabels: prometheus.Labels{"slice": "inner_eval"},
}, },
) )
@ -77,7 +77,7 @@ var (
Namespace: namespace, Namespace: namespace,
Subsystem: subsystem, Subsystem: subsystem,
Name: "query_duration_seconds", Name: "query_duration_seconds",
Help: "Query timmings", Help: "Query timings",
ConstLabels: prometheus.Labels{"slice": "result_append"}, ConstLabels: prometheus.Labels{"slice": "result_append"},
}, },
) )
@ -86,7 +86,7 @@ var (
Namespace: namespace, Namespace: namespace,
Subsystem: subsystem, Subsystem: subsystem,
Name: "query_duration_seconds", Name: "query_duration_seconds",
Help: "Query timmings", Help: "Query timings",
ConstLabels: prometheus.Labels{"slice": "result_sort"}, ConstLabels: prometheus.Labels{"slice": "result_sort"},
}, },
) )

32
web/api/v1/api.go

@ -169,12 +169,24 @@ func (api *API) query(r *http.Request) (interface{}, *apiError) {
ts = api.now() ts = api.now()
} }
ctx := api.context(r)
if to := r.FormValue("timeout"); to != "" {
var cancel context.CancelFunc
timeout, err := parseDuration(to)
if err != nil {
return nil, &apiError{errorBadData, err}
}
ctx, cancel = context.WithTimeout(ctx, timeout)
defer cancel()
}
qry, err := api.QueryEngine.NewInstantQuery(r.FormValue("query"), ts) qry, err := api.QueryEngine.NewInstantQuery(r.FormValue("query"), ts)
if err != nil { if err != nil {
return nil, &apiError{errorBadData, err} return nil, &apiError{errorBadData, err}
} }
res := qry.Exec(api.context(r)) res := qry.Exec(ctx)
if res.Err != nil { if res.Err != nil {
switch res.Err.(type) { switch res.Err.(type) {
case promql.ErrQueryCanceled: case promql.ErrQueryCanceled:
@ -221,12 +233,24 @@ func (api *API) queryRange(r *http.Request) (interface{}, *apiError) {
return nil, &apiError{errorBadData, err} return nil, &apiError{errorBadData, err}
} }
ctx := api.context(r)
if to := r.FormValue("timeout"); to != "" {
var cancel context.CancelFunc
timeout, err := parseDuration(to)
if err != nil {
return nil, &apiError{errorBadData, err}
}
ctx, cancel = context.WithTimeout(ctx, timeout)
defer cancel()
}
qry, err := api.QueryEngine.NewRangeQuery(r.FormValue("query"), start, end, step) qry, err := api.QueryEngine.NewRangeQuery(r.FormValue("query"), start, end, step)
if err != nil { if err != nil {
return nil, &apiError{errorBadData, err} return nil, &apiError{errorBadData, err}
} }
res := qry.Exec(api.context(r)) res := qry.Exec(ctx)
if res.Err != nil { if res.Err != nil {
switch res.Err.(type) { switch res.Err.(type) {
case promql.ErrQueryCanceled: case promql.ErrQueryCanceled:
@ -345,6 +369,7 @@ func (api *API) dropSeries(r *http.Request) (interface{}, *apiError) {
return res, nil return res, nil
} }
// Target has the information for one target.
type Target struct { type Target struct {
// Labels before any processing. // Labels before any processing.
DiscoveredLabels model.LabelSet `json:"discoveredLabels"` DiscoveredLabels model.LabelSet `json:"discoveredLabels"`
@ -358,6 +383,7 @@ type Target struct {
Health retrieval.TargetHealth `json:"health"` Health retrieval.TargetHealth `json:"health"`
} }
// TargetDiscovery has all the active targets.
type TargetDiscovery struct { type TargetDiscovery struct {
ActiveTargets []*Target `json:"activeTargets"` ActiveTargets []*Target `json:"activeTargets"`
} }
@ -386,10 +412,12 @@ func (api *API) targets(r *http.Request) (interface{}, *apiError) {
return res, nil return res, nil
} }
// AlertmanagerDiscovery has all the active Alertmanagers.
type AlertmanagerDiscovery struct { type AlertmanagerDiscovery struct {
ActiveAlertmanagers []*AlertmanagerTarget `json:"activeAlertmanagers"` ActiveAlertmanagers []*AlertmanagerTarget `json:"activeAlertmanagers"`
} }
// AlertmanagerTarget has info on one AM.
type AlertmanagerTarget struct { type AlertmanagerTarget struct {
URL string `json:"url"` URL string `json:"url"`
} }

Loading…
Cancel
Save