Alerting: Fix state cache id duplication (#33480)

This commit is contained in:
Kyle Brandt
2021-04-28 11:42:19 -04:00
committed by GitHub
parent 7509c6cbc4
commit 914443c816
6 changed files with 53 additions and 32 deletions

View File

@@ -6,6 +6,7 @@ import (
"github.com/grafana/grafana-plugin-sdk-go/data"
"github.com/grafana/grafana/pkg/infra/log"
"github.com/grafana/grafana/pkg/services/ngalert/eval"
ngModels "github.com/grafana/grafana/pkg/services/ngalert/models"
prometheusModel "github.com/prometheus/common/model"
@@ -14,11 +15,13 @@ import (
type cache struct {
states map[string]*State
mtxStates sync.RWMutex
log log.Logger
}
func newCache() *cache {
func newCache(logger log.Logger) *cache {
return &cache{
states: make(map[string]*State),
log: logger,
}
}
@@ -32,7 +35,12 @@ func (c *cache) getOrCreate(alertRule *ngModels.AlertRule, result eval.Result) *
lbs[ngModels.NamespaceUIDLabel] = alertRule.NamespaceUID
lbs[prometheusModel.AlertNameLabel] = alertRule.Title
id := fmt.Sprintf("%s", map[string]string(lbs))
il := ngModels.InstanceLabels(lbs)
id, err := il.StringKey()
if err != nil {
c.log.Error("error getting cacheId for entry", "msg", err.Error())
}
if state, ok := c.states[id]; ok {
return state
}