@ -70,20 +70,8 @@ func init() {
prometheus . MustRegister ( evalDuration )
prometheus . MustRegister ( evalDuration )
}
}
// A RuleManager manages recording and alerting rules. Create instances with
// The Manager manages recording and alerting rules.
// NewRuleManager.
type Manager struct {
type RuleManager interface {
// Start the rule manager's periodic rule evaluation.
Run ( )
// Stop the rule manager's rule evaluation cycles.
Stop ( )
// Return all rules.
Rules ( ) [ ] Rule
// Return all alerting rules.
AlertingRules ( ) [ ] * AlertingRule
}
type ruleManager struct {
// Protects the rules list.
// Protects the rules list.
sync . Mutex
sync . Mutex
rules [ ] Rule
rules [ ] Rule
@ -100,8 +88,8 @@ type ruleManager struct {
pathPrefix string
pathPrefix string
}
}
// Rule ManagerOptions bundles options for the Rule Manager.
// ManagerOptions bundles options for the Manager.
type Rule ManagerOptions struct {
type ManagerOptions struct {
EvaluationInterval time . Duration
EvaluationInterval time . Duration
QueryEngine * promql . Engine
QueryEngine * promql . Engine
@ -112,10 +100,10 @@ type RuleManagerOptions struct {
PathPrefix string
PathPrefix string
}
}
// NewRule Manager returns an implementation of Rule Manager, ready to be started
// NewManager returns an implementation of Manager, ready to be started
// by calling the Run method.
// by calling the Run method.
func NewRule Manager ( o * Rule ManagerOptions) Rule Manager {
func NewManager ( o * ManagerOptions ) * Manager {
manager := & rule Manager{
manager := & Manager {
rules : [ ] Rule { } ,
rules : [ ] Rule { } ,
done : make ( chan bool ) ,
done : make ( chan bool ) ,
@ -131,7 +119,8 @@ func NewRuleManager(o *RuleManagerOptions) RuleManager {
return manager
return manager
}
}
func ( m * ruleManager ) Run ( ) {
// Run the rule manager's periodic rule evaluation.
func ( m * Manager ) Run ( ) {
defer glog . Info ( "Rule manager stopped." )
defer glog . Info ( "Rule manager stopped." )
ticker := time . NewTicker ( m . interval )
ticker := time . NewTicker ( m . interval )
@ -158,12 +147,13 @@ func (m *ruleManager) Run() {
}
}
}
}
func ( m * ruleManager ) Stop ( ) {
// Stop the rule manager's rule evaluation cycles.
func ( m * Manager ) Stop ( ) {
glog . Info ( "Stopping rule manager..." )
glog . Info ( "Stopping rule manager..." )
m . done <- true
m . done <- true
}
}
func ( m * rule Manager) queueAlertNotifications ( rule * AlertingRule , timestamp clientmodel . Timestamp ) {
func ( m * Manager ) queueAlertNotifications ( rule * AlertingRule , timestamp clientmodel . Timestamp ) {
activeAlerts := rule . ActiveAlerts ( )
activeAlerts := rule . ActiveAlerts ( )
if len ( activeAlerts ) == 0 {
if len ( activeAlerts ) == 0 {
return
return
@ -217,7 +207,7 @@ func (m *ruleManager) queueAlertNotifications(rule *AlertingRule, timestamp clie
m . notificationHandler . SubmitReqs ( notifications )
m . notificationHandler . SubmitReqs ( notifications )
}
}
func ( m * rule Manager) runIteration ( ) {
func ( m * Manager ) runIteration ( ) {
now := clientmodel . Now ( )
now := clientmodel . Now ( )
wg := sync . WaitGroup { }
wg := sync . WaitGroup { }
@ -268,7 +258,7 @@ func (m *ruleManager) runIteration() {
wg . Wait ( )
wg . Wait ( )
}
}
func ( m * rule Manager) AddAlertingRule ( ctx context . Context , r * promql . AlertStmt ) error {
func ( m * Manager ) AddAlertingRule ( ctx context . Context , r * promql . AlertStmt ) error {
rule := NewAlertingRule ( r . Name , r . Expr , r . Duration , r . Labels , r . Summary , r . Description )
rule := NewAlertingRule ( r . Name , r . Expr , r . Duration , r . Labels , r . Summary , r . Description )
m . Lock ( )
m . Lock ( )
@ -277,7 +267,7 @@ func (m *ruleManager) AddAlertingRule(ctx context.Context, r *promql.AlertStmt)
return nil
return nil
}
}
func ( m * rule Manager) AddRecordingRule ( ctx context . Context , r * promql . RecordStmt ) error {
func ( m * Manager ) AddRecordingRule ( ctx context . Context , r * promql . RecordStmt ) error {
rule := & RecordingRule { r . Name , r . Expr , r . Labels }
rule := & RecordingRule { r . Name , r . Expr , r . Labels }
m . Lock ( )
m . Lock ( )
@ -286,7 +276,7 @@ func (m *ruleManager) AddRecordingRule(ctx context.Context, r *promql.RecordStmt
return nil
return nil
}
}
func ( m * rule Manager) Rules ( ) [ ] Rule {
func ( m * Manager ) Rules ( ) [ ] Rule {
m . Lock ( )
m . Lock ( )
defer m . Unlock ( )
defer m . Unlock ( )
@ -295,7 +285,7 @@ func (m *ruleManager) Rules() []Rule {
return rules
return rules
}
}
func ( m * rule Manager) AlertingRules ( ) [ ] * AlertingRule {
func ( m * Manager ) AlertingRules ( ) [ ] * AlertingRule {
m . Lock ( )
m . Lock ( )
defer m . Unlock ( )
defer m . Unlock ( )