Skip to content

Commit

Permalink
mimirtool rules sync: fix sync when query_offset or evaluation_delay …
Browse files Browse the repository at this point in the history
…change (#8297)

* mimirtool rules sync: fix sync when query_offset or evaluation_delay change

Signed-off-by: Marco Pracucci <[email protected]>

* Fixed and simplified comparison

Signed-off-by: Marco Pracucci <[email protected]>

---------

Signed-off-by: Marco Pracucci <[email protected]>
  • Loading branch information
pracucci authored Jun 7, 2024
1 parent 07722a5 commit 8ce49e3
Show file tree
Hide file tree
Showing 3 changed files with 205 additions and 165 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@
* [ENHANCEMENT] `mimirtool promql format`: Format PromQL query with Prometheus' string or pretty-print formatter. #7742
* [BUGFIX] `mimirtool rules prepare`: do not add aggregation label to `on()` clause if already present in `group_left()` or `group_right()`. #7839
* [BUGFIX] Analyze Grafana: fix parsing queries with variables. #8062
* [BUGFIX] `mimirtool rules sync`: detect a change when the `query_offset` or the deprecated `evaluation_delay` configuration changes. #8297

### Mimir Continuous Test

Expand Down
32 changes: 27 additions & 5 deletions pkg/mimirtool/rules/compare.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,21 @@ import (
"strings"

"github.com/mitchellh/colorstring"
"github.com/prometheus/common/model"
"github.com/prometheus/prometheus/model/rulefmt"
yaml "gopkg.in/yaml.v3"

"github.com/grafana/mimir/pkg/mimirtool/rules/rwrulefmt"
)

var (
errNameDiff = errors.New("rule groups are named differently")
errIntervalDiff = errors.New("rule groups have different intervals")
errDiffRuleLen = errors.New("rule groups have a different number of rules")
errDiffRWConfigs = errors.New("rule groups have different remote write configs")
errDiffSourceTenants = errors.New("rule groups have different source tenants")
errNameDiff = errors.New("rule groups are named differently")
errIntervalDiff = errors.New("rule groups have different intervals")
errDiffRuleLen = errors.New("rule groups have a different number of rules")
errDiffRWConfigs = errors.New("rule groups have different remote write configs")
errDiffSourceTenants = errors.New("rule groups have different source tenants")
errDiffEvaluationDelay = errors.New("rule groups have different evaluation delay")
errDiffQueryOffset = errors.New("rule groups have different query offset")
)

// NamespaceState is used to denote the difference between the staged namespace
Expand Down Expand Up @@ -132,6 +135,15 @@ func CompareGroups(groupOne, groupTwo rwrulefmt.RuleGroup) error {
return errDiffRWConfigs
}

//nolint:staticcheck // We want to intentionally access a deprecated field
if getEvaluationDelayOrQueryOffsetValue(groupOne.EvaluationDelay) != getEvaluationDelayOrQueryOffsetValue(groupTwo.EvaluationDelay) {
return errDiffEvaluationDelay
}

if getEvaluationDelayOrQueryOffsetValue(groupOne.QueryOffset) != getEvaluationDelayOrQueryOffsetValue(groupTwo.QueryOffset) {
return errDiffQueryOffset
}

for i := range groupOne.RWConfigs {
if groupOne.RWConfigs[i].URL != groupTwo.RWConfigs[i].URL {
return errDiffRWConfigs
Expand Down Expand Up @@ -320,3 +332,13 @@ func PrintComparisonResult(results []NamespaceChange, verbose bool) error {
fmt.Printf("Diff Summary: %v Groups Created, %v Groups Updated, %v Groups Deleted\n", created, updated, deleted)
return nil
}

func getEvaluationDelayOrQueryOffsetValue(value *model.Duration) model.Duration {
if value == nil {
// Mimir ruler considers a value of 0 as if the EvaluationDelay has not been set (nil),
// so when we compare the configured value we consider nil as 0.
return 0
}

return *value
}
Loading

0 comments on commit 8ce49e3

Please sign in to comment.