From 983e1fb2a3dc6df581d5f42582fabe89b79894ce Mon Sep 17 00:00:00 2001 From: Lukasz Mierzwa Date: Tue, 27 Aug 2024 10:13:16 +0100 Subject: [PATCH] Fix default state handling --- cmd/pint/scan.go | 3 ++ cmd/pint/tests/0018_match_alerting.txt | 4 +- cmd/pint/tests/0019_match_recording.txt | 4 +- cmd/pint/tests/0020_ignore_kind.txt | 4 +- cmd/pint/tests/0037_disable_checks.txt | 6 +-- cmd/pint/tests/0039_prom_selected_path.txt | 6 +-- cmd/pint/tests/0040_rule_match_label.txt | 8 ++-- cmd/pint/tests/0052_match_multiple.txt | 4 +- cmd/pint/tests/0053_ignore_multiple.txt | 4 +- cmd/pint/tests/0092_dir_symlink.txt | 2 +- cmd/pint/tests/0095_rulefmt_symlink.txt | 4 +- cmd/pint/tests/0099_symlink_outside_glob.txt | 2 +- cmd/pint/tests/0103_file_disable.txt | 2 +- cmd/pint/tests/0111_snooze.txt | 2 +- cmd/pint/tests/0112_expired_snooze.txt | 2 +- cmd/pint/tests/0115_file_disable_tag.txt | 2 +- cmd/pint/tests/0116_file_snooze.txt | 4 +- cmd/pint/tests/0144_discovery_filepath.txt | 2 +- cmd/pint/tests/0148_discovery_prom_zero.txt | 2 +- cmd/pint/tests/0149_discovery_prom.txt | 2 +- .../tests/0152_discovery_prom_dup_uptime.txt | 2 +- cmd/pint/tests/0178_parser_include.txt | 2 +- cmd/pint/tests/0179_parser_exclude.txt | 2 +- cmd/pint/tests/0180_parser_exclude_md.txt | 2 +- cmd/pint/tests/0183_cli_enable.txt | 8 ++-- docs/changelog.md | 6 +++ internal/config/config.go | 5 ++- internal/config/parsed_rule.go | 45 +++++++++++-------- 28 files changed, 79 insertions(+), 62 deletions(-) diff --git a/cmd/pint/scan.go b/cmd/pint/scan.go index e7fed9d7..6b773490 100644 --- a/cmd/pint/scan.go +++ b/cmd/pint/scan.go @@ -78,6 +78,7 @@ func checkRules(ctx context.Context, workers int, isOffline bool, gen *config.Pr slog.String("path", entry.Path.Name), slog.String("record", entry.Rule.RecordingRule.Record.Value), slog.String("lines", entry.Rule.Lines.String()), + slog.String("state", entry.State.String()), ) } if entry.Rule.AlertingRule != nil { @@ -86,12 +87,14 @@ func checkRules(ctx context.Context, workers int, isOffline bool, gen *config.Pr slog.String("path", entry.Path.Name), slog.String("alert", entry.Rule.AlertingRule.Alert.Value), slog.String("lines", entry.Rule.Lines.String()), + slog.String("state", entry.State.String()), ) } if entry.Rule.Error.Err != nil { slog.Debug("Found invalid rule", slog.String("path", entry.Path.Name), slog.String("lines", entry.Rule.Lines.String()), + slog.String("state", entry.State.String()), ) rulesParsedTotal.WithLabelValues(config.InvalidRuleType).Inc() } diff --git a/cmd/pint/tests/0018_match_alerting.txt b/cmd/pint/tests/0018_match_alerting.txt index b60e0ded..9742cf68 100644 --- a/cmd/pint/tests/0018_match_alerting.txt +++ b/cmd/pint/tests/0018_match_alerting.txt @@ -9,9 +9,9 @@ level=INFO msg="Finding all rules to check" paths=["rules"] level=DEBUG msg="File parsed" path=rules/0001.yml rules=2 level=DEBUG msg="Glob finder completed" count=2 level=DEBUG msg="Generated all Prometheus servers" count=0 -level=DEBUG msg="Found recording rule" path=rules/0001.yml record=colo:recording lines=1-2 +level=DEBUG msg="Found recording rule" path=rules/0001.yml record=colo:recording lines=1-2 state=noop level=DEBUG msg="Configured checks for rule" enabled=["promql/syntax","alerts/for","alerts/comparison","alerts/template","promql/fragile","promql/regexp"] path=rules/0001.yml rule=colo:recording -level=DEBUG msg="Found alerting rule" path=rules/0001.yml alert=colo:alerting lines=4-5 +level=DEBUG msg="Found alerting rule" path=rules/0001.yml alert=colo:alerting lines=4-5 state=noop level=DEBUG msg="Configured checks for rule" enabled=["promql/syntax","alerts/for","alerts/comparison","alerts/template","promql/fragile","promql/regexp","promql/aggregate(job:true)"] path=rules/0001.yml rule=colo:alerting rules/0001.yml:5 Warning: Alert query doesn't have any condition, it will always fire if the metric exists. (alerts/comparison) 5 | expr: sum(bar) without(job) diff --git a/cmd/pint/tests/0019_match_recording.txt b/cmd/pint/tests/0019_match_recording.txt index 3b4e568b..b0cae3d7 100644 --- a/cmd/pint/tests/0019_match_recording.txt +++ b/cmd/pint/tests/0019_match_recording.txt @@ -9,9 +9,9 @@ level=INFO msg="Finding all rules to check" paths=["rules"] level=DEBUG msg="File parsed" path=rules/0001.yml rules=2 level=DEBUG msg="Glob finder completed" count=2 level=DEBUG msg="Generated all Prometheus servers" count=0 -level=DEBUG msg="Found recording rule" path=rules/0001.yml record=colo:recording lines=1-2 +level=DEBUG msg="Found recording rule" path=rules/0001.yml record=colo:recording lines=1-2 state=noop level=DEBUG msg="Configured checks for rule" enabled=["promql/syntax","alerts/for","alerts/comparison","alerts/template","promql/fragile","promql/regexp","promql/aggregate(job:true)"] path=rules/0001.yml rule=colo:recording -level=DEBUG msg="Found alerting rule" path=rules/0001.yml alert=colo:alerting lines=4-5 +level=DEBUG msg="Found alerting rule" path=rules/0001.yml alert=colo:alerting lines=4-5 state=noop level=DEBUG msg="Configured checks for rule" enabled=["promql/syntax","alerts/for","alerts/comparison","alerts/template","promql/fragile","promql/regexp"] path=rules/0001.yml rule=colo:alerting rules/0001.yml:2 Warning: `job` label is required and should be preserved when aggregating `^.+$` rules, remove job from `without()`. (promql/aggregate) 2 | expr: sum(foo) without(job) diff --git a/cmd/pint/tests/0020_ignore_kind.txt b/cmd/pint/tests/0020_ignore_kind.txt index f615366c..d0fb375d 100644 --- a/cmd/pint/tests/0020_ignore_kind.txt +++ b/cmd/pint/tests/0020_ignore_kind.txt @@ -9,9 +9,9 @@ level=INFO msg="Finding all rules to check" paths=["rules"] level=DEBUG msg="File parsed" path=rules/0001.yml rules=2 level=DEBUG msg="Glob finder completed" count=2 level=DEBUG msg="Generated all Prometheus servers" count=0 -level=DEBUG msg="Found recording rule" path=rules/0001.yml record=colo:recording lines=4-5 +level=DEBUG msg="Found recording rule" path=rules/0001.yml record=colo:recording lines=4-5 state=noop level=DEBUG msg="Configured checks for rule" enabled=["promql/syntax","alerts/for","alerts/comparison","alerts/template","promql/fragile","promql/regexp","promql/aggregate(job:true)"] path=rules/0001.yml rule=colo:recording -level=DEBUG msg="Found alerting rule" path=rules/0001.yml alert=colo:alerting lines=7-8 +level=DEBUG msg="Found alerting rule" path=rules/0001.yml alert=colo:alerting lines=7-8 state=noop level=DEBUG msg="Configured checks for rule" enabled=["promql/syntax","alerts/for","alerts/comparison","alerts/template","promql/fragile","promql/regexp"] path=rules/0001.yml rule=colo:alerting rules/0001.yml:5 Warning: `job` label is required and should be preserved when aggregating `^.+$` rules, remove job from `without()`. (promql/aggregate) 5 | expr: sum(foo) without(job) diff --git a/cmd/pint/tests/0037_disable_checks.txt b/cmd/pint/tests/0037_disable_checks.txt index 391a03b9..99873d41 100644 --- a/cmd/pint/tests/0037_disable_checks.txt +++ b/cmd/pint/tests/0037_disable_checks.txt @@ -11,11 +11,11 @@ level=DEBUG msg="Glob finder completed" count=3 level=INFO msg="Configured new Prometheus server" name=prom uris=1 uptime=up tags=[] include=[] exclude=[] level=DEBUG msg="Starting query workers" name=prom uri=http://127.0.0.1 workers=16 level=DEBUG msg="Generated all Prometheus servers" count=1 -level=DEBUG msg="Found alerting rule" path=rules/0001.yml alert=default-for lines=1-3 +level=DEBUG msg="Found alerting rule" path=rules/0001.yml alert=default-for lines=1-3 state=noop level=DEBUG msg="Configured checks for rule" enabled=["promql/syntax","alerts/template","promql/fragile","promql/regexp","promql/vector_matching(prom)","rule/duplicate(prom)","labels/conflict(prom)","alerts/absent(prom)"] path=rules/0001.yml rule=default-for -level=DEBUG msg="Found recording rule" path=rules/0001.yml record=sum:job lines=5-6 +level=DEBUG msg="Found recording rule" path=rules/0001.yml record=sum:job lines=5-6 state=noop level=DEBUG msg="Configured checks for rule" enabled=["promql/syntax","alerts/template","promql/fragile","promql/regexp","promql/vector_matching(prom)","rule/duplicate(prom)","labels/conflict(prom)","alerts/absent(prom)","promql/aggregate(job:true)"] path=rules/0001.yml rule=sum:job -level=DEBUG msg="Found alerting rule" path=rules/0001.yml alert=no-comparison lines=8-9 +level=DEBUG msg="Found alerting rule" path=rules/0001.yml alert=no-comparison lines=8-9 state=noop level=DEBUG msg="Configured checks for rule" enabled=["promql/syntax","alerts/template","promql/fragile","promql/regexp","promql/vector_matching(prom)","rule/duplicate(prom)","labels/conflict(prom)","alerts/absent(prom)"] path=rules/0001.yml rule=no-comparison rules/0001.yml:6 Warning: `job` label is required and should be preserved when aggregating `^.+$` rules, use `by(job, ...)`. (promql/aggregate) 6 | expr: sum(foo) diff --git a/cmd/pint/tests/0039_prom_selected_path.txt b/cmd/pint/tests/0039_prom_selected_path.txt index 9af0421c..55406ac1 100644 --- a/cmd/pint/tests/0039_prom_selected_path.txt +++ b/cmd/pint/tests/0039_prom_selected_path.txt @@ -11,11 +11,11 @@ level=DEBUG msg="Glob finder completed" count=3 level=INFO msg="Configured new Prometheus server" name=disabled uris=1 uptime=up tags=[] include=["^invalid/.+$"] exclude=["^invalid/rules/.+$"] level=DEBUG msg="Starting query workers" name=disabled uri=http://127.0.0.1:123 workers=16 level=DEBUG msg="Generated all Prometheus servers" count=1 -level=DEBUG msg="Found alerting rule" path=rules/0001.yml alert=first lines=1-3 +level=DEBUG msg="Found alerting rule" path=rules/0001.yml alert=first lines=1-3 state=noop level=DEBUG msg="Configured checks for rule" enabled=["promql/syntax","alerts/for","alerts/comparison","alerts/template","promql/fragile","promql/regexp"] path=rules/0001.yml rule=first -level=DEBUG msg="Found recording rule" path=rules/0001.yml record=second lines=5-6 +level=DEBUG msg="Found recording rule" path=rules/0001.yml record=second lines=5-6 state=noop level=DEBUG msg="Configured checks for rule" enabled=["promql/syntax","alerts/for","alerts/comparison","alerts/template","promql/fragile","promql/regexp","promql/aggregate(job:true)"] path=rules/0001.yml rule=second -level=DEBUG msg="Found alerting rule" path=rules/0001.yml alert=third lines=8-9 +level=DEBUG msg="Found alerting rule" path=rules/0001.yml alert=third lines=8-9 state=noop level=DEBUG msg="Configured checks for rule" enabled=["promql/syntax","alerts/for","alerts/comparison","alerts/template","promql/fragile","promql/regexp"] path=rules/0001.yml rule=third rules/0001.yml:6 Warning: `job` label is required and should be preserved when aggregating `^.+$` rules, use `by(job, ...)`. (promql/aggregate) 6 | expr: sum(bar) diff --git a/cmd/pint/tests/0040_rule_match_label.txt b/cmd/pint/tests/0040_rule_match_label.txt index ecdaae08..91a1b12c 100644 --- a/cmd/pint/tests/0040_rule_match_label.txt +++ b/cmd/pint/tests/0040_rule_match_label.txt @@ -9,13 +9,13 @@ level=INFO msg="Finding all rules to check" paths=["rules"] level=DEBUG msg="File parsed" path=rules/rules.yml rules=4 level=DEBUG msg="Glob finder completed" count=4 level=DEBUG msg="Generated all Prometheus servers" count=0 -level=DEBUG msg="Found recording rule" path=rules/rules.yml record=ignore lines=1-2 +level=DEBUG msg="Found recording rule" path=rules/rules.yml record=ignore lines=1-2 state=noop level=DEBUG msg="Configured checks for rule" enabled=["promql/syntax","alerts/for","alerts/comparison","alerts/template","promql/fragile","promql/regexp"] path=rules/rules.yml rule=ignore -level=DEBUG msg="Found recording rule" path=rules/rules.yml record=match lines=4-7 +level=DEBUG msg="Found recording rule" path=rules/rules.yml record=match lines=4-7 state=noop level=DEBUG msg="Configured checks for rule" enabled=["promql/syntax","alerts/for","alerts/comparison","alerts/template","promql/fragile","promql/regexp","promql/aggregate(job:true)"] path=rules/rules.yml rule=match -level=DEBUG msg="Found alerting rule" path=rules/rules.yml alert=ignore lines=9-10 +level=DEBUG msg="Found alerting rule" path=rules/rules.yml alert=ignore lines=9-10 state=noop level=DEBUG msg="Configured checks for rule" enabled=["promql/syntax","alerts/for","alerts/comparison","alerts/template","promql/fragile","promql/regexp"] path=rules/rules.yml rule=ignore -level=DEBUG msg="Found alerting rule" path=rules/rules.yml alert=match lines=12-15 +level=DEBUG msg="Found alerting rule" path=rules/rules.yml alert=match lines=12-15 state=noop level=DEBUG msg="Configured checks for rule" enabled=["promql/syntax","alerts/for","alerts/comparison","alerts/template","promql/fragile","promql/regexp","promql/aggregate(job:true)"] path=rules/rules.yml rule=match rules/rules.yml:5 Warning: `job` label is required and should be preserved when aggregating `^.*$` rules, use `by(job, ...)`. (promql/aggregate) 5 | expr: sum(foo) diff --git a/cmd/pint/tests/0052_match_multiple.txt b/cmd/pint/tests/0052_match_multiple.txt index b21b15e8..422f8ef5 100644 --- a/cmd/pint/tests/0052_match_multiple.txt +++ b/cmd/pint/tests/0052_match_multiple.txt @@ -9,9 +9,9 @@ level=INFO msg="Finding all rules to check" paths=["rules"] level=DEBUG msg="File parsed" path=rules/0001.yml rules=2 level=DEBUG msg="Glob finder completed" count=2 level=DEBUG msg="Generated all Prometheus servers" count=0 -level=DEBUG msg="Found recording rule" path=rules/0001.yml record=colo:recording lines=4-5 +level=DEBUG msg="Found recording rule" path=rules/0001.yml record=colo:recording lines=4-5 state=noop level=DEBUG msg="Configured checks for rule" enabled=["promql/syntax","alerts/for","alerts/comparison","alerts/template","promql/fragile","promql/regexp","promql/aggregate(job:true)"] path=rules/0001.yml rule=colo:recording -level=DEBUG msg="Found alerting rule" path=rules/0001.yml alert=colo:alerting lines=7-8 +level=DEBUG msg="Found alerting rule" path=rules/0001.yml alert=colo:alerting lines=7-8 state=noop level=DEBUG msg="Configured checks for rule" enabled=["promql/syntax","alerts/for","alerts/comparison","alerts/template","promql/fragile","promql/regexp","promql/aggregate(job:true)"] path=rules/0001.yml rule=colo:alerting rules/0001.yml:5 Warning: `job` label is required and should be preserved when aggregating `^.+$` rules, remove job from `without()`. (promql/aggregate) 5 | expr: sum(foo) without(job) diff --git a/cmd/pint/tests/0053_ignore_multiple.txt b/cmd/pint/tests/0053_ignore_multiple.txt index a768734b..ee733367 100644 --- a/cmd/pint/tests/0053_ignore_multiple.txt +++ b/cmd/pint/tests/0053_ignore_multiple.txt @@ -9,9 +9,9 @@ level=INFO msg="Finding all rules to check" paths=["rules"] level=DEBUG msg="File parsed" path=rules/0001.yml rules=2 level=DEBUG msg="Glob finder completed" count=2 level=DEBUG msg="Generated all Prometheus servers" count=0 -level=DEBUG msg="Found recording rule" path=rules/0001.yml record=colo:recording lines=4-5 +level=DEBUG msg="Found recording rule" path=rules/0001.yml record=colo:recording lines=4-5 state=noop level=DEBUG msg="Configured checks for rule" enabled=["promql/syntax","alerts/for","alerts/comparison","alerts/template","promql/fragile","promql/regexp"] path=rules/0001.yml rule=colo:recording -level=DEBUG msg="Found alerting rule" path=rules/0001.yml alert=colo:alerting lines=7-8 +level=DEBUG msg="Found alerting rule" path=rules/0001.yml alert=colo:alerting lines=7-8 state=noop level=DEBUG msg="Configured checks for rule" enabled=["promql/syntax","alerts/for","alerts/comparison","alerts/template","promql/fragile","promql/regexp"] path=rules/0001.yml rule=colo:alerting -- rules/0001.yml -- groups: diff --git a/cmd/pint/tests/0092_dir_symlink.txt b/cmd/pint/tests/0092_dir_symlink.txt index bd921c72..950f8fb4 100644 --- a/cmd/pint/tests/0092_dir_symlink.txt +++ b/cmd/pint/tests/0092_dir_symlink.txt @@ -12,7 +12,7 @@ level=INFO msg="Finding all rules to check" paths=["rules","linked","rules/src/r level=DEBUG msg="File parsed" path=rules/src/rule.yaml rules=1 level=DEBUG msg="Glob finder completed" count=1 level=DEBUG msg="Generated all Prometheus servers" count=0 -level=DEBUG msg="Found recording rule" path=rules/src/rule.yaml record=down lines=4-5 +level=DEBUG msg="Found recording rule" path=rules/src/rule.yaml record=down lines=4-5 state=noop level=DEBUG msg="Configured checks for rule" enabled=["promql/syntax","alerts/for","alerts/comparison","alerts/template","promql/fragile","promql/regexp"] path=rules/src/rule.yaml rule=down -- rules/src/rule.yaml -- groups: diff --git a/cmd/pint/tests/0095_rulefmt_symlink.txt b/cmd/pint/tests/0095_rulefmt_symlink.txt index 4569d6b1..37cbcef1 100644 --- a/cmd/pint/tests/0095_rulefmt_symlink.txt +++ b/cmd/pint/tests/0095_rulefmt_symlink.txt @@ -13,9 +13,9 @@ level=DEBUG msg="File parsed" path=rules/relaxed/1.yml rules=1 level=DEBUG msg="File parsed" path=rules/strict/symlink.yml rules=1 level=DEBUG msg="Glob finder completed" count=2 level=DEBUG msg="Generated all Prometheus servers" count=0 -level=DEBUG msg="Found recording rule" path=rules/relaxed/1.yml record=foo lines=1-2 +level=DEBUG msg="Found recording rule" path=rules/relaxed/1.yml record=foo lines=1-2 state=noop level=DEBUG msg="Configured checks for rule" enabled=["promql/syntax","alerts/for","alerts/comparison","alerts/template","promql/fragile","promql/regexp"] path=rules/relaxed/1.yml rule=foo -level=DEBUG msg="Found recording rule" path=rules/strict/symlink.yml record=foo lines=1-2 +level=DEBUG msg="Found recording rule" path=rules/strict/symlink.yml record=foo lines=1-2 state=noop level=DEBUG msg="Configured checks for rule" enabled=["promql/syntax","alerts/for","alerts/comparison","alerts/template","promql/fragile","promql/regexp"] path=rules/strict/symlink.yml rule=foo -- rules/relaxed/1.yml -- - record: foo diff --git a/cmd/pint/tests/0099_symlink_outside_glob.txt b/cmd/pint/tests/0099_symlink_outside_glob.txt index 8fa8a567..53f4124f 100644 --- a/cmd/pint/tests/0099_symlink_outside_glob.txt +++ b/cmd/pint/tests/0099_symlink_outside_glob.txt @@ -12,7 +12,7 @@ level=INFO msg="Finding all rules to check" paths=["rules/relaxed"] level=DEBUG msg="File parsed" path=rules/relaxed/1.yml rules=1 level=DEBUG msg="Glob finder completed" count=1 level=DEBUG msg="Generated all Prometheus servers" count=0 -level=DEBUG msg="Found recording rule" path=rules/relaxed/1.yml record=foo lines=1-2 +level=DEBUG msg="Found recording rule" path=rules/relaxed/1.yml record=foo lines=1-2 state=noop level=DEBUG msg="Configured checks for rule" enabled=["promql/syntax","alerts/for","alerts/comparison","alerts/template","promql/fragile","promql/regexp"] path=rules/relaxed/1.yml rule=foo -- rules/relaxed/1.yml -- - record: foo diff --git a/cmd/pint/tests/0103_file_disable.txt b/cmd/pint/tests/0103_file_disable.txt index ca0b7645..31a37d72 100644 --- a/cmd/pint/tests/0103_file_disable.txt +++ b/cmd/pint/tests/0103_file_disable.txt @@ -11,7 +11,7 @@ level=DEBUG msg="Glob finder completed" count=1 level=INFO msg="Configured new Prometheus server" name=prom uris=1 uptime=up tags=[] include=[] exclude=[] level=DEBUG msg="Starting query workers" name=prom uri=http://127.0.0.1:7103 workers=16 level=DEBUG msg="Generated all Prometheus servers" count=1 -level=DEBUG msg="Found recording rule" path=rules/0001.yml record=colo:test1 lines=9-10 +level=DEBUG msg="Found recording rule" path=rules/0001.yml record=colo:test1 lines=9-10 state=noop level=DEBUG msg="Configured checks for rule" enabled=["promql/syntax","alerts/for","alerts/comparison","alerts/template","promql/fragile","promql/regexp","promql/vector_matching(prom)","labels/conflict(prom)","alerts/external_labels(prom)","alerts/absent(prom)"] path=rules/0001.yml rule=colo:test1 level=DEBUG msg="Stopping query workers" name=prom uri=http://127.0.0.1:7103 -- rules/0001.yml -- diff --git a/cmd/pint/tests/0111_snooze.txt b/cmd/pint/tests/0111_snooze.txt index 899088df..3002dcaf 100644 --- a/cmd/pint/tests/0111_snooze.txt +++ b/cmd/pint/tests/0111_snooze.txt @@ -9,7 +9,7 @@ level=INFO msg="Finding all rules to check" paths=["rules"] level=DEBUG msg="File parsed" path=rules/0001.yml rules=1 level=DEBUG msg="Glob finder completed" count=1 level=DEBUG msg="Generated all Prometheus servers" count=0 -level=DEBUG msg="Found recording rule" path=rules/0001.yml record=sum:job lines=2-3 +level=DEBUG msg="Found recording rule" path=rules/0001.yml record=sum:job lines=2-3 state=noop level=DEBUG msg="Check snoozed by comment" check=promql/aggregate(job:true) match=promql/aggregate until="2099-11-28T10:24:18Z" level=DEBUG msg="Configured checks for rule" enabled=["promql/syntax","alerts/for","alerts/comparison","alerts/template","promql/fragile","promql/regexp"] path=rules/0001.yml rule=sum:job -- rules/0001.yml -- diff --git a/cmd/pint/tests/0112_expired_snooze.txt b/cmd/pint/tests/0112_expired_snooze.txt index 172b6d4b..fe3071a6 100644 --- a/cmd/pint/tests/0112_expired_snooze.txt +++ b/cmd/pint/tests/0112_expired_snooze.txt @@ -9,7 +9,7 @@ level=INFO msg="Finding all rules to check" paths=["rules"] level=DEBUG msg="File parsed" path=rules/0001.yml rules=1 level=DEBUG msg="Glob finder completed" count=1 level=DEBUG msg="Generated all Prometheus servers" count=0 -level=DEBUG msg="Found recording rule" path=rules/0001.yml record=sum:job lines=2-3 +level=DEBUG msg="Found recording rule" path=rules/0001.yml record=sum:job lines=2-3 state=noop level=DEBUG msg="Configured checks for rule" enabled=["promql/syntax","alerts/for","alerts/comparison","alerts/template","promql/fragile","promql/regexp","promql/aggregate(job:true)"] path=rules/0001.yml rule=sum:job rules/0001.yml:3 Bug: `job` label is required and should be preserved when aggregating `^.+$` rules, use `by(job, ...)`. (promql/aggregate) 3 | expr: sum(foo) diff --git a/cmd/pint/tests/0115_file_disable_tag.txt b/cmd/pint/tests/0115_file_disable_tag.txt index db49d85f..4539e095 100644 --- a/cmd/pint/tests/0115_file_disable_tag.txt +++ b/cmd/pint/tests/0115_file_disable_tag.txt @@ -11,7 +11,7 @@ level=DEBUG msg="Glob finder completed" count=1 level=INFO msg="Configured new Prometheus server" name=prom uris=1 uptime=up tags=["foo","bar"] include=[] exclude=[] level=DEBUG msg="Starting query workers" name=prom uri=http://127.0.0.1:7103 workers=16 level=DEBUG msg="Generated all Prometheus servers" count=1 -level=DEBUG msg="Found recording rule" path=rules/0001.yml record=colo:test1 lines=6-8 +level=DEBUG msg="Found recording rule" path=rules/0001.yml record=colo:test1 lines=6-8 state=noop level=DEBUG msg="Configured checks for rule" enabled=["promql/syntax","alerts/for","alerts/comparison","alerts/template","promql/fragile","promql/regexp","alerts/external_labels(prom)","promql/counter(prom)","alerts/absent(prom)"] path=rules/0001.yml rule=colo:test1 level=DEBUG msg="Scheduling Prometheus metrics metadata query" uri=http://127.0.0.1:7103 metric=foo level=DEBUG msg="Getting prometheus metrics metadata" uri=http://127.0.0.1:7103 metric=foo diff --git a/cmd/pint/tests/0116_file_snooze.txt b/cmd/pint/tests/0116_file_snooze.txt index 11aed777..96878964 100644 --- a/cmd/pint/tests/0116_file_snooze.txt +++ b/cmd/pint/tests/0116_file_snooze.txt @@ -11,9 +11,9 @@ level=DEBUG msg="Check snoozed by comment" check=alerts/for match=alerts/for unt level=DEBUG msg="File parsed" path=rules/0001.yml rules=2 level=DEBUG msg="Glob finder completed" count=2 level=DEBUG msg="Generated all Prometheus servers" count=0 -level=DEBUG msg="Found recording rule" path=rules/0001.yml record=sum:job lines=4-5 +level=DEBUG msg="Found recording rule" path=rules/0001.yml record=sum:job lines=4-5 state=noop level=DEBUG msg="Configured checks for rule" enabled=["promql/syntax","alerts/comparison","alerts/template","promql/fragile","promql/regexp"] path=rules/0001.yml rule=sum:job -level=DEBUG msg="Found alerting rule" path=rules/0001.yml alert=Down lines=7-9 +level=DEBUG msg="Found alerting rule" path=rules/0001.yml alert=Down lines=7-9 state=noop level=DEBUG msg="Configured checks for rule" enabled=["promql/syntax","alerts/comparison","alerts/template","promql/fragile","promql/regexp"] path=rules/0001.yml rule=Down -- rules/0001.yml -- # pint file/snooze 2099-11-28T10:24:18Z promql/aggregate(job:true) diff --git a/cmd/pint/tests/0144_discovery_filepath.txt b/cmd/pint/tests/0144_discovery_filepath.txt index 3ab79b22..6372d00a 100644 --- a/cmd/pint/tests/0144_discovery_filepath.txt +++ b/cmd/pint/tests/0144_discovery_filepath.txt @@ -28,7 +28,7 @@ level=INFO msg="Configured new Prometheus server" name=prom2 uris=2 uptime=up ta level=DEBUG msg="Starting query workers" name=prom2 uri=https://prom2.example.com workers=16 level=DEBUG msg="Starting query workers" name=prom2 uri=https://prom2-backup.example.com workers=16 level=DEBUG msg="Generated all Prometheus servers" count=2 -level=DEBUG msg="Found recording rule" path=rules/0001.yml record=sum:up lines=4-5 +level=DEBUG msg="Found recording rule" path=rules/0001.yml record=sum:up lines=4-5 state=noop level=DEBUG msg="Configured checks for rule" enabled=["promql/syntax","alerts/for","alerts/comparison","alerts/template","promql/fragile","promql/regexp"] path=rules/0001.yml rule=sum:up level=DEBUG msg="Stopping query workers" name=prom1 uri=https://prom1.example.com level=DEBUG msg="Stopping query workers" name=prom1 uri=https://prom1-backup.example.com diff --git a/cmd/pint/tests/0148_discovery_prom_zero.txt b/cmd/pint/tests/0148_discovery_prom_zero.txt index 4e84ad84..4b2df58b 100644 --- a/cmd/pint/tests/0148_discovery_prom_zero.txt +++ b/cmd/pint/tests/0148_discovery_prom_zero.txt @@ -18,7 +18,7 @@ level=DEBUG msg="Running prometheus query" uri=http://127.0.0.1:7148 query=prome level=DEBUG msg="Parsed response" uri=http://127.0.0.1:7148 query=prometheus_ready series=0 level=DEBUG msg="Stopping query workers" name=discovery uri=http://127.0.0.1:7148 level=DEBUG msg="Generated all Prometheus servers" count=0 -level=DEBUG msg="Found recording rule" path=rules/0001.yml record=sum:up lines=4-5 +level=DEBUG msg="Found recording rule" path=rules/0001.yml record=sum:up lines=4-5 state=noop level=DEBUG msg="Configured checks for rule" enabled=["promql/syntax","alerts/for","alerts/comparison","alerts/template","promql/fragile","promql/regexp"] path=rules/0001.yml rule=sum:up -- rules/0001.yml -- groups: diff --git a/cmd/pint/tests/0149_discovery_prom.txt b/cmd/pint/tests/0149_discovery_prom.txt index e40b0581..d9e07eaa 100644 --- a/cmd/pint/tests/0149_discovery_prom.txt +++ b/cmd/pint/tests/0149_discovery_prom.txt @@ -24,7 +24,7 @@ level=INFO msg="Configured new Prometheus server" name=prom-ha uris=2 uptime=up level=DEBUG msg="Starting query workers" name=prom-ha uri=https://prom1.example.com workers=16 level=DEBUG msg="Starting query workers" name=prom-ha uri=https://prom2.example.com workers=16 level=DEBUG msg="Generated all Prometheus servers" count=1 -level=DEBUG msg="Found recording rule" path=rules/0001.yml record=sum:up lines=4-5 +level=DEBUG msg="Found recording rule" path=rules/0001.yml record=sum:up lines=4-5 state=noop level=DEBUG msg="Configured checks for rule" enabled=["promql/syntax","alerts/for","alerts/comparison","alerts/template","promql/fragile","promql/regexp"] path=rules/0001.yml rule=sum:up level=DEBUG msg="Stopping query workers" name=prom-ha uri=https://prom1.example.com level=DEBUG msg="Stopping query workers" name=prom-ha uri=https://prom2.example.com diff --git a/cmd/pint/tests/0152_discovery_prom_dup_uptime.txt b/cmd/pint/tests/0152_discovery_prom_dup_uptime.txt index c71e09c3..9462e6c1 100644 --- a/cmd/pint/tests/0152_discovery_prom_dup_uptime.txt +++ b/cmd/pint/tests/0152_discovery_prom_dup_uptime.txt @@ -32,7 +32,7 @@ level=INFO msg="Configured new Prometheus server" name=prom-ha uris=2 uptime=pro level=DEBUG msg="Starting query workers" name=prom-ha uri=https://prom1.example.com workers=16 level=DEBUG msg="Starting query workers" name=prom-ha uri=https://prom2.example.com workers=16 level=DEBUG msg="Generated all Prometheus servers" count=1 -level=DEBUG msg="Found recording rule" path=rules/0001.yml record=sum:up lines=4-5 +level=DEBUG msg="Found recording rule" path=rules/0001.yml record=sum:up lines=4-5 state=noop level=DEBUG msg="Configured checks for rule" enabled=["promql/syntax","alerts/for","alerts/comparison","alerts/template","promql/fragile","promql/regexp"] path=rules/0001.yml rule=sum:up level=DEBUG msg="Stopping query workers" name=prom-ha uri=https://prom1.example.com level=DEBUG msg="Stopping query workers" name=prom-ha uri=https://prom2.example.com diff --git a/cmd/pint/tests/0178_parser_include.txt b/cmd/pint/tests/0178_parser_include.txt index b5601d93..4892da95 100644 --- a/cmd/pint/tests/0178_parser_include.txt +++ b/cmd/pint/tests/0178_parser_include.txt @@ -10,7 +10,7 @@ level=DEBUG msg="File path is in the include list" path=rules/0001.yml include=[ level=DEBUG msg="File parsed" path=rules/0001.yml rules=1 level=DEBUG msg="Glob finder completed" count=1 level=DEBUG msg="Generated all Prometheus servers" count=0 -level=DEBUG msg="Found recording rule" path=rules/0001.yml record=ok lines=1-2 +level=DEBUG msg="Found recording rule" path=rules/0001.yml record=ok lines=1-2 state=noop level=DEBUG msg="Configured checks for rule" enabled=["promql/syntax","alerts/for","alerts/comparison","alerts/template","promql/fragile","promql/regexp"] path=rules/0001.yml rule=ok -- rules/0001.yml -- - record: ok diff --git a/cmd/pint/tests/0179_parser_exclude.txt b/cmd/pint/tests/0179_parser_exclude.txt index 6f29295c..aa000c5c 100644 --- a/cmd/pint/tests/0179_parser_exclude.txt +++ b/cmd/pint/tests/0179_parser_exclude.txt @@ -10,7 +10,7 @@ level=DEBUG msg="File parsed" path=rules/0001.yml rules=1 level=DEBUG msg="File path is in the exclude list" path=rules/0002.yml exclude=["^rules/0002.yml$","^.pint.hcl$"] level=DEBUG msg="Glob finder completed" count=1 level=DEBUG msg="Generated all Prometheus servers" count=0 -level=DEBUG msg="Found recording rule" path=rules/0001.yml record=ok lines=1-2 +level=DEBUG msg="Found recording rule" path=rules/0001.yml record=ok lines=1-2 state=noop level=DEBUG msg="Configured checks for rule" enabled=["promql/syntax","alerts/for","alerts/comparison","alerts/template","promql/fragile","promql/regexp"] path=rules/0001.yml rule=ok -- rules/0001.yml -- - record: ok diff --git a/cmd/pint/tests/0180_parser_exclude_md.txt b/cmd/pint/tests/0180_parser_exclude_md.txt index aa238fe7..5a4f2edd 100644 --- a/cmd/pint/tests/0180_parser_exclude_md.txt +++ b/cmd/pint/tests/0180_parser_exclude_md.txt @@ -10,7 +10,7 @@ level=DEBUG msg="File parsed" path=rules/0001.yml rules=1 level=DEBUG msg="File path is in the exclude list" path=rules/README.md exclude=["^.*.md$","^.pint.hcl$"] level=DEBUG msg="Glob finder completed" count=1 level=DEBUG msg="Generated all Prometheus servers" count=0 -level=DEBUG msg="Found recording rule" path=rules/0001.yml record=ok lines=1-2 +level=DEBUG msg="Found recording rule" path=rules/0001.yml record=ok lines=1-2 state=noop level=DEBUG msg="Configured checks for rule" enabled=["promql/syntax","alerts/for","alerts/comparison","alerts/template","promql/fragile","promql/regexp"] path=rules/0001.yml rule=ok -- rules/0001.yml -- - record: ok diff --git a/cmd/pint/tests/0183_cli_enable.txt b/cmd/pint/tests/0183_cli_enable.txt index 5ce9f29d..54d4703e 100644 --- a/cmd/pint/tests/0183_cli_enable.txt +++ b/cmd/pint/tests/0183_cli_enable.txt @@ -9,11 +9,11 @@ level=INFO msg="Finding all rules to check" paths=["rules"] level=DEBUG msg="File parsed" path=rules/0001.yml rules=3 level=DEBUG msg="Glob finder completed" count=3 level=DEBUG msg="Generated all Prometheus servers" count=0 -level=DEBUG msg="Found alerting rule" path=rules/0001.yml alert=default-for lines=1-3 +level=DEBUG msg="Found alerting rule" path=rules/0001.yml alert=default-for lines=1-3 state=noop level=DEBUG msg="Configured checks for rule" enabled=[] path=rules/0001.yml rule=default-for -level=DEBUG msg="Found recording rule" path=rules/0001.yml record=sum:job lines=5-6 +level=DEBUG msg="Found recording rule" path=rules/0001.yml record=sum:job lines=5-6 state=noop level=DEBUG msg="Configured checks for rule" enabled=["promql/aggregate(job:true)"] path=rules/0001.yml rule=sum:job -level=DEBUG msg="Found alerting rule" path=rules/0001.yml alert=no-comparison lines=8-9 +level=DEBUG msg="Found alerting rule" path=rules/0001.yml alert=no-comparison lines=8-9 state=noop level=DEBUG msg="Configured checks for rule" enabled=[] path=rules/0001.yml rule=no-comparison rules/0001.yml:6 Warning: `job` label is required and should be preserved when aggregating `^.+$` rules, use `by(job, ...)`. (promql/aggregate) 6 | expr: sum(foo) @@ -41,4 +41,4 @@ rule { aggregate ".+" { keep = [ "job" ] } -} \ No newline at end of file +} diff --git a/docs/changelog.md b/docs/changelog.md index 5f33d296..f8e3279a 100644 --- a/docs/changelog.md +++ b/docs/changelog.md @@ -1,5 +1,11 @@ # Changelog +## v0.65.1 + +### Fixed + +- Fixed a bug in `match` block `state` handling that caused all rules to always match any state. + ## v0.65.0 ### Added diff --git a/internal/config/config.go b/internal/config/config.go index 641b11f3..dec29433 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -83,7 +83,8 @@ func (cfg Config) String() string { func (cfg *Config) GetChecksForEntry(ctx context.Context, gen *PrometheusGenerator, entry discovery.Entry) []checks.RuleChecker { enabled := []checks.RuleChecker{} - defaultMatch := []Match{{State: defaultMatchStates(commandFromContext(ctx))}} + defaultStates := defaultMatchStates(commandFromContext(ctx)) + defaultMatch := []Match{{State: defaultStates}} proms := gen.ServersForPath(entry.Path.Name) if entry.PathError != nil || entry.Rule.Error.Err != nil { @@ -98,7 +99,7 @@ func (cfg *Config) GetChecksForEntry(ctx context.Context, gen *PrometheusGenerat enabled = pr.entryChecks(ctx, cfg.Checks.Enabled, cfg.Checks.Disabled, enabled, entry) } for _, rule := range cfg.Rules { - for _, pr := range parseRule(rule, proms, defaultMatch) { + for _, pr := range parseRule(rule, proms, defaultStates) { enabled = pr.entryChecks(ctx, cfg.Checks.Enabled, cfg.Checks.Disabled, enabled, entry) } } diff --git a/internal/config/parsed_rule.go b/internal/config/parsed_rule.go index f759939e..c69867a5 100644 --- a/internal/config/parsed_rule.go +++ b/internal/config/parsed_rule.go @@ -178,14 +178,21 @@ func baseRules(proms []*promapi.FailoverGroup, match []Match) (rules []parsedRul return rules } -func defaultRuleMatch(match, fallback []Match) []Match { +func defaultRuleMatch(match []Match, defaultStates []string) []Match { if len(match) == 0 { - return fallback + return []Match{{State: defaultStates}} } - return match + dst := make([]Match, 0, len(match)) + for _, m := range match { + if len(m.State) == 0 { + m.State = defaultStates + } + dst = append(dst, m) + } + return dst } -func parseRule(rule Rule, prometheusServers []*promapi.FailoverGroup, defaultMatch []Match) (rules []parsedRule) { +func parseRule(rule Rule, prometheusServers []*promapi.FailoverGroup, defaultStates []string) (rules []parsedRule) { if len(rule.Aggregate) > 0 { var nameRegex *checks.TemplatedRegexp for _, aggr := range rule.Aggregate { @@ -195,7 +202,7 @@ func parseRule(rule Rule, prometheusServers []*promapi.FailoverGroup, defaultMat severity := aggr.getSeverity(checks.Warning) for _, label := range aggr.Keep { rules = append(rules, parsedRule{ - match: defaultRuleMatch(rule.Match, defaultMatch), + match: defaultRuleMatch(rule.Match, defaultStates), ignore: rule.Ignore, name: checks.AggregationCheckName, check: checks.NewAggregationCheck(nameRegex, label, true, aggr.Comment, severity), @@ -203,7 +210,7 @@ func parseRule(rule Rule, prometheusServers []*promapi.FailoverGroup, defaultMat } for _, label := range aggr.Strip { rules = append(rules, parsedRule{ - match: defaultRuleMatch(rule.Match, defaultMatch), + match: defaultRuleMatch(rule.Match, defaultStates), ignore: rule.Ignore, name: checks.AggregationCheckName, check: checks.NewAggregationCheck(nameRegex, label, false, aggr.Comment, severity), @@ -217,7 +224,7 @@ func parseRule(rule Rule, prometheusServers []*promapi.FailoverGroup, defaultMat evalDur, _ := parseDuration(rule.Cost.MaxEvaluationDuration) for _, prom := range prometheusServers { rules = append(rules, parsedRule{ - match: defaultRuleMatch(rule.Match, defaultMatch), + match: defaultRuleMatch(rule.Match, defaultStates), ignore: rule.Ignore, name: checks.CostCheckName, check: checks.NewCostCheck(prom, rule.Cost.MaxSeries, rule.Cost.MaxTotalSamples, rule.Cost.MaxPeakSamples, evalDur, rule.Cost.Comment, severity), @@ -237,7 +244,7 @@ func parseRule(rule Rule, prometheusServers []*promapi.FailoverGroup, defaultMat } severity := ann.getSeverity(checks.Warning) rules = append(rules, parsedRule{ - match: defaultRuleMatch(rule.Match, defaultMatch), + match: defaultRuleMatch(rule.Match, defaultStates), ignore: rule.Ignore, name: checks.AnnotationCheckName, check: checks.NewAnnotationCheck(checks.MustTemplatedRegexp(ann.Key), tokenRegex, valueRegex, ann.Values, ann.Required, ann.Comment, severity), @@ -256,7 +263,7 @@ func parseRule(rule Rule, prometheusServers []*promapi.FailoverGroup, defaultMat } severity := lab.getSeverity(checks.Warning) rules = append(rules, parsedRule{ - match: defaultRuleMatch(rule.Match, defaultMatch), + match: defaultRuleMatch(rule.Match, defaultStates), ignore: rule.Ignore, name: checks.LabelCheckName, check: checks.NewLabelCheck(checks.MustTemplatedRegexp(lab.Key), tokenRegex, valueRegex, lab.Values, lab.Required, lab.Comment, severity), @@ -280,7 +287,7 @@ func parseRule(rule Rule, prometheusServers []*promapi.FailoverGroup, defaultMat severity := rule.Alerts.getSeverity(checks.Information) for _, prom := range prometheusServers { rules = append(rules, parsedRule{ - match: defaultRuleMatch(rule.Match, defaultMatch), + match: defaultRuleMatch(rule.Match, defaultStates), ignore: rule.Ignore, name: checks.AlertsCheckName, check: checks.NewAlertsCheck(prom, qRange, qStep, qResolve, rule.Alerts.MinCount, rule.Alerts.Comment, severity), @@ -295,7 +302,7 @@ func parseRule(rule Rule, prometheusServers []*promapi.FailoverGroup, defaultMat re := checks.MustTemplatedRegexp(reject.Regex) if reject.LabelKeys { rules = append(rules, parsedRule{ - match: defaultRuleMatch(rule.Match, defaultMatch), + match: defaultRuleMatch(rule.Match, defaultStates), ignore: rule.Ignore, name: checks.RejectCheckName, check: checks.NewRejectCheck(true, false, re, nil, severity), @@ -303,7 +310,7 @@ func parseRule(rule Rule, prometheusServers []*promapi.FailoverGroup, defaultMat } if reject.LabelValues { rules = append(rules, parsedRule{ - match: defaultRuleMatch(rule.Match, defaultMatch), + match: defaultRuleMatch(rule.Match, defaultStates), ignore: rule.Ignore, name: checks.RejectCheckName, check: checks.NewRejectCheck(true, false, nil, re, severity), @@ -311,7 +318,7 @@ func parseRule(rule Rule, prometheusServers []*promapi.FailoverGroup, defaultMat } if reject.AnnotationKeys { rules = append(rules, parsedRule{ - match: defaultRuleMatch(rule.Match, defaultMatch), + match: defaultRuleMatch(rule.Match, defaultStates), ignore: rule.Ignore, name: checks.RejectCheckName, check: checks.NewRejectCheck(false, true, re, nil, severity), @@ -319,7 +326,7 @@ func parseRule(rule Rule, prometheusServers []*promapi.FailoverGroup, defaultMat } if reject.AnnotationValues { rules = append(rules, parsedRule{ - match: defaultRuleMatch(rule.Match, defaultMatch), + match: defaultRuleMatch(rule.Match, defaultStates), ignore: rule.Ignore, name: checks.RejectCheckName, check: checks.NewRejectCheck(false, true, nil, re, severity), @@ -338,7 +345,7 @@ func parseRule(rule Rule, prometheusServers []*promapi.FailoverGroup, defaultMat timeout = time.Minute } rules = append(rules, parsedRule{ - match: defaultRuleMatch(rule.Match, defaultMatch), + match: defaultRuleMatch(rule.Match, defaultStates), ignore: rule.Ignore, name: checks.RuleLinkCheckName, check: checks.NewRuleLinkCheck(re, link.URI, timeout, link.Headers, link.Comment, severity), @@ -348,7 +355,7 @@ func parseRule(rule Rule, prometheusServers []*promapi.FailoverGroup, defaultMat if rule.For != nil { severity, minFor, maxFor := rule.For.resolve() rules = append(rules, parsedRule{ - match: defaultRuleMatch(rule.Match, defaultMatch), + match: defaultRuleMatch(rule.Match, defaultStates), ignore: rule.Ignore, name: checks.RuleForCheckName, check: checks.NewRuleForCheck(checks.RuleForFor, minFor, maxFor, rule.For.Comment, severity), @@ -358,7 +365,7 @@ func parseRule(rule Rule, prometheusServers []*promapi.FailoverGroup, defaultMat if rule.KeepFiringFor != nil { severity, minFor, maxFor := rule.KeepFiringFor.resolve() rules = append(rules, parsedRule{ - match: defaultRuleMatch(rule.Match, defaultMatch), + match: defaultRuleMatch(rule.Match, defaultStates), ignore: rule.Ignore, name: checks.RuleForCheckName, check: checks.NewRuleForCheck(checks.RuleForKeepFiringFor, minFor, maxFor, rule.KeepFiringFor.Comment, severity), @@ -369,7 +376,7 @@ func parseRule(rule Rule, prometheusServers []*promapi.FailoverGroup, defaultMat re := checks.MustTemplatedRegexp(name.Regex) severity := name.getSeverity(checks.Information) rules = append(rules, parsedRule{ - match: defaultRuleMatch(rule.Match, defaultMatch), + match: defaultRuleMatch(rule.Match, defaultStates), ignore: rule.Ignore, name: checks.RuleNameCheckName, check: checks.NewRuleNameCheck(re, name.Comment, severity), @@ -380,7 +387,7 @@ func parseRule(rule Rule, prometheusServers []*promapi.FailoverGroup, defaultMat severity := rule.RangeQuery.getSeverity(checks.Warning) limit, _ := parseDuration(rule.RangeQuery.Max) rules = append(rules, parsedRule{ - match: defaultRuleMatch(rule.Match, defaultMatch), + match: defaultRuleMatch(rule.Match, defaultStates), ignore: rule.Ignore, name: checks.CostCheckName, check: checks.NewRangeQueryCheck(nil, limit, rule.RangeQuery.Comment, severity),