rule_task.go
rule_task.go - Overview
-
Overview This file defines the
RuleTask
struct and its associated methods for managing and evaluating a set of rules at a specific frequency. It handles rule execution, state management, and interaction with a rule database. -
Detailed Documentation
RuleTask
- Purpose: Holds a rule (with composite queries) and evaluates the rule at a given frequency.
- Fields:
name
: string - The name of the rule task.file
: string - The file from which the rule task was loaded.frequency
: time.Duration - The frequency at which the rules are evaluated.rules
:[]Rule
- A slice ofRule
interfaces to be evaluated.opts
:*ManagerOptions
- Options for the rule manager.mtx
:sync.Mutex
- A mutex to protect concurrent access to the RuleTask.evaluationDuration
:time.Duration
- The duration of the last rule evaluation.evaluationTime
:time.Duration
- The time spent in the last evaluation.lastEvaluation
:time.Time
- The time of the last evaluation.done
:chan struct{}
- A channel to signal task completion.terminated
:chan struct{}
- A channel to signal task termination.pause
: bool - A flag to pause rule evaluation.notify
:NotifyFunc
- A function to send notifications.ruleDB
:RuleDB
- Interface for accessing the rule database.
DefaultFrequency
- Purpose: Defines a default frequency for rule evaluation if an invalid frequency is provided.
- Value:
1 * time.Minute
NewRuleTask
- Purpose: Creates a new
RuleTask
instance. - Parameters:
name
: string - The name of the rule task.file
: string - The file from which the rules are loaded.frequency
:time.Duration
- The frequency at which the rules should be evaluated.rules
:[]Rule
- A slice ofRule
interfaces.opts
:*ManagerOptions
- Options for the rule manager.notify
:NotifyFunc
- A function to send notifications.ruleDB
:RuleDB
- Interface for accessing the rule database.
- Returns:
*RuleTask
- A pointer to the newly createdRuleTask
.
(*RuleTask) Name
- Purpose: Returns the name of the rule task.
- Returns: string - The name of the rule task.
(*RuleTask) Key
- Purpose: Returns a unique key for the rule task.
- Returns: string - The key, which is a concatenation of the name and file name separated by a semicolon.
(*RuleTask) Type
- Purpose: Returns the task type.
- Returns:
TaskType
- The task type (TaskTypeCh
).
(*RuleTask) Rules
- Purpose: Returns the rules associated with the rule task.
- Returns:
[]Rule
- A slice ofRule
interfaces.
(*RuleTask) Interval
- Purpose: Returns the evaluation frequency of the rule task.
- Returns:
time.Duration
- The frequency at which the rules are evaluated.
(*RuleTask) Pause
- Purpose: Pauses or unpauses the rule evaluation.
- Parameters:
b
: bool - A boolean indicating whether to pause (true) or unpause (false) the evaluation.
- Returns: None
QueryOrigin
NewQueryOriginContext
- Purpose: Creates a new context with query origin information.
- Parameters:
ctx
:context.Context
- The parent context.data
:map[string]interface{}
- A map containing query origin data.
- Returns:
context.Context
- A new context with the query origin information.
(*RuleTask) Run
- Purpose: Starts the rule evaluation loop.
- Parameters:
ctx
:context.Context
- The context for the rule task.
- Returns: None
(*RuleTask) Stop
- Purpose: Stops the rule evaluation loop.
- Returns: None
(*RuleTask) hash
- Purpose: Generates a hash value for the rule task based on its name.
- Returns: uint64 - The hash value.
(*RuleTask) ThresholdRules
- Purpose: Returns a list of threshold rules sorted by state and name.
- Returns:
[]*ThresholdRule
- A slice of pointers toThresholdRule
objects.
(*RuleTask) HasAlertingRules
- Purpose: Checks if the group contains at least one alerting rule.
- Returns: bool - True if the group contains an alerting rule, false otherwise.
(*RuleTask) GetEvaluationDuration
- Purpose: Returns the evaluation duration of the rule group.
- Returns:
time.Duration
- The time it took to evaluate the rule group.
(*RuleTask) SetEvaluationDuration
- Purpose: Sets the evaluation duration of the rule group.
- Parameters:
dur
:time.Duration
- The evaluation duration.
- Returns: None
(*RuleTask) GetEvaluationTime
- Purpose: Returns the time spent in the last evaluation of the rule group.
- Returns:
time.Duration
- The time spent in the last evaluation.
(*RuleTask) setEvaluationTime
- Purpose: Sets the time spent in the last evaluation of the rule group.
- Parameters:
dur
:time.Duration
- The time spent in the last evaluation.
- Returns: None
(*RuleTask) GetLastEvaluation
- Purpose: Returns the time of the last evaluation of the rule group.
- Returns:
time.Time
- The time of the last evaluation.
(*RuleTask) setLastEvaluation
- Purpose: Sets the time of the last evaluation of the rule group.
- Parameters:
ts
:time.Time
- The time of the last evaluation.
- Returns: None
(*RuleTask) EvalTimestamp
- Purpose: Returns the previous aligned evaluation timestamp.
- Parameters:
startTime
: int64 - Start timestamp
- Returns:
time.Time
- The aligned evaluation timestamp.
nameAndLabels
- Purpose: Returns a string containing the rule name and labels.
- Parameters:
rule
:Rule
- The rule.
- Returns: string - The combined name and labels.
(*RuleTask) CopyState
- Purpose: Copies the alerting rule and staleness related state from another
RuleTask
. - Parameters:
fromTask
:Task
- The sourceRuleTask
to copy state from.
- Returns: error - An error if the
fromTask
is not aRuleTask
.
(*RuleTask) Eval
- Purpose: Evaluates all rules in the group sequentially.
- Parameters:
ctx
:context.Context
- The context for the evaluation.ts
:time.Time
- The evaluation timestamp.
- Returns: None
- Code Examples N/A
Include in Getting Started: NO