Files
cairnobs/alerting/internal/evaluator/condition.go
T
jcoffey-dev 13cf9a30cb Rebrand: Sentry -> Cairn OBS
Full rebrand across cosmetic branding, code identifiers, and
infrastructure/data-plane naming, using the supplied Cairn OBS logo
package. Cosmetic: favicon/logo swap (also closes a stale license-audit
finding -- the old favicon was SvelteKit's unreplaced scaffold logo),
new centered welcome landing page, larger/legible sidebar logo, page
titles, CLAUDE.md/README/docs prose.

Code identifiers: Go module path github.com/sentry/sentry ->
github.com/cairnobs/cairnobs across all 13 modules and ~91 files (protoc
regenerated); Rust crates sentry-agent/sentry-parser/sentry-search ->
cairnobs-*; CLI sentryctl -> cairnobsctl; Terraform provider fully
renamed (sentry_dashboard etc. -> cairnobs_dashboard, provider type,
env vars); every session/auth cookie name; agent config paths and
Windows service identity.

Deliberately preserved: the gRPC wire protocol's protobuf packages
(sentry.logs.v1, sentry.agent.v1) and their Go import directory
(proto/sentry/...) -- renaming the wire-level package would break every
currently-deployed agent binary (confirmed two real hosts, including
mail.inbuxa.com, are actively streaming through this exact contract)
until rebuilt and redeployed in lockstep with an ingest cutover. Only
the Go module path wrapping the generated code changes.

Infrastructure: every docker-compose container name (root and three
component-level compose files); the Helm chart (directory, Chart.yaml,
named-template helpers, all templates, values.yaml image repos);
Kubernetes Operator (CRD group sentry.io -> cairnobs.io, both CRD YAML
files, Go identifiers, RBAC markers); the coupled enterprise/tenantcrd
package. Caught and fixed real path-coupling bugs along the way: the
Helm chart's search/ingest volume mounts and the dev-only-credential
detection constant vs. docker-compose.yml's literal values had to move
together or a security warning would have silently stopped firing.

Data plane: Postgres database sentry_metadata -> cairnobs_metadata and
role sentry -> cairnobs; ClickHouse database sentry -> cairnobs; Kafka
topic sentry.logs.raw -> cairnobs.logs.raw and its consumer groups.
Source-level defaults, docker-compose.yml, and every migrate.sh/
provision script default updated together; already-applied migration
files left untouched per this repo's immutable-migration convention.

Verified at every layer: all 13 Go modules build/vet/test clean, both
Rust workspaces (agent, search) build/clippy/test clean, npm run check/
build clean, docker compose config validates on all four compose files.
Live-verified against a real docker stack multiple times through this
work, including a final fresh-volume run confirming the actual renamed
Postgres database/role, ClickHouse database, and Kafka topic all work
end to end with a real login and query, zero console errors.
2026-08-21 20:53:32 -07:00

75 lines
2.6 KiB
Go

package evaluator
import (
"fmt"
"github.com/cairnobs/cairnobs/alerting/internal/queryclient"
"github.com/cairnobs/cairnobs/alerting/internal/rulestore"
)
// evaluateCondition implements fixes 3 and 4 from
// /docs/phase-3-alerting-design.md: a threshold rule's query must
// resolve to exactly one row, and zero (or more than one) rows is
// returned as an error, never coerced to a value -- "nothing ran" and
// "ran and found nothing" are different failure/success modes, and
// conflating them can hide the more alarming case. This function never
// itself decides "condition false" on an error; it returns an error, and
// the caller (evaluator.go) routes that to rulestore.RecordError, never
// to ComputeTransition.
func evaluateCondition(rule rulestore.Rule, result *queryclient.Result) (conditionTrue bool, value *float64, err error) {
switch rule.ConditionType {
case rulestore.ConditionAbsence:
// The window is whatever earliest=/latest= the rule's own query
// already expresses -- no separate window field, per the design doc.
return len(result.Rows) == 0, nil, nil
case rulestore.ConditionThreshold:
if len(result.Rows) != 1 {
return false, nil, fmt.Errorf("threshold rule query returned %d rows, want exactly 1", len(result.Rows))
}
row := result.Rows[0]
if len(row) == 0 {
return false, nil, fmt.Errorf("threshold rule query returned a row with no columns")
}
v, ok := toFloat64(row[0])
if !ok {
return false, nil, fmt.Errorf("threshold rule's first column value %v is not numeric", row[0])
}
if rule.Comparator == nil || rule.ThresholdValue == nil {
return false, nil, fmt.Errorf("threshold rule is missing comparator or threshold_value")
}
return compare(v, *rule.Comparator, *rule.ThresholdValue), &v, nil
default:
return false, nil, fmt.Errorf("unknown condition_type %q", rule.ConditionType)
}
}
func compare(value float64, comparator rulestore.Comparator, threshold float64) bool {
switch comparator {
case rulestore.Gt:
return value > threshold
case rulestore.Gte:
return value >= threshold
case rulestore.Lt:
return value < threshold
case rulestore.Lte:
return value <= threshold
case rulestore.Eq:
return value == threshold
case rulestore.Ne:
return value != threshold
default:
return false
}
}
// toFloat64 handles the one shape /query responses actually come back
// as: Go's encoding/json always decodes JSON numbers into interface{}
// as float64, regardless of whether ClickHouse serialized an integer or
// a float -- there's no separate int64 case to handle.
func toFloat64(v any) (float64, bool) {
f, ok := v.(float64)
return f, ok
}