From 26fa830fca2fa748bf8ca41a2d650c5db5e0663a Mon Sep 17 00:00:00 2001 From: like Date: Thu, 20 Jul 2023 19:12:35 +0800 Subject: [PATCH] =?UTF-8?q?perf:=20=E5=AE=8C=E5=96=84=E9=A2=84=E8=AD=A6?= =?UTF-8?q?=E8=A7=84=E5=88=99=E8=AE=BE=E7=BD=AE=E9=A3=8E=E9=99=A9=E7=AD=89?= =?UTF-8?q?=E7=BA=A7=E9=98=88=E5=80=BC=E4=B8=8A=E4=B8=8B=E9=99=90=E9=80=BB?= =?UTF-8?q?=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/bean/entity/alert_rules.go | 6 ++--- src/controller/alert_rules.go | 18 +------------- src/service/alert_rules.go | 6 +++++ src/service/prometheusrule.go | 45 +++++++++++++++++++++++++++++++++- 4 files changed, 54 insertions(+), 21 deletions(-) diff --git a/src/bean/entity/alert_rules.go b/src/bean/entity/alert_rules.go index ef3f6f6..e4cc8ed 100644 --- a/src/bean/entity/alert_rules.go +++ b/src/bean/entity/alert_rules.go @@ -36,9 +36,9 @@ type RulesAlertRange struct { } type AlertCondition struct { - ThresholdsMin int `json:"thresholds_min" form:"thresholds_min" binding:"required,ltfield=ThresholdsMax"` - ThresholdsMax int `json:"thresholds_max" form:"thresholds_max" binding:"required"` - RiskLevel int `json:"risk_level" form:"risk_level" binding:"required,oneof=1 2 3 4"` + ThresholdsMin *float64 `json:"thresholds_min" form:"thresholds_min" binding:"omitempty,required_without=Id"` + ThresholdsMax *float64 `json:"thresholds_max" form:"thresholds_max" binding:"omitempty"` + RiskLevel int `json:"risk_level" form:"risk_level" binding:"required,oneof=1 2 3 4"` } type NotifyRecipients struct { diff --git a/src/controller/alert_rules.go b/src/controller/alert_rules.go index 9bea8b0..4bb8946 100644 --- a/src/controller/alert_rules.go +++ b/src/controller/alert_rules.go @@ -16,24 +16,8 @@ func AddAlertRules(c *gin.Context) { SendJsonResponse(c, resp.InvalidParam.TranslateError(err), nil) return } - - /* - [ - { - "thresholds_min": 12, - "thresholds_max": 66, - "risk_level": 1 - }, - { - "thresholds_min": 66, - "thresholds_max": 80, - "risk_level": 2 - } - ] - */ - // TODO 阈值范围判断,阈值上下限是否存在交叉 sort.SliceStable(req.AlertCondition, func(i, j int) bool { - return req.AlertCondition[i].ThresholdsMin < req.AlertCondition[j].ThresholdsMin + return *req.AlertCondition[i].ThresholdsMin < *req.AlertCondition[j].ThresholdsMin }) svc := service.AlertRulesSvc{User: header.GetUser(c)} diff --git a/src/service/alert_rules.go b/src/service/alert_rules.go index 2fa275b..749f101 100644 --- a/src/service/alert_rules.go +++ b/src/service/alert_rules.go @@ -200,9 +200,14 @@ func (a *AlertRulesSvc) UpdateIsEnabled(req request.UpdateIsEnabledAlertRules) e UpdatedBy: a.User.SystemAccount, UpdatedAt: now, } + if req.IsEnabled == 2 { + // TODO 关闭状态需要删除prometheus规则 + + } session := db.NewSession() defer session.Close() _, err = session.Table(data.TableName()).Cols("is_enabled,updated_by,updated_at").Where("id = ?", req.Id).Update(&data) + return err } @@ -274,6 +279,7 @@ func (a *AlertRulesSvc) Delete(ids []string) (err error) { } if !exist { _, err = db.NewSession().Where("id = ?", id).Delete(new(entity.AlertRules)) + // TODO 删除普罗米修斯规则 } else { return errors.New("alert_rules_id already exists in opensearch") } diff --git a/src/service/prometheusrule.go b/src/service/prometheusrule.go index 1b8b856..77daae5 100644 --- a/src/service/prometheusrule.go +++ b/src/service/prometheusrule.go @@ -52,7 +52,30 @@ func (p *PrometheusRuleSvc) Create(data response.AlertRulesItem) (err error) { "description": "", }, } - expr := fmt.Sprintf("%d <= %s <=%d", v.ThresholdsMin, data.Expr, v.ThresholdsMax) + + var ( + condition int + expr string + ) + + if v.ThresholdsMin != nil { + condition += 1 + } + if v.ThresholdsMax != nil { + condition += 2 + } + + switch condition { + default: + expr = data.Expr + case 1: + expr = fmt.Sprintf("%s <= %s", cast.ToString(v.ThresholdsMin), data.Expr) + case 2: + expr = fmt.Sprintf("%s <=%s", data.Expr, cast.ToString(v.ThresholdsMax)) + case 3: + expr = fmt.Sprintf("%s <= %s <=%s", cast.ToString(v.ThresholdsMin), data.Expr, cast.ToString(v.ThresholdsMax)) + } + rule.Expr = intstr.FromString(expr) group.Rules = append(group.Rules, rule) } @@ -69,3 +92,23 @@ func (p *PrometheusRuleSvc) Create(data response.AlertRulesItem) (err error) { } return } + +func (p *PrometheusRuleSvc) Delete(data response.AlertRulesItem) (err error) { + pr := monitoringv1.PrometheusRule{} + prometheusRuleName := k8s.GetPrometheusRuleName(data.Id) + pr.Name = prometheusRuleName + pr.Namespace = k8s.Namespace + pr.Labels = k8s.AlertDefLabels + + header := make(map[string]string) + header["Authorization"] = "Bearer " + conf.Options.KubernetesToken + prSvc := k8s.PrometheusRule{Header: header} + conf.Logger.Info("创建规则", zap.Any("pr", pr)) + err = prSvc.Delete(pr.Namespace, pr.Name) + if err != nil { + fmt.Println("删除失败" + err.Error()) + } else { + fmt.Println("删除成功") + } + return +} -- 2.26.0