-
Notifications
You must be signed in to change notification settings - Fork 833
Configurable delay on rule evaluation #2423
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
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,8 +2,12 @@ package ruler | |
|
||
import ( | ||
"context" | ||
"time" | ||
|
||
"github.com/pkg/errors" | ||
"github.com/prometheus/prometheus/pkg/labels" | ||
"github.com/prometheus/prometheus/promql" | ||
"github.com/prometheus/prometheus/rules" | ||
"github.com/prometheus/prometheus/storage" | ||
"github.com/weaveworks/common/user" | ||
|
||
|
@@ -78,3 +82,32 @@ func (t *tsdb) StartTime() (int64, error) { | |
func (t *tsdb) Close() error { | ||
return nil | ||
} | ||
|
||
// engineQueryFunc returns a new query function that executes instant queries against | ||
// the given engine, after subtracting the provided delay from the instant query timestamp. | ||
// It converts scalar into vector results. | ||
// Based on https://github.com/prometheus/prometheus/blob/ecda6013edf58bf645c6661b9f78ccce03b1f315/rules/manager.go#L162-L187 | ||
func engineQueryFunc(engine *promql.Engine, q storage.Queryable, delay time.Duration) rules.QueryFunc { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This whole function is a copy of
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. +1 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will do. |
||
return func(ctx context.Context, qs string, t time.Time) (promql.Vector, error) { | ||
t = t.Add(-delay) | ||
q, err := engine.NewInstantQuery(q, qs, t) | ||
if err != nil { | ||
return nil, err | ||
} | ||
res := q.Exec(ctx) | ||
if res.Err != nil { | ||
return nil, res.Err | ||
} | ||
switch v := res.Value.(type) { | ||
case promql.Vector: | ||
return v, nil | ||
case promql.Scalar: | ||
return promql.Vector{promql.Sample{ | ||
Point: promql.Point(v), | ||
Metric: labels.Labels{}, | ||
}}, nil | ||
default: | ||
return nil, errors.New("rule result is not a vector or scalar") | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -61,6 +61,9 @@ type Config struct { | |||||
ExternalURL flagext.URLValue `yaml:"external_url"` | ||||||
// How frequently to evaluate rules by default. | ||||||
EvaluationInterval time.Duration `yaml:"evaluation_interval"` | ||||||
// Delay the evaluation of all rules by a set interval to give a buffer | ||||||
// to metric that haven't been forwarded to cortex yet. | ||||||
EvaluationDelay time.Duration `yaml:"evaluation_delay"` | ||||||
// How frequently to poll for updated rules. | ||||||
PollInterval time.Duration `yaml:"poll_interval"` | ||||||
// Rule Storage and Polling configuration. | ||||||
|
@@ -103,6 +106,7 @@ func (cfg *Config) RegisterFlags(f *flag.FlagSet) { | |||||
cfg.ExternalURL.URL, _ = url.Parse("") // Must be non-nil | ||||||
f.Var(&cfg.ExternalURL, "ruler.external.url", "URL of alerts return path.") | ||||||
f.DurationVar(&cfg.EvaluationInterval, "ruler.evaluation-interval", 1*time.Minute, "How frequently to evaluate rules") | ||||||
f.DurationVar(&cfg.EvaluationDelay, "ruler.evaluation-delay", 0, "Set interval to delay the evaluation of rules to ensure they underlying metrics have been pushed to cortex. Default ") | ||||||
|
f.DurationVar(&cfg.EvaluationDelay, "ruler.evaluation-delay", 0, "Set interval to delay the evaluation of rules to ensure they underlying metrics have been pushed to cortex. Default ") | |
f.DurationVar(&cfg.EvaluationDelay, "ruler.evaluation-delay", 0, "Set interval to delay the evaluation of rules to ensure they underlying metrics have been pushed to cortex. ") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
evaluation_delay_time
/evaluation_delay_duration
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
evaluation_delay_duration
makes sense