Files
cairnobs/alerting/internal/evaluator/condition_test.go
T
jcoffey-dev 9435115ab7 Phase 3: dashboards and alerting
Saved, shareable multi-panel dashboards (table/line/bar/single-stat
panels via gridstack + uPlot, global + per-panel time range, JSON
export/import) and threshold/absence alert rules with an
ok/pending/firing evaluator and webhook/Slack/PagerDuty delivery.

- New /metadata component: Postgres control-plane store for dashboards,
  panels, notification targets, alert rules/state, and delivery log --
  see docs/phase-3-dashboard-design.md for why ClickHouse's MergeTree
  family isn't a fit for this access pattern (needs real row-level
  locking and read-your-writes consistency).
- api/internal/dashboards: dashboard/panel CRUD, pure -- panel query
  execution stays client-side, reusing the existing /query endpoint.
- New /alerting service: rule/target CRUD, a ticker-driven evaluator
  (claim-then-evaluate concurrency control, transactional-outbox
  delivery, query errors and threshold zero-rows never coerced into a
  false transition) and webhook/Slack/PagerDuty delivery with
  retry/backoff. See docs/phase-3-alerting-design.md for the full
  state-machine design and the four correctness properties it
  implements.
- web: /dashboards and /alerts UIs; cli: sentryctl dashboards/alerts
  list/get/apply, seeding a future Terraform provider's JSON contract.
- hack/alert-load-test: 500 rules against real ClickHouse data, real
  measured results in docs/phase-3-runbook.md.

Five real bugs found by actually running this against a live stack
(documented in the runbook, not just fixed silently): a latent Phase 2
bug where ClickHouse rejected the timestamp format used for
earliest=/latest= queries; a "now" literal token injected into query
text; a GridStack/uPlot layout-timing race; JS's Date.parse being too
lenient to use as a timestamp-detection heuristic; a rule's "enabled"
field silently defaulting to false when omitted; and the evaluator's
claim-batch-size and worker-pool-concurrency defaulting to the same
value, causing 500 concurrently-due rules to take 125s to cycle through
instead of the configured 60s.
2026-08-13 17:29:38 -07:00

119 lines
3.7 KiB
Go

package evaluator
import (
"testing"
"github.com/sentry/sentry/alerting/internal/queryclient"
"github.com/sentry/sentry/alerting/internal/rulestore"
)
func thresholdRule(comparator rulestore.Comparator, threshold float64) rulestore.Rule {
return rulestore.Rule{
ConditionType: rulestore.ConditionThreshold,
Comparator: &comparator,
ThresholdValue: &threshold,
}
}
func TestEvaluateConditionThresholdTrue(t *testing.T) {
rule := thresholdRule(rulestore.Gt, 100)
result := &queryclient.Result{Columns: []string{"count"}, Rows: [][]any{{150.0}}}
got, value, err := evaluateCondition(rule, result)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if !got {
t.Fatalf("expected condition true for 150 > 100")
}
if value == nil || *value != 150.0 {
t.Fatalf("expected value=150, got %v", value)
}
}
func TestEvaluateConditionThresholdFalse(t *testing.T) {
rule := thresholdRule(rulestore.Gt, 100)
result := &queryclient.Result{Columns: []string{"count"}, Rows: [][]any{{50.0}}}
got, _, err := evaluateCondition(rule, result)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if got {
t.Fatalf("expected condition false for 50 > 100")
}
}
// TestEvaluateConditionThresholdZeroRowsIsError pins down fix 4: zero
// rows on a threshold rule must be an error, not silently coerced to 0
// (which would make `count > 100` falsely report "fine" when actually
// nothing ran).
func TestEvaluateConditionThresholdZeroRowsIsError(t *testing.T) {
rule := thresholdRule(rulestore.Gt, 100)
result := &queryclient.Result{Columns: []string{"count"}, Rows: [][]any{}}
_, value, err := evaluateCondition(rule, result)
if err == nil {
t.Fatalf("expected an error for zero rows on a threshold rule, got condition evaluated with value=%v", value)
}
}
func TestEvaluateConditionThresholdMultipleRowsIsError(t *testing.T) {
rule := thresholdRule(rulestore.Gt, 100)
result := &queryclient.Result{Columns: []string{"host", "count"}, Rows: [][]any{{"h1", 50.0}, {"h2", 200.0}}}
_, _, err := evaluateCondition(rule, result)
if err == nil {
t.Fatalf("expected an error for multiple rows on a threshold rule (no per-group alerting, a named non-goal)")
}
}
func TestEvaluateConditionThresholdNonNumericIsError(t *testing.T) {
rule := thresholdRule(rulestore.Gt, 100)
result := &queryclient.Result{Columns: []string{"host"}, Rows: [][]any{{"host-01"}}}
_, _, err := evaluateCondition(rule, result)
if err == nil {
t.Fatalf("expected an error for a non-numeric first column")
}
}
func TestEvaluateConditionAbsenceTrueWhenZeroRows(t *testing.T) {
rule := rulestore.Rule{ConditionType: rulestore.ConditionAbsence}
result := &queryclient.Result{Columns: []string{"message"}, Rows: [][]any{}}
got, value, err := evaluateCondition(rule, result)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if !got {
t.Fatalf("expected absence condition true for zero rows")
}
if value != nil {
t.Fatalf("expected nil value for an absence rule, got %v", value)
}
}
func TestEvaluateConditionAbsenceFalseWhenRowsPresent(t *testing.T) {
rule := rulestore.Rule{ConditionType: rulestore.ConditionAbsence}
result := &queryclient.Result{Columns: []string{"message"}, Rows: [][]any{{"something happened"}}}
got, _, err := evaluateCondition(rule, result)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if got {
t.Fatalf("expected absence condition false when rows are present")
}
}
func TestEvaluateConditionUnknownTypeIsError(t *testing.T) {
rule := rulestore.Rule{ConditionType: "bogus"}
result := &queryclient.Result{Columns: []string{"count"}, Rows: [][]any{{1.0}}}
_, _, err := evaluateCondition(rule, result)
if err == nil {
t.Fatalf("expected an error for an unknown condition_type")
}
}