From 557c764477776b8aab7577e960b56883483878d2 Mon Sep 17 00:00:00 2001 From: Cairry <115769353+Cairry@users.noreply.github.com> Date: Thu, 28 Nov 2024 09:44:44 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=9A=A7=20Fix=20goroutine=20panic=20of=20t?= =?UTF-8?q?he=20rule=20eval?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- alert/eval/eval.go | 8 +++++++- pkg/provider/metrics.go | 3 ++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/alert/eval/eval.go b/alert/eval/eval.go index f3ec1a8..53151a2 100644 --- a/alert/eval/eval.go +++ b/alert/eval/eval.go @@ -64,7 +64,13 @@ 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)) - defer timer.Stop() + defer func() { + timer.Stop() + if r := recover(); r != nil { + logc.Error(t.ctx.Ctx, fmt.Sprintf("Recovered from rule eval goroutine panic: %s", r)) + } + }() + for { select { case <-timer.C: diff --git a/pkg/provider/metrics.go b/pkg/provider/metrics.go index 07b8264..f73aad3 100644 --- a/pkg/provider/metrics.go +++ b/pkg/provider/metrics.go @@ -1,6 +1,7 @@ package provider import ( + "fmt" "strconv" "watchAlert/pkg/tools" ) @@ -31,7 +32,7 @@ func (m Metrics) GetFingerprint() string { for labelName, labelValue := range m.Metric { sum := tools.HashNew() sum = tools.HashAdd(sum, labelName) - sum = tools.HashAdd(sum, labelValue.(string)) + sum = tools.HashAdd(sum, fmt.Sprintf("%v", labelValue)) result ^= sum }