Skip to content

Commit 7a5955b

Browse files
authored
Query: range query histogram (#4019)
* range query histogram Adds a range query histogram that tracks the distribution of range queries. This can be useful for determining storage retention requirements. Signed-off-by: Thor <[email protected]> * changelog Signed-off-by: Thor <[email protected]> * make docs Signed-off-by: Thor <[email protected]> * fixed comment Signed-off-by: Thor <[email protected]> * renamed histogram, added thanos prefix Signed-off-by: Thor <[email protected]>
1 parent 35fedc8 commit 7a5955b

File tree

4 files changed

+33
-0
lines changed

4 files changed

+33
-0
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ We use _breaking :warning:_ to mark changes that are not backward compatible (re
1212

1313
## Unreleased
1414

15+
- [#4019](https://github.com/thanos-io/thanos/pull/4019) Query: Adds query range histogram.
1516
- [#3350](https://github.com/thanos-io/thanos/pull/3350) Query/Sidecar: Added targets API support. You can now configure you Querier to fetch Prometheus targets from leaf Prometheus-es!
1617

1718
### Added

cmd/thanos/query.go

+1
Original file line numberDiff line numberDiff line change
@@ -585,6 +585,7 @@ func runQuery(
585585
extprom.WrapRegistererWithPrefix("thanos_query_concurrent_", reg),
586586
maxConcurrentQueries,
587587
),
588+
reg,
588589
)
589590

590591
api.Register(router.WithPrefix("/api/v1"), tracer, logger, ins, logMiddleware)

pkg/api/query/v1.go

+14
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ import (
3232
"github.com/go-kit/kit/log"
3333
"github.com/opentracing/opentracing-go"
3434
"github.com/pkg/errors"
35+
"github.com/prometheus/client_golang/prometheus"
36+
"github.com/prometheus/client_golang/prometheus/promauto"
3537
"github.com/prometheus/common/model"
3638
"github.com/prometheus/common/route"
3739
"github.com/prometheus/prometheus/pkg/labels"
@@ -95,6 +97,8 @@ type QueryAPI struct {
9597
defaultRangeQueryStep time.Duration
9698
defaultInstantQueryMaxSourceResolution time.Duration
9799
defaultMetadataTimeRange time.Duration
100+
101+
queryRangeHist prometheus.Histogram
98102
}
99103

100104
// NewQueryAPI returns an initialized QueryAPI type.
@@ -119,6 +123,7 @@ func NewQueryAPI(
119123
defaultMetadataTimeRange time.Duration,
120124
disableCORS bool,
121125
gate gate.Gate,
126+
reg *prometheus.Registry,
122127
) *QueryAPI {
123128
return &QueryAPI{
124129
baseAPI: api.NewBaseAPI(logger, disableCORS, flagsMap),
@@ -142,6 +147,12 @@ func NewQueryAPI(
142147
defaultInstantQueryMaxSourceResolution: defaultInstantQueryMaxSourceResolution,
143148
defaultMetadataTimeRange: defaultMetadataTimeRange,
144149
disableCORS: disableCORS,
150+
151+
queryRangeHist: promauto.With(reg).NewHistogram(prometheus.HistogramOpts{
152+
Name: "thanos_query_range_requested_timespan_duration_seconds",
153+
Help: "A histogram of the query range window in seconds",
154+
Buckets: prometheus.ExponentialBuckets(15*60, 2, 12),
155+
}),
145156
}
146157
}
147158

@@ -430,6 +441,9 @@ func (qapi *QueryAPI) queryRange(r *http.Request) (interface{}, []error, *api.Ap
430441

431442
qe := qapi.queryEngine(maxSourceResolution)
432443

444+
// Record the query range requested.
445+
qapi.queryRangeHist.Observe(end.Sub(start).Seconds())
446+
433447
// We are starting promQL tracing span here, because we have no control over promQL code.
434448
span, ctx := tracing.StartSpan(ctx, "promql_range_query")
435449
defer span.Finish()

pkg/api/query/v1_test.go

+17
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ import (
3232
"time"
3333

3434
"github.com/go-kit/kit/log"
35+
"github.com/prometheus/client_golang/prometheus"
36+
"github.com/prometheus/client_golang/prometheus/promauto"
3537
"github.com/prometheus/common/route"
3638
promgate "github.com/prometheus/prometheus/pkg/gate"
3739
"github.com/prometheus/prometheus/pkg/labels"
@@ -186,6 +188,9 @@ func TestQueryEndpoints(t *testing.T) {
186188
},
187189
gate: gate.New(nil, 4),
188190
defaultRangeQueryStep: time.Second,
191+
queryRangeHist: promauto.With(prometheus.NewRegistry()).NewHistogram(prometheus.HistogramOpts{
192+
Name: "query_range_hist",
193+
}),
189194
}
190195

191196
start := time.Unix(0, 0)
@@ -695,6 +700,9 @@ func TestMetadataEndpoints(t *testing.T) {
695700
return qe
696701
},
697702
gate: gate.New(nil, 4),
703+
queryRangeHist: promauto.With(prometheus.NewRegistry()).NewHistogram(prometheus.HistogramOpts{
704+
Name: "query_range_hist",
705+
}),
698706
}
699707
apiWithLabelLookback := &QueryAPI{
700708
baseAPI: &baseAPI.BaseAPI{
@@ -706,6 +714,9 @@ func TestMetadataEndpoints(t *testing.T) {
706714
},
707715
gate: gate.New(nil, 4),
708716
defaultMetadataTimeRange: apiLookbackDelta,
717+
queryRangeHist: promauto.With(prometheus.NewRegistry()).NewHistogram(prometheus.HistogramOpts{
718+
Name: "query_range_hist",
719+
}),
709720
}
710721

711722
var tests = []endpointTestCase{
@@ -1326,6 +1337,9 @@ func TestParseDownsamplingParamMillis(t *testing.T) {
13261337
api := QueryAPI{
13271338
enableAutodownsampling: test.enableAutodownsampling,
13281339
gate: gate.New(nil, 4),
1340+
queryRangeHist: promauto.With(prometheus.NewRegistry()).NewHistogram(prometheus.HistogramOpts{
1341+
Name: "query_range_hist",
1342+
}),
13291343
}
13301344
v := url.Values{}
13311345
v.Set(MaxSourceResolutionParam, test.maxSourceResolutionParam)
@@ -1374,6 +1388,9 @@ func TestParseStoreDebugMatchersParam(t *testing.T) {
13741388
t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
13751389
api := QueryAPI{
13761390
gate: promgate.New(4),
1391+
queryRangeHist: promauto.With(prometheus.NewRegistry()).NewHistogram(prometheus.HistogramOpts{
1392+
Name: "query_range_hist",
1393+
}),
13771394
}
13781395
v := url.Values{}
13791396
v.Set(StoreMatcherParam, tc.storeMatchers)

0 commit comments

Comments
 (0)