From 87bd71bdd55aac530b7fd0cb01c5af5a9756e6f8 Mon Sep 17 00:00:00 2001 From: Kemal Akkoyun Date: Wed, 9 Sep 2020 18:54:05 +0300 Subject: [PATCH] Limit the default time window for label name and label value APIs Signed-off-by: Kemal Akkoyun --- CHANGELOG.md | 3 ++- cmd/thanos/query.go | 6 ++++++ docs/components/query.md | 4 ++++ pkg/api/query/v1.go | 20 ++++++++++++++------ 4 files changed, 26 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c4e9b530337..a4a1c7ec2db 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,7 +13,8 @@ We use *breaking :warning:* to mark changes that are not backward compatible (re ### Added -- [#3133](https://github.com/thanos-io/thanos/pull/3133) Query: Allow passing a `storeMatch[]` to Labels APIs. Also time range metadata based store filtering is supported on Labels APIs. +- [#3133](https://github.com/thanos-io/thanos/pull/3133) Querier: Allow passing a `storeMatch[]` to Labels APIs. Also time range metadata based store filtering is supported on Labels APIs. +- [#3147](https://github.com/thanos-io/thanos/pull/3147) Querier: Add `query.labels.lookback-delta` flag to specify the default lookback duration for retrieving labels through Labels APIs when time range parameters are not specified. ### Changed diff --git a/cmd/thanos/query.go b/cmd/thanos/query.go index db2c7b7bf13..6426bce6990 100644 --- a/cmd/thanos/query.go +++ b/cmd/thanos/query.go @@ -23,6 +23,7 @@ import ( "github.com/prometheus/prometheus/discovery/targetgroup" "github.com/prometheus/prometheus/pkg/labels" "github.com/prometheus/prometheus/promql" + "github.com/thanos-io/thanos/pkg/extkingpin" v1 "github.com/thanos-io/thanos/pkg/api/query" @@ -80,6 +81,8 @@ func registerQuery(app *extkingpin.App) { instantDefaultMaxSourceResolution := modelDuration(cmd.Flag("query.instant.default.max_source_resolution", "default value for max_source_resolution for instant queries. If not set, defaults to 0s only taking raw resolution into account. 1h can be a good value if you use instant queries over time ranges that incorporate times outside of your raw-retention.").Default("0s").Hidden()) + defaultLabelLookbackDelta := cmd.Flag("query.labels.lookback-delta", "The default lookback duration for retrieving labels through Labels API when the range parameters are not specified.").Default("2h").Duration() + selectorLabels := cmd.Flag("selector-label", "Query selector labels that will be exposed in info endpoint (repeated)."). PlaceHolder("=\"\"").Strings() @@ -192,6 +195,7 @@ func registerQuery(app *extkingpin.App) { *dnsSDResolver, time.Duration(*unhealthyStoreTimeout), time.Duration(*instantDefaultMaxSourceResolution), + time.Duration(*defaultLabelLookbackDelta), *strictStores, component.Query, ) @@ -240,6 +244,7 @@ func runQuery( dnsSDResolver string, unhealthyStoreTimeout time.Duration, instantDefaultMaxSourceResolution time.Duration, + defaultLabelLookbackDelta time.Duration, strictStores []string, comp component.Component, ) error { @@ -436,6 +441,7 @@ func runQuery( queryReplicaLabels, flagsMap, instantDefaultMaxSourceResolution, + defaultLabelLookbackDelta, maxConcurrentQueries, ) diff --git a/docs/components/query.md b/docs/components/query.md index 8ac85cc9afc..64f9b047346 100644 --- a/docs/components/query.md +++ b/docs/components/query.md @@ -389,6 +389,10 @@ Flags: able to query without deduplication using 'dedup=false' parameter. Data includes time series, recording rules, and alerting rules. + --query.labels.lookback-delta=2h + The default lookback duration for retrieving + labels through Labels API when the range + parameters are not specified. --selector-label=="" ... Query selector labels that will be exposed in info endpoint (repeated). diff --git a/pkg/api/query/v1.go b/pkg/api/query/v1.go index dbd76e067b7..2d5cc1cc20c 100644 --- a/pkg/api/query/v1.go +++ b/pkg/api/query/v1.go @@ -73,10 +73,12 @@ type QueryAPI struct { enableAutodownsampling bool enableQueryPartialResponse bool enableRulePartialResponse bool - replicaLabels []string - storeSet *query.StoreSet + replicaLabels []string + storeSet *query.StoreSet + defaultInstantQueryMaxSourceResolution time.Duration + defaultLabelLookbackDelta time.Duration } // NewQueryAPI returns an initialized QueryAPI type. @@ -93,6 +95,7 @@ func NewQueryAPI( replicaLabels []string, flagsMap map[string]string, defaultInstantQueryMaxSourceResolution time.Duration, + defaultLabelLookbackDelta time.Duration, maxConcurrentQueries int, ) *QueryAPI { return &QueryAPI{ @@ -110,6 +113,7 @@ func NewQueryAPI( replicaLabels: replicaLabels, storeSet: storeSet, defaultInstantQueryMaxSourceResolution: defaultInstantQueryMaxSourceResolution, + defaultLabelLookbackDelta: defaultLabelLookbackDelta, } } @@ -423,11 +427,13 @@ func (qapi *QueryAPI) labelValues(r *http.Request) (interface{}, []error, *api.A return nil, nil, &api.ApiError{Typ: api.ErrorBadData, Err: errors.Errorf("invalid label name: %q", name)} } - start, err := parseTimeParam(r, "start", minTime) + // If start and end time not specified, we get the last 2h window by default. + now := time.Now() + start, err := parseTimeParam(r, "start", now.Add(-qapi.defaultLabelLookbackDelta)) if err != nil { return nil, nil, &api.ApiError{Typ: api.ErrorBadData, Err: err} } - end, err := parseTimeParam(r, "end", maxTime) + end, err := parseTimeParam(r, "end", now) if err != nil { return nil, nil, &api.ApiError{Typ: api.ErrorBadData, Err: err} } @@ -589,11 +595,13 @@ func parseDuration(s string) (time.Duration, error) { func (qapi *QueryAPI) labelNames(r *http.Request) (interface{}, []error, *api.ApiError) { ctx := r.Context() - start, err := parseTimeParam(r, "start", minTime) + // If start and end time not specified, we get the last 2h window by default. + now := time.Now() + start, err := parseTimeParam(r, "start", now.Add(-qapi.defaultLabelLookbackDelta)) if err != nil { return nil, nil, &api.ApiError{Typ: api.ErrorBadData, Err: err} } - end, err := parseTimeParam(r, "end", maxTime) + end, err := parseTimeParam(r, "end", now) if err != nil { return nil, nil, &api.ApiError{Typ: api.ErrorBadData, Err: err} }