Skip to content

Commit 0136efd

Browse files
author
Cairry
committed
♻️ Remove alert rule EvalTimeType field and add validate func
1 parent 0ba77d4 commit 0136efd

File tree

4 files changed

+21
-20
lines changed

4 files changed

+21
-20
lines changed

alert/eval/eval.go

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,6 @@ const (
3030
DatasourceTypeCloudWatch = "CloudWatch"
3131
DatasourceTypeKubernetesEvent = "KubernetesEvent"
3232

33-
// 时间类型
34-
TimeTypeMillisecond = "millisecond"
35-
TimeTypeSecond = "second"
36-
3733
// 默认恢复等待时间
3834
DefaultRecoverWaitTime = 1
3935

@@ -103,8 +99,14 @@ func (t *AlertRule) Restart(rule models.AlertRule) {
10399
}
104100

105101
func (t *AlertRule) Eval(ctx context.Context, rule models.AlertRule) {
102+
err := rule.Validate()
103+
if err != nil {
104+
logc.Errorf(t.ctx.Ctx, fmt.Sprintf("Rule validation failed, RuleName: %s, RuleId: %s, Error: %v", rule.RuleName, rule.RuleId, err))
105+
return
106+
}
107+
106108
taskChan := make(chan struct{}, TaskChannelBufferSize)
107-
timer := time.NewTicker(t.getEvalTimeDuration(rule.EvalTimeType, rule.EvalInterval))
109+
timer := time.NewTicker(t.getEvalTimeDuration(rule.EvalInterval))
108110
defer func() {
109111
timer.Stop()
110112
if r := recover(); r != nil {
@@ -125,7 +127,7 @@ func (t *AlertRule) Eval(ctx context.Context, rule models.AlertRule) {
125127
logc.Infof(t.ctx.Ctx, fmt.Sprintf("停止 RuleId: %v, RuleName: %s 的 Watch 协程", rule.RuleId, rule.RuleName))
126128
return
127129
}
128-
timer.Reset(t.getEvalTimeDuration(rule.EvalTimeType, rule.EvalInterval))
130+
timer.Reset(t.getEvalTimeDuration(rule.EvalInterval))
129131
}
130132
}
131133

@@ -214,13 +216,8 @@ func (t *AlertRule) processSingleDatasource(dsId string, rule models.AlertRule)
214216
}
215217

216218
// getEvalTimeDuration 获取评估时间间隔
217-
func (t *AlertRule) getEvalTimeDuration(evalTimeType string, evalInterval int64) time.Duration {
218-
switch evalTimeType {
219-
case TimeTypeMillisecond:
220-
return time.Duration(evalInterval) * time.Millisecond
221-
default:
222-
return time.Duration(evalInterval) * time.Second
223-
}
219+
func (t *AlertRule) getEvalTimeDuration(evalInterval int64) time.Duration {
220+
return time.Duration(evalInterval) * time.Second
224221
}
225222

226223
func (t *AlertRule) Recover(tenantId, ruleId string, eventCacheKey models.AlertEventCacheKey, faultCenterInfoKey models.FaultCenterInfoCacheKey, curFingerprints []string) {

internal/models/rule.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
package models
22

3+
import (
4+
"fmt"
5+
)
6+
37
type AlertRule struct {
48
//gorm.Model
59
TenantId string `json:"tenantId"`
@@ -10,7 +14,6 @@ type AlertRule struct {
1014
DatasourceIdList []string `json:"datasourceId" gorm:"datasourceId;serializer:json"`
1115
RuleName string `json:"ruleName"`
1216
EvalInterval int64 `json:"evalInterval"`
13-
EvalTimeType string `json:"evalTimeType"` // second, millisecond
1417
RepeatNoticeInterval int64 `json:"repeatNoticeInterval"`
1518
Description string `json:"description"`
1619
EffectiveTime EffectiveTime `json:"effectiveTime" gorm:"effectiveTime;serializer:json"`
@@ -173,3 +176,10 @@ func (a *AlertRule) GetForDuration(severity string) int64 {
173176
}
174177
return 0
175178
}
179+
180+
func (t *AlertRule) Validate() error {
181+
if t.EvalInterval < 5 {
182+
return fmt.Errorf("EvalInterval must be greater than 5")
183+
}
184+
return nil
185+
}

internal/services/rule.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ func (rs ruleService) Create(req interface{}) (interface{}, interface{}) {
5252
DatasourceIdList: r.DatasourceIdList,
5353
RuleName: r.RuleName,
5454
EvalInterval: r.EvalInterval,
55-
EvalTimeType: r.EvalTimeType,
5655
RepeatNoticeInterval: r.RepeatNoticeInterval,
5756
Description: r.Description,
5857
EffectiveTime: r.EffectiveTime,
@@ -133,7 +132,6 @@ func (rs ruleService) Update(req interface{}) (interface{}, interface{}) {
133132
DatasourceIdList: r.DatasourceIdList,
134133
RuleName: r.RuleName,
135134
EvalInterval: r.EvalInterval,
136-
EvalTimeType: r.EvalTimeType,
137135
RepeatNoticeInterval: r.RepeatNoticeInterval,
138136
Description: r.Description,
139137
EffectiveTime: r.EffectiveTime,
@@ -336,7 +334,6 @@ func (rs ruleService) Import(req interface{}) (interface{}, interface{}) {
336334
DatasourceIdList: r.DatasourceIdList,
337335
RuleName: alert.Alert,
338336
EvalInterval: 15,
339-
EvalTimeType: "second",
340337
PrometheusConfig: models.PrometheusConfig{
341338
PromQL: alert.Expr,
342339
Annotations: alert.Annotations.Description,
@@ -379,7 +376,6 @@ func (rs ruleService) Import(req interface{}) (interface{}, interface{}) {
379376
DatasourceIdList: rule.DatasourceIdList,
380377
RuleName: rule.RuleName,
381378
EvalInterval: rule.EvalInterval,
382-
EvalTimeType: rule.EvalTimeType,
383379
RepeatNoticeInterval: rule.RepeatNoticeInterval,
384380
Description: rule.Description,
385381
EffectiveTime: rule.EffectiveTime,

internal/types/rule.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ type RequestRuleCreate struct {
1010
DatasourceIdList []string `json:"datasourceId"`
1111
RuleName string `json:"ruleName"`
1212
EvalInterval int64 `json:"evalInterval"`
13-
EvalTimeType string `json:"evalTimeType"` // second, millisecond
1413
RepeatNoticeInterval int64 `json:"repeatNoticeInterval"`
1514
Description string `json:"description"`
1615
EffectiveTime models.EffectiveTime `json:"effectiveTime"`
@@ -47,7 +46,6 @@ type RequestRuleUpdate struct {
4746
DatasourceIdList []string `json:"datasourceId"`
4847
RuleName string `json:"ruleName"`
4948
EvalInterval int64 `json:"evalInterval"`
50-
EvalTimeType string `json:"evalTimeType"` // second, millisecond
5149
RepeatNoticeInterval int64 `json:"repeatNoticeInterval"`
5250
Description string `json:"description"`
5351
EffectiveTime models.EffectiveTime `json:"effectiveTime"`

0 commit comments

Comments
 (0)