Skip to content

Commit

Permalink
🚀 Supported millisecond level rule eval (#137)
Browse files Browse the repository at this point in the history
* 🚀 Supported millisecond level rule eval
  • Loading branch information
Cairry authored Feb 22, 2025
1 parent c4608e3 commit 432f786
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
14 changes: 12 additions & 2 deletions alert/eval/eval.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func (t *AlertRule) Stop(ruleId string) {
}

func (t *AlertRule) Eval(ctx context.Context, rule models.AlertRule) {
timer := time.NewTicker(time.Second * time.Duration(rule.EvalInterval))
timer := time.NewTicker(t.getEvalTimeDuration(rule.EvalTimeType, rule.EvalInterval))
defer func() {
timer.Stop()
if r := recover(); r != nil {
Expand Down Expand Up @@ -116,7 +116,17 @@ func (t *AlertRule) Eval(ctx context.Context, rule models.AlertRule) {
logc.Infof(t.ctx.Ctx, fmt.Sprintf("停止 RuleId: %v, RuleName: %s 的 Watch 协程", rule.RuleId, rule.RuleName))
return
}
timer.Reset(time.Second * time.Duration(rule.EvalInterval))
timer.Reset(t.getEvalTimeDuration(rule.EvalTimeType, rule.EvalInterval) * time.Duration(rule.EvalInterval))
}
}

// getEvalTimeDuration 获取评估时间
func (t *AlertRule) getEvalTimeDuration(evalTimeType string, evalInterval int64) time.Duration {
switch evalTimeType {
case "millisecond":
return time.Millisecond * time.Duration(evalInterval)
default:
return time.Second * time.Duration(evalInterval)
}
}

Expand Down
1 change: 1 addition & 0 deletions internal/models/rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ type AlertRule struct {
DatasourceIdList []string `json:"datasourceId" gorm:"datasourceId;serializer:json"`
RuleName string `json:"ruleName"`
EvalInterval int64 `json:"evalInterval"`
EvalTimeType string `json:"evalTimeType"` // second, millisecond
RepeatNoticeInterval int64 `json:"repeatNoticeInterval"`
Description string `json:"description"`
Labels LabelsMap `json:"labels" gorm:"labels;serializer:json"`
Expand Down

0 comments on commit 432f786

Please sign in to comment.