alerting.go
alerting.go - Overview
This file defines data structures and helper functions related to alerting rules. It includes definitions for alert states, rule types, rule health, alert structures, comparison operators, match types, and rule conditions. It also provides functions for preparing rule generator URLs.
Detailed Documentation
ResolvedRetention
- Purpose: Defines the duration before re-sending a resolved alert.
- Value:
15 * time.Minute
- Parameters: None
- Returns: None
TestAlertPostFix
- Purpose: Defines a postfix for test alert names.
- Value:
"_TEST_ALERT"
- Parameters: None
- Returns: None
RuleType
- Purpose: Defines the type for rule types.
- Type:
string
- Parameters: None
- Returns: None
RuleTypeThreshold
- Purpose: Defines a rule type for threshold-based rules.
- Value:
"threshold_rule"
- Parameters: None
- Returns: None
RuleTypeProm
- Purpose: Defines a rule type for PromQL-based rules.
- Value:
"promql_rule"
- Parameters: None
- Returns: None
RuleTypeAnomaly
- Purpose: Defines a rule type for anomaly-based rules.
- Value:
"anomaly_rule"
- Parameters: None
- Returns: None
RuleHealth
- Purpose: Defines the type for rule health.
- Type:
string
- Parameters: None
- Returns: None
HealthUnknown
- Purpose: Defines a rule health status for unknown health.
- Value:
"unknown"
- Parameters: None
- Returns: None
HealthGood
- Purpose: Defines a rule health status for good health.
- Value:
"ok"
- Parameters: None
- Returns: None
HealthBad
- Purpose: Defines a rule health status for bad health.
- Value:
"err"
- Parameters: None
- Returns: None
Alert
- Purpose: Defines the structure for an alert.
- Fields:
State
(model.AlertState
): The current state of the alert.Labels
(labels.BaseLabels
): Labels associated with the alert.Annotations
(labels.BaseLabels
): Annotations associated with the alert.QueryResultLables
(labels.BaseLabels
): Labels from the query result that triggered the alert.GeneratorURL
(string
): URL to generate the alert (link back to rule).Receivers
([]string
): List of preferred receivers (e.g., slack).Value
(float64
): The value that triggered the alert.ActiveAt
(time.Time
): The time the alert became active.FiredAt
(time.Time
): The time the alert fired.ResolvedAt
(time.Time
): The time the alert was resolved.LastSentAt
(time.Time
): The time the alert was last sent.ValidUntil
(time.Time
): The time the alert is valid until.Missing
(bool
): Indicates if the alert is missing.
- Parameters: None
- Returns: None
Alert.needsSending
- Purpose: Determines if an alert needs to be sent based on its state, resolution time, last sent time, and a resend delay.
- Parameters:
ts
(time.Time
): The current timestamp.resendDelay
(time.Duration
): The delay before resending an alert.
- Returns:
bool
:true
if the alert needs to be sent,false
otherwise.
NamedAlert
- Purpose: Defines a structure that associates a name with an alert.
- Fields:
Name
(string
): The name of the alert.Alert
(*Alert
): A pointer to anAlert
struct.
- Parameters: None
- Returns: None
CompareOp
- Purpose: Defines the type for compare operators.
- Type:
string
- Parameters: None
- Returns: None
CompareOpNone
- Purpose: Defines a compare operator for "none".
- Value:
"0"
- Parameters: None
- Returns: None
ValueIsAbove
- Purpose: Defines a compare operator for "value is above".
- Value:
"1"
- Parameters: None
- Returns: None
ValueIsBelow
- Purpose: Defines a compare operator for "value is below".
- Value:
"2"
- Parameters: None
- Returns: None
ValueIsEq
- Purpose: Defines a compare operator for "value is equal".
- Value:
"3"
- Parameters: None
- Returns: None
ValueIsNotEq
- Purpose: Defines a compare operator for "value is not equal".
- Value:
"4"
- Parameters: None
- Returns: None
ValueAboveOrEq
- Purpose: Defines a compare operator for "value is above or equal".
- Value:
"5"
- Parameters: None
- Returns: None
ValueBelowOrEq
- Purpose: Defines a compare operator for "value is below or equal".
- Value:
"6"
- Parameters: None
- Returns: None
ValueOutsideBounds
- Purpose: Defines a compare operator for "value is outside bounds".
- Value:
"7"
- Parameters: None
- Returns: None
MatchType
- Purpose: Defines the type for match types.
- Type:
string
- Parameters: None
- Returns: None
MatchTypeNone
- Purpose: Defines a match type for "none".
- Value:
"0"
- Parameters: None
- Returns: None
AtleastOnce
- Purpose: Defines a match type for "at least once".
- Value:
"1"
- Parameters: None
- Returns: None
AllTheTimes
- Purpose: Defines a match type for "all the times".
- Value:
"2"
- Parameters: None
- Returns: None
OnAverage
- Purpose: Defines a match type for "on average".
- Value:
"3"
- Parameters: None
- Returns: None
InTotal
- Purpose: Defines a match type for "in total".
- Value:
"4"
- Parameters: None
- Returns: None
Last
- Purpose: Defines a match type for "last".
- Value:
"5"
- Parameters: None
- Returns: None
RuleCondition
- Purpose: Defines the conditions that must be met for a rule to trigger.
- Fields:
CompositeQuery
(*v3.CompositeQuery
): The composite query to evaluate.CompareOp
(CompareOp
): The comparison operator to use.Target
(*float64
): The target value for comparison.AlertOnAbsent
(bool
): Whether to alert when data is absent.AbsentFor
(uint64
): The duration for which data must be absent before alerting (in seconds).MatchType
(MatchType
): The type of match required.TargetUnit
(string
): The unit of the target value.Algorithm
(string
): The algorithm to use.Seasonality
(string
): Seasonality setting for the anomaly detection algorithmSelectedQuery
(string
): The query name to use from composite query.RequireMinPoints
(bool
): Whether a minimum number of points is required.RequiredNumPoints
(int
): The required number of points.
- Parameters: None
- Returns: None
RuleCondition.GetSelectedQueryName
- Purpose: Gets the query name to use.
- Parameters: None
- Returns:
string
: The name of the query to be used, extracted based on specific logic.
RuleCondition.IsValid
- Purpose: Checks if rule condition is valid based on the fields.
- Parameters: None
- Returns:
bool
: Whether theRuleCondition
struct is valid.
RuleCondition.QueryType
- Purpose: Return query type of the rule.
- Parameters: None
- Returns:
v3.QueryType
: The query type used in rule condition.
RuleCondition.String
- Purpose: Stringer implementation for rule condition to print in logs.
- Parameters: None
- Returns:
string
: JSON string representation of RuleCondition
Duration
- Purpose: Defines a custom
Duration
type to handle JSON marshalling and unmarshalling. - Type:
time.Duration
- Parameters: None
- Returns: None
Duration.MarshalJSON
- Purpose: Marshals a
Duration
to JSON as a string. - Parameters: None
- Returns:
[]byte
: JSON representation of the duration.error
: An error if marshalling fails.
Duration.UnmarshalJSON
- Purpose: Unmarshals a
Duration
from JSON, supporting both float64 (seconds) and string representations. - Parameters:
b
([]byte
): The JSON data to unmarshal.
- Returns:
error
: An error if unmarshalling fails.
prepareRuleGeneratorURL
- Purpose: Creates the URL for the rule based on the rule ID and source URL.
- Parameters:
ruleId
(string
): The ID of the rule.source
(string
): The source URL where the rule was created.
- Returns:
string
: The generated URL for the rule.
Include in Getting Started: NO