Skip to content

Commit 002d14d

Browse files
yhabteabjulianbrost
authored andcommitted
Fix logging & method naming inconsistency
1 parent 8410cac commit 002d14d

File tree

2 files changed

+43
-29
lines changed

2 files changed

+43
-29
lines changed

internal/incident/incident.go

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -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

internal/incident/sync.go

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
package incident
22

33
import (
4-
"fmt"
4+
"errors"
55
"github.com/icinga/icinga-notifications/internal/event"
66
"github.com/icinga/icinga-notifications/internal/recipient"
77
"github.com/icinga/icinga-notifications/internal/rule"
88
"github.com/icinga/icinga-notifications/internal/utils"
99
"github.com/icinga/icingadb/pkg/types"
10+
"go.uber.org/zap"
1011
"time"
1112
)
1213

@@ -53,16 +54,13 @@ func (i *Incident) AddHistory(historyRow *HistoryRow, fetchId bool) (types.Int,
5354
return types.Int{}, nil
5455
}
5556

56-
func (i *Incident) AddEscalationTriggered(state *EscalationState, hr *HistoryRow) (types.Int, error) {
57+
func (i *Incident) AddEscalationTriggered(state *EscalationState) error {
5758
state.IncidentID = i.incidentRowID
5859

5960
stmt, _ := i.db.BuildUpsertStmt(state)
6061
_, err := i.db.NamedExec(stmt, state)
61-
if err != nil {
62-
return types.Int{}, err
63-
}
6462

65-
return i.AddHistory(hr, true)
63+
return err
6664
}
6765

6866
// AddEvent Inserts incident history record to the database and returns an error on db failure.
@@ -111,7 +109,12 @@ func (i *Incident) AddRecipient(escalation *rule.Escalation, eventId int64) erro
111109

112110
_, err := i.AddHistory(hr, false)
113111
if err != nil {
114-
return err
112+
i.logger.Errorw(
113+
"Failed to insert recipient role changed incident history", zap.String("escalation", escalation.DisplayName()),
114+
zap.String("recipients", r.String()), zap.Error(err),
115+
)
116+
117+
return errors.New("failed to insert recipient role changed incident history")
115118
}
116119
}
117120
cr.Role = state.Role
@@ -120,24 +123,26 @@ func (i *Incident) AddRecipient(escalation *rule.Escalation, eventId int64) erro
120123
stmt, _ := i.db.BuildUpsertStmt(cr)
121124
_, err := i.db.NamedExec(stmt, cr)
122125
if err != nil {
123-
return fmt.Errorf("failed to upsert incident contact %s: %w", r, err)
126+
i.logger.Errorw(
127+
"Failed to upsert incident recipient", zap.String("escalation", escalation.DisplayName()),
128+
zap.String("recipient", r.String()), zap.Error(err),
129+
)
130+
131+
return errors.New("failed to upsert incident recipient")
124132
}
125133
}
126134

127135
return nil
128136
}
129137

130-
// AddRuleMatchedHistory syncs the given *rule.Rule and history entry to the database.
138+
// AddRuleMatched syncs the given *rule.Rule to the database.
131139
// Returns an error on database failure.
132-
func (i *Incident) AddRuleMatchedHistory(r *rule.Rule, hr *HistoryRow) (types.Int, error) {
140+
func (i *Incident) AddRuleMatched(r *rule.Rule) error {
133141
rr := &RuleRow{IncidentID: i.incidentRowID, RuleID: r.ID}
134142
stmt, _ := i.db.BuildUpsertStmt(rr)
135143
_, err := i.db.NamedExec(stmt, rr)
136-
if err != nil {
137-
return types.Int{}, err
138-
}
139144

140-
return i.AddHistory(hr, true)
145+
return err
141146
}
142147

143148
func (i *Incident) AddSourceSeverity(severity event.Severity, sourceID int64) error {

0 commit comments

Comments
 (0)