rule

package
v0.0.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 25, 2026 License: MIT Imports: 14 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package rule provides the rule matching and routing engine for CoreC data points.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func EvalArith

func EvalArith(exprStr string, value float64) (float64, error)

EvalArith parses and evaluates an arithmetic expression against the given value. The expression must reference the variable as `value`. Compiled programs are cached for reuse across calls with the same expression.

Types

type Engine

type Engine struct {
	// contains filtered or unexported fields
}

Engine is the rule matching engine. It evaluates DataPoints against a priority-sorted list of rules using a linear scan, so that every evaluated rule records its hit/miss statistics and the priority order is preserved (see the Match doc comment for the full rationale).

func NewEngine

func NewEngine() *Engine

NewEngine creates a new rule engine.

func (*Engine) AddProvider

func (e *Engine) AddProvider(p core.RuleProvider)

AddProvider registers a rule provider for RULE-SET references.

func (*Engine) CloseProviders added in v0.0.2

func (e *Engine) CloseProviders()

CloseProviders closes and removes all registered rule providers, stopping any background goroutines they started (e.g. reload loops). This should be called during engine Stop/Reload to prevent goroutine leaks (problem 4).

func (*Engine) Match

func (e *Engine) Match(point core.DataPoint) *MatchResult

Match finds the first matching rule for a DataPoint. Rules are evaluated in priority order (lower priority = higher precedence). Returns nil if no rule matches.

Match intentionally uses a linear scan over e.rules rather than tag/driver indexes. Using indexes would break two invariants that existing tests rely on:

  1. Priority order — candidates gathered from separate index buckets are each sorted by priority but are NOT globally sorted across buckets, so the first bucket hit could be a lower-priority rule than one in another bucket (see TestEngineMatch).
  2. Statistics — ruleWrapper.Match updates hit/miss counters on every evaluation. Indexed lookup skips rules whose indexed tag/driver differs from the point, so those rules would never record their misses even though they sit before the first match in priority order (see TestRuleStats / TestWrapperStats). Because matchInRules returns on the first match, only rules up to and including that match are evaluated — exactly the set the linear scan covers.

Correctness is preserved over performance.

func (*Engine) ProviderCount added in v0.0.2

func (e *Engine) ProviderCount() int

ProviderCount returns the number of registered rule providers.

func (*Engine) RuleStats

func (e *Engine) RuleStats() []core.RuleStat

RuleStats returns runtime statistics for all rules.

func (*Engine) Rules

func (e *Engine) Rules() []core.Rule

Rules returns the active rules.

func (*Engine) SetRuleDisabled

func (e *Engine) SetRuleDisabled(index int, disabled bool) error

SetRuleDisabled enables or disables a rule by index.

func (*Engine) SetRules

func (e *Engine) SetRules(configs []core.RuleConfig) error

SetRules parses rule configs and sets the active rule set.

func (*Engine) SetSubRules

func (e *Engine) SetSubRules(groups map[string][]core.RuleConfig) error

SetSubRules parses named sub-rule groups for SUB-RULE references.

type FileProvider

type FileProvider struct {
	// contains filtered or unexported fields
}

FileProvider loads match-only rules from a YAML file and supports hot-reload at a configurable interval.

func NewFileProvider

func NewFileProvider(name, path, intervalStr string) (*FileProvider, error)

NewFileProvider creates a file-based rule provider. interval is the hot-reload interval; 0 means no auto-reload.

func (*FileProvider) Close

func (p *FileProvider) Close()

Close stops the reload goroutine.

func (*FileProvider) Count

func (p *FileProvider) Count() int

func (*FileProvider) Initial

func (p *FileProvider) Initial() error

Initial loads the provider data (already done in constructor, kept for interface compat).

func (*FileProvider) Match

func (p *FileProvider) Match(point core.DataPoint) bool

Match returns true if any rule in the provider matches the DataPoint.

func (*FileProvider) Name

func (p *FileProvider) Name() string

func (*FileProvider) Update

func (p *FileProvider) Update() error

Update forces a reload of the provider file.

type MatchResult

type MatchResult = core.RuleMatchResult

MatchResult contains the matched rule and resolved targets. It is an alias for core.RuleMatchResult so the concrete rule.Engine satisfies the core.RuleEngine port (whose Match returns *core.RuleMatchResult) without any conversion at the call site.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL