diff --git a/cmd/pint/main.go b/cmd/pint/main.go index aaad92af2..617f9486f 100644 --- a/cmd/pint/main.go +++ b/cmd/pint/main.go @@ -70,7 +70,7 @@ func newApp() *cli.Command { Name: offlineFlag, Aliases: []string{"o"}, Value: false, - Usage: "Disable all checks that send live queries to Prometheus servers.", + Usage: "Offline mode, no network requests will be sent, some parts of check logic might be disabled or checks might be skipped entirely.", }, &cli.BoolFlag{ Name: showDupsFlag, @@ -123,10 +123,7 @@ func actionSetup(c *cli.Command) (meta actionMeta, err error) { meta.cfg.Checks.Enabled = enabled } - if c.Bool(offlineFlag) { - meta.isOffline = true - meta.cfg.DisableOnlineChecks() - } + meta.isOffline = c.Bool(offlineFlag) return meta, nil } diff --git a/cmd/pint/scan.go b/cmd/pint/scan.go index 13690ffcc..29dc616cd 100644 --- a/cmd/pint/scan.go +++ b/cmd/pint/scan.go @@ -43,13 +43,14 @@ func checkRules(ctx context.Context, workers int, isOffline bool, gen *config.Pr var reports []reporter.Report ctx = context.WithValue(ctx, promapi.AllPrometheusServers, gen.Servers()) + ctx = promapi.WithOffline(ctx, isOffline) for _, s := range cfg.Check { settings, _ := s.Decode() key := checks.SettingsKey(s.Name) ctx = context.WithValue(ctx, key, settings) } - var onlineChecksCount, offlineChecksCount, checkedEntriesCount atomic.Int64 + var checkedEntriesCount, totalChecksCount atomic.Int64 for _, entry := range entries { switch { case entry.PathError != nil && entry.State == discovery.Removed: @@ -91,11 +92,7 @@ func checkRules(ctx context.Context, workers int, isOffline bool, gen *config.Pr checkList := cfg.GetChecksForEntry(ctx, gen, entry) for _, check := range checkList { checkIterationChecks.Inc() - if check.Meta().Online { - onlineChecksCount.Add(1) - } else { - offlineChecksCount.Add(1) - } + totalChecksCount.Add(1) concurrencyLimit <- struct{}{} wg.Go(func() { defer func() { <-concurrencyLimit }() @@ -117,8 +114,7 @@ func checkRules(ctx context.Context, workers int, isOffline bool, gen *config.Pr summary.Duration = time.Since(start) summary.TotalEntries = len(entries) summary.CheckedEntries = checkedEntriesCount.Load() - summary.OnlineChecks = onlineChecksCount.Load() - summary.OfflineChecks = offlineChecksCount.Load() + summary.TotalChecks = totalChecksCount.Load() for _, prom := range gen.Servers() { for api, names := range prom.GetDisabledChecks() { diff --git a/cmd/pint/scan_test.go b/cmd/pint/scan_test.go index c817258df..958722ebf 100644 --- a/cmd/pint/scan_test.go +++ b/cmd/pint/scan_test.go @@ -18,7 +18,7 @@ type testCheck struct { } func (tc testCheck) Meta() checks.CheckMeta { - return checks.CheckMeta{Online: false} + return checks.CheckMeta{} } func (tc testCheck) String() string { diff --git a/cmd/pint/tests/0032_ci_github.txt b/cmd/pint/tests/0032_ci_github.txt index 8765b65dd..b60d2f45b 100644 --- a/cmd/pint/tests/0032_ci_github.txt +++ b/cmd/pint/tests/0032_ci_github.txt @@ -218,8 +218,7 @@ body: |+ | Number of rules parsed | 3 | | Number of rules checked | 3 | | Number of problems found | 3 | - | Number of offline checks | 21 | - | Number of online checks | 0 | + | Number of checks | 21 | | Checks duration | 0 |

diff --git a/cmd/pint/tests/0033_ci_github_multi.txt b/cmd/pint/tests/0033_ci_github_multi.txt index 6a0d01373..5ef3d9848 100644 --- a/cmd/pint/tests/0033_ci_github_multi.txt +++ b/cmd/pint/tests/0033_ci_github_multi.txt @@ -206,8 +206,7 @@ body: |+ | Number of rules parsed | 2 | | Number of rules checked | 2 | | Number of problems found | 4 | - | Number of offline checks | 20 | - | Number of online checks | 0 | + | Number of checks | 20 | | Checks duration | 0 |

diff --git a/cmd/pint/tests/0054_watch_metrics_prometheus.txt b/cmd/pint/tests/0054_watch_metrics_prometheus.txt index 6818c3459..cf70a56da 100644 --- a/cmd/pint/tests/0054_watch_metrics_prometheus.txt +++ b/cmd/pint/tests/0054_watch_metrics_prometheus.txt @@ -102,16 +102,16 @@ pint_last_run_duration_seconds pint_last_run_time_seconds # HELP pint_problem Prometheus rule problem reported by pint. # TYPE pint_problem gauge -pint_problem{filename="rules/1.yml",kind="recording",name="aggregate",owner="",problem="unable to run checks: Couldn't run some online checks due to `prom1` Prometheus server at http://127.0.0.1:7054 error: `bad_data: bogus query`.",reporter="promql/series",severity="bug"} -pint_problem{filename="rules/1.yml",kind="recording",name="aggregate",owner="",problem="unable to run checks: Couldn't run some online checks due to `prom1` Prometheus server at http://127.0.0.1:7054 error: `server_error: 500 Internal Server Error`.",reporter="promql/rate",severity="bug"} -pint_problem{filename="rules/1.yml",kind="recording",name="aggregate",owner="",problem="unable to run checks: Couldn't run some online checks due to `prom2` Prometheus server at http://127.0.0.1:1054 error: `connection refused`.",reporter="promql/counter",severity="bug"} -pint_problem{filename="rules/1.yml",kind="recording",name="aggregate",owner="",problem="unable to run checks: Couldn't run some online checks due to `prom2` Prometheus server at http://127.0.0.1:1054 error: `connection refused`.",reporter="promql/offset",severity="bug"} -pint_problem{filename="rules/1.yml",kind="recording",name="aggregate",owner="",problem="unable to run checks: Couldn't run some online checks due to `prom2` Prometheus server at http://127.0.0.1:1054 error: `connection refused`.",reporter="promql/range_query",severity="bug"} -pint_problem{filename="rules/1.yml",kind="recording",name="aggregate",owner="",problem="unable to run checks: Couldn't run some online checks due to `prom2` Prometheus server at http://127.0.0.1:1054 error: `connection refused`.",reporter="promql/rate",severity="bug"} -pint_problem{filename="rules/1.yml",kind="recording",name="aggregate",owner="",problem="unable to run checks: Couldn't run some online checks due to `prom2` Prometheus server at http://127.0.0.1:1054 error: `connection refused`.",reporter="promql/series",severity="bug"} +pint_problem{filename="rules/1.yml",kind="recording",name="aggregate",owner="",problem="unable to run checks: Couldn't run some checks due to `prom1` Prometheus server at http://127.0.0.1:7054 error: `bad_data: bogus query`.",reporter="promql/series",severity="bug"} +pint_problem{filename="rules/1.yml",kind="recording",name="aggregate",owner="",problem="unable to run checks: Couldn't run some checks due to `prom1` Prometheus server at http://127.0.0.1:7054 error: `server_error: 500 Internal Server Error`.",reporter="promql/rate",severity="bug"} +pint_problem{filename="rules/1.yml",kind="recording",name="aggregate",owner="",problem="unable to run checks: Couldn't run some checks due to `prom2` Prometheus server at http://127.0.0.1:1054 error: `connection refused`.",reporter="promql/counter",severity="bug"} +pint_problem{filename="rules/1.yml",kind="recording",name="aggregate",owner="",problem="unable to run checks: Couldn't run some checks due to `prom2` Prometheus server at http://127.0.0.1:1054 error: `connection refused`.",reporter="promql/offset",severity="bug"} +pint_problem{filename="rules/1.yml",kind="recording",name="aggregate",owner="",problem="unable to run checks: Couldn't run some checks due to `prom2` Prometheus server at http://127.0.0.1:1054 error: `connection refused`.",reporter="promql/range_query",severity="bug"} +pint_problem{filename="rules/1.yml",kind="recording",name="aggregate",owner="",problem="unable to run checks: Couldn't run some checks due to `prom2` Prometheus server at http://127.0.0.1:1054 error: `connection refused`.",reporter="promql/rate",severity="bug"} +pint_problem{filename="rules/1.yml",kind="recording",name="aggregate",owner="",problem="unable to run checks: Couldn't run some checks due to `prom2` Prometheus server at http://127.0.0.1:1054 error: `connection refused`.",reporter="promql/series",severity="bug"} pint_problem{filename="rules/1.yml",kind="recording",name="broken",owner="",problem="PromQL syntax error: unexpected right parenthesis ')'",reporter="promql/syntax",severity="fatal"} -pint_problem{filename="rules/2.yml",kind="alerting",name="comparison",owner="bob and alice",problem="unable to run checks: Couldn't run some online checks due to `prom1` Prometheus server at http://127.0.0.1:7054 error: `server_error: 500 Internal Server Error`.",reporter="alerts/external_labels",severity="bug"} -pint_problem{filename="rules/2.yml",kind="alerting",name="comparison",owner="bob and alice",problem="unable to run checks: Couldn't run some online checks due to `prom2` Prometheus server at http://127.0.0.1:1054 error: `connection refused`.",reporter="alerts/external_labels",severity="bug"} +pint_problem{filename="rules/2.yml",kind="alerting",name="comparison",owner="bob and alice",problem="unable to run checks: Couldn't run some checks due to `prom1` Prometheus server at http://127.0.0.1:7054 error: `server_error: 500 Internal Server Error`.",reporter="alerts/external_labels",severity="bug"} +pint_problem{filename="rules/2.yml",kind="alerting",name="comparison",owner="bob and alice",problem="unable to run checks: Couldn't run some checks due to `prom2` Prometheus server at http://127.0.0.1:1054 error: `connection refused`.",reporter="alerts/external_labels",severity="bug"} # HELP pint_problems Total number of problems reported by pint. # TYPE pint_problems gauge pint_problems diff --git a/cmd/pint/tests/0057_watch_metrics_prometheus_ignore.txt b/cmd/pint/tests/0057_watch_metrics_prometheus_ignore.txt index 1af4e2f79..0f1c70789 100644 --- a/cmd/pint/tests/0057_watch_metrics_prometheus_ignore.txt +++ b/cmd/pint/tests/0057_watch_metrics_prometheus_ignore.txt @@ -100,7 +100,7 @@ pint_last_run_duration_seconds pint_last_run_time_seconds # HELP pint_problem Prometheus rule problem reported by pint. # TYPE pint_problem gauge -pint_problem{filename="rules/1.yml",kind="recording",name="aggregate",owner="",problem="unable to run checks: Couldn't run some online checks due to `prom1` Prometheus server at http://127.0.0.1:7057 error: `bad_data: bogus query`.",reporter="promql/series",severity="bug"} +pint_problem{filename="rules/1.yml",kind="recording",name="aggregate",owner="",problem="unable to run checks: Couldn't run some checks due to `prom1` Prometheus server at http://127.0.0.1:7057 error: `bad_data: bogus query`.",reporter="promql/series",severity="bug"} pint_problem{filename="rules/1.yml",kind="recording",name="broken",owner="",problem="PromQL syntax error: unexpected right parenthesis ')'",reporter="promql/syntax",severity="fatal"} # HELP pint_problems Total number of problems reported by pint. # TYPE pint_problems gauge diff --git a/cmd/pint/tests/0083_github_action.txt b/cmd/pint/tests/0083_github_action.txt index 124b67540..c48e2b1e2 100644 --- a/cmd/pint/tests/0083_github_action.txt +++ b/cmd/pint/tests/0083_github_action.txt @@ -174,8 +174,7 @@ body: |+ | Number of rules parsed | 2 | | Number of rules checked | 2 | | Number of problems found | 2 | - | Number of offline checks | 10 | - | Number of online checks | 0 | + | Number of checks | 10 | | Checks duration | 0 |

diff --git a/cmd/pint/tests/0084_github_action_override.txt b/cmd/pint/tests/0084_github_action_override.txt index ebe763ad5..60973588f 100644 --- a/cmd/pint/tests/0084_github_action_override.txt +++ b/cmd/pint/tests/0084_github_action_override.txt @@ -177,8 +177,7 @@ body: |+ | Number of rules parsed | 2 | | Number of rules checked | 2 | | Number of problems found | 2 | - | Number of offline checks | 10 | - | Number of online checks | 0 | + | Number of checks | 10 | | Checks duration | 0 |

diff --git a/cmd/pint/tests/0085_github_no_envs.txt b/cmd/pint/tests/0085_github_no_envs.txt index 99278e396..33b78d1b6 100644 --- a/cmd/pint/tests/0085_github_no_envs.txt +++ b/cmd/pint/tests/0085_github_no_envs.txt @@ -172,8 +172,7 @@ body: |+ | Number of rules parsed | 2 | | Number of rules checked | 2 | | Number of problems found | 2 | - | Number of offline checks | 10 | - | Number of online checks | 0 | + | Number of checks | 10 | | Checks duration | 0 |

diff --git a/cmd/pint/tests/0098_rule_file_symlink_gh.txt b/cmd/pint/tests/0098_rule_file_symlink_gh.txt index ee41ab451..a2149355e 100644 --- a/cmd/pint/tests/0098_rule_file_symlink_gh.txt +++ b/cmd/pint/tests/0098_rule_file_symlink_gh.txt @@ -151,8 +151,7 @@ body: |+ | Number of rules parsed | 4 | | Number of rules checked | 4 | | Number of problems found | 2 | - | Number of offline checks | 40 | - | Number of online checks | 0 | + | Number of checks | 40 | | Checks duration | 0 |

diff --git a/cmd/pint/tests/0103_file_disable.txt b/cmd/pint/tests/0103_file_disable.txt index 2933a2323..13d1b4dd5 100644 --- a/cmd/pint/tests/0103_file_disable.txt +++ b/cmd/pint/tests/0103_file_disable.txt @@ -15,7 +15,7 @@ level=DEBUG msg="Generated all Prometheus servers" count=1 level=DEBUG msg="Found recording rule" path=rules/0001.yml record=colo:test1 lines=10-11 state=noop level=DEBUG msg="Configured checks for rule" enabled=["promql/syntax","alerts/for","alerts/comparison","alerts/template","promql/fragile","promql/regexp","rule/dependency","promql/impossible","promql/nan","group/interval","promql/vector_matching(prom)","labels/conflict(prom)","alerts/external_labels(prom)","alerts/absent(prom)","promql/features(prom)"] path=rules/0001.yml rule=colo:test1 -- rules/0001.yml -- -# This should skip all online checks +# This should skip all checks that require Prometheus API # pint file/disable promql/series # pint file/disable promql/rate # diff --git a/cmd/pint/tests/0104_file_ignore_prom.txt b/cmd/pint/tests/0104_file_ignore_prom.txt index fc55b868a..b6c9ad30a 100644 --- a/cmd/pint/tests/0104_file_ignore_prom.txt +++ b/cmd/pint/tests/0104_file_ignore_prom.txt @@ -9,7 +9,7 @@ stderr 'level=ERROR msg="Query returned an error" err="502 Bad Gateway" uri=http stderr 'level=ERROR msg="Query returned an error" err="502 Bad Gateway" uri=http://127.0.0.1:7104 query=count\(\\nfoo\\n\)' stderr 'level=INFO msg="Problems found" Bug=4' -- rules/0001.yml -- -# This should skip all online checks +# This should skip all checks that require Prometheus API # pint file/disable promql/series # pint file/disable promql/rate # diff --git a/cmd/pint/tests/0108_rule_duplicate.txt b/cmd/pint/tests/0108_rule_duplicate.txt index cc81e7daf..d08bd081e 100644 --- a/cmd/pint/tests/0108_rule_duplicate.txt +++ b/cmd/pint/tests/0108_rule_duplicate.txt @@ -21,8 +21,8 @@ Bug: unable to run checks (labels/conflict) ---> rules/0001.yml:5-8 -> `colo:labels:mismatch` [+4 duplicates] 5 | - record: "colo:labels:mismatch" ^^^^^^^^^^^^^^^^^^^^ - Couldn't run some online checks due to `prom` Prometheus server at http://127.0.0.1:7108 - error: `connection refused`. + Couldn't run some checks due to `prom` Prometheus server at http://127.0.0.1:7108 error: + `connection refused`. Bug: duplicated recording rule (rule/duplicate) ---> rules/0001.yml:9-12 -> `colo:labels:equal` diff --git a/cmd/pint/tests/0115_file_disable_tag.txt b/cmd/pint/tests/0115_file_disable_tag.txt index 1ea87d779..a9b6d6e27 100644 --- a/cmd/pint/tests/0115_file_disable_tag.txt +++ b/cmd/pint/tests/0115_file_disable_tag.txt @@ -21,8 +21,8 @@ Warning: unable to run checks (promql/counter) ---> rules/0001.yml:7-9 -> `colo:test1` 7 | - record: "colo:test1" ^^^^^^^^^^ - Couldn't run some online checks due to `prom` Prometheus server at http://127.0.0.1:7103 - error: `connection refused`. + Couldn't run some checks due to `prom` Prometheus server at http://127.0.0.1:7103 error: + `connection refused`. level=INFO msg="Problems found" Warning=1 -- rules/0001.yml -- diff --git a/cmd/pint/tests/0118_ci_dir_move.txt b/cmd/pint/tests/0118_ci_dir_move.txt index 61084421a..67144363a 100644 --- a/cmd/pint/tests/0118_ci_dir_move.txt +++ b/cmd/pint/tests/0118_ci_dir_move.txt @@ -93,8 +93,7 @@ body: |+ | Number of rules parsed | 2 | | Number of rules checked | 2 | | Number of problems found | 0 | - | Number of offline checks | 0 | - | Number of online checks | 0 | + | Number of checks | 0 | | Checks duration | 0 |

diff --git a/cmd/pint/tests/0119_ci_fail_on_warning.txt b/cmd/pint/tests/0119_ci_fail_on_warning.txt index 48102126c..7f7144b7d 100644 --- a/cmd/pint/tests/0119_ci_fail_on_warning.txt +++ b/cmd/pint/tests/0119_ci_fail_on_warning.txt @@ -211,8 +211,7 @@ body: |+ | Number of rules parsed | 2 | | Number of rules checked | 2 | | Number of problems found | 3 | - | Number of offline checks | 11 | - | Number of online checks | 0 | + | Number of checks | 11 | | Checks duration | 0 |

diff --git a/cmd/pint/tests/0246_ci_github_new_file.txt b/cmd/pint/tests/0246_ci_github_new_file.txt index 30e76e5e6..717e8422f 100644 --- a/cmd/pint/tests/0246_ci_github_new_file.txt +++ b/cmd/pint/tests/0246_ci_github_new_file.txt @@ -123,8 +123,7 @@ body: |+ | Number of rules parsed | 1 | | Number of rules checked | 1 | | Number of problems found | 1 | - | Number of offline checks | 10 | - | Number of online checks | 0 | + | Number of checks | 10 | | Checks duration | 0 |

diff --git a/cmd/pint/tests/0247_ci_github_rename.txt b/cmd/pint/tests/0247_ci_github_rename.txt index dce0c9e27..e13944ffc 100644 --- a/cmd/pint/tests/0247_ci_github_rename.txt +++ b/cmd/pint/tests/0247_ci_github_rename.txt @@ -164,8 +164,7 @@ body: |+ | Number of rules parsed | 1 | | Number of rules checked | 1 | | Number of problems found | 2 | - | Number of offline checks | 9 | - | Number of online checks | 0 | + | Number of checks | 9 | | Checks duration | 0 |

diff --git a/cmd/pint/tests/0248_ci_github_deleted_line.txt b/cmd/pint/tests/0248_ci_github_deleted_line.txt index d0fdefb14..feb3e6d18 100644 --- a/cmd/pint/tests/0248_ci_github_deleted_line.txt +++ b/cmd/pint/tests/0248_ci_github_deleted_line.txt @@ -169,8 +169,7 @@ body: |+ | Number of rules parsed | 5 | | Number of rules checked | 5 | | Number of problems found | 1 | - | Number of offline checks | 2 | - | Number of online checks | 0 | + | Number of checks | 2 | | Checks duration | 0 |

diff --git a/cmd/pint/tests/0249_ci_github_shifted_line.txt b/cmd/pint/tests/0249_ci_github_shifted_line.txt index 7221298d0..1bd2c3f66 100644 --- a/cmd/pint/tests/0249_ci_github_shifted_line.txt +++ b/cmd/pint/tests/0249_ci_github_shifted_line.txt @@ -165,8 +165,7 @@ body: |+ | Number of rules parsed | 2 | | Number of rules checked | 2 | | Number of problems found | 2 | - | Number of offline checks | 20 | - | Number of online checks | 0 | + | Number of checks | 20 | | Checks duration | 0 |

diff --git a/cmd/pint/tests/0251_ci_github_line_outside_diff.txt b/cmd/pint/tests/0251_ci_github_line_outside_diff.txt index 320d7e170..a7e189a46 100644 --- a/cmd/pint/tests/0251_ci_github_line_outside_diff.txt +++ b/cmd/pint/tests/0251_ci_github_line_outside_diff.txt @@ -152,8 +152,7 @@ body: |+ | Number of rules parsed | 1 | | Number of rules checked | 1 | | Number of problems found | 1 | - | Number of offline checks | 10 | - | Number of online checks | 0 | + | Number of checks | 10 | | Checks duration | 0 |

diff --git a/cmd/pint/tests/0270_ci_github_long_expr.txt b/cmd/pint/tests/0270_ci_github_long_expr.txt index 6c3ea529b..82f5d0d22 100644 --- a/cmd/pint/tests/0270_ci_github_long_expr.txt +++ b/cmd/pint/tests/0270_ci_github_long_expr.txt @@ -238,8 +238,7 @@ body: |+ | Number of rules parsed | 2 | | Number of rules checked | 2 | | Number of problems found | 3 | - | Number of offline checks | 20 | - | Number of online checks | 0 | + | Number of checks | 20 | | Checks duration | 0 |

diff --git a/cmd/pint/tests/0271_ci_github_stale_general_comment.txt b/cmd/pint/tests/0271_ci_github_stale_general_comment.txt index 487940bbc..28461476d 100644 --- a/cmd/pint/tests/0271_ci_github_stale_general_comment.txt +++ b/cmd/pint/tests/0271_ci_github_stale_general_comment.txt @@ -104,8 +104,7 @@ body: |+ | Number of rules parsed | 1 | | Number of rules checked | 1 | | Number of problems found | 0 | - | Number of offline checks | 10 | - | Number of online checks | 0 | + | Number of checks | 10 | | Checks duration | 0 |

diff --git a/docs/index.md b/docs/index.md index 7de9339e2..ee02155e1 100644 --- a/docs/index.md +++ b/docs/index.md @@ -13,13 +13,13 @@ pint is a Prometheus rule linter/validator. pint will run checks on Prometheus alerting & recording rules to detect potential problems with those rules. -Some checks rely only on the rule itself and can be run "offline" - without talking to any -Prometheus server. -You can run pint in "offline" mode if you: - -- Don't pass any configuration file to pint. -- You pass configuration file to pint that **doesn't** contain any `prometheus` definition. -- You pass `--offline` flag to `pint` command. +You can run pint in "offline" mode by passing the `--offline` flag. +In this mode no network requests will be sent. Some checks might be skipped +entirely while others will run with reduced functionality — only the parts +of their logic that don't require network access will be executed. +This means that passing `--offline` will disable Prometheus API queries and so +any check, or part of a check, that needs to talk to a live Prometheus server +will be skipped. Most checks included in pint will require sending queries to a running Prometheus server where those rules are, or would be, deployed. diff --git a/internal/checks/alerts_absent.go b/internal/checks/alerts_absent.go index c71893590..9decbed6a 100644 --- a/internal/checks/alerts_absent.go +++ b/internal/checks/alerts_absent.go @@ -40,7 +40,6 @@ func (c AlertsAbsentCheck) Meta() CheckMeta { discovery.Modified, discovery.Moved, }, - Online: true, AlwaysEnabled: false, } } @@ -54,6 +53,10 @@ func (c AlertsAbsentCheck) Reporter() string { } func (c AlertsAbsentCheck) Check(ctx context.Context, entry *discovery.Entry, _ []*discovery.Entry) (problems []Problem) { + if promapi.IsOffline(ctx) { + return problems + } + if entry.Rule.AlertingRule == nil { return problems } diff --git a/internal/checks/alerts_absent_test.go b/internal/checks/alerts_absent_test.go index 422bc4773..d5df48427 100644 --- a/internal/checks/alerts_absent_test.go +++ b/internal/checks/alerts_absent_test.go @@ -1,6 +1,7 @@ package checks_test import ( + "context" "net/http" "testing" "time" @@ -15,6 +16,15 @@ func newAlertsAbsentCheck(prom *promapi.FailoverGroup) checks.RuleChecker { func TestAlertsAbsentCheck(t *testing.T) { testCases := []checkTest{ + { + description: "offline", + content: "- alert: foo\n expr: absent(foo)\n", + checker: newAlertsAbsentCheck, + prometheus: newSimpleProm, + ctx: func(ctx context.Context, _ string) context.Context { + return promapi.WithOffline(ctx, true) + }, + }, { description: "ignores recording rules", content: "- record: foo\n expr: sum(foo)\n", diff --git a/internal/checks/alerts_absent_test.snap b/internal/checks/alerts_absent_test.snap index c304a199f..d21050fbe 100644 --- a/internal/checks/alerts_absent_test.snap +++ b/internal/checks/alerts_absent_test.snap @@ -12,14 +12,14 @@ output: | 1 | - alert: foo ^^^ - Couldn't run some online checks due to `prom` Prometheus server at http://127.0.0.1:XXXXX - error: `bad_response: 600 status code 600`. + Couldn't run some checks due to `prom` Prometheus server at http://127.0.0.1:XXXXX error: + `bad_response: 600 status code 600`. problem: reporter: alerts/absent summary: unable to run checks details: "" diagnostics: - - message: 'Couldn''t run some online checks due to `prom` Prometheus server at http://127.0.0.1:XXXXX error: `bad_response: 600 status code 600`.' + - message: 'Couldn''t run some checks due to `prom` Prometheus server at http://127.0.0.1:XXXXX error: `bad_response: 600 status code 600`.' firstcolumn: 1 lastcolumn: 3 kind: 0 @@ -201,14 +201,14 @@ output: | 1 | - alert: foo ^^^ - Couldn't run some online checks due to `prom` Prometheus server at http://127.0.0.1:1111 - error: `connection refused`. + Couldn't run some checks due to `prom` Prometheus server at http://127.0.0.1:1111 error: + `connection refused`. problem: reporter: alerts/absent summary: unable to run checks details: "" diagnostics: - - message: 'Couldn''t run some online checks due to `prom` Prometheus server at http://127.0.0.1:1111 error: `connection refused`.' + - message: 'Couldn''t run some checks due to `prom` Prometheus server at http://127.0.0.1:1111 error: `connection refused`.' firstcolumn: 1 lastcolumn: 3 kind: 0 @@ -277,15 +277,15 @@ output: | 1 | - alert: foo ^^^ - Couldn't run some online checks due to `prom` Prometheus server at http://127.0.0.1:XXXXX - error: `failed to decode config data in http://127.0.0.1:XXXXX response: yaml: line 2: - could not find expected ':'`. + Couldn't run some checks due to `prom` Prometheus server at http://127.0.0.1:XXXXX error: + `failed to decode config data in http://127.0.0.1:XXXXX response: yaml: line 2: could not + find expected ':'`. problem: reporter: alerts/absent summary: unable to run checks details: "" diagnostics: - - message: 'Couldn''t run some online checks due to `prom` Prometheus server at http://127.0.0.1:XXXXX error: `failed to decode config data in http://127.0.0.1:XXXXX response: yaml: line 2: could not find expected '':''`.' + - message: 'Couldn''t run some checks due to `prom` Prometheus server at http://127.0.0.1:XXXXX error: `failed to decode config data in http://127.0.0.1:XXXXX response: yaml: line 2: could not find expected '':''`.' firstcolumn: 1 lastcolumn: 3 kind: 0 @@ -305,14 +305,14 @@ output: | 1 | - alert: foo ^^^ - Couldn't run some online checks due to `prom` Prometheus server at http://127.0.0.1:XXXXX - error: `bad_data: bad input data`. + Couldn't run some checks due to `prom` Prometheus server at http://127.0.0.1:XXXXX error: + `bad_data: bad input data`. problem: reporter: alerts/absent summary: unable to run checks details: "" diagnostics: - - message: 'Couldn''t run some online checks due to `prom` Prometheus server at http://127.0.0.1:XXXXX error: `bad_data: bad input data`.' + - message: 'Couldn''t run some checks due to `prom` Prometheus server at http://127.0.0.1:XXXXX error: `bad_data: bad input data`.' firstcolumn: 1 lastcolumn: 3 kind: 0 @@ -323,3 +323,8 @@ anchor: 0 --- + +[TestAlertsAbsentCheck/offline - 1] +[] + +--- diff --git a/internal/checks/alerts_annotation.go b/internal/checks/alerts_annotation.go index cb1af539f..10067651b 100644 --- a/internal/checks/alerts_annotation.go +++ b/internal/checks/alerts_annotation.go @@ -54,7 +54,6 @@ func (c AnnotationCheck) Meta() CheckMeta { discovery.Modified, discovery.Moved, }, - Online: false, AlwaysEnabled: false, } } diff --git a/internal/checks/alerts_comparison.go b/internal/checks/alerts_comparison.go index bc0ed1be2..1466a5eed 100644 --- a/internal/checks/alerts_comparison.go +++ b/internal/checks/alerts_comparison.go @@ -30,7 +30,6 @@ func (c ComparisonCheck) Meta() CheckMeta { discovery.Modified, discovery.Moved, }, - Online: false, AlwaysEnabled: false, } } diff --git a/internal/checks/alerts_count.go b/internal/checks/alerts_count.go index 838dbe740..2346f9f40 100644 --- a/internal/checks/alerts_count.go +++ b/internal/checks/alerts_count.go @@ -49,7 +49,6 @@ func (c AlertsCheck) Meta() CheckMeta { discovery.Modified, discovery.Moved, }, - Online: true, AlwaysEnabled: false, } } @@ -63,6 +62,10 @@ func (c AlertsCheck) Reporter() string { } func (c AlertsCheck) Check(ctx context.Context, entry *discovery.Entry, _ []*discovery.Entry) (problems []Problem) { + if promapi.IsOffline(ctx) { + return problems + } + if entry.Rule.AlertingRule == nil { return problems } diff --git a/internal/checks/alerts_count_test.go b/internal/checks/alerts_count_test.go index 269de1fbc..d22a42e36 100644 --- a/internal/checks/alerts_count_test.go +++ b/internal/checks/alerts_count_test.go @@ -1,6 +1,7 @@ package checks_test import ( + "context" "testing" "time" @@ -18,6 +19,15 @@ func TestAlertsCountCheck(t *testing.T) { content := "- alert: Foo Is Down\n expr: up{job=\"foo\"} == 0\n" testCases := []checkTest{ + { + description: "offline", + content: "- alert: foo\n expr: up == 0\n", + checker: newAlertsCheck, + prometheus: newSimpleProm, + ctx: func(ctx context.Context, _ string) context.Context { + return promapi.WithOffline(ctx, true) + }, + }, { description: "ignores recording rules", content: "- record: foo\n expr: up == 0\n", diff --git a/internal/checks/alerts_count_test.snap b/internal/checks/alerts_count_test.snap index e08b25862..81f76ec9a 100644 --- a/internal/checks/alerts_count_test.snap +++ b/internal/checks/alerts_count_test.snap @@ -7,14 +7,14 @@ output: | 1 | - alert: Foo Is Down ^^^^^^^^^^^ - Couldn't run some online checks due to `prom` Prometheus server at http://127.0.0.1:XXXXX - error: `bad_data: bad input data`. + Couldn't run some checks due to `prom` Prometheus server at http://127.0.0.1:XXXXX error: + `bad_data: bad input data`. problem: reporter: alerts/count summary: unable to run checks details: "" diagnostics: - - message: 'Couldn''t run some online checks due to `prom` Prometheus server at http://127.0.0.1:XXXXX error: `bad_data: bad input data`.' + - message: 'Couldn''t run some checks due to `prom` Prometheus server at http://127.0.0.1:XXXXX error: `bad_data: bad input data`.' firstcolumn: 1 lastcolumn: 11 kind: 0 @@ -34,14 +34,14 @@ output: | 1 | - alert: Foo Is Down ^^^^^^^^^^^ - Couldn't run some online checks due to `prom` Prometheus server at http://127.0.0.1:1111 - error: `connection refused`. + Couldn't run some checks due to `prom` Prometheus server at http://127.0.0.1:1111 error: + `connection refused`. problem: reporter: alerts/count summary: unable to run checks details: "" diagnostics: - - message: 'Couldn''t run some online checks due to `prom` Prometheus server at http://127.0.0.1:1111 error: `connection refused`.' + - message: 'Couldn''t run some checks due to `prom` Prometheus server at http://127.0.0.1:1111 error: `connection refused`.' firstcolumn: 1 lastcolumn: 11 kind: 0 @@ -265,6 +265,11 @@ --- +[TestAlertsCountCheck/offline - 1] +[] + +--- + [TestAlertsCountCheck/ranges_merge_when_separated_by_Prometheus_downtime - 1] - description: ranges merge when separated by Prometheus downtime content: | diff --git a/internal/checks/alerts_external_labels.go b/internal/checks/alerts_external_labels.go index 056ea7fc6..8af899d2b 100644 --- a/internal/checks/alerts_external_labels.go +++ b/internal/checks/alerts_external_labels.go @@ -34,7 +34,6 @@ func (c AlertsExternalLabelsCheck) Meta() CheckMeta { discovery.Modified, discovery.Moved, }, - Online: true, AlwaysEnabled: false, } } @@ -48,6 +47,10 @@ func (c AlertsExternalLabelsCheck) Reporter() string { } func (c AlertsExternalLabelsCheck) Check(ctx context.Context, entry *discovery.Entry, _ []*discovery.Entry) (problems []Problem) { + if promapi.IsOffline(ctx) { + return problems + } + if entry.Rule.AlertingRule == nil { return problems } diff --git a/internal/checks/alerts_external_labels_test.go b/internal/checks/alerts_external_labels_test.go index 479e52eb7..80ec8549e 100644 --- a/internal/checks/alerts_external_labels_test.go +++ b/internal/checks/alerts_external_labels_test.go @@ -1,6 +1,7 @@ package checks_test import ( + "context" "net/http" "testing" "time" @@ -27,6 +28,15 @@ func TestAlertsExternalLabelsCountCheck(t *testing.T) { ` testCases := []checkTest{ + { + description: "offline", + content: "- alert: foo\n expr: up == 0\n", + checker: newAlertsExternalLabelsCheck, + prometheus: newSimpleProm, + ctx: func(ctx context.Context, _ string) context.Context { + return promapi.WithOffline(ctx, true) + }, + }, { description: "ignores recording rules", content: "- record: foo\n expr: up == 0\n", diff --git a/internal/checks/alerts_external_labels_test.snap b/internal/checks/alerts_external_labels_test.snap index b569fb48c..9f4f2ac76 100644 --- a/internal/checks/alerts_external_labels_test.snap +++ b/internal/checks/alerts_external_labels_test.snap @@ -20,14 +20,14 @@ output: | 2 | - alert: Foo Is Down ^^^^^^^^^^^ - Couldn't run some online checks due to `prom` Prometheus server at http://127.0.0.1:XXXXX - error: `bad_data: bad input data`. + Couldn't run some checks due to `prom` Prometheus server at http://127.0.0.1:XXXXX error: + `bad_data: bad input data`. problem: reporter: alerts/external_labels summary: unable to run checks details: "" diagnostics: - - message: 'Couldn''t run some online checks due to `prom` Prometheus server at http://127.0.0.1:XXXXX error: `bad_data: bad input data`.' + - message: 'Couldn''t run some checks due to `prom` Prometheus server at http://127.0.0.1:XXXXX error: `bad_data: bad input data`.' firstcolumn: 1 lastcolumn: 11 kind: 0 @@ -60,14 +60,14 @@ output: | 2 | - alert: Foo Is Down ^^^^^^^^^^^ - Couldn't run some online checks due to `prom` Prometheus server at http://127.0.0.1:1111 - error: `connection refused`. + Couldn't run some checks due to `prom` Prometheus server at http://127.0.0.1:1111 error: + `connection refused`. problem: reporter: alerts/external_labels summary: unable to run checks details: "" diagnostics: - - message: 'Couldn''t run some online checks due to `prom` Prometheus server at http://127.0.0.1:1111 error: `connection refused`.' + - message: 'Couldn''t run some checks due to `prom` Prometheus server at http://127.0.0.1:1111 error: `connection refused`.' firstcolumn: 1 lastcolumn: 11 kind: 0 @@ -265,3 +265,8 @@ anchor: 0 --- + +[TestAlertsExternalLabelsCountCheck/offline - 1] +[] + +--- diff --git a/internal/checks/alerts_for.go b/internal/checks/alerts_for.go index 6ddfe4724..aa887819a 100644 --- a/internal/checks/alerts_for.go +++ b/internal/checks/alerts_for.go @@ -28,7 +28,6 @@ func (c AlertsForChecksFor) Meta() CheckMeta { discovery.Modified, discovery.Moved, }, - Online: false, AlwaysEnabled: false, } } diff --git a/internal/checks/alerts_template.go b/internal/checks/alerts_template.go index 1bd715168..6e1a15ee4 100644 --- a/internal/checks/alerts_template.go +++ b/internal/checks/alerts_template.go @@ -104,7 +104,6 @@ func (c TemplateCheck) Meta() CheckMeta { discovery.Modified, discovery.Moved, }, - Online: false, AlwaysEnabled: false, } } diff --git a/internal/checks/base.go b/internal/checks/base.go index c6b349bb9..dd28ae54c 100644 --- a/internal/checks/base.go +++ b/internal/checks/base.go @@ -14,57 +14,40 @@ import ( "github.com/cloudflare/pint/internal/promapi" ) -var ( - CheckNames = []string{ - AlertsAbsentCheckName, - AnnotationCheckName, - ComparisonCheckName, - AlertsCheckName, - AlertsExternalLabelsCheckName, - AlertForCheckName, - TemplateCheckName, - LabelsConflictCheckName, - AggregationCheckName, - CounterCheckName, - FeaturesCheckName, - FragileCheckName, - GroupIntervalCheckName, - ImpossibleCheckName, - NaNCheckName, - OffsetCheckName, - RangeQueryCheckName, - RateCheckName, - RegexpCheckName, - SelectorCheckName, - SeriesCheckName, - SyntaxCheckName, - VectorMatchingCheckName, - CostCheckName, - RuleDependencyCheckName, - RuleDuplicateCheckName, - RuleForCheckName, - LabelCheckName, - RuleLinkCheckName, - RuleNameCheckName, - RejectCheckName, - ReportCheckName, - } - OnlineChecks = []string{ - AlertsAbsentCheckName, - AlertsCheckName, - AlertsExternalLabelsCheckName, - LabelsConflictCheckName, - CounterCheckName, - FeaturesCheckName, - OffsetCheckName, - RangeQueryCheckName, - RateCheckName, - SeriesCheckName, - VectorMatchingCheckName, - CostCheckName, - RuleLinkCheckName, - } -) +var CheckNames = []string{ + AlertsAbsentCheckName, + AnnotationCheckName, + ComparisonCheckName, + AlertsCheckName, + AlertsExternalLabelsCheckName, + AlertForCheckName, + TemplateCheckName, + LabelsConflictCheckName, + AggregationCheckName, + CounterCheckName, + FeaturesCheckName, + FragileCheckName, + GroupIntervalCheckName, + ImpossibleCheckName, + NaNCheckName, + OffsetCheckName, + RangeQueryCheckName, + RateCheckName, + RegexpCheckName, + SelectorCheckName, + SeriesCheckName, + SyntaxCheckName, + VectorMatchingCheckName, + CostCheckName, + RuleDependencyCheckName, + RuleDuplicateCheckName, + RuleForCheckName, + LabelCheckName, + RuleLinkCheckName, + RuleNameCheckName, + RejectCheckName, + ReportCheckName, +} // Severity of the problem reported. type Severity uint8 @@ -133,7 +116,6 @@ type Problem struct { type CheckMeta struct { States []discovery.ChangeType - Online bool AlwaysEnabled bool } @@ -157,16 +139,16 @@ func problemFromError(err error, rule parser.Rule, reporter, prom string, s Seve var severity Severity switch { case promapi.IsQueryTooExpensive(err): - text = fmt.Sprintf("Couldn't run some online checks on %s because some queries are too expensive: `%s`.", promDesc, err) + text = fmt.Sprintf("Couldn't run some checks on %s because some queries are too expensive: `%s`.", promDesc, err) severity = Warning case promapi.IsUnavailableError(err): - text = fmt.Sprintf("Couldn't run some online checks due to %s error: `%s`.", promDesc, err) + text = fmt.Sprintf("Couldn't run some checks due to %s error: `%s`.", promDesc, err) severity = Warning if perrOk && perr.IsStrict() { severity = Bug } default: - text = fmt.Sprintf("Couldn't run some online checks due to %s error: `%s`.", promDesc, err) + text = fmt.Sprintf("Couldn't run some checks due to %s error: `%s`.", promDesc, err) severity = s } diff --git a/internal/checks/error.go b/internal/checks/error.go index 46594626f..655d4d529 100644 --- a/internal/checks/error.go +++ b/internal/checks/error.go @@ -40,7 +40,6 @@ func (c ErrorCheck) Meta() CheckMeta { discovery.Moved, discovery.Removed, }, - Online: false, AlwaysEnabled: true, } } diff --git a/internal/checks/group_interval.go b/internal/checks/group_interval.go index e02e50cb8..902c3b789 100644 --- a/internal/checks/group_interval.go +++ b/internal/checks/group_interval.go @@ -26,7 +26,6 @@ func (c GroupIntervalCheck) Meta() CheckMeta { discovery.Modified, discovery.Moved, }, - Online: false, AlwaysEnabled: false, } } diff --git a/internal/checks/labels_conflict.go b/internal/checks/labels_conflict.go index 276c70450..2e67a525b 100644 --- a/internal/checks/labels_conflict.go +++ b/internal/checks/labels_conflict.go @@ -35,7 +35,6 @@ func (c LabelsConflictCheck) Meta() CheckMeta { discovery.Modified, discovery.Moved, }, - Online: true, AlwaysEnabled: false, } } @@ -49,6 +48,10 @@ func (c LabelsConflictCheck) Reporter() string { } func (c LabelsConflictCheck) Check(ctx context.Context, entry *discovery.Entry, _ []*discovery.Entry) (problems []Problem) { + if promapi.IsOffline(ctx) { + return problems + } + var labels *parser.YamlMap if entry.Rule.AlertingRule != nil && entry.Rule.AlertingRule.Expr.SyntaxError() == nil && entry.Rule.AlertingRule.Labels != nil { labels = entry.Rule.AlertingRule.Labels diff --git a/internal/checks/labels_conflict_test.go b/internal/checks/labels_conflict_test.go index 28d833e0b..3626b341a 100644 --- a/internal/checks/labels_conflict_test.go +++ b/internal/checks/labels_conflict_test.go @@ -1,6 +1,7 @@ package checks_test import ( + "context" "net/http" "testing" "time" @@ -15,6 +16,15 @@ func newLabelsConflict(prom *promapi.FailoverGroup) checks.RuleChecker { func TestLabelsConflictCheck(t *testing.T) { testCases := []checkTest{ + { + description: "offline", + content: "- alert: foo\n expr: up == 0\n labels:\n job: bar\n", + checker: newLabelsConflict, + prometheus: newSimpleProm, + ctx: func(ctx context.Context, _ string) context.Context { + return promapi.WithOffline(ctx, true) + }, + }, { description: "ignores rules with syntax errors", content: "- record: foo\n expr: sum(foo) without(\n", diff --git a/internal/checks/labels_conflict_test.snap b/internal/checks/labels_conflict_test.snap index 8a61dabf3..ab1395929 100644 --- a/internal/checks/labels_conflict_test.snap +++ b/internal/checks/labels_conflict_test.snap @@ -97,14 +97,14 @@ output: | 1 | - record: foo ^^^ - Couldn't run some online checks due to `prom` Prometheus server at http://127.0.0.1:1111 - error: `connection refused`. + Couldn't run some checks due to `prom` Prometheus server at http://127.0.0.1:1111 error: + `connection refused`. problem: reporter: labels/conflict summary: unable to run checks details: "" diagnostics: - - message: 'Couldn''t run some online checks due to `prom` Prometheus server at http://127.0.0.1:1111 error: `connection refused`.' + - message: 'Couldn''t run some checks due to `prom` Prometheus server at http://127.0.0.1:1111 error: `connection refused`.' firstcolumn: 1 lastcolumn: 3 kind: 0 @@ -150,3 +150,8 @@ [] --- + +[TestLabelsConflictCheck/offline - 1] +[] + +--- diff --git a/internal/checks/promql_aggregation.go b/internal/checks/promql_aggregation.go index 5e8270241..fee9d177d 100644 --- a/internal/checks/promql_aggregation.go +++ b/internal/checks/promql_aggregation.go @@ -43,7 +43,6 @@ func (c AggregationCheck) Meta() CheckMeta { discovery.Modified, discovery.Moved, }, - Online: false, AlwaysEnabled: false, } } diff --git a/internal/checks/promql_counter.go b/internal/checks/promql_counter.go index 743c904a3..3a9cff435 100644 --- a/internal/checks/promql_counter.go +++ b/internal/checks/promql_counter.go @@ -42,7 +42,6 @@ func (c CounterCheck) Meta() CheckMeta { discovery.Modified, discovery.Moved, }, - Online: true, AlwaysEnabled: false, } } @@ -56,6 +55,10 @@ func (c CounterCheck) Reporter() string { } func (c CounterCheck) Check(ctx context.Context, entry *discovery.Entry, _ []*discovery.Entry) (problems []Problem) { + if promapi.IsOffline(ctx) { + return problems + } + expr := entry.Rule.Expr() if expr.SyntaxError() != nil { diff --git a/internal/checks/promql_counter_test.go b/internal/checks/promql_counter_test.go index 61ff8f615..b84b75730 100644 --- a/internal/checks/promql_counter_test.go +++ b/internal/checks/promql_counter_test.go @@ -1,6 +1,7 @@ package checks_test import ( + "context" "testing" v1 "github.com/prometheus/client_golang/api/prometheus/v1" @@ -15,6 +16,15 @@ func newCounterCheck(prom *promapi.FailoverGroup) checks.RuleChecker { func TestCounterCheck(t *testing.T) { testCases := []checkTest{ + { + description: "offline", + content: "- record: foo\n expr: rate(bar[5m])\n", + checker: newCounterCheck, + prometheus: newSimpleProm, + ctx: func(ctx context.Context, _ string) context.Context { + return promapi.WithOffline(ctx, true) + }, + }, { description: "ignores rules with syntax errors", content: "- record: foo\n expr: sum(foo) without(\n", diff --git a/internal/checks/promql_counter_test.snap b/internal/checks/promql_counter_test.snap index 68926f362..2e9eff001 100644 --- a/internal/checks/promql_counter_test.snap +++ b/internal/checks/promql_counter_test.snap @@ -7,14 +7,14 @@ output: | 1 | - record: foo ^^^ - Couldn't run some online checks due to `prom` Prometheus server at http://127.0.0.1:XXXXX - error: `server_error: internal error`. + Couldn't run some checks due to `prom` Prometheus server at http://127.0.0.1:XXXXX error: + `server_error: internal error`. problem: reporter: promql/counter summary: unable to run checks details: "" diagnostics: - - message: 'Couldn''t run some online checks due to `prom` Prometheus server at http://127.0.0.1:XXXXX error: `server_error: internal error`.' + - message: 'Couldn''t run some checks due to `prom` Prometheus server at http://127.0.0.1:XXXXX error: `server_error: internal error`.' firstcolumn: 1 lastcolumn: 3 kind: 0 @@ -247,14 +247,14 @@ output: | 1 | - record: foo ^^^ - Couldn't run some online checks due to `prom` Prometheus server at http://127.0.0.1:XXXXX - error: `bad_data: bad input data`. + Couldn't run some checks due to `prom` Prometheus server at http://127.0.0.1:XXXXX error: + `bad_data: bad input data`. problem: reporter: promql/counter summary: unable to run checks details: "" diagnostics: - - message: 'Couldn''t run some online checks due to `prom` Prometheus server at http://127.0.0.1:XXXXX error: `bad_data: bad input data`.' + - message: 'Couldn''t run some checks due to `prom` Prometheus server at http://127.0.0.1:XXXXX error: `bad_data: bad input data`.' firstcolumn: 1 lastcolumn: 3 kind: 0 @@ -266,6 +266,11 @@ --- +[TestCounterCheck/offline - 1] +[] + +--- + [TestCounterCheck/ok_unless_counter - 1] [] diff --git a/internal/checks/promql_features.go b/internal/checks/promql_features.go index 2c7ff370f..56b7f95b2 100644 --- a/internal/checks/promql_features.go +++ b/internal/checks/promql_features.go @@ -41,7 +41,6 @@ func (c FeaturesCheck) Meta() CheckMeta { discovery.Modified, discovery.Moved, }, - Online: true, AlwaysEnabled: false, } } @@ -55,6 +54,10 @@ func (c FeaturesCheck) Reporter() string { } func (c FeaturesCheck) Check(ctx context.Context, entry *discovery.Entry, _ []*discovery.Entry) (problems []Problem) { + if promapi.IsOffline(ctx) { + return problems + } + expr := entry.Rule.Expr() if expr.SyntaxError() != nil { return problems diff --git a/internal/checks/promql_features_test.go b/internal/checks/promql_features_test.go index 2335614ec..110b8c21f 100644 --- a/internal/checks/promql_features_test.go +++ b/internal/checks/promql_features_test.go @@ -1,6 +1,7 @@ package checks_test import ( + "context" "net/http" "testing" @@ -15,6 +16,15 @@ func newFeaturesCheck(prom *promapi.FailoverGroup) checks.RuleChecker { func TestFeaturesCheck(t *testing.T) { testCases := []checkTest{ + { + description: "offline", + content: "- record: foo\n expr: bar @ start()\n", + checker: newFeaturesCheck, + prometheus: newSimpleProm, + ctx: func(ctx context.Context, _ string) context.Context { + return promapi.WithOffline(ctx, true) + }, + }, // Verifies that rules with syntax errors are skipped. { description: "ignores rules with syntax errors", diff --git a/internal/checks/promql_features_test.snap b/internal/checks/promql_features_test.snap index ba638bada..175e9bf5a 100644 --- a/internal/checks/promql_features_test.snap +++ b/internal/checks/promql_features_test.snap @@ -80,14 +80,14 @@ output: | 1 | - record: foo ^^^ - Couldn't run some online checks due to "prom" error: `failed to parse Prometheus version + Couldn't run some checks due to "prom" error: `failed to parse Prometheus version "not-a-version": expected major.minor.patch format`. problem: reporter: promql/features summary: unable to run checks details: "" diagnostics: - - message: 'Couldn''t run some online checks due to "prom" error: `failed to parse Prometheus version "not-a-version": expected major.minor.patch format`.' + - message: 'Couldn''t run some checks due to "prom" error: `failed to parse Prometheus version "not-a-version": expected major.minor.patch format`.' firstcolumn: 1 lastcolumn: 3 kind: 0 @@ -364,14 +364,14 @@ output: | 1 | - record: foo ^^^ - Couldn't run some online checks due to `prom` Prometheus server at http://127.0.0.1:XXXXX - error: `server_error: internal error`. + Couldn't run some checks due to `prom` Prometheus server at http://127.0.0.1:XXXXX error: + `server_error: internal error`. problem: reporter: promql/features summary: unable to run checks details: "" diagnostics: - - message: 'Couldn''t run some online checks due to `prom` Prometheus server at http://127.0.0.1:XXXXX error: `server_error: internal error`.' + - message: 'Couldn''t run some checks due to `prom` Prometheus server at http://127.0.0.1:XXXXX error: `server_error: internal error`.' firstcolumn: 1 lastcolumn: 3 kind: 0 @@ -663,6 +663,11 @@ --- +[TestFeaturesCheck/offline - 1] +[] + +--- + [TestFeaturesCheck/range()_missing_feature_flag - 1] - description: range() missing feature flag content: | diff --git a/internal/checks/promql_fragile.go b/internal/checks/promql_fragile.go index 336fe02f6..e7f66651a 100644 --- a/internal/checks/promql_fragile.go +++ b/internal/checks/promql_fragile.go @@ -41,7 +41,6 @@ func (c FragileCheck) Meta() CheckMeta { discovery.Modified, discovery.Moved, }, - Online: false, AlwaysEnabled: false, } } diff --git a/internal/checks/promql_impossible.go b/internal/checks/promql_impossible.go index 3f6be0c9a..20434fec5 100644 --- a/internal/checks/promql_impossible.go +++ b/internal/checks/promql_impossible.go @@ -27,7 +27,6 @@ func (c ImpossibleCheck) Meta() CheckMeta { discovery.Modified, discovery.Moved, }, - Online: false, AlwaysEnabled: false, } } diff --git a/internal/checks/promql_nan.go b/internal/checks/promql_nan.go index ff81bb6f9..05a497a02 100644 --- a/internal/checks/promql_nan.go +++ b/internal/checks/promql_nan.go @@ -46,7 +46,6 @@ func (c NaNCheck) Meta() CheckMeta { discovery.Modified, discovery.Moved, }, - Online: false, AlwaysEnabled: false, } } diff --git a/internal/checks/promql_offset.go b/internal/checks/promql_offset.go index 50e93f5ae..b110e52d2 100644 --- a/internal/checks/promql_offset.go +++ b/internal/checks/promql_offset.go @@ -39,7 +39,6 @@ func (c OffsetCheck) Meta() CheckMeta { discovery.Modified, discovery.Moved, }, - Online: true, AlwaysEnabled: false, } } @@ -53,6 +52,10 @@ func (c OffsetCheck) Reporter() string { } func (c OffsetCheck) Check(ctx context.Context, entry *discovery.Entry, _ []*discovery.Entry) (problems []Problem) { + if promapi.IsOffline(ctx) { + return problems + } + expr := entry.Rule.Expr() if expr.SyntaxError() != nil { return problems diff --git a/internal/checks/promql_offset_test.go b/internal/checks/promql_offset_test.go index 421a88cd4..f1e926d18 100644 --- a/internal/checks/promql_offset_test.go +++ b/internal/checks/promql_offset_test.go @@ -1,6 +1,7 @@ package checks_test import ( + "context" "net/http" "testing" @@ -14,6 +15,15 @@ func newOffsetCheck(prom *promapi.FailoverGroup) checks.RuleChecker { func TestOffsetCheck(t *testing.T) { testCases := []checkTest{ + { + description: "offline", + content: "- record: foo\n expr: bar\n", + checker: newOffsetCheck, + prometheus: newSimpleProm, + ctx: func(ctx context.Context, _ string) context.Context { + return promapi.WithOffline(ctx, true) + }, + }, { description: "ignores rules with syntax errors", content: "- record: foo\n expr: sum(foo) without(\n", diff --git a/internal/checks/promql_offset_test.snap b/internal/checks/promql_offset_test.snap index 155a61f29..8ab8d4524 100644 --- a/internal/checks/promql_offset_test.snap +++ b/internal/checks/promql_offset_test.snap @@ -116,14 +116,14 @@ output: | 1 | - record: foo ^^^ - Couldn't run some online checks due to `prom` Prometheus server at http://127.0.0.1:XXXXX - error: `server_error: internal error`. + Couldn't run some checks due to `prom` Prometheus server at http://127.0.0.1:XXXXX error: + `server_error: internal error`. problem: reporter: promql/offset summary: unable to run checks details: "" diagnostics: - - message: 'Couldn''t run some online checks due to `prom` Prometheus server at http://127.0.0.1:XXXXX error: `server_error: internal error`.' + - message: 'Couldn''t run some checks due to `prom` Prometheus server at http://127.0.0.1:XXXXX error: `server_error: internal error`.' firstcolumn: 1 lastcolumn: 3 kind: 0 @@ -272,6 +272,11 @@ --- +[TestOffsetCheck/offline - 1] +[] + +--- + [TestOffsetCheck/retention_11d,_offset_10d - 1] [] diff --git a/internal/checks/promql_range_query.go b/internal/checks/promql_range_query.go index 75e4ea969..00fbf7d88 100644 --- a/internal/checks/promql_range_query.go +++ b/internal/checks/promql_range_query.go @@ -54,7 +54,6 @@ func (c RangeQueryCheck) Meta() CheckMeta { discovery.Modified, discovery.Moved, }, - Online: true, AlwaysEnabled: false, } } @@ -68,6 +67,10 @@ func (c RangeQueryCheck) Reporter() string { } func (c RangeQueryCheck) Check(ctx context.Context, entry *discovery.Entry, _ []*discovery.Entry) (problems []Problem) { + if promapi.IsOffline(ctx) { + return problems + } + expr := entry.Rule.Expr() if expr.SyntaxError() != nil { return problems diff --git a/internal/checks/promql_range_query_test.go b/internal/checks/promql_range_query_test.go index ea3722676..c464fce5e 100644 --- a/internal/checks/promql_range_query_test.go +++ b/internal/checks/promql_range_query_test.go @@ -1,6 +1,7 @@ package checks_test import ( + "context" "net/http" "testing" "time" @@ -19,6 +20,15 @@ func newRangeQueryCheckWithLimit(prom *promapi.FailoverGroup) checks.RuleChecker func TestRangeQueryCheck(t *testing.T) { testCases := []checkTest{ + { + description: "offline", + content: "- record: foo\n expr: rate(bar[5m])\n", + checker: newRangeQueryCheck, + prometheus: newSimpleProm, + ctx: func(ctx context.Context, _ string) context.Context { + return promapi.WithOffline(ctx, true) + }, + }, { description: "ignores rules with syntax errors", content: "- record: foo\n expr: sum(foo) without(\n", diff --git a/internal/checks/promql_range_query_test.snap b/internal/checks/promql_range_query_test.snap index 5977639bd..6765acb2d 100644 --- a/internal/checks/promql_range_query_test.snap +++ b/internal/checks/promql_range_query_test.snap @@ -67,14 +67,14 @@ output: | 1 | - record: foo ^^^ - Couldn't run some online checks due to `prom` Prometheus server at http://127.0.0.1:XXXXX - error: `server_error: internal error`. + Couldn't run some checks due to `prom` Prometheus server at http://127.0.0.1:XXXXX error: + `server_error: internal error`. problem: reporter: promql/range_query summary: unable to run checks details: "" diagnostics: - - message: 'Couldn''t run some online checks due to `prom` Prometheus server at http://127.0.0.1:XXXXX error: `server_error: internal error`.' + - message: 'Couldn''t run some checks due to `prom` Prometheus server at http://127.0.0.1:XXXXX error: `server_error: internal error`.' firstcolumn: 1 lastcolumn: 3 kind: 0 @@ -297,3 +297,8 @@ anchor: 0 --- + +[TestRangeQueryCheck/offline - 1] +[] + +--- diff --git a/internal/checks/promql_rate.go b/internal/checks/promql_rate.go index 4729b6c0a..60f4c9273 100644 --- a/internal/checks/promql_rate.go +++ b/internal/checks/promql_rate.go @@ -61,7 +61,6 @@ func (c RateCheck) Meta() CheckMeta { discovery.Modified, discovery.Moved, }, - Online: true, AlwaysEnabled: false, } } @@ -75,6 +74,10 @@ func (c RateCheck) Reporter() string { } func (c RateCheck) Check(ctx context.Context, entry *discovery.Entry, entries []*discovery.Entry) (problems []Problem) { + if promapi.IsOffline(ctx) { + return problems + } + expr := entry.Rule.Expr() if expr.SyntaxError() != nil { diff --git a/internal/checks/promql_rate_test.go b/internal/checks/promql_rate_test.go index 43a77df2f..991eed9df 100644 --- a/internal/checks/promql_rate_test.go +++ b/internal/checks/promql_rate_test.go @@ -1,6 +1,7 @@ package checks_test import ( + "context" "errors" "net/http" "testing" @@ -20,6 +21,15 @@ func newRateCheck(prom *promapi.FailoverGroup) checks.RuleChecker { func TestRateCheck(t *testing.T) { testCases := []checkTest{ + { + description: "offline", + content: "- record: foo\n expr: rate(bar[5m])\n", + checker: newRateCheck, + prometheus: newSimpleProm, + ctx: func(ctx context.Context, _ string) context.Context { + return promapi.WithOffline(ctx, true) + }, + }, { description: "ignores rules with syntax errors", content: "- record: foo\n expr: sum(foo) without(\n", diff --git a/internal/checks/promql_rate_test.snap b/internal/checks/promql_rate_test.snap index ee2cf094b..37df0b040 100644 --- a/internal/checks/promql_rate_test.snap +++ b/internal/checks/promql_rate_test.snap @@ -7,14 +7,14 @@ output: | 1 | - record: foo ^^^ - Couldn't run some online checks due to `prom` Prometheus server at http://127.0.0.1:XXXXX - error: `server_error: internal error`. + Couldn't run some checks due to `prom` Prometheus server at http://127.0.0.1:XXXXX error: + `server_error: internal error`. problem: reporter: promql/rate summary: unable to run checks details: "" diagnostics: - - message: 'Couldn''t run some online checks due to `prom` Prometheus server at http://127.0.0.1:XXXXX error: `server_error: internal error`.' + - message: 'Couldn''t run some checks due to `prom` Prometheus server at http://127.0.0.1:XXXXX error: `server_error: internal error`.' firstcolumn: 1 lastcolumn: 3 kind: 0 @@ -39,14 +39,14 @@ output: | 1 | - record: foo ^^^ - Couldn't run some online checks due to `prom` Prometheus server at http://127.0.0.1:1111 - error: `connection refused`. + Couldn't run some checks due to `prom` Prometheus server at http://127.0.0.1:1111 error: + `connection refused`. problem: reporter: promql/rate summary: unable to run checks details: "" diagnostics: - - message: 'Couldn''t run some online checks due to `prom` Prometheus server at http://127.0.0.1:1111 error: `connection refused`.' + - message: 'Couldn''t run some checks due to `prom` Prometheus server at http://127.0.0.1:1111 error: `connection refused`.' firstcolumn: 1 lastcolumn: 3 kind: 0 @@ -116,15 +116,15 @@ output: | 1 | - record: foo ^^^ - Couldn't run some online checks due to `prom` Prometheus server at http://127.0.0.1:XXXXX - error: `failed to decode config data in http://127.0.0.1:XXXXX response: yaml: line 2: - could not find expected ':'`. + Couldn't run some checks due to `prom` Prometheus server at http://127.0.0.1:XXXXX error: + `failed to decode config data in http://127.0.0.1:XXXXX response: yaml: line 2: could not + find expected ':'`. problem: reporter: promql/rate summary: unable to run checks details: "" diagnostics: - - message: 'Couldn''t run some online checks due to `prom` Prometheus server at http://127.0.0.1:XXXXX error: `failed to decode config data in http://127.0.0.1:XXXXX response: yaml: line 2: could not find expected '':''`.' + - message: 'Couldn''t run some checks due to `prom` Prometheus server at http://127.0.0.1:XXXXX error: `failed to decode config data in http://127.0.0.1:XXXXX response: yaml: line 2: could not find expected '':''`.' firstcolumn: 1 lastcolumn: 3 kind: 0 @@ -144,14 +144,14 @@ output: | 1 | - record: foo ^^^ - Couldn't run some online checks due to `prom` Prometheus server at http://127.0.0.1:XXXXX - error: `bad_data: bad input data`. + Couldn't run some checks due to `prom` Prometheus server at http://127.0.0.1:XXXXX error: + `bad_data: bad input data`. problem: reporter: promql/rate summary: unable to run checks details: "" diagnostics: - - message: 'Couldn''t run some online checks due to `prom` Prometheus server at http://127.0.0.1:XXXXX error: `bad_data: bad input data`.' + - message: 'Couldn''t run some checks due to `prom` Prometheus server at http://127.0.0.1:XXXXX error: `bad_data: bad input data`.' firstcolumn: 1 lastcolumn: 3 kind: 0 @@ -277,14 +277,14 @@ output: | 1 | - record: foo ^^^ - Couldn't run some online checks due to `prom` Prometheus server at http://127.0.0.1:XXXXX - error: `server_error: internal error`. + Couldn't run some checks due to `prom` Prometheus server at http://127.0.0.1:XXXXX error: + `server_error: internal error`. problem: reporter: promql/rate summary: unable to run checks details: "" diagnostics: - - message: 'Couldn''t run some online checks due to `prom` Prometheus server at http://127.0.0.1:XXXXX error: `server_error: internal error`.' + - message: 'Couldn''t run some checks due to `prom` Prometheus server at http://127.0.0.1:XXXXX error: `server_error: internal error`.' firstcolumn: 1 lastcolumn: 3 kind: 0 @@ -367,6 +367,11 @@ --- +[TestRateCheck/offline - 1] +[] + +--- + [TestRateCheck/rate(counter)__/_rate(gauge) - 1] - description: rate(counter) / rate(gauge) content: | @@ -614,14 +619,14 @@ output: | 1 | - alert: my alert ^^^^^^^^ - Couldn't run some online checks due to `prom` Prometheus server at http://127.0.0.1:XXXXX - error: `server_error: internal error`. + Couldn't run some checks due to `prom` Prometheus server at http://127.0.0.1:XXXXX error: + `server_error: internal error`. problem: reporter: promql/rate summary: unable to run checks details: "" diagnostics: - - message: 'Couldn''t run some online checks due to `prom` Prometheus server at http://127.0.0.1:XXXXX error: `server_error: internal error`.' + - message: 'Couldn''t run some checks due to `prom` Prometheus server at http://127.0.0.1:XXXXX error: `server_error: internal error`.' firstcolumn: 1 lastcolumn: 8 kind: 0 diff --git a/internal/checks/promql_regexp.go b/internal/checks/promql_regexp.go index 76bf70b1c..24dec5e3a 100644 --- a/internal/checks/promql_regexp.go +++ b/internal/checks/promql_regexp.go @@ -54,7 +54,6 @@ func (c RegexpCheck) Meta() CheckMeta { discovery.Modified, discovery.Moved, }, - Online: false, AlwaysEnabled: false, } } diff --git a/internal/checks/promql_selector.go b/internal/checks/promql_selector.go index 85bb7fc4b..1e474233c 100644 --- a/internal/checks/promql_selector.go +++ b/internal/checks/promql_selector.go @@ -45,7 +45,6 @@ func (c SelectorCheck) Meta() CheckMeta { discovery.Modified, discovery.Moved, }, - Online: false, AlwaysEnabled: false, } } diff --git a/internal/checks/promql_series.go b/internal/checks/promql_series.go index 6cd78516e..e768be0c4 100644 --- a/internal/checks/promql_series.go +++ b/internal/checks/promql_series.go @@ -118,7 +118,6 @@ func (c SeriesCheck) Meta() CheckMeta { discovery.Modified, discovery.Moved, }, - Online: true, AlwaysEnabled: false, } } @@ -137,6 +136,10 @@ func (c SeriesCheck) Reporter() string { } func (c SeriesCheck) Check(ctx context.Context, entry *discovery.Entry, entries []*discovery.Entry) (problems []Problem) { + if promapi.IsOffline(ctx) { + return problems + } + var settings *PromqlSeriesSettings if s := ctx.Value(SettingsKey(c.Reporter())); s != nil { settings = s.(*PromqlSeriesSettings) diff --git a/internal/checks/promql_series_test.go b/internal/checks/promql_series_test.go index 268d3d516..c3cee58a4 100644 --- a/internal/checks/promql_series_test.go +++ b/internal/checks/promql_series_test.go @@ -22,6 +22,15 @@ func TestSeriesCheck(t *testing.T) { now := time.Now() testCases := []checkTest{ + { + description: "offline", + content: "- record: foo\n expr: bar\n", + checker: newSeriesCheck, + prometheus: newSimpleProm, + ctx: func(ctx context.Context, _ string) context.Context { + return promapi.WithOffline(ctx, true) + }, + }, { description: "ignores rules with syntax errors", content: "- record: foo\n expr: sum(foo) without(\n", diff --git a/internal/checks/promql_series_test.snap b/internal/checks/promql_series_test.snap index 0442563eb..bffce6b7d 100644 --- a/internal/checks/promql_series_test.snap +++ b/internal/checks/promql_series_test.snap @@ -7,14 +7,14 @@ output: | 1 | - record: foo ^^^ - Couldn't run some online checks due to `prom` Prometheus server at http://127.0.0.1:XXXXX - error: `server_error: internal error`. + Couldn't run some checks due to `prom` Prometheus server at http://127.0.0.1:XXXXX error: + `server_error: internal error`. problem: reporter: promql/series summary: unable to run checks details: "" diagnostics: - - message: 'Couldn''t run some online checks due to `prom` Prometheus server at http://127.0.0.1:XXXXX error: `server_error: internal error`.' + - message: 'Couldn''t run some checks due to `prom` Prometheus server at http://127.0.0.1:XXXXX error: `server_error: internal error`.' firstcolumn: 1 lastcolumn: 3 kind: 0 @@ -39,14 +39,14 @@ output: | 1 | - record: foo ^^^ - Couldn't run some online checks due to `prom` Prometheus server at http://127.0.0.1:XXXXX - error: `server_error: internal error`. + Couldn't run some checks due to `prom` Prometheus server at http://127.0.0.1:XXXXX error: + `server_error: internal error`. problem: reporter: promql/series summary: unable to run checks details: "" diagnostics: - - message: 'Couldn''t run some online checks due to `prom` Prometheus server at http://127.0.0.1:XXXXX error: `server_error: internal error`.' + - message: 'Couldn''t run some checks due to `prom` Prometheus server at http://127.0.0.1:XXXXX error: `server_error: internal error`.' firstcolumn: 1 lastcolumn: 3 kind: 0 @@ -330,14 +330,14 @@ output: | 1 | - record: foo ^^^ - Couldn't run some online checks due to `prom` Prometheus server at http://127.0.0.1:XXXXX - error: `server_error: internal error`. + Couldn't run some checks due to `prom` Prometheus server at http://127.0.0.1:XXXXX error: + `server_error: internal error`. problem: reporter: promql/series summary: unable to run checks details: "" diagnostics: - - message: 'Couldn''t run some online checks due to `prom` Prometheus server at http://127.0.0.1:XXXXX error: `server_error: internal error`.' + - message: 'Couldn''t run some checks due to `prom` Prometheus server at http://127.0.0.1:XXXXX error: `server_error: internal error`.' firstcolumn: 1 lastcolumn: 3 kind: 0 @@ -785,14 +785,14 @@ output: | 1 | - record: foo ^^^ - Couldn't run some online checks due to `prom` Prometheus server at http://127.0.0.1:XXXXX - error: `server_error: internal error`. + Couldn't run some checks due to `prom` Prometheus server at http://127.0.0.1:XXXXX error: + `server_error: internal error`. problem: reporter: promql/series summary: unable to run checks details: "" diagnostics: - - message: 'Couldn''t run some online checks due to `prom` Prometheus server at http://127.0.0.1:XXXXX error: `server_error: internal error`.' + - message: 'Couldn''t run some checks due to `prom` Prometheus server at http://127.0.0.1:XXXXX error: `server_error: internal error`.' firstcolumn: 1 lastcolumn: 3 kind: 0 @@ -1156,14 +1156,14 @@ output: | 1 | - record: foo ^^^ - Couldn't run some online checks due to `prom` Prometheus server at http://127.0.0.1:XXXXX - error: `bad_data: bad input data`. + Couldn't run some checks due to `prom` Prometheus server at http://127.0.0.1:XXXXX error: + `bad_data: bad input data`. problem: reporter: promql/series summary: unable to run checks details: "" diagnostics: - - message: 'Couldn''t run some online checks due to `prom` Prometheus server at http://127.0.0.1:XXXXX error: `bad_data: bad input data`.' + - message: 'Couldn''t run some checks due to `prom` Prometheus server at http://127.0.0.1:XXXXX error: `bad_data: bad input data`.' firstcolumn: 1 lastcolumn: 3 kind: 0 @@ -1183,14 +1183,14 @@ output: | 1 | - record: foo ^^^ - Couldn't run some online checks due to `prom` Prometheus server at - http://127.127.127.127:9999 error: `connection refused`. + Couldn't run some checks due to `prom` Prometheus server at http://127.127.127.127:9999 + error: `connection refused`. problem: reporter: promql/series summary: unable to run checks details: "" diagnostics: - - message: 'Couldn''t run some online checks due to `prom` Prometheus server at http://127.127.127.127:9999 error: `connection refused`.' + - message: 'Couldn''t run some checks due to `prom` Prometheus server at http://127.127.127.127:9999 error: `connection refused`.' firstcolumn: 1 lastcolumn: 3 kind: 0 @@ -1406,15 +1406,14 @@ output: | 1 | - record: foo ^^^ - Couldn't run some online checks on `prom` Prometheus server at http://127.0.0.1:XXXXX - because some queries are too expensive: `execution: expanding series: context deadline - exceeded`. + Couldn't run some checks on `prom` Prometheus server at http://127.0.0.1:XXXXX because + some queries are too expensive: `execution: expanding series: context deadline exceeded`. problem: reporter: promql/series summary: unable to run checks details: "" diagnostics: - - message: 'Couldn''t run some online checks on `prom` Prometheus server at http://127.0.0.1:XXXXX because some queries are too expensive: `execution: expanding series: context deadline exceeded`.' + - message: 'Couldn''t run some checks on `prom` Prometheus server at http://127.0.0.1:XXXXX because some queries are too expensive: `execution: expanding series: context deadline exceeded`.' firstcolumn: 1 lastcolumn: 3 kind: 0 @@ -1712,6 +1711,11 @@ --- +[TestSeriesCheck/offline - 1] +[] + +--- + [TestSeriesCheck/offset - 1] [] @@ -1725,15 +1729,15 @@ output: | 1 | - record: foo ^^^ - Couldn't run some online checks on `prom` Prometheus server at http://127.0.0.1:XXXXX - because some queries are too expensive: `execution: query processing would load too many - samples into memory in query execution`. + Couldn't run some checks on `prom` Prometheus server at http://127.0.0.1:XXXXX because + some queries are too expensive: `execution: query processing would load too many samples + into memory in query execution`. problem: reporter: promql/series summary: unable to run checks details: "" diagnostics: - - message: 'Couldn''t run some online checks on `prom` Prometheus server at http://127.0.0.1:XXXXX because some queries are too expensive: `execution: query processing would load too many samples into memory in query execution`.' + - message: 'Couldn''t run some checks on `prom` Prometheus server at http://127.0.0.1:XXXXX because some queries are too expensive: `execution: query processing would load too many samples into memory in query execution`.' firstcolumn: 1 lastcolumn: 3 kind: 0 @@ -2532,14 +2536,14 @@ output: | 1 | - record: foo ^^^ - Couldn't run some online checks due to `prom` Prometheus server at http://127.0.0.1:XXXXX - error: `client_error: 404 Not Found`. + Couldn't run some checks due to `prom` Prometheus server at http://127.0.0.1:XXXXX error: + `client_error: 404 Not Found`. problem: reporter: promql/series summary: unable to run checks details: "" diagnostics: - - message: 'Couldn''t run some online checks due to `prom` Prometheus server at http://127.0.0.1:XXXXX error: `client_error: 404 Not Found`.' + - message: 'Couldn''t run some checks due to `prom` Prometheus server at http://127.0.0.1:XXXXX error: `client_error: 404 Not Found`.' firstcolumn: 1 lastcolumn: 3 kind: 0 @@ -2559,14 +2563,14 @@ output: | 1 | - record: foo ^^^ - Couldn't run some online checks due to `prom` Prometheus server at http://127.0.0.1:XXXXX - error: `client_error: 404 Not Found`. + Couldn't run some checks due to `prom` Prometheus server at http://127.0.0.1:XXXXX error: + `client_error: 404 Not Found`. problem: reporter: promql/series summary: unable to run checks details: "" diagnostics: - - message: 'Couldn''t run some online checks due to `prom` Prometheus server at http://127.0.0.1:XXXXX error: `client_error: 404 Not Found`.' + - message: 'Couldn''t run some checks due to `prom` Prometheus server at http://127.0.0.1:XXXXX error: `client_error: 404 Not Found`.' firstcolumn: 1 lastcolumn: 3 kind: 0 diff --git a/internal/checks/promql_syntax.go b/internal/checks/promql_syntax.go index a9a815358..cb23c9364 100644 --- a/internal/checks/promql_syntax.go +++ b/internal/checks/promql_syntax.go @@ -29,7 +29,6 @@ func (c SyntaxCheck) Meta() CheckMeta { discovery.Modified, discovery.Moved, }, - Online: false, AlwaysEnabled: false, } } diff --git a/internal/checks/promql_vector_matching.go b/internal/checks/promql_vector_matching.go index 272da5ae6..bfe4cc199 100644 --- a/internal/checks/promql_vector_matching.go +++ b/internal/checks/promql_vector_matching.go @@ -46,7 +46,6 @@ func (c VectorMatchingCheck) Meta() CheckMeta { discovery.Modified, discovery.Moved, }, - Online: true, AlwaysEnabled: false, } } @@ -60,6 +59,10 @@ func (c VectorMatchingCheck) Reporter() string { } func (c VectorMatchingCheck) Check(ctx context.Context, entry *discovery.Entry, _ []*discovery.Entry) (problems []Problem) { + if promapi.IsOffline(ctx) { + return problems + } + expr := entry.Rule.Expr() if expr.SyntaxError() != nil { return nil diff --git a/internal/checks/promql_vector_matching_test.go b/internal/checks/promql_vector_matching_test.go index b1cef82ac..59560c04f 100644 --- a/internal/checks/promql_vector_matching_test.go +++ b/internal/checks/promql_vector_matching_test.go @@ -1,6 +1,7 @@ package checks_test import ( + "context" "testing" "time" @@ -16,6 +17,15 @@ func newVectorMatchingCheck(prom *promapi.FailoverGroup) checks.RuleChecker { func TestVectorMatchingCheck(t *testing.T) { testCases := []checkTest{ + { + description: "offline", + content: "- record: foo\n expr: bar / baz\n", + checker: newVectorMatchingCheck, + prometheus: newSimpleProm, + ctx: func(ctx context.Context, _ string) context.Context { + return promapi.WithOffline(ctx, true) + }, + }, { description: "ignores rules with syntax errors", content: "- record: foo\n expr: sum(foo) without(\n", diff --git a/internal/checks/promql_vector_matching_test.snap b/internal/checks/promql_vector_matching_test.snap index 3fa491034..920b9283a 100644 --- a/internal/checks/promql_vector_matching_test.snap +++ b/internal/checks/promql_vector_matching_test.snap @@ -50,14 +50,14 @@ output: | 1 | - record: foo ^^^ - Couldn't run some online checks due to `prom` Prometheus server at http://127.0.0.1:1111 - error: `connection refused`. + Couldn't run some checks due to `prom` Prometheus server at http://127.0.0.1:1111 error: + `connection refused`. problem: reporter: promql/vector_matching summary: unable to run checks details: "" diagnostics: - - message: 'Couldn''t run some online checks due to `prom` Prometheus server at http://127.0.0.1:1111 error: `connection refused`.' + - message: 'Couldn''t run some checks due to `prom` Prometheus server at http://127.0.0.1:1111 error: `connection refused`.' firstcolumn: 1 lastcolumn: 3 kind: 0 @@ -77,14 +77,14 @@ output: | 1 | - record: foo ^^^ - Couldn't run some online checks due to `prom` Prometheus server at http://127.0.0.1:1111 - error: `connection refused`. + Couldn't run some checks due to `prom` Prometheus server at http://127.0.0.1:1111 error: + `connection refused`. problem: reporter: promql/vector_matching summary: unable to run checks details: "" diagnostics: - - message: 'Couldn''t run some online checks due to `prom` Prometheus server at http://127.0.0.1:1111 error: `connection refused`.' + - message: 'Couldn''t run some checks due to `prom` Prometheus server at http://127.0.0.1:1111 error: `connection refused`.' firstcolumn: 1 lastcolumn: 3 kind: 0 @@ -104,14 +104,14 @@ output: | 1 | - record: foo ^^^ - Couldn't run some online checks due to `prom` Prometheus server at http://127.0.0.1:XXXXX - error: `server_error: internal error`. + Couldn't run some checks due to `prom` Prometheus server at http://127.0.0.1:XXXXX error: + `server_error: internal error`. problem: reporter: promql/vector_matching summary: unable to run checks details: "" diagnostics: - - message: 'Couldn''t run some online checks due to `prom` Prometheus server at http://127.0.0.1:XXXXX error: `server_error: internal error`.' + - message: 'Couldn''t run some checks due to `prom` Prometheus server at http://127.0.0.1:XXXXX error: `server_error: internal error`.' firstcolumn: 1 lastcolumn: 3 kind: 0 @@ -131,14 +131,14 @@ output: | 1 | - record: foo ^^^ - Couldn't run some online checks due to `prom` Prometheus server at http://127.0.0.1:XXXXX - error: `server_error: internal error`. + Couldn't run some checks due to `prom` Prometheus server at http://127.0.0.1:XXXXX error: + `server_error: internal error`. problem: reporter: promql/vector_matching summary: unable to run checks details: "" diagnostics: - - message: 'Couldn''t run some online checks due to `prom` Prometheus server at http://127.0.0.1:XXXXX error: `server_error: internal error`.' + - message: 'Couldn''t run some checks due to `prom` Prometheus server at http://127.0.0.1:XXXXX error: `server_error: internal error`.' firstcolumn: 1 lastcolumn: 3 kind: 0 @@ -366,6 +366,11 @@ --- +[TestVectorMatchingCheck/offline - 1] +[] + +--- + [TestVectorMatchingCheck/one_to_one_matching - 1] - description: one to one matching content: | diff --git a/internal/checks/query_cost.go b/internal/checks/query_cost.go index aa009f69c..90b2161b4 100644 --- a/internal/checks/query_cost.go +++ b/internal/checks/query_cost.go @@ -68,7 +68,6 @@ func (c CostCheck) Meta() CheckMeta { discovery.Modified, discovery.Moved, }, - Online: true, AlwaysEnabled: false, } } @@ -82,6 +81,10 @@ func (c CostCheck) Reporter() string { } func (c CostCheck) Check(ctx context.Context, entry *discovery.Entry, entries []*discovery.Entry) (problems []Problem) { + if promapi.IsOffline(ctx) { + return problems + } + expr := entry.Rule.Expr() if expr.SyntaxError() != nil { diff --git a/internal/checks/query_cost_test.go b/internal/checks/query_cost_test.go index 1adfd698d..8b3c6e2ab 100644 --- a/internal/checks/query_cost_test.go +++ b/internal/checks/query_cost_test.go @@ -1,6 +1,7 @@ package checks_test import ( + "context" "testing" "time" @@ -14,6 +15,17 @@ func TestCostCheck(t *testing.T) { content := "- record: foo\n expr: sum(foo)\n" testCases := []checkTest{ + { + description: "offline", + content: "- record: foo\n expr: sum(bar)\n", + checker: func(prom *promapi.FailoverGroup) checks.RuleChecker { + return checks.NewCostCheck(prom, 0, 0, 0, 0, "", checks.Bug) + }, + prometheus: newSimpleProm, + ctx: func(ctx context.Context, _ string) context.Context { + return promapi.WithOffline(ctx, true) + }, + }, { description: "ignores rules with syntax errors", content: "- record: foo\n expr: sum(foo) without(\n", diff --git a/internal/checks/query_cost_test.snap b/internal/checks/query_cost_test.snap index e96b77f8e..c7576a61a 100644 --- a/internal/checks/query_cost_test.snap +++ b/internal/checks/query_cost_test.snap @@ -202,14 +202,14 @@ output: | 1 | - record: foo ^^^ - Couldn't run some online checks due to `prom` Prometheus server at http://127.0.0.1:XXXXX - error: `bad_data: bad input data`. + Couldn't run some checks due to `prom` Prometheus server at http://127.0.0.1:XXXXX error: + `bad_data: bad input data`. problem: reporter: query/cost summary: unable to run checks details: "" diagnostics: - - message: 'Couldn''t run some online checks due to `prom` Prometheus server at http://127.0.0.1:XXXXX error: `bad_data: bad input data`.' + - message: 'Couldn''t run some checks due to `prom` Prometheus server at http://127.0.0.1:XXXXX error: `bad_data: bad input data`.' firstcolumn: 1 lastcolumn: 3 kind: 0 @@ -234,14 +234,14 @@ output: | 1 | - record: foo ^^^ - Couldn't run some online checks due to `prom` Prometheus server at http://127.0.0.1:1111 - error: `connection refused`. + Couldn't run some checks due to `prom` Prometheus server at http://127.0.0.1:1111 error: + `connection refused`. problem: reporter: query/cost summary: unable to run checks details: "" diagnostics: - - message: 'Couldn''t run some online checks due to `prom` Prometheus server at http://127.0.0.1:1111 error: `connection refused`.' + - message: 'Couldn''t run some checks due to `prom` Prometheus server at http://127.0.0.1:1111 error: `connection refused`.' firstcolumn: 1 lastcolumn: 3 kind: 0 @@ -273,6 +273,11 @@ --- +[TestCostCheck/offline - 1] +[] + +--- + [TestCostCheck/response_timeout - 1] - description: response timeout content: | @@ -281,14 +286,14 @@ output: | 1 | - record: foo ^^^ - Couldn't run some online checks due to `prom` Prometheus server at http://127.0.0.1:XXXXX - error: `connection timeout`. + Couldn't run some checks due to `prom` Prometheus server at http://127.0.0.1:XXXXX error: + `connection timeout`. problem: reporter: query/cost summary: unable to run checks details: "" diagnostics: - - message: 'Couldn''t run some online checks due to `prom` Prometheus server at http://127.0.0.1:XXXXX error: `connection timeout`.' + - message: 'Couldn''t run some checks due to `prom` Prometheus server at http://127.0.0.1:XXXXX error: `connection timeout`.' firstcolumn: 1 lastcolumn: 3 kind: 0 diff --git a/internal/checks/rule_dependency.go b/internal/checks/rule_dependency.go index 5f53c7ada..8ef45462f 100644 --- a/internal/checks/rule_dependency.go +++ b/internal/checks/rule_dependency.go @@ -51,7 +51,6 @@ func (c RuleDependencyCheck) Meta() CheckMeta { discovery.Modified, discovery.Removed, }, - Online: false, AlwaysEnabled: false, } } diff --git a/internal/checks/rule_dependency_test.go b/internal/checks/rule_dependency_test.go index 02cb9a296..ffffd019b 100644 --- a/internal/checks/rule_dependency_test.go +++ b/internal/checks/rule_dependency_test.go @@ -630,9 +630,6 @@ func TestRuleDependencyCheck_Interface(t *testing.T) { t.Errorf("Reporter() = %q, want %q", c.Reporter(), checks.RuleDependencyCheckName) } meta := c.Meta() - if meta.Online { - t.Error("Meta().Online = true, want false") - } if meta.AlwaysEnabled { t.Error("Meta().AlwaysEnabled = true, want false") } diff --git a/internal/checks/rule_duplicate.go b/internal/checks/rule_duplicate.go index db2c0c6a8..0a0635422 100644 --- a/internal/checks/rule_duplicate.go +++ b/internal/checks/rule_duplicate.go @@ -39,7 +39,6 @@ func (c RuleDuplicateCheck) Meta() CheckMeta { discovery.Modified, discovery.Moved, }, - Online: false, AlwaysEnabled: false, } } diff --git a/internal/checks/rule_for.go b/internal/checks/rule_for.go index 63ec9805a..75338cb31 100644 --- a/internal/checks/rule_for.go +++ b/internal/checks/rule_for.go @@ -49,7 +49,6 @@ func (c RuleForCheck) Meta() CheckMeta { discovery.Modified, discovery.Moved, }, - Online: false, AlwaysEnabled: false, } } diff --git a/internal/checks/rule_label.go b/internal/checks/rule_label.go index be9c4031f..20b023d0f 100644 --- a/internal/checks/rule_label.go +++ b/internal/checks/rule_label.go @@ -54,7 +54,6 @@ func (c LabelCheck) Meta() CheckMeta { discovery.Modified, discovery.Moved, }, - Online: false, AlwaysEnabled: false, } } diff --git a/internal/checks/rule_link.go b/internal/checks/rule_link.go index 1aeee0516..484723f37 100644 --- a/internal/checks/rule_link.go +++ b/internal/checks/rule_link.go @@ -14,6 +14,7 @@ import ( "github.com/cloudflare/pint/internal/diags" "github.com/cloudflare/pint/internal/discovery" "github.com/cloudflare/pint/internal/parser" + "github.com/cloudflare/pint/internal/promapi" ) const ( @@ -52,7 +53,6 @@ func (c RuleLinkCheck) Meta() CheckMeta { discovery.Modified, discovery.Moved, }, - Online: true, AlwaysEnabled: false, } } @@ -66,6 +66,11 @@ func (c RuleLinkCheck) Reporter() string { } func (c RuleLinkCheck) Check(ctx context.Context, entry *discovery.Entry, _ []*discovery.Entry) (problems []Problem) { + // This check makes HTTP requests to verify annotation links, skip when running in offline mode. + if promapi.IsOffline(ctx) { + return problems + } + if entry.Rule.AlertingRule == nil || entry.Rule.AlertingRule.Annotations == nil { return nil } diff --git a/internal/checks/rule_link_test.go b/internal/checks/rule_link_test.go index 05afc20e3..068071a5d 100644 --- a/internal/checks/rule_link_test.go +++ b/internal/checks/rule_link_test.go @@ -1,6 +1,7 @@ package checks_test import ( + "context" "fmt" "net/http" "net/http/httptest" @@ -47,6 +48,21 @@ func TestRuleLinkCheck(t *testing.T) { t.Cleanup(srv.Close) testCases := []checkTest{ + { + description: "offline", + content: "- alert: foo\n expr: up == 0\n annotations:\n link: http://example.com\n", + checker: func(_ *promapi.FailoverGroup) checks.RuleChecker { + return checks.NewRuleLinkCheck( + checks.MustTemplatedRegexp(".*"), + "", time.Second, nil, "", + checks.Bug, + ) + }, + prometheus: noProm, + ctx: func(ctx context.Context, _ string) context.Context { + return promapi.WithOffline(ctx, true) + }, + }, { description: "ignores recording rules", content: "- record: foo\n expr: sum(foo)\n", diff --git a/internal/checks/rule_link_test.snap b/internal/checks/rule_link_test.snap index 942aa6b42..1a374a54b 100644 --- a/internal/checks/rule_link_test.snap +++ b/internal/checks/rule_link_test.snap @@ -151,3 +151,8 @@ anchor: 0 --- + +[TestRuleLinkCheck/offline - 1] +[] + +--- diff --git a/internal/checks/rule_name.go b/internal/checks/rule_name.go index 9600cb105..7c0b1f030 100644 --- a/internal/checks/rule_name.go +++ b/internal/checks/rule_name.go @@ -36,7 +36,6 @@ func (c RuleNameCheck) Meta() CheckMeta { discovery.Modified, discovery.Moved, }, - Online: false, AlwaysEnabled: false, } } diff --git a/internal/checks/rule_reject.go b/internal/checks/rule_reject.go index 7328875d5..72a1e4383 100644 --- a/internal/checks/rule_reject.go +++ b/internal/checks/rule_reject.go @@ -52,7 +52,6 @@ func (c Reject) Meta() CheckMeta { discovery.Modified, discovery.Moved, }, - Online: false, AlwaysEnabled: false, } } diff --git a/internal/checks/rule_report.go b/internal/checks/rule_report.go index ec5de0064..ffb7cf27a 100644 --- a/internal/checks/rule_report.go +++ b/internal/checks/rule_report.go @@ -28,7 +28,6 @@ func (c ReportCheck) Meta() CheckMeta { discovery.Modified, discovery.Moved, }, - Online: false, AlwaysEnabled: false, } } diff --git a/internal/config/config.go b/internal/config/config.go index 6222377d5..c776dbea8 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -39,18 +39,6 @@ type Config struct { staticRules []staticRule } -func (cfg *Config) DisableOnlineChecks() { - for _, name := range checks.OnlineChecks { - var found bool - if slices.Contains(cfg.Checks.Disabled, name) { - found = true - } - if !found { - cfg.Checks.Disabled = append(cfg.Checks.Disabled, name) - } - } -} - func (cfg *Config) SetDisabledChecks(l []string) { disabled := map[string]struct{}{} for _, s := range l { diff --git a/internal/config/config_test.go b/internal/config/config_test.go index 0fbc7ddd9..c485879fe 100644 --- a/internal/config/config_test.go +++ b/internal/config/config_test.go @@ -80,85 +80,6 @@ func TestConfigLoadIgnoreKeepFiringForOnly(t *testing.T) { require.NoError(t, err) } -func TestDisableOnlineChecksWithPrometheus(t *testing.T) { - dir := t.TempDir() - path := path.Join(dir, "config.hcl") - err := os.WriteFile(path, []byte(` -prometheus "prom" { - uri = "http://localhost" -} -`), 0o644) - require.NoError(t, err) - - cfg, ok, err := config.Load(path, true) - require.NoError(t, err) - require.True(t, ok) - - gen := config.NewPrometheusGenerator(cfg, prometheus.NewRegistry()) - defer gen.Stop() - gen.GenerateStatic() - - require.Empty(t, cfg.Checks.Disabled) - - cfg.DisableOnlineChecks() - for _, c := range checks.OnlineChecks { - require.Contains(t, cfg.Checks.Disabled, c) - } -} - -func TestDisableOnlineChecksWithoutPrometheus(t *testing.T) { - dir := t.TempDir() - path := path.Join(dir, "config.hcl") - err := os.WriteFile(path, []byte(``), 0o644) - require.NoError(t, err) - - cfg, _, err := config.Load(path, true) - require.NoError(t, err) - - gen := config.NewPrometheusGenerator(cfg, prometheus.NewRegistry()) - defer gen.Stop() - gen.GenerateStatic() - - require.Empty(t, cfg.Checks.Disabled) - - cfg.DisableOnlineChecks() - for _, c := range checks.OnlineChecks { - require.Contains(t, cfg.Checks.Disabled, c) - } -} - -func TestDisableOnlineChecksAfterSetDisabledChecks(t *testing.T) { - dir := t.TempDir() - path := path.Join(dir, "config.hcl") - err := os.WriteFile(path, []byte(` -prometheus "prom" { - uri = "http://localhost" - timeout = "1s" -} -`), 0o644) - require.NoError(t, err) - - cfg, _, err := config.Load(path, true) - require.NoError(t, err) - - gen := config.NewPrometheusGenerator(cfg, prometheus.NewRegistry()) - defer gen.Stop() - gen.GenerateStatic() - - require.Empty(t, cfg.Checks.Disabled) - - cfg.SetDisabledChecks([]string{checks.SyntaxCheckName}) - require.Contains(t, cfg.Checks.Disabled, checks.SyntaxCheckName) - - cfg.SetDisabledChecks([]string{checks.RateCheckName}) - require.Contains(t, cfg.Checks.Disabled, checks.RateCheckName) - - cfg.DisableOnlineChecks() - for _, c := range checks.OnlineChecks { - require.Contains(t, cfg.Checks.Disabled, c) - } -} - func TestSetDisabledChecks(t *testing.T) { dir := t.TempDir() path := path.Join(dir, "config.hcl") diff --git a/internal/promapi/buildinfo_test.go b/internal/promapi/buildinfo_test.go index 4c31bd053..30c9ae6d3 100644 --- a/internal/promapi/buildinfo_test.go +++ b/internal/promapi/buildinfo_test.go @@ -24,6 +24,17 @@ func TestBuildInfo(t *testing.T) { } testCases := []testCaseT{ + { + name: "offline", + timeout: time.Second, + ctx: func(t *testing.T) context.Context { + return promapi.WithOffline(t.Context(), true) + }, + assertErr: func(t *testing.T, err error) { + require.EqualError(t, err, "disabled by --offline flag") + }, + mock: httpmock.New(func(_ *httpmock.Server) {}), + }, { name: "valid response", timeout: time.Second, diff --git a/internal/promapi/config_test.go b/internal/promapi/config_test.go index 1bdfa3c5b..d18774c9e 100644 --- a/internal/promapi/config_test.go +++ b/internal/promapi/config_test.go @@ -37,6 +37,19 @@ func TestConfig(t *testing.T) { } testCases := []testCaseT{ + { + name: "offline", + timeout: time.Second, + useFailover: true, + ctx: func(t *testing.T) context.Context { + return promapi.WithOffline(t.Context(), true) + }, + errCheck: func(t *testing.T, err error) { + t.Helper() + require.EqualError(t, err, "disabled by --offline flag") + }, + mock: httpmock.New(func(_ *httpmock.Server) {}), + }, { name: "default config", timeout: time.Second, diff --git a/internal/promapi/errors.go b/internal/promapi/errors.go index 0eddd2dea..7f0d4f809 100644 --- a/internal/promapi/errors.go +++ b/internal/promapi/errors.go @@ -30,6 +30,8 @@ func IsUnavailableError(err error) bool { return false } +var ErrOffline = errors.New("disabled by --offline flag") + func IsQueryTooExpensive(err error) bool { if e1, ok := errors.AsType[APIError](err); ok { if e1.ErrorType != v1.ErrExec { diff --git a/internal/promapi/failover.go b/internal/promapi/failover.go index d6434c938..e9e0c0e0e 100644 --- a/internal/promapi/failover.go +++ b/internal/promapi/failover.go @@ -240,6 +240,9 @@ func (fg *FailoverGroup) Config( ctx context.Context, cacheTTL time.Duration, ) *Request[*ConfigResult] { + if IsOffline(ctx) { + return newRequest(func() (*ConfigResult, error) { return nil, ErrOffline }) + } return newRequest(func() (*ConfigResult, error) { var cfg *ConfigResult var uri string @@ -262,6 +265,9 @@ func (fg *FailoverGroup) Query( ctx context.Context, expr string, ) *Request[*QueryResult] { + if IsOffline(ctx) { + return newRequest(func() (*QueryResult, error) { return nil, ErrOffline }) + } return newRequest(func() (*QueryResult, error) { var qr *QueryResult var uri string @@ -294,6 +300,9 @@ func (fg *FailoverGroup) RangeQuery( expr string, params RangeQueryTimes, ) *Request[*RangeQueryResult] { + if IsOffline(ctx) { + return newRequest(func() (*RangeQueryResult, error) { return nil, ErrOffline }) + } return newRequest(func() (*RangeQueryResult, error) { var rqr *RangeQueryResult var uri string @@ -316,6 +325,9 @@ func (fg *FailoverGroup) Metadata( ctx context.Context, metric string, ) *Request[*MetadataResult] { + if IsOffline(ctx) { + return newRequest(func() (*MetadataResult, error) { return nil, ErrOffline }) + } return newRequest(func() (*MetadataResult, error) { var metadata *MetadataResult var uri string @@ -337,6 +349,9 @@ func (fg *FailoverGroup) Metadata( func (fg *FailoverGroup) Flags( ctx context.Context, ) *Request[*FlagsResult] { + if IsOffline(ctx) { + return newRequest(func() (*FlagsResult, error) { return nil, ErrOffline }) + } return newRequest(func() (*FlagsResult, error) { var flags *FlagsResult var uri string @@ -358,6 +373,9 @@ func (fg *FailoverGroup) Flags( func (fg *FailoverGroup) BuildInfo( ctx context.Context, ) *Request[*BuildInfoResult] { + if IsOffline(ctx) { + return newRequest(func() (*BuildInfoResult, error) { return nil, ErrOffline }) + } return newRequest(func() (*BuildInfoResult, error) { var bi *BuildInfoResult var uri string diff --git a/internal/promapi/flags_test.go b/internal/promapi/flags_test.go index 60a612b38..33b6304f3 100644 --- a/internal/promapi/flags_test.go +++ b/internal/promapi/flags_test.go @@ -25,6 +25,17 @@ func TestFlags(t *testing.T) { } testCases := []testCaseT{ + { + name: "offline", + timeout: time.Second, + ctx: func(t *testing.T) context.Context { + return promapi.WithOffline(t.Context(), true) + }, + assertErr: func(t *testing.T, err error) { + require.EqualError(t, err, "disabled by --offline flag") + }, + mock: httpmock.New(func(_ *httpmock.Server) {}), + }, { name: "empty flags", timeout: time.Second, diff --git a/internal/promapi/metadata_test.go b/internal/promapi/metadata_test.go index 10a9a747f..dc6e5b092 100644 --- a/internal/promapi/metadata_test.go +++ b/internal/promapi/metadata_test.go @@ -26,6 +26,17 @@ func TestMetadata(t *testing.T) { } testCases := []testCaseT{ + { + name: "offline", + timeout: time.Second, + ctx: func(t *testing.T) context.Context { + return promapi.WithOffline(t.Context(), true) + }, + assertErr: func(t *testing.T, err error) { + require.EqualError(t, err, "disabled by --offline flag") + }, + mock: httpmock.New(func(_ *httpmock.Server) {}), + }, { name: "gauge", timeout: time.Second, diff --git a/internal/promapi/prometheus.go b/internal/promapi/prometheus.go index f7b0f4669..faf69648f 100644 --- a/internal/promapi/prometheus.go +++ b/internal/promapi/prometheus.go @@ -24,8 +24,18 @@ type PrometheusContextKey string const ( AllPrometheusServers = PrometheusContextKey("allServers") + isOfflineKey = PrometheusContextKey("isOffline") ) +func WithOffline(ctx context.Context, offline bool) context.Context { + return context.WithValue(ctx, isOfflineKey, offline) +} + +func IsOffline(ctx context.Context) bool { + v, ok := ctx.Value(isOfflineKey).(bool) + return ok && v +} + type QueryError struct { err error msg string diff --git a/internal/promapi/query_test.go b/internal/promapi/query_test.go index 35dac59d7..13658e957 100644 --- a/internal/promapi/query_test.go +++ b/internal/promapi/query_test.go @@ -32,6 +32,17 @@ func TestQuery(t *testing.T) { } testCases := []testCaseT{ + { + name: "offline", + timeout: time.Second, + ctx: func(t *testing.T) context.Context { + return promapi.WithOffline(t.Context(), true) + }, + assertErr: func(t *testing.T, err error) { + require.EqualError(t, err, "disabled by --offline flag") + }, + mock: httpmock.New(func(_ *httpmock.Server) {}), + }, { name: "empty result", timeout: time.Second, diff --git a/internal/promapi/range_test.go b/internal/promapi/range_test.go index b596c00ba..e2616b09c 100644 --- a/internal/promapi/range_test.go +++ b/internal/promapi/range_test.go @@ -85,6 +85,20 @@ func TestRange(t *testing.T) { } testCases := []testCaseT{ + { + query: "offline", + start: timeParse("2022-06-14T00:00:00Z"), + end: timeParse("2022-06-14T00:01:00Z"), + step: time.Minute, + timeout: time.Second, + ctx: func(t *testing.T) context.Context { + return promapi.WithOffline(t.Context(), true) + }, + assertErr: func(t *testing.T, err error) { + require.EqualError(t, err, "disabled by --offline flag") + }, + mock: httpmock.New(func(_ *httpmock.Server) {}), + }, { query: "1m", start: timeParse("2022-06-14T00:00:00Z"), diff --git a/internal/reporter/comments.go b/internal/reporter/comments.go index 41336c82a..181a53313 100644 --- a/internal/reporter/comments.go +++ b/internal/reporter/comments.go @@ -428,8 +428,6 @@ func updateDestination(ctx context.Context, s Summary, c Commenter, dst any, sho ctx, slog.LevelInfo, "Creating report summary", slog.String("reporter", c.Describe()), slog.Int("reports", len(s.reports)), - slog.Int("online", int(s.OnlineChecks)), - slog.Int("offline", int(s.OfflineChecks)), slog.String("duration", output.HumanizeDuration(s.Duration)), slog.Int("entries", s.TotalEntries), slog.Int("checked", int(s.CheckedEntries)), @@ -502,7 +500,7 @@ func makePrometheusDetailsComment(s Summary) string { var buf strings.Builder buf.WriteString(`Some checks were disabled because one or more configured Prometheus server doesn't seem to support all required Prometheus APIs. This usually means that you're running pint against a service like Thanos or Mimir that allows to query metrics but doesn't implement all APIs documented [here](https://prometheus.io/docs/prometheus/latest/querying/api/). -Since pint uses many of these API endpoint for querying information needed to run online checks only a real Prometheus server will allow it to run all of these checks. +Since pint uses many of these API endpoints for querying information needed to run checks only a real Prometheus server will allow it to run all of these checks. Below is the list of checks that were disabled for each Prometheus server defined in pint config file. `) diff --git a/internal/reporter/github.go b/internal/reporter/github.go index b9cefaf7c..16596012e 100644 --- a/internal/reporter/github.go +++ b/internal/reporter/github.go @@ -369,12 +369,8 @@ func formatGHReviewBody(ctx context.Context, version string, summary Summary, sh b.WriteString(strconv.Itoa(len(summary.Reports()))) b.WriteString(" |\n") - b.WriteString("| Number of offline checks | ") - b.WriteString(strconv.FormatInt(summary.OfflineChecks, 10)) - b.WriteString(" |\n") - - b.WriteString("| Number of online checks | ") - b.WriteString(strconv.FormatInt(summary.OnlineChecks, 10)) + b.WriteString("| Number of checks | ") + b.WriteString(strconv.FormatInt(summary.TotalChecks, 10)) b.WriteString(" |\n") b.WriteString("| Checks duration | ") diff --git a/internal/reporter/github_test.go b/internal/reporter/github_test.go index 73df06a12..145f167cf 100644 --- a/internal/reporter/github_test.go +++ b/internal/reporter/github_test.go @@ -1151,8 +1151,7 @@ func TestGitHubReporter(t *testing.T) { | Number of rules parsed | 0 | | Number of rules checked | 0 | | Number of problems found | 1 | -| Number of offline checks | 0 | -| Number of online checks | 0 | +| Number of checks | 0 | | Checks duration | 0 |

@@ -1231,8 +1230,7 @@ Fatal: syntax error (mock) | Number of rules parsed | 0 | | Number of rules checked | 0 | | Number of problems found | 0 | -| Number of offline checks | 0 | -| Number of online checks | 0 | +| Number of checks | 0 | | Checks duration | 0 |

@@ -1247,7 +1245,7 @@ No problems reported Some checks were disabled because one or more configured Prometheus server doesn't seem to support all required Prometheus APIs. This usually means that you're running pint against a service like Thanos or Mimir that allows to query metrics but doesn't implement all APIs documented [here](https://prometheus.io/docs/prometheus/latest/querying/api/). -Since pint uses many of these API endpoint for querying information needed to run online checks only a real Prometheus server will allow it to run all of these checks. +Since pint uses many of these API endpoints for querying information needed to run checks only a real Prometheus server will allow it to run all of these checks. Below is the list of checks that were disabled for each Prometheus server defined in pint config file. - ` + "`prom1`" + ` diff --git a/internal/reporter/gitlab_test.go b/internal/reporter/gitlab_test.go index 15cc9c28b..298afe4b2 100644 --- a/internal/reporter/gitlab_test.go +++ b/internal/reporter/gitlab_test.go @@ -565,7 +565,7 @@ func TestGitLabReporter(t *testing.T) { s.ExpectPost(apiDiscussions(1, false)).WithBodyJSON(gitlab.CreateMergeRequestDiscussionOptions{ Body: new(`Some checks were disabled because one or more configured Prometheus server doesn't seem to support all required Prometheus APIs. This usually means that you're running pint against a service like Thanos or Mimir that allows to query metrics but doesn't implement all APIs documented [here](https://prometheus.io/docs/prometheus/latest/querying/api/). -Since pint uses many of these API endpoint for querying information needed to run online checks only a real Prometheus server will allow it to run all of these checks. +Since pint uses many of these API endpoints for querying information needed to run checks only a real Prometheus server will allow it to run all of these checks. Below is the list of checks that were disabled for each Prometheus server defined in pint config file. - ` + "`prom1`" + ` diff --git a/internal/reporter/reporter.go b/internal/reporter/reporter.go index 30bb6674d..ff5b27300 100644 --- a/internal/reporter/reporter.go +++ b/internal/reporter/reporter.go @@ -85,11 +85,10 @@ type PrometheusDetails struct { type Summary struct { promDetails map[string]PrometheusDetails reports []Report - OfflineChecks int64 - OnlineChecks int64 Duration time.Duration TotalEntries int CheckedEntries int64 + TotalChecks int64 } func NewSummary(reports []Report) Summary { diff --git a/internal/reporter/reporter_test.go b/internal/reporter/reporter_test.go index d67e8df78..14a7850e1 100644 --- a/internal/reporter/reporter_test.go +++ b/internal/reporter/reporter_test.go @@ -333,7 +333,7 @@ func TestMakePrometheusDetailsComment(t *testing.T) { expected := `Some checks were disabled because one or more configured Prometheus server doesn't seem to support all required Prometheus APIs. This usually means that you're running pint against a service like Thanos or Mimir that allows to query metrics but doesn't implement all APIs documented [here](https://prometheus.io/docs/prometheus/latest/querying/api/). -Since pint uses many of these API endpoint for querying information needed to run online checks only a real Prometheus server will allow it to run all of these checks. +Since pint uses many of these API endpoints for querying information needed to run checks only a real Prometheus server will allow it to run all of these checks. Below is the list of checks that were disabled for each Prometheus server defined in pint config file. - ` + "`prom-a`" + `