From 54271b78ad955a71af19bfe7ad9d410c2069fb45 Mon Sep 17 00:00:00 2001 From: --global <1422928955@qq.com> Date: Wed, 16 Dec 2020 21:36:57 +0800 Subject: [PATCH] feat(monitor): add alert slience time and modify scoperesource FetchUniqValues func MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1.增加报警静默时间 2.后端修改 scoperesource FetchUniqValues :回显scope 属性 --- pkg/apis/monitor/alertrecord.go | 6 ++++-- pkg/apis/monitor/commalert.go | 18 ++++++++++++------ pkg/cloudcommon/db/scoperesource.go | 2 +- pkg/monitor/alerting/eval_context.go | 2 +- pkg/monitor/alerting/notifier.go | 2 ++ pkg/monitor/alerting/rule.go | 1 + pkg/monitor/models/commonalert.go | 9 ++++++++- 7 files changed, 29 insertions(+), 11 deletions(-) diff --git a/pkg/apis/monitor/alertrecord.go b/pkg/apis/monitor/alertrecord.go index 66b5e266bb..d452bb0dee 100644 --- a/pkg/apis/monitor/alertrecord.go +++ b/pkg/apis/monitor/alertrecord.go @@ -42,6 +42,8 @@ type AlertRecordRule struct { // 比较运算符, 比如: >, <, >=, <= Comparator string `json:"comparator"` // 报警阀值 - Threshold string `json:"threshold"` - Period string `json:"period"` + Threshold string `json:"threshold"` + Period string `json:"period"` + AlertDuration int64 `json:"alert_duration"` + ConditionType string `json:"condition_type"` } diff --git a/pkg/apis/monitor/commalert.go b/pkg/apis/monitor/commalert.go index 4a8a8af0f9..80a97becb0 100644 --- a/pkg/apis/monitor/commalert.go +++ b/pkg/apis/monitor/commalert.go @@ -36,6 +36,8 @@ type CommonAlertCreateInput struct { // 查询指标周期 Period string `json:"period"` + // 报警连续持续周期数 + AlertDuration int64 `json:"alert_duration"` // 通知方式, 比如: email, mobile Channel []string `json:"channel"` // 通知接受者 @@ -89,6 +91,8 @@ type CommonAlertUpdateInput struct { // 查询指标周期 Period string `json:"period"` + // 报警连续持续周期数 + AlertDuration int64 `json:"alert_duration"` // 通知方式, 比如: email, mobile Channel []string `json:"channel"` // 通知接受者 @@ -101,12 +105,14 @@ type CommonAlertUpdateInput struct { type CommonAlertDetails struct { AlertDetails - Period string `json:"period"` - Level string `json:"level"` - NotifierId string `json:"notifier_id"` - Channel []string `json:"channel"` - Recipients []string `json:"recipients"` - Status string `json:"status"` + Period string `json:"period"` + // 报警连续持续周期数 + AlertDuration int64 `json:"alert_duration"` + Level string `json:"level"` + NotifierId string `json:"notifier_id"` + Channel []string `json:"channel"` + Recipients []string `json:"recipients"` + Status string `json:"status"` // 报警类型 AlertType string `json:"alert_type"` CommonAlertMetricDetails []*CommonAlertMetricDetails `json:"common_alert_metric_details"` diff --git a/pkg/cloudcommon/db/scoperesource.go b/pkg/cloudcommon/db/scoperesource.go index b2cf58c218..9a2c4f1686 100644 --- a/pkg/cloudcommon/db/scoperesource.go +++ b/pkg/cloudcommon/db/scoperesource.go @@ -54,7 +54,6 @@ func (m *SScopedResourceBaseManager) FetchUniqValues(ctx context.Context, data j uniqValues := sUniqValues{} switch parentScope { case rbacutils.ScopeSystem: - uniqValues.Scope = scope case rbacutils.ScopeDomain: domain, _ := data.GetString("project_domain") uniqValues.Domain = domain @@ -62,6 +61,7 @@ func (m *SScopedResourceBaseManager) FetchUniqValues(ctx context.Context, data j project, _ := data.GetString("project") uniqValues.Project = project } + uniqValues.Scope = scope return jsonutils.Marshal(uniqValues) } diff --git a/pkg/monitor/alerting/eval_context.go b/pkg/monitor/alerting/eval_context.go index 5086f7fa67..071e15ae72 100644 --- a/pkg/monitor/alerting/eval_context.go +++ b/pkg/monitor/alerting/eval_context.go @@ -139,7 +139,7 @@ func (c *EvalContext) GetNewState() monitor.AlertStateType { } since := time.Since(c.Rule.LastStateChange) - if c.PrevAlertState == monitor.AlertStatePending && since > c.Rule.For { + if c.PrevAlertState == monitor.AlertStatePending && since >= c.Rule.For { return monitor.AlertStateAlerting } diff --git a/pkg/monitor/alerting/notifier.go b/pkg/monitor/alerting/notifier.go index 8133247212..930f02d055 100644 --- a/pkg/monitor/alerting/notifier.go +++ b/pkg/monitor/alerting/notifier.go @@ -228,5 +228,7 @@ func newAlertRecordRule(evalCtx *EvalContext) monitor.AlertRecordRule { alertRule.Period = fmt.Sprintf("%dm", evalCtx.Rule.Frequency/60) } + alertRule.AlertDuration = int64(evalCtx.Rule.For)/evalCtx.Rule.Frequency + 1 + return alertRule } diff --git a/pkg/monitor/alerting/rule.go b/pkg/monitor/alerting/rule.go index d22d17edd1..3e7ab02a80 100644 --- a/pkg/monitor/alerting/rule.go +++ b/pkg/monitor/alerting/rule.go @@ -177,6 +177,7 @@ func newRuleDescription(rule *Rule, alertDetails *monitor.CommonAlertMetricDetai FieldDesc: alertDetails.FieldDescription.DisplayName, Comparator: alertDetails.Comparator, Threshold: RationalizeValueFromUnit(alertDetails.Threshold, alertDetails.FieldDescription.Unit, ""), + ConditionType: alertDetails.ConditionType, }, } rule.RuleDescription = append(rule.RuleDescription, &ruleDes) diff --git a/pkg/monitor/models/commonalert.go b/pkg/monitor/models/commonalert.go index 858e85f9a8..7aa879c1be 100644 --- a/pkg/monitor/models/commonalert.go +++ b/pkg/monitor/models/commonalert.go @@ -89,6 +89,9 @@ func (man *SCommonAlertManager) ValidateCreateData( if data.Period == "" { data.Period = "5m" } + if data.AlertDuration == 0 { + data.AlertDuration = 1 + } if data.Name == "" { return data, merrors.NewArgIsEmptyErr("name") } @@ -478,7 +481,7 @@ func (alert *SCommonAlert) GetMoreDetails(ctx context.Context, out monitor.Commo } else { out.Period = fmt.Sprintf("%dm", alert.Frequency/60) } - + out.AlertDuration = alert.For/alert.Frequency + 1 err = alert.getCommonAlertMetricDetails(&out) if err != nil { return out, err @@ -626,6 +629,9 @@ func getMetricDescriptionDetails(metricDetails *monitor.CommonAlertMetricDetails } if fieldDes, ok := influxdbMeasurements[0].FieldDescriptions[field]; ok { metricDetails.FieldDescription = fieldDes + if metricDetails.FieldDescription.Unit == monitor.METRIC_UNIT_COUNT { + metricDetails.FieldDescription.Unit = "" + } if len(metricDetails.FieldOpt) != 0 { metricDetails.FieldDescription.Name = metricDetails.Field metricDetails.FieldDescription.DisplayName = metricDetails.Field @@ -661,6 +667,7 @@ func (man *SCommonAlertManager) toAlertCreatInput(input monitor.CommonAlertCreat ret := new(monitor.AlertCreateInput) ret.Name = input.Name ret.Frequency = int64(freq / time.Second) + ret.For = ret.Frequency * (input.AlertDuration - 1) ret.Level = input.Level //ret.Settings =monitor.AlertSetting{} for _, metricquery := range input.CommonMetricInputQuery.MetricQuery {