Refactoring for simplicity

Include labels

Signed-off-by: Danny Kopping <danny.kopping@grafana.com>
This commit is contained in:
Danny Kopping 2023-01-09 10:53:49 +02:00
parent d8f3e7d16c
commit 72527b5f12
No known key found for this signature in database
GPG key ID: 41DF981954E816FD
5 changed files with 51 additions and 21 deletions

View file

@ -311,7 +311,7 @@ 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 time.Time, query QueryFunc, externalURL *url.URL, limit int) (promql.Vector, error) { func (r *AlertingRule) Eval(ctx context.Context, ts time.Time, query QueryFunc, externalURL *url.URL, limit int) (promql.Vector, error) {
ctx = NewOriginContext(ctx, NewRuleDetail(r.Name(), r.Query().String(), KindAlerting)) ctx = NewOriginContext(ctx, NewRuleDetail(r))
res, err := query(ctx, r.vector.String(), ts) res, err := query(ctx, r.vector.String(), ts)
if err != nil { if err != nil {

View file

@ -713,14 +713,19 @@ func TestSendAlertsDontAffectActiveAlerts(t *testing.T) {
require.Equal(t, labels.FromStrings("a1", "1"), rule.active[h].Labels) require.Equal(t, labels.FromStrings("a1", "1"), rule.active[h].Labels)
} }
// TestAlertingEvalWithOrigin checks that the alerting rule details are passed through the context.
func TestAlertingEvalWithOrigin(t *testing.T) { func TestAlertingEvalWithOrigin(t *testing.T) {
ctx := context.Background() ctx := context.Background()
now := time.Now() now := time.Now()
const name = "my-recording-rule" const (
const query = `count(metric{foo="bar"}) > 0` name = "my-recording-rule"
query = `count(metric{foo="bar"}) > 0`
var detail RuleDetail )
var (
detail RuleDetail
lbs = labels.FromStrings("test", "test")
)
expr, err := parser.ParseExpr(query) expr, err := parser.ParseExpr(query)
require.NoError(t, err) require.NoError(t, err)
@ -729,7 +734,7 @@ func TestAlertingEvalWithOrigin(t *testing.T) {
name, name,
expr, expr,
time.Minute, time.Minute,
labels.FromStrings("test", "test"), lbs,
nil, nil,
nil, nil,
"", "",
@ -742,5 +747,5 @@ func TestAlertingEvalWithOrigin(t *testing.T) {
}, nil, 0) }, nil, 0)
require.NoError(t, err) require.NoError(t, err)
require.Equal(t, detail, NewRuleDetail(name, query, KindAlerting)) require.Equal(t, detail, NewRuleDetail(rule))
} }

View file

@ -1,13 +1,19 @@
package rules package rules
import "context" import (
"context"
"github.com/prometheus/prometheus/model/labels"
)
type ruleOrigin struct{} type ruleOrigin struct{}
// RuleDetail contains information about the rule that is being evaluated.
type RuleDetail struct { type RuleDetail struct {
Name string Name string
Kind string Query string
Query string Labels labels.Labels
Kind string
} }
const ( const (
@ -15,11 +21,23 @@ const (
KindRecording = "recording" KindRecording = "recording"
) )
func NewRuleDetail(name, query, kind string) RuleDetail { // NewRuleDetail creates a RuleDetail from a given Rule.
func NewRuleDetail(r Rule) RuleDetail {
var kind string
switch r.(type) {
case *AlertingRule:
kind = KindAlerting
case *RecordingRule:
kind = KindRecording
default:
kind = "unknown"
}
return RuleDetail{ return RuleDetail{
Name: name, Name: r.Name(),
Query: query, Query: r.Query().String(),
Kind: kind, Labels: r.Labels(),
Kind: kind,
} }
} }
@ -28,6 +46,7 @@ func NewOriginContext(ctx context.Context, rule RuleDetail) context.Context {
return context.WithValue(ctx, ruleOrigin{}, rule) return context.WithValue(ctx, ruleOrigin{}, rule)
} }
// FromOriginContext returns the RuleDetail origin data from the context.
func FromOriginContext(ctx context.Context) RuleDetail { func FromOriginContext(ctx context.Context) RuleDetail {
if rule, ok := ctx.Value(ruleOrigin{}).(RuleDetail); ok { if rule, ok := ctx.Value(ruleOrigin{}).(RuleDetail); ok {
return rule return rule

View file

@ -73,7 +73,7 @@ func (rule *RecordingRule) Labels() labels.Labels {
// 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, ts time.Time, query QueryFunc, _ *url.URL, limit int) (promql.Vector, error) { func (rule *RecordingRule) Eval(ctx context.Context, ts time.Time, query QueryFunc, _ *url.URL, limit int) (promql.Vector, error) {
ctx = NewOriginContext(ctx, NewRuleDetail(rule.Name(), rule.Query().String(), KindRecording)) ctx = NewOriginContext(ctx, NewRuleDetail(rule))
vector, err := query(ctx, rule.vector.String(), ts) vector, err := query(ctx, rule.vector.String(), ts)
if err != nil { if err != nil {

View file

@ -157,24 +157,30 @@ func TestRecordingRuleLimit(t *testing.T) {
} }
} }
// TestRecordingEvalWithOrigin checks that the recording rule details are passed through the context.
func TestRecordingEvalWithOrigin(t *testing.T) { func TestRecordingEvalWithOrigin(t *testing.T) {
ctx := context.Background() ctx := context.Background()
now := time.Now() now := time.Now()
const name = "my-recording-rule" const (
const query = `count(metric{foo="bar"})` name = "my-recording-rule"
query = `count(metric{foo="bar"})`
)
var detail RuleDetail var (
detail RuleDetail
lbs = labels.FromStrings("foo", "bar")
)
expr, err := parser.ParseExpr(query) expr, err := parser.ParseExpr(query)
require.NoError(t, err) require.NoError(t, err)
rule := NewRecordingRule(name, expr, []labels.Label{}) rule := NewRecordingRule(name, expr, lbs)
_, err = rule.Eval(ctx, now, func(ctx context.Context, qs string, _ time.Time) (promql.Vector, error) { _, err = rule.Eval(ctx, now, func(ctx context.Context, qs string, _ time.Time) (promql.Vector, error) {
detail = FromOriginContext(ctx) detail = FromOriginContext(ctx)
return nil, nil return nil, nil
}, nil, 0) }, nil, 0)
require.NoError(t, err) require.NoError(t, err)
require.Equal(t, detail, NewRuleDetail(name, query, KindRecording)) require.Equal(t, detail, NewRuleDetail(rule))
} }