Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: query: Expose query API request counts #1420

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ We use *breaking* word for marking changes that are not backward compatible (rel

### Added

- [#xxx](https://github.com/thanos-io/thanos/pull/xxx) Thanos Query API now exposes `thanos_query_api_instant_query_total`, `thanos_query_api_instant_query_failures_total`, `thanos_query_api_range_query_total` and `thanos_query_api_range_query_failures_total` metrics to track error rates of query api
- [#1363](https://github.com/thanos-io/thanos/pull/1378) Thanos Receive now exposes `thanos_receive_config_hash`, `thanos_receive_config_last_reload_successful` and `thanos_receive_config_last_reload_success_timestamp_seconds` metrics to track latest configuration change
- [#1358](https://github.com/thanos-io/thanos/pull/1358) Added `part_size` configuration option for HTTP multipart requests minimum part size for S3 storage type
- [#1363](https://github.com/thanos-io/thanos/pull/1363) Thanos Receive now exposes `thanos_receive_hashring_nodes` and `thanos_receive_hashring_tenants` metrics to monitor status of hash-rings

Expand Down
38 changes: 36 additions & 2 deletions pkg/query/api/v1.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,11 @@ type API struct {
queryableCreate query.QueryableCreator
queryEngine *promql.Engine

instantQueries prometheus.Counter
instantQueryFailures prometheus.Counter
instantQueryDuration prometheus.Histogram
rangeQueries prometheus.Counter
rangeQueryFailures prometheus.Counter
rangeQueryDuration prometheus.Histogram
enableAutodownsampling bool
enablePartialResponse bool
Expand All @@ -116,13 +120,29 @@ func NewAPI(
enableAutodownsampling bool,
enablePartialResponse bool,
) *API {
instantQueries := prometheus.NewCounter(prometheus.CounterOpts{
Name: "thanos_query_api_instant_query_total",
Help: "Total number of instant queries.",
})
instantQueryFailures := prometheus.NewCounter(prometheus.CounterOpts{
Name: "thanos_query_api_instant_query_failures_total",
Help: "Total number of failed instant queries.",
})
instantQueryDuration := prometheus.NewHistogram(prometheus.HistogramOpts{
Name: "thanos_query_api_instant_query_duration_seconds",
Help: "Time it takes to perform instant query on promEngine backed up with thanos querier.",
Buckets: []float64{
0.05, 0.1, 0.25, 0.6, 1, 2, 3.5, 5, 7.5, 10, 15, 20,
},
})
rangeQueries := prometheus.NewCounter(prometheus.CounterOpts{
Name: "thanos_query_api_range_query_total",
Help: "Total number of range queries.",
})
rangeQueryFailures := prometheus.NewCounter(prometheus.CounterOpts{
Name: "thanos_query_api_range_query_failures_total",
Help: "Total number of failed range queries.",
})
rangeQueryDuration := prometheus.NewHistogram(prometheus.HistogramOpts{
Name: "thanos_query_api_range_query_duration_seconds",
Help: "Time it takes to perform range query on promEngine backed up with thanos querier.",
Expand All @@ -132,14 +152,22 @@ func NewAPI(
})

reg.MustRegister(
instantQueries,
instantQueryFailures,
instantQueryDuration,
rangeQueries,
rangeQueryFailures,
rangeQueryDuration,
)
return &API{
logger: logger,
queryEngine: qe,
queryableCreate: c,
instantQueries: instantQueries,
instantQueryFailures: instantQueryFailures,
instantQueryDuration: instantQueryDuration,
rangeQueries: rangeQueries,
rangeQueryFailures: rangeQueryFailures,
rangeQueryDuration: rangeQueryDuration,
enableAutodownsampling: enableAutodownsampling,
enablePartialResponse: enablePartialResponse,
Expand Down Expand Up @@ -282,13 +310,16 @@ func (api *API) query(r *http.Request) (interface{}, []error, *ApiError) {
defer span.Finish()

begin := api.now()
defer api.instantQueryDuration.Observe(time.Since(begin).Seconds())

qry, err := api.queryEngine.NewInstantQuery(api.queryableCreate(enableDedup, 0, enablePartialResponse), r.FormValue("query"), ts)
if err != nil {
return nil, nil, &ApiError{errorBadData, err}
}

res := qry.Exec(ctx)
if res.Err != nil {
api.instantQueryFailures.Inc()
switch res.Err.(type) {
case promql.ErrQueryCanceled:
return nil, nil, &ApiError{errorCanceled, res.Err}
Expand All @@ -299,7 +330,7 @@ func (api *API) query(r *http.Request) (interface{}, []error, *ApiError) {
}
return nil, nil, &ApiError{errorExec, res.Err}
}
api.instantQueryDuration.Observe(time.Since(begin).Seconds())
api.instantQueries.Inc()

return &queryData{
ResultType: res.Value.Type(),
Expand Down Expand Up @@ -370,6 +401,8 @@ func (api *API) queryRange(r *http.Request) (interface{}, []error, *ApiError) {
defer span.Finish()

begin := api.now()
defer api.rangeQueryDuration.Observe(time.Since(begin).Seconds())

qry, err := api.queryEngine.NewRangeQuery(
api.queryableCreate(enableDedup, maxSourceResolution, enablePartialResponse),
r.FormValue("query"),
Expand All @@ -383,6 +416,7 @@ func (api *API) queryRange(r *http.Request) (interface{}, []error, *ApiError) {

res := qry.Exec(ctx)
if res.Err != nil {
api.rangeQueryFailures.Inc()
switch res.Err.(type) {
case promql.ErrQueryCanceled:
return nil, nil, &ApiError{errorCanceled, res.Err}
Expand All @@ -391,7 +425,7 @@ func (api *API) queryRange(r *http.Request) (interface{}, []error, *ApiError) {
}
return nil, nil, &ApiError{errorExec, res.Err}
}
api.rangeQueryDuration.Observe(time.Since(begin).Seconds())
api.rangeQueries.Inc()

return &queryData{
ResultType: res.Value.Type(),
Expand Down
4 changes: 4 additions & 0 deletions pkg/query/api/v1_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,11 @@ func TestEndpoints(t *testing.T) {
queryableCreate: testQueryableCreator(suite.Storage()),
queryEngine: suite.QueryEngine(),

instantQueries: prometheus.NewCounter(prometheus.CounterOpts{}),
instantQueryFailures: prometheus.NewCounter(prometheus.CounterOpts{}),
instantQueryDuration: prometheus.NewHistogram(prometheus.HistogramOpts{}),
rangeQueries: prometheus.NewCounter(prometheus.CounterOpts{}),
rangeQueryFailures: prometheus.NewCounter(prometheus.CounterOpts{}),
rangeQueryDuration: prometheus.NewHistogram(prometheus.HistogramOpts{}),

now: func() time.Time { return now },
Expand Down