Alerting: Add context to the logger in state manager Warm (#96228)

This commit is contained in:
Alexander Akhmetov 2024-11-12 19:41:05 +01:00 committed by GitHub
parent d4d9d56e9d
commit 580d073b96
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -124,16 +124,19 @@ func (st *Manager) Run(ctx context.Context) error {
}
func (st *Manager) Warm(ctx context.Context, rulesReader RuleReader, instanceReader InstanceReader) {
logger := st.log.FromContext(ctx)
if st.instanceStore == nil {
st.log.Info("Skip warming the state because instance store is not configured")
logger.Info("Skip warming the state because instance store is not configured")
return
}
startTime := time.Now()
st.log.Info("Warming state cache for startup")
logger.Info("Warming state cache for startup")
orgIds, err := instanceReader.FetchOrgIds(ctx)
if err != nil {
st.log.Error("Unable to fetch orgIds", "error", err)
logger.Error("Unable to fetch orgIds", "error", err)
}
statesCount := 0
@ -144,7 +147,7 @@ func (st *Manager) Warm(ctx context.Context, rulesReader RuleReader, instanceRea
}
alertRules, err := rulesReader.ListAlertRules(ctx, &ruleCmd)
if err != nil {
st.log.Error("Unable to fetch previous state", "error", err)
logger.Error("Unable to fetch previous state", "error", err)
}
ruleByUID := make(map[string]*ngModels.AlertRule, len(alertRules))
@ -158,7 +161,7 @@ func (st *Manager) Warm(ctx context.Context, rulesReader RuleReader, instanceRea
// We will not enforce this here, but it's convenient to emit the warning here as we load up all the rules.
for name, size := range groupSizes {
if st.rulesPerRuleGroupLimit > 0 && size > st.rulesPerRuleGroupLimit {
st.log.Warn(
logger.Warn(
"Large rule group was loaded. Large groups are discouraged and changes to them may be disallowed in the future.",
"limit", st.rulesPerRuleGroupLimit,
"actual", size,
@ -173,7 +176,7 @@ func (st *Manager) Warm(ctx context.Context, rulesReader RuleReader, instanceRea
}
alertInstances, err := instanceReader.ListAlertInstances(ctx, &cmd)
if err != nil {
st.log.Error("Unable to fetch previous state", "error", err)
logger.Error("Unable to fetch previous state", "error", err)
}
for _, entry := range alertInstances {
@ -195,7 +198,7 @@ func (st *Manager) Warm(ctx context.Context, rulesReader RuleReader, instanceRea
if entry.ResultFingerprint != "" {
fp, err := strconv.ParseUint(entry.ResultFingerprint, 16, 64)
if err != nil {
st.log.Error("Failed to parse result fingerprint of alert instance", "error", err, "ruleUID", entry.RuleUID)
logger.Error("Failed to parse result fingerprint of alert instance", "error", err, "ruleUID", entry.RuleUID)
}
resultFp = data.Fingerprint(fp)
}
@ -215,12 +218,12 @@ func (st *Manager) Warm(ctx context.Context, rulesReader RuleReader, instanceRea
ResolvedAt: entry.ResolvedAt,
LastSentAt: entry.LastSentAt,
}
st.cache.getOrAdd(state, st.log)
st.cache.getOrAdd(state, logger)
statesCount++
}
}
st.log.Info("State cache has been initialized", "states", statesCount, "duration", time.Since(startTime))
logger.Info("State cache has been initialized", "states", statesCount, "duration", time.Since(startTime))
}
func (st *Manager) Get(orgID int64, alertRuleUID string, stateId data.Fingerprint) *State {