prometheus/scrape/rules_test.go
Filip Petkovski 2ccea83913
Implement rule evaluations
This commit extends Prometheus with the option to run aggregation rules
during scrape time, before relabeling takes place.

All rules are executed independently for each target in the context of
individual scrapes. Newly calculated samples are
committed to TSDB atomically with all samples from that scrape.

Metric relabeling can also be applied on series recorded from scrape rules.

Signed-off-by: Filip Petkovski <filip.petkovsky@gmail.com>
2025-03-05 13:37:57 +01:00

76 lines
2 KiB
Go

// Copyright 2022 The Prometheus Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package scrape
import (
"testing"
"time"
"github.com/prometheus/prometheus/promql"
"github.com/stretchr/testify/require"
"github.com/prometheus/prometheus/config"
"github.com/prometheus/prometheus/model/labels"
)
func TestRuleEngine(t *testing.T) {
now := time.Now()
samples := []Sample{
{
metric: labels.FromStrings("__name__", "http_requests_total", "code", "200", "handler", "/"),
t: now.UnixMilli(),
f: 10,
},
{
metric: labels.FromStrings("__name__", "http_requests_total", "code", "200", "handler", "/metrics"),
t: now.UnixMilli(),
f: 6,
},
}
rules := []*config.ScrapeRuleConfig{
{
Expr: "sum by (code) (http_requests_total)",
Record: "code:http_requests_total:sum",
},
}
engine := promql.NewEngine(promql.EngineOpts{
MaxSamples: 50000000,
Timeout: 10 * time.Second,
LookbackDelta: 5 * time.Minute,
})
re := newRuleEngine(rules, engine)
b := re.NewScrapeBatch()
for _, s := range samples {
b.AddFloatSample(s.metric, s.t, s.f)
}
result, err := re.EvaluateRules(b, now, nopMutator)
require.NoError(t, err)
if len(result) != 1 {
t.Fatalf("Invalid sample count, got %d, want %d", len(result), 3)
}
expectedSamples := []Sample{
{
metric: labels.FromStrings("__name__", "code:http_requests_total:sum", "code", "200"),
t: now.UnixMilli(),
f: 16,
},
}
require.ElementsMatch(t, expectedSamples, result)
}