Skip to content

Commit

Permalink
Consistent logging
Browse files Browse the repository at this point in the history
  • Loading branch information
yhabteab committed Dec 11, 2023
1 parent 10d5b1b commit 79aa254
Show file tree
Hide file tree
Showing 21 changed files with 92 additions and 123 deletions.
6 changes: 3 additions & 3 deletions cmd/channel/email/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func (ch *Email) SendNotification(req *plugin.NotificationRequest) error {
}

if len(to) == 0 {
return fmt.Errorf("contact user %s doesn't have an e-mail address", req.Contact.FullName)
return fmt.Errorf("contact user %s does not have an e-mail address", req.Contact.FullName)
}

var msg bytes.Buffer
Expand Down Expand Up @@ -68,12 +68,12 @@ func (ch *Email) SetConfig(jsonStr json.RawMessage) error {
if ch.From == "" {
hostname, err := os.Hostname()
if err != nil {
return fmt.Errorf("failed to get the os's hostname: %w", err)
return fmt.Errorf("failed to get the OS hostname: %w", err)
}

usr, err := user.Current()
if err != nil {
return fmt.Errorf("failed to get the os's current user: %w", err)
return fmt.Errorf("failed to get the OS current user: %w", err)
}

ch.From = usr.Username + "@" + hostname
Expand Down
4 changes: 2 additions & 2 deletions cmd/channel/rocketchat/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func (ch *RocketChat) SendNotification(req *plugin.NotificationRequest) error {
}

if roomId == "" {
return fmt.Errorf("contact user %s doesn't specify a rocketchat channel or username", req.Contact.FullName)
return fmt.Errorf("contact user %s does not specify a rocketchat channel or username", req.Contact.FullName)
}

message := struct {
Expand Down Expand Up @@ -64,7 +64,7 @@ func (ch *RocketChat) SendNotification(req *plugin.NotificationRequest) error {
client := &http.Client{Timeout: 10 * time.Second}
resp, err := client.Do(request)
if err != nil {
return fmt.Errorf("error while sending http request to rocketchat server: %s", err)
return fmt.Errorf("error while sending http request to rocketchat server: %w", err)
}

defer resp.Body.Close()
Expand Down
8 changes: 4 additions & 4 deletions cmd/icinga-notifications-daemon/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,13 @@ func main() {
logger.Infof("Starting Icinga Notifications daemon (%s)", internal.Version.Version)
db, err := conf.Database.Open(logs.GetChildLogger("database"))
if err != nil {
logger.Fatalw("cannot create database connection from config", zap.Error(err))
logger.Fatalw("Cannot create database connection from config", zap.Error(err))
}
defer db.Close()
{
logger.Infof("Connecting to database at '%s'", utils.JoinHostPort(conf.Database.Host, conf.Database.Port))
if err := db.Ping(); err != nil {
logger.Fatalw("cannot connect to database", zap.Error(err))
logger.Fatalw("Cannot connect to database", zap.Error(err))
}
}

Expand All @@ -87,14 +87,14 @@ func main() {

runtimeConfig := config.NewRuntimeConfig(db, logs)
if err := runtimeConfig.UpdateFromDatabase(ctx); err != nil {
logger.Fatalw("failed to load config from database", zap.Error(err))
logger.Fatalw("Failed to load config from database", zap.Error(err))
}

go runtimeConfig.PeriodicUpdates(ctx, 1*time.Second)

err = incident.LoadOpenIncidents(ctx, db, logs.GetChildLogger("incident"), runtimeConfig)
if err != nil {
logger.Fatalw("Can't load incidents from database", zap.Error(err))
logger.Fatalw("Cannot load incidents from database", zap.Error(err))
}

if err := listener.NewListener(db, runtimeConfig, logs).Run(ctx); err != nil {
Expand Down
4 changes: 2 additions & 2 deletions internal/config/channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@ func (r *RuntimeConfig) fetchChannels(ctx context.Context, tx *sqlx.Tx) error {
zap.String("type", c.Type),
)
if channelsById[c.ID] != nil {
channelLogger.Warnw("ignoring duplicate config for channel type")
channelLogger.Warnw("Ignoring duplicate config for channel type")
} else if err := channel.ValidateType(c.Type); err != nil {
channelLogger.Errorw("Cannot load channel config", zap.Error(err))
} else {
channelsById[c.ID] = c

channelLogger.Debugw("loaded channel config")
channelLogger.Debugw("Successfully loaded channel config")
}
}

Expand Down
2 changes: 1 addition & 1 deletion internal/config/contact.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func (r *RuntimeConfig) fetchContacts(ctx context.Context, tx *sqlx.Tx) error {
for _, c := range contacts {
contactsByID[c.ID] = c

r.logger.Debugw("loaded contact config",
r.logger.Debugw("Successfully loaded contact config",
zap.Int64("id", c.ID),
zap.String("name", c.FullName))
}
Expand Down
4 changes: 2 additions & 2 deletions internal/config/contact_address.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func (r *RuntimeConfig) fetchContactAddresses(ctx context.Context, tx *sqlx.Tx)
addressesById := make(map[int64]*recipient.Address)
for _, a := range addresses {
addressesById[a.ID] = a
r.logger.Debugw("loaded contact_address config",
r.logger.Debugw("Successfully loaded contact_address config",
zap.Int64("id", a.ID),
zap.Int64("contact_id", a.ContactID),
zap.String("type", a.Type),
Expand Down Expand Up @@ -70,7 +70,7 @@ func (r *RuntimeConfig) addContactAddress(addr *recipient.Address) {
if i := slices.Index(contact.Addresses, addr); i < 0 {
contact.Addresses = append(contact.Addresses, addr)

r.logger.Debugw("added new address to contact",
r.logger.Debugw("Successfully added new address to contact",
zap.Any("contact", contact),
zap.Any("address", addr))
}
Expand Down
6 changes: 3 additions & 3 deletions internal/config/group.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func (r *RuntimeConfig) fetchGroups(ctx context.Context, tx *sqlx.Tx) error {
for _, g := range groups {
groupsById[g.ID] = g

r.logger.Debugw("loaded group config",
r.logger.Debugw("Successfully loaded group config",
zap.Int64("id", g.ID),
zap.String("name", g.Name))
}
Expand All @@ -49,11 +49,11 @@ func (r *RuntimeConfig) fetchGroups(ctx context.Context, tx *sqlx.Tx) error {
)

if g := groupsById[m.GroupId]; g == nil {
memberLogger.Warnw("ignoring member for unknown contactgroup_id")
memberLogger.Warnw("Ignoring member for unknown contactgroup_id")
} else {
g.MemberIDs = append(g.MemberIDs, m.ContactId)

memberLogger.Debugw("loaded contact group member",
memberLogger.Debugw("Successfully loaded contact group member",
zap.String("contactgroup_name", g.Name))
}
}
Expand Down
26 changes: 13 additions & 13 deletions internal/config/rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func (r *RuntimeConfig) fetchRules(ctx context.Context, tx *sqlx.Tx) error {
if ru.ObjectFilterExpr.Valid {
f, err := filter.Parse(ru.ObjectFilterExpr.String)
if err != nil {
ruleLogger.Warnw("ignoring rule as parsing object_filter failed", zap.Error(err))
ruleLogger.Warnw("Ignoring rule as parsing object_filter failed", zap.Error(err))
continue
}

Expand All @@ -42,7 +42,7 @@ func (r *RuntimeConfig) fetchRules(ctx context.Context, tx *sqlx.Tx) error {
ru.Escalations = make(map[int64]*rule.Escalation)

rulesByID[ru.ID] = ru
ruleLogger.Debugw("loaded rule config")
ruleLogger.Debugw("Successfully loaded rule config")
}

var escalationPtr *rule.Escalation
Expand All @@ -67,14 +67,14 @@ func (r *RuntimeConfig) fetchRules(ctx context.Context, tx *sqlx.Tx) error {

rule := rulesByID[escalation.RuleID]
if rule == nil {
escalationLogger.Warnw("ignoring escalation for unknown rule_id")
escalationLogger.Warnw("Ignoring escalation for unknown rule_id")
continue
}

if escalation.ConditionExpr.Valid {
cond, err := filter.Parse(escalation.ConditionExpr.String)
if err != nil {
escalationLogger.Warnw("ignoring escalation, failed to parse condition", zap.Error(err))
escalationLogger.Warnw("Ignoring escalation, failed to parse condition", zap.Error(err))
continue
}

Expand All @@ -83,7 +83,7 @@ func (r *RuntimeConfig) fetchRules(ctx context.Context, tx *sqlx.Tx) error {

if escalation.FallbackForID.Valid {
// TODO: implement fallbacks (needs extra validation: mismatching rule_id, cycles)
escalationLogger.Warnw("ignoring fallback escalation (not yet implemented)")
escalationLogger.Warnw("Ignoring fallback escalation (not yet implemented)")
continue
}

Expand All @@ -93,7 +93,7 @@ func (r *RuntimeConfig) fetchRules(ctx context.Context, tx *sqlx.Tx) error {

rule.Escalations[escalation.ID] = escalation
escalationsByID[escalation.ID] = escalation
escalationLogger.Debugw("loaded escalation config")
escalationLogger.Debugw("Successfully loaded escalation config")
}

var recipientPtr *rule.EscalationRecipient
Expand All @@ -114,10 +114,10 @@ func (r *RuntimeConfig) fetchRules(ctx context.Context, tx *sqlx.Tx) error {

escalation := escalationsByID[recipient.EscalationID]
if escalation == nil {
recipientLogger.Warnw("ignoring recipient for unknown escalation")
recipientLogger.Warnw("Ignoring recipient for unknown escalation")
} else {
escalation.Recipients = append(escalation.Recipients, recipient)
recipientLogger.Debugw("loaded escalation recipient config")
recipientLogger.Debugw("Successfully loaded escalation recipient config")
}
}

Expand Down Expand Up @@ -153,7 +153,7 @@ func (r *RuntimeConfig) applyPendingRules() {

if pendingRule.TimePeriodID.Valid {
if p := r.TimePeriods[pendingRule.TimePeriodID.Int64]; p == nil {
ruleLogger.Warnw("ignoring rule with unknown timeperiod_id")
ruleLogger.Warnw("Ignoring rule with unknown timeperiod_id")
continue
} else {
pendingRule.TimePeriod = p
Expand All @@ -173,7 +173,7 @@ func (r *RuntimeConfig) applyPendingRules() {
if c := r.Contacts[id]; c != nil {
recipient.Recipient = c
} else {
recipientLogger.Warnw("ignoring unknown escalation recipient")
recipientLogger.Warnw("Ignoring unknown escalation recipient")
escalation.Recipients[i] = nil
}
} else if recipient.GroupID.Valid {
Expand All @@ -182,7 +182,7 @@ func (r *RuntimeConfig) applyPendingRules() {
if g := r.Groups[id]; g != nil {
recipient.Recipient = g
} else {
recipientLogger.Warnw("ignoring unknown escalation recipient")
recipientLogger.Warnw("Ignoring unknown escalation recipient")
escalation.Recipients[i] = nil
}
} else if recipient.ScheduleID.Valid {
Expand All @@ -191,11 +191,11 @@ func (r *RuntimeConfig) applyPendingRules() {
if s := r.Schedules[id]; s != nil {
recipient.Recipient = s
} else {
recipientLogger.Warnw("ignoring unknown escalation recipient")
recipientLogger.Warnw("Ignoring unknown escalation recipient")
escalation.Recipients[i] = nil
}
} else {
recipientLogger.Warnw("ignoring unknown escalation recipient")
recipientLogger.Warnw("Ignoring unknown escalation recipient")
escalation.Recipients[i] = nil
}
}
Expand Down
12 changes: 6 additions & 6 deletions internal/config/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,10 @@ func (r *RuntimeConfig) PeriodicUpdates(ctx context.Context, interval time.Durat
for {
select {
case <-ticker.C:
r.logger.Debug("periodically updating config")
r.logger.Debug("Periodically updating config")
err := r.UpdateFromDatabase(ctx)
if err != nil {
r.logger.Errorw("periodic config update failed, continuing with previous config", zap.Error(err))
r.logger.Errorw("Failed periodic config update, continuing with previous config", zap.Error(err))
}
case <-ctx.Done():
return
Expand Down Expand Up @@ -182,7 +182,7 @@ func (r *RuntimeConfig) GetSourceFromCredentials(user, pass string, logger *logg
}

func (r *RuntimeConfig) fetchFromDatabase(ctx context.Context) error {
r.logger.Debug("fetching configuration from database")
r.logger.Debug("Fetching configuration from database")
start := time.Now()

// Reset all pending state to start from a clean state.
Expand Down Expand Up @@ -214,7 +214,7 @@ func (r *RuntimeConfig) fetchFromDatabase(ctx context.Context) error {
}
}

r.logger.Debugw("fetched configuration from database", zap.Duration("took", time.Since(start)))
r.logger.Debugw("Fetched configuration from database", zap.Duration("took", time.Since(start)))

return nil
}
Expand All @@ -223,7 +223,7 @@ func (r *RuntimeConfig) applyPending() {
r.mu.Lock()
defer r.mu.Unlock()

r.logger.Debug("applying pending configuration")
r.logger.Debug("Applying pending configuration")
start := time.Now()

r.applyPendingChannels()
Expand All @@ -235,5 +235,5 @@ func (r *RuntimeConfig) applyPending() {
r.applyPendingRules()
r.applyPendingSources()

r.logger.Debugw("applied pending configuration", zap.Duration("took", time.Since(start)))
r.logger.Debugw("Successfully applied pending configuration", zap.Duration("took", time.Since(start)))
}
12 changes: 6 additions & 6 deletions internal/config/schedule.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func (r *RuntimeConfig) fetchSchedules(ctx context.Context, tx *sqlx.Tx) error {
for _, g := range schedules {
schedulesById[g.ID] = g

r.logger.Debugw("loaded schedule config",
r.logger.Debugw("Successfully loaded schedule config",
zap.Int64("id", g.ID),
zap.String("name", g.Name))
}
Expand All @@ -41,11 +41,11 @@ func (r *RuntimeConfig) fetchSchedules(ctx context.Context, tx *sqlx.Tx) error {
memberLogger := makeScheduleMemberLogger(r.logger.SugaredLogger, member)

if s := schedulesById[member.ScheduleID]; s == nil {
memberLogger.Warnw("ignoring schedule member for unknown schedule_id")
memberLogger.Warnw("Ignoring schedule member for unknown schedule_id")
} else {
s.MemberRows = append(s.MemberRows, member)

memberLogger.Debugw("member")
memberLogger.Debugw("Found member")
}
}

Expand Down Expand Up @@ -77,15 +77,15 @@ func (r *RuntimeConfig) applyPendingSchedules() {

period := r.TimePeriods[memberRow.TimePeriodID]
if period == nil {
memberLogger.Warnw("ignoring schedule member for unknown timeperiod_id")
memberLogger.Warnw("Ignoring schedule member for unknown timeperiod_id")
continue
}

var contact *recipient.Contact
if memberRow.ContactID.Valid {
contact = r.Contacts[memberRow.ContactID.Int64]
if contact == nil {
memberLogger.Warnw("ignoring schedule member for unknown contact_id")
memberLogger.Warnw("Ignoring schedule member for unknown contact_id")
continue
}
}
Expand All @@ -94,7 +94,7 @@ func (r *RuntimeConfig) applyPendingSchedules() {
if memberRow.GroupID.Valid {
group = r.Groups[memberRow.GroupID.Int64]
if group == nil {
memberLogger.Warnw("ignoring schedule member for unknown contactgroup_id")
memberLogger.Warnw("Ignoring schedule member for unknown contactgroup_id")
continue
}
}
Expand Down
4 changes: 2 additions & 2 deletions internal/config/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ func (r *RuntimeConfig) fetchSources(ctx context.Context, tx *sqlx.Tx) error {
zap.String("type", s.Type),
)
if sourcesById[s.ID] != nil {
sourceLogger.Warnw("ignoring duplicate config for source ID")
sourceLogger.Warnw("Ignoring duplicate config for source ID")
} else {
sourcesById[s.ID] = s

sourceLogger.Debugw("loaded source config")
sourceLogger.Debugw("Successfully loaded source config")
}
}

Expand Down
8 changes: 4 additions & 4 deletions internal/config/timeperiod.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func (r *RuntimeConfig) fetchTimePeriods(ctx context.Context, tx *sqlx.Tx) error
for _, row := range entries {
p := timePeriodsById[row.TimePeriodID]
if p == nil {
r.logger.Warnw("ignoring entry for unknown timeperiod_id",
r.logger.Warnw("Ignoring entry for unknown timeperiod_id",
zap.Int64("timeperiod_entry_id", row.ID),
zap.Int64("timeperiod_id", row.TimePeriodID))
continue
Expand All @@ -64,7 +64,7 @@ func (r *RuntimeConfig) fetchTimePeriods(ctx context.Context, tx *sqlx.Tx) error

loc, err := time.LoadLocation(row.Timezone)
if err != nil {
r.logger.Warnw("ignoring time period entry with unknown timezone",
r.logger.Warnw("Ignoring time period entry with unknown timezone",
zap.Int64("timeperiod_entry_id", row.ID),
zap.String("timezone", row.Timezone),
zap.Error(err))
Expand All @@ -83,7 +83,7 @@ func (r *RuntimeConfig) fetchTimePeriods(ctx context.Context, tx *sqlx.Tx) error

err = entry.Init()
if err != nil {
r.logger.Warnw("ignoring time period entry",
r.logger.Warnw("Ignoring time period entry",
zap.Int64("timeperiod_entry_id", row.ID),
zap.String("rrule", entry.RecurrenceRule),
zap.Error(err))
Expand All @@ -92,7 +92,7 @@ func (r *RuntimeConfig) fetchTimePeriods(ctx context.Context, tx *sqlx.Tx) error

p.Entries = append(p.Entries, entry)

r.logger.Debugw("loaded time period entry",
r.logger.Debugw("Successfully loaded time period entry",
zap.String("timeperiod", p.Name),
zap.Time("start", entry.Start),
zap.Time("end", entry.End),
Expand Down
Loading

0 comments on commit 79aa254

Please sign in to comment.