Documentation
¶
Index ¶
- func EnvForResource(rt ResourceType) (*cel.Env, error)
- func LockEnv() (*cel.Env, error)
- func QueryEnv() (*cel.Env, error)
- func TransactionEnv() (*cel.Env, error)
- type CancelAction
- type Engine
- func (e *Engine) Evaluate(ctx context.Context, snap Snapshot)
- func (e *Engine) ManualAction(ruleName string, pid int) string
- func (e *Engine) RecentViolations() []Violation
- func (e *Engine) RuleCount() int
- func (e *Engine) Rules() []Rule
- func (e *Engine) UpdateRules(rules []Rule)
- func (e *Engine) ViolatedPIDs() map[int]Violation
- type EventKind
- type Executor
- type LogAction
- type ResourceType
- type Rule
- type RuleConfig
- type Snapshot
- type TerminateAction
- type Violation
- type ViolationEvent
- type ViolationStore
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func EnvForResource ¶
func EnvForResource(rt ResourceType) (*cel.Env, error)
EnvForResource returns the appropriate CEL environment for the given resource type.
func TransactionEnv ¶
TransactionEnv returns a CEL environment for evaluating rules against db.Transaction resources.
Types ¶
type CancelAction ¶
type CancelAction struct{}
CancelAction calls pg_cancel_backend.
func (*CancelAction) Name ¶
func (a *CancelAction) Name() string
type Engine ¶
type Engine struct {
// contains filtered or unexported fields
}
Engine evaluates rules against snapshots and manages violation history.
func NewEngine ¶
func NewEngine(rules []Rule, database *db.DB, violationTTL time.Duration, maxViolations int) *Engine
NewEngine creates a new rules engine.
func (*Engine) ManualAction ¶
ManualAction manually fires an action for a violation and appends events to the log. Returns the result message for display.
func (*Engine) RecentViolations ¶
RecentViolations returns violation history within TTL, newest first.
func (*Engine) UpdateRules ¶
UpdateRules hot-swaps the rule set.
func (*Engine) ViolatedPIDs ¶
ViolatedPIDs returns a map of PIDs currently in active violation.
type Executor ¶
type Executor interface {
Execute(ctx context.Context, database *db.DB, pid int) error
Name() string
}
Executor is the interface for rule actions.
type ResourceType ¶
type ResourceType string
ResourceType identifies the kind of Postgres resource a rule targets.
const ( ResourceQuery ResourceType = "query" ResourceTransaction ResourceType = "transaction" ResourceLock ResourceType = "lock" )
type Rule ¶
type Rule struct {
Name string
Enabled bool
Resource ResourceType
Program cel.Program
Expression string
Action Executor
Cooldown time.Duration
DryRun bool
}
Rule binds a CEL expression to a resource type, an action, and cooldown config.
func BuildRules ¶
func BuildRules(configs []RuleConfig, readonly bool) ([]Rule, error)
BuildRules compiles rule configs into executable rules. If readonly is true, all rules are forced to dry-run mode.
type RuleConfig ¶
type RuleConfig struct {
Name string `yaml:"name"`
Enabled *bool `yaml:"enabled"`
DryRun bool `yaml:"dry_run"`
Resource string `yaml:"resource"`
When string `yaml:"when"`
Action string `yaml:"action"`
Cooldown string `yaml:"cooldown"`
}
RuleConfig is the YAML representation of a rule.
type TerminateAction ¶
type TerminateAction struct{}
TerminateAction calls pg_terminate_backend.
func (*TerminateAction) Name ¶
func (a *TerminateAction) Name() string
type Violation ¶
type Violation struct {
RuleName string
ResourceType ResourceType
PID int
Expression string
ActionName string
Active bool
DryRun bool
Events []ViolationEvent
QuerySnap *db.Query
TransactionSnap *db.Transaction
LockSnap *db.Lock
}
Violation records that a rule fired against a resource, with a full lifecycle audit log of events.
func (*Violation) LastEvent ¶
func (v *Violation) LastEvent() ViolationEvent
LastEvent returns the most recent event, or a zero event if empty.
type ViolationEvent ¶
ViolationEvent is a timestamped entry in a violation's audit log.
type ViolationStore ¶
type ViolationStore struct {
// contains filtered or unexported fields
}
ViolationStore retains violations with TTL eviction and cooldown dedup.
func NewViolationStore ¶
func NewViolationStore(ttl time.Duration, maxSize int) *ViolationStore
NewViolationStore creates a new violation store.
func (*ViolationStore) MarkActive ¶
func (s *ViolationStore) MarkActive(activePIDs map[int]bool)
MarkActive updates the Active flag and appends a closed event for violations whose PID is no longer present.
func (*ViolationStore) Prune ¶
func (s *ViolationStore) Prune()
Prune removes violations whose last event is older than TTL.
func (*ViolationStore) Recent ¶
func (s *ViolationStore) Recent() []Violation
Recent returns violations within the TTL window, newest first.
func (*ViolationStore) RecordOrUpdate ¶
RecordOrUpdate finds an existing active violation for the same rule+PID and returns it for event appending, or creates a new one. Returns the violation pointer and whether the action should fire (not in cooldown).
func (*ViolationStore) ViolatedPIDs ¶
func (s *ViolationStore) ViolatedPIDs() map[int]Violation
ViolatedPIDs returns a map of PIDs currently in active violation.