Skip to content

Commit

Permalink
Limit the default time window for label name and label value APIs
Browse files Browse the repository at this point in the history
Signed-off-by: Kemal Akkoyun <kakkoyun@gmail.com>
  • Loading branch information
kakkoyun committed Sep 11, 2020
1 parent e0b7f7b commit 87bd71b
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 7 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
6 changes: 6 additions & 0 deletions cmd/thanos/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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("<name>=\"<value>\"").Strings()

Expand Down Expand Up @@ -192,6 +195,7 @@ func registerQuery(app *extkingpin.App) {
*dnsSDResolver,
time.Duration(*unhealthyStoreTimeout),
time.Duration(*instantDefaultMaxSourceResolution),
time.Duration(*defaultLabelLookbackDelta),
*strictStores,
component.Query,
)
Expand Down Expand Up @@ -240,6 +244,7 @@ func runQuery(
dnsSDResolver string,
unhealthyStoreTimeout time.Duration,
instantDefaultMaxSourceResolution time.Duration,
defaultLabelLookbackDelta time.Duration,
strictStores []string,
comp component.Component,
) error {
Expand Down Expand Up @@ -436,6 +441,7 @@ func runQuery(
queryReplicaLabels,
flagsMap,
instantDefaultMaxSourceResolution,
defaultLabelLookbackDelta,
maxConcurrentQueries,
)

Expand Down
4 changes: 4 additions & 0 deletions docs/components/query.md
Original file line number Diff line number Diff line change
Expand Up @@ -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=<name>="<value>" ...
Query selector labels that will be exposed in
info endpoint (repeated).
Expand Down
20 changes: 14 additions & 6 deletions pkg/api/query/v1.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -93,6 +95,7 @@ func NewQueryAPI(
replicaLabels []string,
flagsMap map[string]string,
defaultInstantQueryMaxSourceResolution time.Duration,
defaultLabelLookbackDelta time.Duration,
maxConcurrentQueries int,
) *QueryAPI {
return &QueryAPI{
Expand All @@ -110,6 +113,7 @@ func NewQueryAPI(
replicaLabels: replicaLabels,
storeSet: storeSet,
defaultInstantQueryMaxSourceResolution: defaultInstantQueryMaxSourceResolution,
defaultLabelLookbackDelta: defaultLabelLookbackDelta,
}
}

Expand Down Expand Up @@ -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}
}
Expand Down Expand Up @@ -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}
}
Expand Down

0 comments on commit 87bd71b

Please sign in to comment.