Ruler: Move inner eval function (#15688)

Refactoring in prevision of https://github.com/prometheus/prometheus/pull/15681

By moving the `eval` function outside of the for loop, we can modify the rule execution order more easily (without huge git changes)

Signed-off-by: Julien Duchesne <julien.duchesne@grafana.com>
This commit is contained in:
Julien Duchesne 2024-12-17 16:11:55 -05:00 committed by GitHub
parent ff398062cb
commit 615195372d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -23,6 +23,9 @@ import (
"sync"
"time"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/codes"
"go.uber.org/atomic"
"github.com/prometheus/prometheus/promql/parser"
@ -30,9 +33,6 @@ import (
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/common/model"
"github.com/prometheus/common/promslog"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/codes"
"github.com/prometheus/prometheus/model/labels"
"github.com/prometheus/prometheus/model/timestamp"
@ -512,18 +512,8 @@ func (g *Group) CopyState(from *Group) {
func (g *Group) Eval(ctx context.Context, ts time.Time) {
var (
samplesTotal atomic.Float64
wg sync.WaitGroup
ruleQueryOffset = g.QueryOffset()
)
ruleQueryOffset := g.QueryOffset()
for i, rule := range g.rules {
select {
case <-g.done:
return
default:
}
eval := func(i int, rule Rule, cleanup func()) {
if cleanup != nil {
defer cleanup()
@ -656,6 +646,14 @@ func (g *Group) Eval(ctx context.Context, ts time.Time) {
}
}
var wg sync.WaitGroup
for i, rule := range g.rules {
select {
case <-g.done:
return
default:
}
if ctrl := g.concurrencyController; ctrl.Allow(ctx, g, rule) {
wg.Add(1)
@ -667,7 +665,6 @@ func (g *Group) Eval(ctx context.Context, ts time.Time) {
eval(i, rule, nil)
}
}
wg.Wait()
g.metrics.GroupSamples.WithLabelValues(GroupKey(g.File(), g.Name())).Set(samplesTotal.Load())