Skip to content

Commit f47fc78

Browse files
committed
Add back lookback delta
Signed-off-by: Justin Jung <[email protected]>
1 parent 2bab107 commit f47fc78

File tree

8 files changed

+28
-18
lines changed

8 files changed

+28
-18
lines changed

docs/configuration/config-file-reference.md

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5091,14 +5091,15 @@ otel:
50915091
# Regex that the query string should match. If not set, it won't be checked.
50925092
[regex: <string> | default = ""]
50935093
5094-
# Time window that the query should be within. If not set, it won't be checked.
5094+
# Time window that the evaluated query (including subqueries and querier
5095+
# lookback period) should be within. If not set, it won't be checked.
50955096
time_window:
5096-
# Start of the time window that the query should be within. If set to 0, it
5097-
# won't be checked.
5097+
# Start of the time window that the evaluated query (including subqueries and
5098+
# querier lookback period) should be within. If set to 0, it won't be checked.
50985099
[start: <int> | default = 0]
50995100
5100-
# End of the time window that the query should be within. If set to 0, it
5101-
# won't be checked.
5101+
# End of the time window that the evaluated query (including subqueries and
5102+
# querier lookback period) should be within. If set to 0, it won't be checked.
51025103
[end: <int> | default = 0]
51035104
```
51045105

pkg/cortex/modules.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -478,6 +478,7 @@ func (t *Cortex) initQueryFrontendTripperware() (serv services.Service, err erro
478478
queryAnalyzer,
479479
t.Cfg.Querier.DefaultEvaluationInterval,
480480
t.Cfg.Querier.MaxSubQuerySteps,
481+
t.Cfg.Querier.LookbackDelta,
481482
)
482483

483484
return services.NewIdleService(nil, func(_ error) error {

pkg/querier/tripperware/priority.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ var (
1717
errParseExpr = errors.New("failed to parse expr")
1818
)
1919

20-
func GetPriority(r *http.Request, userID string, limits Limits, now time.Time) (int64, error) {
20+
func GetPriority(r *http.Request, userID string, limits Limits, now time.Time, lookbackDelta time.Duration) (int64, error) {
2121
isQuery := strings.HasSuffix(r.URL.Path, "/query")
2222
isQueryRange := strings.HasSuffix(r.URL.Path, "/query_range")
2323
queryPriority := limits.QueryPriority(userID)
@@ -54,9 +54,10 @@ func GetPriority(r *http.Request, userID string, limits Limits, now time.Time) (
5454
}
5555

5656
es := &parser.EvalStmt{
57-
Expr: expr,
58-
Start: util.TimeFromMillis(startTime),
59-
End: util.TimeFromMillis(endTime),
57+
Expr: expr,
58+
Start: util.TimeFromMillis(startTime),
59+
End: util.TimeFromMillis(endTime),
60+
LookbackDelta: lookbackDelta,
6061
}
6162

6263
minTime, maxTime := promql.FindMinMaxTime(es)

pkg/querier/tripperware/priority_test.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ func Test_GetPriorityShouldReturnDefaultPriorityIfNotEnabledOrInvalidQueryString
6868
t.Run(testName, func(t *testing.T) {
6969
limits.queryPriority.Enabled = testData.queryPriorityEnabled
7070
req, _ := http.NewRequest(http.MethodPost, testData.url, bytes.NewReader([]byte{}))
71-
priority, err := GetPriority(req, "", limits, now)
71+
priority, err := GetPriority(req, "", limits, now, 0)
7272
if err != nil {
7373
assert.Equal(t, testData.err, err)
7474
} else {
@@ -132,7 +132,7 @@ func Test_GetPriorityShouldConsiderRegex(t *testing.T) {
132132
limits.queryPriority.Priorities[0].QueryAttributes[0].Regex = testData.regex
133133
limits.queryPriority.Priorities[0].QueryAttributes[0].CompiledRegex = regexp.MustCompile(testData.regex)
134134
req, _ := http.NewRequest(http.MethodPost, "/query?query="+testData.query, bytes.NewReader([]byte{}))
135-
priority, err := GetPriority(req, "", limits, now)
135+
priority, err := GetPriority(req, "", limits, now, 0)
136136
assert.NoError(t, err)
137137
assert.Equal(t, int64(testData.expectedPriority), priority)
138138
})
@@ -165,6 +165,7 @@ func Test_GetPriorityShouldConsiderStartAndEndTime(t *testing.T) {
165165
start time.Time
166166
end time.Time
167167
expectedPriority int
168+
lookbackDelta time.Duration
168169
}
169170

170171
tests := map[string]testCase{
@@ -217,6 +218,11 @@ func Test_GetPriorityShouldConsiderStartAndEndTime(t *testing.T) {
217218
end: now.Add(-10 * time.Minute),
218219
expectedPriority: 0,
219220
},
221+
"should consider lookback delta": {
222+
time: now.Add(-45 * time.Minute),
223+
expectedPriority: 0,
224+
lookbackDelta: 2 * time.Minute,
225+
},
220226
}
221227

222228
for testName, testData := range tests {
@@ -231,7 +237,7 @@ func Test_GetPriorityShouldConsiderStartAndEndTime(t *testing.T) {
231237
url = "/query?query=sum(up)"
232238
}
233239
req, _ := http.NewRequest(http.MethodPost, url, bytes.NewReader([]byte{}))
234-
priority, err := GetPriority(req, "", limits, now)
240+
priority, err := GetPriority(req, "", limits, now, testData.lookbackDelta)
235241
assert.NoError(t, err)
236242
assert.Equal(t, int64(testData.expectedPriority), priority)
237243
})
@@ -286,7 +292,7 @@ func Test_GetPriorityShouldNotConsiderStartAndEndTimeIfEmpty(t *testing.T) {
286292
url = "/query?query=sum(up)"
287293
}
288294
req, _ := http.NewRequest(http.MethodPost, url, bytes.NewReader([]byte{}))
289-
priority, err := GetPriority(req, "", limits, now)
295+
priority, err := GetPriority(req, "", limits, now, 0)
290296
assert.NoError(t, err)
291297
assert.Equal(t, int64(1), priority)
292298
})

pkg/querier/tripperware/queryrange/query_range_middlewares_test.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,6 @@ func TestRoundTrip(t *testing.T) {
7474
qa,
7575
time.Minute,
7676
0,
77-
0,
7877
)
7978

8079
for i, tc := range []struct {

pkg/querier/tripperware/roundtrip.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ func NewQueryTripperware(
105105
queryAnalyzer querysharding.Analyzer,
106106
defaultSubQueryInterval time.Duration,
107107
maxSubQuerySteps int64,
108+
lookbackDelta time.Duration,
108109
) Tripperware {
109110
// Per tenant query metrics.
110111
queriesPerTenant := promauto.With(registerer).NewCounterVec(prometheus.CounterOpts{
@@ -159,7 +160,7 @@ func NewQueryTripperware(
159160
}
160161

161162
if limits != nil && limits.QueryPriority(userStr).Enabled {
162-
priority, err := GetPriority(r, userStr, limits, now)
163+
priority, err := GetPriority(r, userStr, limits, now, lookbackDelta)
163164
if err != nil && err == errParseExpr {
164165
// If query is invalid, no need to go through tripperwares
165166
// for further splitting.

pkg/querier/tripperware/roundtrip_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,7 @@ func TestRoundTrip(t *testing.T) {
201201
querysharding.NewQueryAnalyzer(),
202202
time.Minute,
203203
tc.maxSubQuerySteps,
204+
0,
204205
)
205206
resp, err := tw(downstream).RoundTrip(req)
206207
if tc.expectedErr == nil {

pkg/util/validation/limits.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,13 +64,13 @@ type PriorityDef struct {
6464

6565
type QueryAttribute struct {
6666
Regex string `yaml:"regex" json:"regex" doc:"nocli|description=Regex that the query string should match. If not set, it won't be checked."`
67-
TimeWindow TimeWindow `yaml:"time_window" json:"time_window" doc:"nocli|description=Time window that the query should be within. If not set, it won't be checked."`
67+
TimeWindow TimeWindow `yaml:"time_window" json:"time_window" doc:"nocli|description=Time window that the evaluated query (including subqueries and querier lookback period) should be within. If not set, it won't be checked."`
6868
CompiledRegex *regexp.Regexp
6969
}
7070

7171
type TimeWindow struct {
72-
Start model.Duration `yaml:"start" json:"start" doc:"nocli|description=Start of the time window that the query should be within. If set to 0, it won't be checked.|default=0"`
73-
End model.Duration `yaml:"end" json:"end" doc:"nocli|description=End of the time window that the query should be within. If set to 0, it won't be checked.|default=0"`
72+
Start model.Duration `yaml:"start" json:"start" doc:"nocli|description=Start of the time window that the evaluated query (including subqueries and querier lookback period) should be within. If set to 0, it won't be checked.|default=0"`
73+
End model.Duration `yaml:"end" json:"end" doc:"nocli|description=End of the time window that the evaluated query (including subqueries and querier lookback period) should be within. If set to 0, it won't be checked.|default=0"`
7474
}
7575

7676
// Limits describe all the limits for users; can be used to describe global default

0 commit comments

Comments
 (0)