mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Alerting: Create AlertInstanceKey in one place (#58278)
* use method GetAlertInstanceKey * do not add key if error
This commit is contained in:
parent
1fb37b54b3
commit
623de12e35
@ -291,18 +291,13 @@ func (st *Manager) saveAlertStates(ctx context.Context, logger log.Logger, state
|
|||||||
instances := make([]ngModels.AlertInstance, 0, len(states))
|
instances := make([]ngModels.AlertInstance, 0, len(states))
|
||||||
|
|
||||||
for _, s := range states {
|
for _, s := range states {
|
||||||
labels := ngModels.InstanceLabels(s.Labels)
|
key, err := s.GetAlertInstanceKey()
|
||||||
_, hash, err := labels.StringAndHash()
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Error("Failed to create a key for alert state to save it to database. The state will be ignored ", "cacheID", s.CacheID, "error", err)
|
logger.Error("Failed to create a key for alert state to save it to database. The state will be ignored ", "cacheID", s.CacheID, "error", err)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
fields := ngModels.AlertInstance{
|
fields := ngModels.AlertInstance{
|
||||||
AlertInstanceKey: ngModels.AlertInstanceKey{
|
AlertInstanceKey: key,
|
||||||
RuleOrgID: s.OrgID,
|
|
||||||
RuleUID: s.AlertRuleUID,
|
|
||||||
LabelsHash: hash,
|
|
||||||
},
|
|
||||||
Labels: ngModels.InstanceLabels(s.Labels),
|
Labels: ngModels.InstanceLabels(s.Labels),
|
||||||
CurrentState: ngModels.InstanceStateType(s.State.State.String()),
|
CurrentState: ngModels.InstanceStateType(s.State.State.String()),
|
||||||
CurrentReason: s.StateReason,
|
CurrentReason: s.StateReason,
|
||||||
@ -352,13 +347,13 @@ func (st *Manager) staleResultsHandler(ctx context.Context, evaluatedAt time.Tim
|
|||||||
if _, ok := states[s.CacheID]; !ok && stateIsStale(evaluatedAt, s.LastEvaluationTime, alertRule.IntervalSeconds) {
|
if _, ok := states[s.CacheID]; !ok && stateIsStale(evaluatedAt, s.LastEvaluationTime, alertRule.IntervalSeconds) {
|
||||||
logger.Info("Removing stale state entry", "cacheID", s.CacheID, "state", s.State, "reason", s.StateReason)
|
logger.Info("Removing stale state entry", "cacheID", s.CacheID, "state", s.State, "reason", s.StateReason)
|
||||||
st.cache.deleteEntry(s.OrgID, s.AlertRuleUID, s.CacheID)
|
st.cache.deleteEntry(s.OrgID, s.AlertRuleUID, s.CacheID)
|
||||||
ilbs := ngModels.InstanceLabels(s.Labels)
|
|
||||||
_, labelsHash, err := ilbs.StringAndHash()
|
|
||||||
if err != nil {
|
|
||||||
logger.Error("Unable to get labelsHash", "error", err.Error(), s.AlertRuleUID)
|
|
||||||
}
|
|
||||||
|
|
||||||
toDelete = append(toDelete, ngModels.AlertInstanceKey{RuleOrgID: s.OrgID, RuleUID: s.AlertRuleUID, LabelsHash: labelsHash})
|
key, err := s.GetAlertInstanceKey()
|
||||||
|
if err != nil {
|
||||||
|
logger.Error("Unable to get alert instance key to delete it from database. Ignoring", "error", err.Error())
|
||||||
|
} else {
|
||||||
|
toDelete = append(toDelete, key)
|
||||||
|
}
|
||||||
|
|
||||||
if s.State == eval.Alerting {
|
if s.State == eval.Alerting {
|
||||||
oldState := s.State
|
oldState := s.State
|
||||||
|
@ -73,6 +73,15 @@ func (a *State) GetRuleKey() models.AlertRuleKey {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (a *State) GetAlertInstanceKey() (models.AlertInstanceKey, error) {
|
||||||
|
instanceLabels := models.InstanceLabels(a.Labels)
|
||||||
|
_, labelsHash, err := instanceLabels.StringAndHash()
|
||||||
|
if err != nil {
|
||||||
|
return models.AlertInstanceKey{}, err
|
||||||
|
}
|
||||||
|
return models.AlertInstanceKey{RuleOrgID: a.OrgID, RuleUID: a.AlertRuleUID, LabelsHash: labelsHash}, nil
|
||||||
|
}
|
||||||
|
|
||||||
// StateTransition describes the transition from one state to another.
|
// StateTransition describes the transition from one state to another.
|
||||||
type StateTransition struct {
|
type StateTransition struct {
|
||||||
*State
|
*State
|
||||||
|
Loading…
Reference in New Issue
Block a user