Skip to content

Commit

Permalink
Fix logging & method naming inconsistency
Browse files Browse the repository at this point in the history
  • Loading branch information
yhabteab authored and julianbrost committed May 26, 2023
1 parent 8410cac commit 002d14d
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 29 deletions.
39 changes: 24 additions & 15 deletions internal/incident/incident.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,18 +269,25 @@ func (i *Incident) evaluateRules(eventID int64, causedBy types.Int) (types.Int,
i.Rules[r.ID] = struct{}{}
i.logger.Infof("Rule %q matches", r.Name)

err := i.AddRuleMatched(r)
if err != nil {
i.logger.Errorw("Failed to upsert incident rule", zap.String("rule", r.Name), zap.Error(err))

return types.Int{}, errors.New("failed to insert incident rule")
}

history := &HistoryRow{
Time: types.UnixMilli(time.Now()),
EventID: utils.ToDBInt(eventID),
RuleID: utils.ToDBInt(r.ID),
Type: RuleMatched,
CausedByIncidentHistoryID: causedBy,
}
insertedID, err := i.AddRuleMatchedHistory(r, history)
insertedID, err := i.AddHistory(history, true)
if err != nil {
i.logger.Errorw("Failed to add incident rule matched history", zap.String("rule", r.Name), zap.Error(err))
i.logger.Errorw("Failed to insert rule matched incident history", zap.String("rule", r.Name), zap.Error(err))

return types.Int{}, errors.New("failed to add incident rule matched history")
return types.Int{}, errors.New("failed to insert rule matched incident history")
}

if insertedID.Valid && !causedBy.Valid {
Expand Down Expand Up @@ -365,6 +372,16 @@ func (i *Incident) notifyContacts(ev *event.Event, causedBy types.Int) error {
r := i.runtimeConfig.Rules[escalation.RuleID]
i.logger.Infof("Rule %q reached escalation %q", r.Name, escalation.DisplayName())

err := i.AddEscalationTriggered(state)
if err != nil {
i.logger.Errorw(
"Failed to upsert escalation state", zap.String("rule", r.Name),
zap.String("escalation", escalation.DisplayName()), zap.Error(err),
)

return errors.New("failed to upsert escalation state")
}

history := &HistoryRow{
Time: state.TriggeredAt,
EventID: utils.ToDBInt(ev.ID),
Expand All @@ -373,27 +390,19 @@ func (i *Incident) notifyContacts(ev *event.Event, causedBy types.Int) error {
Type: EscalationTriggered,
CausedByIncidentHistoryID: causedBy,
}

causedByHistoryId, err := i.AddEscalationTriggered(state, history)
causedBy, err = i.AddHistory(history, true)
if err != nil {
i.logger.Errorw(
"Failed to add escalation triggered history", zap.String("rule", r.Name),
"Failed to insert escalation triggered incident history", zap.String("rule", r.Name),
zap.String("escalation", escalation.DisplayName()), zap.Error(err),
)

return errors.New("failed to add escalation triggered history")
return errors.New("failed to insert escalation triggered incident history")
}

causedBy = causedByHistoryId

err = i.AddRecipient(escalation, ev.ID)
if err != nil {
i.logger.Errorw(
"Failed to add incident recipients", zap.String("rule", r.Name), zap.String("escalation", escalation.DisplayName()),
zap.String("recipients", escalation.DisplayName()), zap.Error(err),
)

return errors.New("failed to add incident recipients")
return err
}
}

Expand Down
33 changes: 19 additions & 14 deletions internal/incident/sync.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package incident

import (
"fmt"
"errors"
"github.com/icinga/icinga-notifications/internal/event"
"github.com/icinga/icinga-notifications/internal/recipient"
"github.com/icinga/icinga-notifications/internal/rule"
"github.com/icinga/icinga-notifications/internal/utils"
"github.com/icinga/icingadb/pkg/types"
"go.uber.org/zap"
"time"
)

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

func (i *Incident) AddEscalationTriggered(state *EscalationState, hr *HistoryRow) (types.Int, error) {
func (i *Incident) AddEscalationTriggered(state *EscalationState) error {
state.IncidentID = i.incidentRowID

stmt, _ := i.db.BuildUpsertStmt(state)
_, err := i.db.NamedExec(stmt, state)
if err != nil {
return types.Int{}, err
}

return i.AddHistory(hr, true)
return err
}

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

_, err := i.AddHistory(hr, false)
if err != nil {
return err
i.logger.Errorw(
"Failed to insert recipient role changed incident history", zap.String("escalation", escalation.DisplayName()),
zap.String("recipients", r.String()), zap.Error(err),
)

return errors.New("failed to insert recipient role changed incident history")
}
}
cr.Role = state.Role
Expand All @@ -120,24 +123,26 @@ func (i *Incident) AddRecipient(escalation *rule.Escalation, eventId int64) erro
stmt, _ := i.db.BuildUpsertStmt(cr)
_, err := i.db.NamedExec(stmt, cr)
if err != nil {
return fmt.Errorf("failed to upsert incident contact %s: %w", r, err)
i.logger.Errorw(
"Failed to upsert incident recipient", zap.String("escalation", escalation.DisplayName()),
zap.String("recipient", r.String()), zap.Error(err),
)

return errors.New("failed to upsert incident recipient")
}
}

return nil
}

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

return i.AddHistory(hr, true)
return err
}

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

0 comments on commit 002d14d

Please sign in to comment.