@@ -269,18 +269,25 @@ func (i *Incident) evaluateRules(eventID int64, causedBy types.Int) (types.Int,
269269 i .Rules [r .ID ] = struct {}{}
270270 i .logger .Infof ("Rule %q matches" , r .Name )
271271
272+ err := i .AddRuleMatched (r )
273+ if err != nil {
274+ i .logger .Errorw ("Failed to upsert incident rule" , zap .String ("rule" , r .Name ), zap .Error (err ))
275+
276+ return types.Int {}, errors .New ("failed to insert incident rule" )
277+ }
278+
272279 history := & HistoryRow {
273280 Time : types .UnixMilli (time .Now ()),
274281 EventID : utils .ToDBInt (eventID ),
275282 RuleID : utils .ToDBInt (r .ID ),
276283 Type : RuleMatched ,
277284 CausedByIncidentHistoryID : causedBy ,
278285 }
279- insertedID , err := i .AddRuleMatchedHistory ( r , history )
286+ insertedID , err := i .AddHistory ( history , true )
280287 if err != nil {
281- i .logger .Errorw ("Failed to add incident rule matched history" , zap .String ("rule" , r .Name ), zap .Error (err ))
288+ i .logger .Errorw ("Failed to insert rule matched incident history" , zap .String ("rule" , r .Name ), zap .Error (err ))
282289
283- return types.Int {}, errors .New ("failed to add incident rule matched history" )
290+ return types.Int {}, errors .New ("failed to insert rule matched incident history" )
284291 }
285292
286293 if insertedID .Valid && ! causedBy .Valid {
@@ -365,6 +372,16 @@ func (i *Incident) notifyContacts(ev *event.Event, causedBy types.Int) error {
365372 r := i .runtimeConfig .Rules [escalation .RuleID ]
366373 i .logger .Infof ("Rule %q reached escalation %q" , r .Name , escalation .DisplayName ())
367374
375+ err := i .AddEscalationTriggered (state )
376+ if err != nil {
377+ i .logger .Errorw (
378+ "Failed to upsert escalation state" , zap .String ("rule" , r .Name ),
379+ zap .String ("escalation" , escalation .DisplayName ()), zap .Error (err ),
380+ )
381+
382+ return errors .New ("failed to upsert escalation state" )
383+ }
384+
368385 history := & HistoryRow {
369386 Time : state .TriggeredAt ,
370387 EventID : utils .ToDBInt (ev .ID ),
@@ -373,27 +390,19 @@ func (i *Incident) notifyContacts(ev *event.Event, causedBy types.Int) error {
373390 Type : EscalationTriggered ,
374391 CausedByIncidentHistoryID : causedBy ,
375392 }
376-
377- causedByHistoryId , err := i .AddEscalationTriggered (state , history )
393+ causedBy , err = i .AddHistory (history , true )
378394 if err != nil {
379395 i .logger .Errorw (
380- "Failed to add escalation triggered history" , zap .String ("rule" , r .Name ),
396+ "Failed to insert escalation triggered incident history" , zap .String ("rule" , r .Name ),
381397 zap .String ("escalation" , escalation .DisplayName ()), zap .Error (err ),
382398 )
383399
384- return errors .New ("failed to add escalation triggered history" )
400+ return errors .New ("failed to insert escalation triggered incident history" )
385401 }
386402
387- causedBy = causedByHistoryId
388-
389403 err = i .AddRecipient (escalation , ev .ID )
390404 if err != nil {
391- i .logger .Errorw (
392- "Failed to add incident recipients" , zap .String ("rule" , r .Name ), zap .String ("escalation" , escalation .DisplayName ()),
393- zap .String ("recipients" , escalation .DisplayName ()), zap .Error (err ),
394- )
395-
396- return errors .New ("failed to add incident recipients" )
405+ return err
397406 }
398407 }
399408
0 commit comments