Alerting: introduce AlertRuleGroupKey and use it in API handlers (#48945)
* create AlertGroupKey structure * update PrometheusSrv. - extract creation of RuleGroup to a separate method. Use group key for grouping * update RuleSrv - update calculateChanges to use groupKey - authorize to use groupkey
This commit is contained in:
@@ -167,6 +167,17 @@ type AlertRuleKey struct {
|
||||
UID string
|
||||
}
|
||||
|
||||
// AlertRuleGroupKey is the identifier of a group of alerts
|
||||
type AlertRuleGroupKey struct {
|
||||
OrgID int64
|
||||
NamespaceUID string
|
||||
RuleGroup string
|
||||
}
|
||||
|
||||
func (k AlertRuleGroupKey) String() string {
|
||||
return fmt.Sprintf("{orgID: %d, namespaceUID: %s, groupName: %s}", k.OrgID, k.NamespaceUID, k.RuleGroup)
|
||||
}
|
||||
|
||||
func (k AlertRuleKey) String() string {
|
||||
return fmt.Sprintf("{orgID: %d, UID: %s}", k.OrgID, k.UID)
|
||||
}
|
||||
@@ -176,6 +187,11 @@ func (alertRule *AlertRule) GetKey() AlertRuleKey {
|
||||
return AlertRuleKey{OrgID: alertRule.OrgID, UID: alertRule.UID}
|
||||
}
|
||||
|
||||
// GetGroupKey returns the identifier of a group the rule belongs to
|
||||
func (alertRule *AlertRule) GetGroupKey() AlertRuleGroupKey {
|
||||
return AlertRuleGroupKey{OrgID: alertRule.OrgID, NamespaceUID: alertRule.NamespaceUID, RuleGroup: alertRule.RuleGroup}
|
||||
}
|
||||
|
||||
// GetKey returns the alert definitions identifier
|
||||
func (alertRule *SchedulableAlertRule) GetKey() AlertRuleKey {
|
||||
return AlertRuleKey{OrgID: alertRule.OrgID, UID: alertRule.UID}
|
||||
|
||||
@@ -133,6 +133,15 @@ func GenerateAlertRules(count int, f func() *AlertRule) []*AlertRule {
|
||||
return result
|
||||
}
|
||||
|
||||
// GenerateGroupKey generates many random alert rules. Does not guarantee that rules are unique (by UID)
|
||||
func GenerateGroupKey(orgID int64) AlertRuleGroupKey {
|
||||
return AlertRuleGroupKey{
|
||||
OrgID: orgID,
|
||||
NamespaceUID: util.GenerateShortUID(),
|
||||
RuleGroup: util.GenerateShortUID(),
|
||||
}
|
||||
}
|
||||
|
||||
// CopyRule creates a deep copy of AlertRule
|
||||
func CopyRule(r *AlertRule) *AlertRule {
|
||||
result := AlertRule{
|
||||
|
||||
Reference in New Issue
Block a user