Make rule evaluation publicly available

Means that a third-party can parse rules and run them with their own
execution model.
This commit is contained in:
Jonathan Lange 2016-11-18 16:12:50 +00:00
parent 9b7e097a76
commit 2a2da40223
5 changed files with 8 additions and 8 deletions

View file

@ -146,9 +146,9 @@ func (r *AlertingRule) sample(alert *Alert, ts model.Time, set bool) *model.Samp
// is kept in memory state and consequentally repeatedly sent to the AlertManager. // is kept in memory state and consequentally repeatedly sent to the AlertManager.
const resolvedRetention = 15 * time.Minute const resolvedRetention = 15 * time.Minute
// eval evaluates the rule expression and then creates pending alerts and fires // Eval evaluates the rule expression and then creates pending alerts and fires
// or removes previously pending alerts accordingly. // or removes previously pending alerts accordingly.
func (r *AlertingRule) eval(ctx context.Context, ts model.Time, engine *promql.Engine, externalURLPath string) (model.Vector, error) { func (r *AlertingRule) Eval(ctx context.Context, ts model.Time, engine *promql.Engine, externalURLPath string) (model.Vector, error) {
query, err := engine.NewInstantQuery(r.vector.String(), ts) query, err := engine.NewInstantQuery(r.vector.String(), ts)
if err != nil { if err != nil {
return nil, err return nil, err

View file

@ -106,7 +106,7 @@ const (
type Rule interface { type Rule interface {
Name() string Name() string
// eval evaluates the rule, including any associated recording or alerting actions. // eval evaluates the rule, including any associated recording or alerting actions.
eval(context.Context, model.Time, *promql.Engine, string) (model.Vector, error) Eval(context.Context, model.Time, *promql.Engine, string) (model.Vector, error)
// String returns a human-readable string representation of the rule. // String returns a human-readable string representation of the rule.
String() string String() string
// HTMLSnippet returns a human-readable string representation of the rule, // HTMLSnippet returns a human-readable string representation of the rule,
@ -257,7 +257,7 @@ func (g *Group) eval() {
evalTotal.WithLabelValues(rtyp).Inc() evalTotal.WithLabelValues(rtyp).Inc()
vector, err := rule.eval(g.opts.Context, now, g.opts.QueryEngine, g.opts.ExternalURL.Path) vector, err := rule.Eval(g.opts.Context, now, g.opts.QueryEngine, g.opts.ExternalURL.Path)
if err != nil { if err != nil {
// Canceled queries are intentional termination of queries. This normally // Canceled queries are intentional termination of queries. This normally
// happens on shutdown and thus we skip logging of any errors here. // happens on shutdown and thus we skip logging of any errors here.

View file

@ -105,7 +105,7 @@ func TestAlertingRule(t *testing.T) {
for i, test := range tests { for i, test := range tests {
evalTime := model.Time(0).Add(test.time) evalTime := model.Time(0).Add(test.time)
res, err := rule.eval(suite.Context(), evalTime, suite.QueryEngine(), "") res, err := rule.Eval(suite.Context(), evalTime, suite.QueryEngine(), "")
if err != nil { if err != nil {
t.Fatalf("Error during alerting rule evaluation: %s", err) t.Fatalf("Error during alerting rule evaluation: %s", err)
} }

View file

@ -45,8 +45,8 @@ func (rule RecordingRule) Name() string {
return rule.name return rule.name
} }
// eval evaluates the rule and then overrides the metric names and labels accordingly. // Eval evaluates the rule and then overrides the metric names and labels accordingly.
func (rule RecordingRule) eval(ctx context.Context, timestamp model.Time, engine *promql.Engine, _ string) (model.Vector, error) { func (rule RecordingRule) Eval(ctx context.Context, timestamp model.Time, engine *promql.Engine, _ string) (model.Vector, error) {
query, err := engine.NewInstantQuery(rule.vector.String(), timestamp) query, err := engine.NewInstantQuery(rule.vector.String(), timestamp)
if err != nil { if err != nil {
return nil, err return nil, err

View file

@ -63,7 +63,7 @@ func TestRuleEval(t *testing.T) {
for _, test := range suite { for _, test := range suite {
rule := NewRecordingRule(test.name, test.expr, test.labels) rule := NewRecordingRule(test.name, test.expr, test.labels)
result, err := rule.eval(ctx, now, engine, "") result, err := rule.Eval(ctx, now, engine, "")
if err != nil { if err != nil {
t.Fatalf("Error evaluating %s", test.name) t.Fatalf("Error evaluating %s", test.name)
} }