custom alerting optimize

Signed-off-by: junotx <junotx@126.com>
This commit is contained in:
junotx
2021-01-04 16:35:50 +08:00
parent 371c9b187d
commit 9831eb3533
8 changed files with 102 additions and 158 deletions

View File

@@ -104,7 +104,7 @@ func (c *RuleCache) getResourceRuleCaches(ruler Ruler, ruleNamespace *corev1.Nam
}
func (c *RuleCache) GetRule(ruler Ruler, ruleNamespace *corev1.Namespace,
extraRuleResourceSelector labels.Selector, idOrName string) (*ResourceRule, error) {
extraRuleResourceSelector labels.Selector, idOrName string) (*ResourceRuleItem, error) {
caches, err := c.getResourceRuleCaches(ruler, ruleNamespace, extraRuleResourceSelector)
if err != nil {
@@ -114,12 +114,12 @@ func (c *RuleCache) GetRule(ruler Ruler, ruleNamespace *corev1.Namespace,
return nil, nil
}
var rules []*ResourceRule
var rules []*ResourceRuleItem
switch ruler.(type) {
case *PrometheusRuler:
for rn, rc := range caches {
if rule, ok := rc.IdRules[idOrName]; ok {
rules = append(rules, &ResourceRule{
rules = append(rules, &ResourceRuleItem{
Group: rule.Group,
Id: rule.Id,
Rule: rule.Rule.DeepCopy(),
@@ -131,7 +131,7 @@ func (c *RuleCache) GetRule(ruler Ruler, ruleNamespace *corev1.Namespace,
for rn, rc := range caches {
if nrules, ok := rc.NameRules[idOrName]; ok {
for _, nrule := range nrules {
rules = append(rules, &ResourceRule{
rules = append(rules, &ResourceRuleItem{
Group: nrule.Group,
Id: nrule.Id,
Rule: nrule.Rule.DeepCopy(),
@@ -156,7 +156,7 @@ func (c *RuleCache) GetRule(ruler Ruler, ruleNamespace *corev1.Namespace,
}
func (c *RuleCache) ListRules(ruler Ruler, ruleNamespace *corev1.Namespace,
extraRuleResourceSelector labels.Selector) (map[string]*ResourceRules, error) {
extraRuleResourceSelector labels.Selector) (map[string]*ResourceRuleCollection, error) {
caches, err := c.getResourceRuleCaches(ruler, ruleNamespace, extraRuleResourceSelector)
if err != nil {
@@ -166,17 +166,17 @@ func (c *RuleCache) ListRules(ruler Ruler, ruleNamespace *corev1.Namespace,
return nil, nil
}
ret := make(map[string]*ResourceRules)
ret := make(map[string]*ResourceRuleCollection)
for rn, rc := range caches {
rrs := &ResourceRules{
rrs := &ResourceRuleCollection{
GroupSet: make(map[string]struct{}),
IdRules: make(map[string]*ResourceRule),
NameRules: make(map[string][]*ResourceRule),
IdRules: make(map[string]*ResourceRuleItem),
NameRules: make(map[string][]*ResourceRuleItem),
}
for name, rules := range rc.NameRules {
for _, rule := range rules {
rrs.GroupSet[rule.Group] = struct{}{}
rr := &ResourceRule{
rr := &ResourceRuleItem{
Group: rule.Group,
Id: rule.Id,
Rule: rule.Rule.DeepCopy(),

View File

@@ -5,27 +5,27 @@ import (
"kubesphere.io/kubesphere/pkg/api/customalerting/v1alpha1"
)
type ResourceRules struct {
type ResourceRuleCollection struct {
GroupSet map[string]struct{}
IdRules map[string]*ResourceRule
NameRules map[string][]*ResourceRule
IdRules map[string]*ResourceRuleItem
NameRules map[string][]*ResourceRuleItem
}
type ResourceRule struct {
type ResourceRuleItem struct {
ResourceName string
Group string
Id string
Rule *promresourcesv1.Rule
}
type ResourceRuleSole struct {
type ResourceRule struct {
Level v1alpha1.RuleLevel
Custom bool
ResourceRule
ResourceRuleItem
}
type ResourceRuleChunk struct {
Level v1alpha1.RuleLevel
Custom bool
ResourceRulesMap map[string]*ResourceRules
ResourceRulesMap map[string]*ResourceRuleCollection
}

View File

@@ -21,12 +21,10 @@ import (
const (
ErrGenRuleId = "error generating rule id"
LabelKeyInternalRuleGroup = "__rule_group__"
LabelKeyInternalRuleName = "__rule_name__"
LabelKeyInternalRuleQuery = "__rule_query__"
LabelKeyInternalRuleDuration = "__rule_duration__"
LabelKeyInternalRuleAlias = "__rule_alias__"
LabelKeyInternalRuleDescription = "__rule_description__"
LabelKeyInternalRuleGroup = "__rule_group__"
LabelKeyInternalRuleName = "__rule_name__"
LabelKeyInternalRuleQuery = "__rule_query__"
LabelKeyInternalRuleDuration = "__rule_duration__"
)
func FormatExpr(expr string) (string, error) {
@@ -127,9 +125,9 @@ func GenEndpointRuleId(group string, epRule *customalerting.AlertingRule,
return prommodel.Fingerprint(prommodel.LabelsToSignature(lbls)).String(), nil
}
// MixAlertingRules mix rules from prometheusrule custom resources and rules from endpoints.
// GetAlertingRulesStatus mix rules from prometheusrule custom resources and rules from endpoints.
// Use rules from prometheusrule custom resources as the main reference.
func MixAlertingRules(ruleNamespace string, ruleChunk *ResourceRuleChunk, epRuleGroups []*customalerting.RuleGroup,
func GetAlertingRulesStatus(ruleNamespace string, ruleChunk *ResourceRuleChunk, epRuleGroups []*customalerting.RuleGroup,
extLabels func() map[string]string) ([]*v1alpha1.GettableAlertingRule, error) {
var (
@@ -161,7 +159,7 @@ func MixAlertingRules(ruleNamespace string, ruleChunk *ResourceRuleChunk, epRule
}
if ruleChunk.Custom {
// guarantee the names of the custom alerting rules not to be repeated
var m = make(map[string][]*ResourceRule)
var m = make(map[string][]*ResourceRuleItem)
for _, resourceRules := range ruleChunk.ResourceRulesMap {
for name, rrArr := range resourceRules.NameRules {
m[name] = append(m[name], rrArr...)
@@ -176,7 +174,7 @@ func MixAlertingRules(ruleNamespace string, ruleChunk *ResourceRuleChunk, epRule
}
resRule := rrArr[0]
epRule := idEpRules[resRule.Id]
if r := mixAlertingRule(resRule, epRule, ruleChunk.Custom, ruleChunk.Level); r != nil {
if r := getAlertingRuleStatus(resRule, epRule, ruleChunk.Custom, ruleChunk.Level); r != nil {
ret = append(ret, r)
}
}
@@ -186,7 +184,7 @@ func MixAlertingRules(ruleNamespace string, ruleChunk *ResourceRuleChunk, epRule
var m = make(map[string]*v1alpha1.GettableAlertingRule)
for _, resourceRules := range ruleChunk.ResourceRulesMap {
for id, rule := range resourceRules.IdRules {
if r := mixAlertingRule(rule, idEpRules[id], ruleChunk.Custom, ruleChunk.Level); r != nil {
if r := getAlertingRuleStatus(rule, idEpRules[id], ruleChunk.Custom, ruleChunk.Level); r != nil {
m[id] = r
}
}
@@ -199,7 +197,7 @@ func MixAlertingRules(ruleNamespace string, ruleChunk *ResourceRuleChunk, epRule
return ret, nil
}
func MixAlertingRule(ruleNamespace string, rule *ResourceRuleSole, epRuleGroups []*customalerting.RuleGroup,
func GetAlertingRuleStatus(ruleNamespace string, rule *ResourceRule, epRuleGroups []*customalerting.RuleGroup,
extLabels func() map[string]string) (*v1alpha1.GettableAlertingRule, error) {
if rule == nil || rule.Rule == nil {
@@ -245,52 +243,27 @@ func MixAlertingRule(ruleNamespace string, rule *ResourceRuleSole, epRuleGroups
epRule = epRules[rule.Id]
}
return mixAlertingRule(&rule.ResourceRule, epRule, rule.Custom, rule.Level), nil
return getAlertingRuleStatus(&rule.ResourceRuleItem, epRule, rule.Custom, rule.Level), nil
}
func mixAlertingRule(resRule *ResourceRule, epRule *customalerting.AlertingRule,
func getAlertingRuleStatus(resRule *ResourceRuleItem, epRule *customalerting.AlertingRule,
custom bool, level v1alpha1.RuleLevel) *v1alpha1.GettableAlertingRule {
if resRule == nil || resRule.Rule == nil {
return nil
}
var (
alias string
descrption string
lbls map[string]string
)
if len(resRule.Rule.Labels) > 0 {
lbls = make(map[string]string)
for k, v := range resRule.Rule.Labels {
switch k {
case LabelKeyInternalRuleAlias:
alias = v
case LabelKeyInternalRuleDescription:
descrption = v
default:
lbls[k] = v
}
}
}
rule := v1alpha1.GettableAlertingRule{
AlertingRuleQualifier: v1alpha1.AlertingRuleQualifier{
Id: resRule.Id,
Name: resRule.Rule.Alert,
Custom: custom,
Level: level,
},
AlertingRuleProps: v1alpha1.AlertingRuleProps{
AlertingRule: v1alpha1.AlertingRule{
Id: resRule.Id,
Name: resRule.Rule.Alert,
Query: resRule.Rule.Expr.String(),
Duration: resRule.Rule.For,
Labels: lbls,
Labels: resRule.Rule.Labels,
Annotations: resRule.Rule.Annotations,
},
Alias: alias,
Description: descrption,
State: stateInactiveString,
Health: string(rules.HealthUnknown),
State: stateInactiveString,
Health: string(rules.HealthUnknown),
}
if epRule != nil {
@@ -316,25 +289,15 @@ func mixAlertingRule(resRule *ResourceRule, epRule *customalerting.AlertingRule,
rule.State = aState
}
}
var lbls map[string]string
if len(a.Labels) > 0 {
lbls = make(map[string]string)
for k, v := range a.Labels {
switch k {
case LabelKeyInternalRuleAlias, LabelKeyInternalRuleDescription:
default:
lbls[k] = v
}
}
}
rule.Alerts = append(rule.Alerts, &v1alpha1.Alert{
ActiveAt: a.ActiveAt,
Labels: lbls,
Labels: a.Labels,
Annotations: a.Annotations,
State: aState,
Value: a.Value,
Rule: &rule.AlertingRuleQualifier,
RuleId: rule.Id,
RuleName: rule.Name,
})
}
}
@@ -353,13 +316,9 @@ func ParseAlertingRules(epRuleGroups []*customalerting.RuleGroup, custom bool, l
}
if filterFunc(g.Name, id, r) {
rule := &v1alpha1.GettableAlertingRule{
AlertingRuleQualifier: v1alpha1.AlertingRuleQualifier{
Id: id,
Name: r.Name,
Custom: custom,
Level: level,
},
AlertingRuleProps: v1alpha1.AlertingRuleProps{
AlertingRule: v1alpha1.AlertingRule{
Id: id,
Name: r.Name,
Query: r.Query,
Duration: parseDurationSeconds(r.Duration),
Labels: r.Labels,
@@ -392,7 +351,8 @@ func ParseAlertingRules(epRuleGroups []*customalerting.RuleGroup, custom bool, l
State: aState,
Value: a.Value,
Rule: &rule.AlertingRuleQualifier,
RuleId: rule.Id,
RuleName: rule.Name,
})
}
ret = append(ret, rule)

View File

@@ -11,7 +11,7 @@ import (
"kubesphere.io/kubesphere/pkg/simple/client/customalerting"
)
func TestMixAlertingRules(t *testing.T) {
func TestGetAlertingRulesStatus(t *testing.T) {
var tests = []struct {
description string
ruleNamespace string
@@ -20,26 +20,26 @@ func TestMixAlertingRules(t *testing.T) {
extLabels func() map[string]string
expected []*v1alpha1.GettableAlertingRule
}{{
description: "mix custom rules",
description: "get alerting rules status",
ruleNamespace: "test",
resourceRuleChunk: &ResourceRuleChunk{
Level: v1alpha1.RuleLevelNamespace,
Custom: true,
ResourceRulesMap: map[string]*ResourceRules{
"custom-alerting-rule-jqbgn": &ResourceRules{
ResourceRulesMap: map[string]*ResourceRuleCollection{
"custom-alerting-rule-jqbgn": &ResourceRuleCollection{
GroupSet: map[string]struct{}{"alerting.custom.defaults": struct{}{}},
NameRules: map[string][]*ResourceRule{
"f89836879157ca88": []*ResourceRule{{
NameRules: map[string][]*ResourceRuleItem{
"ca7f09e76954e67c": []*ResourceRuleItem{{
ResourceName: "custom-alerting-rule-jqbgn",
Group: "alerting.custom.defaults",
Id: "f89836879157ca88",
Id: "ca7f09e76954e67c",
Rule: &promresourcesv1.Rule{
Alert: "TestCPUUsageHigh",
Expr: intstr.FromString(`namespace:workload_cpu_usage:sum{namespace="test"} > 1`),
For: "1m",
Labels: map[string]string{
LabelKeyInternalRuleAlias: "The alias is here",
LabelKeyInternalRuleDescription: "The description is here",
Annotations: map[string]string{
"alias": "The alias is here",
"description": "The description is here",
},
},
}},
@@ -56,34 +56,31 @@ func TestMixAlertingRules(t *testing.T) {
Duration: 60,
Health: string(rules.HealthGood),
State: stateInactiveString,
Labels: map[string]string{
LabelKeyInternalRuleAlias: "The alias is here",
LabelKeyInternalRuleDescription: "The description is here",
Annotations: map[string]string{
"alias": "The alias is here",
"description": "The description is here",
},
}},
}},
expected: []*v1alpha1.GettableAlertingRule{{
AlertingRuleQualifier: v1alpha1.AlertingRuleQualifier{
Id: "f89836879157ca88",
Name: "TestCPUUsageHigh",
Level: v1alpha1.RuleLevelNamespace,
Custom: true,
},
AlertingRuleProps: v1alpha1.AlertingRuleProps{
AlertingRule: v1alpha1.AlertingRule{
Id: "ca7f09e76954e67c",
Name: "TestCPUUsageHigh",
Query: `namespace:workload_cpu_usage:sum{namespace="test"} > 1`,
Duration: "1m",
Labels: map[string]string{},
Annotations: map[string]string{
"alias": "The alias is here",
"description": "The description is here",
},
},
Alias: "The alias is here",
Description: "The description is here",
Health: string(rules.HealthGood),
State: stateInactiveString,
Health: string(rules.HealthGood),
State: stateInactiveString,
}},
}}
for _, test := range tests {
t.Run(test.description, func(t *testing.T) {
rules, err := MixAlertingRules(test.ruleNamespace, test.resourceRuleChunk, test.ruleGroups, test.extLabels)
rules, err := GetAlertingRulesStatus(test.ruleNamespace, test.resourceRuleChunk, test.ruleGroups, test.extLabels)
if err != nil {
t.Fatal(err)
}