Skip to content

Commit

Permalink
Enhance logging
Browse files Browse the repository at this point in the history
  • Loading branch information
yhabteab authored and julianbrost committed May 10, 2023
1 parent ff0ae53 commit 17c2541
Show file tree
Hide file tree
Showing 11 changed files with 175 additions and 170 deletions.
31 changes: 20 additions & 11 deletions cmd/noma-daemon/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"flag"
"fmt"
"github.com/icinga/icingadb/pkg/logging"
"github.com/icinga/icingadb/pkg/utils"
"github.com/icinga/noma/internal/config"
"github.com/icinga/noma/internal/listener"
"go.uber.org/zap"
Expand All @@ -29,31 +30,39 @@ func main() {
os.Exit(1)
}

// TODO: proper logging config
logs, err := logging.NewLogging("noma", zap.DebugLevel, logging.CONSOLE, logging.Options{}, 10*time.Second)
logs, err := logging.NewLogging(
"noma",
conf.Logging.Level,
conf.Logging.Output,
conf.Logging.Options,
conf.Logging.Interval,
)
if err != nil {
_, _ = fmt.Fprintln(os.Stderr, "cannot initialize logging:", err)
os.Exit(1)
}

logger := logs.GetLogger()
logger.Info("connecting to database")
db, err := conf.Database.Open(logger)
db, err := conf.Database.Open(logs.GetChildLogger("database"))
if err != nil {
_, _ = fmt.Fprintln(os.Stderr, "cannot connect to database:", err)
os.Exit(1)
logger.Fatalw("cannot create database connection from config", zap.Error(err))
}
logger.Debugw("pinged database", zap.Error(db.Ping()))
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))
}
}

var runtimeConfig config.RuntimeConfig
if err := runtimeConfig.UpdateFromDatabase(context.TODO(), db, logger); err != nil {
runtimeConfig := config.NewRuntimeConfig(db, logs.GetChildLogger("runtime-updates"))
if err := runtimeConfig.UpdateFromDatabase(context.TODO()); err != nil {
logger.Fatalw("failed to load config from database", zap.Error(err))
}

go runtimeConfig.PeriodicUpdates(context.TODO(), db, logger, 1*time.Second)
go runtimeConfig.PeriodicUpdates(context.TODO(), 1*time.Second)

if err := listener.NewListener(db, conf, &runtimeConfig).Run(); err != nil {
if err := listener.NewListener(db, conf, runtimeConfig, logs.GetChildLogger("listener")).Run(); err != nil {
panic(err)
}
}
15 changes: 6 additions & 9 deletions internal/config/channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,25 @@ package config

import (
"context"
"github.com/icinga/icingadb/pkg/icingadb"
"github.com/icinga/icingadb/pkg/logging"
"github.com/icinga/noma/internal/channel"
"github.com/jmoiron/sqlx"
"go.uber.org/zap"
"log"
)

func (r *RuntimeConfig) fetchChannels(ctx context.Context, db *icingadb.DB, tx *sqlx.Tx, logger *logging.Logger) error {
func (r *RuntimeConfig) fetchChannels(ctx context.Context, tx *sqlx.Tx) error {
var channelPtr *channel.Channel
stmt := db.BuildSelectStmt(channelPtr, channelPtr)
log.Println(stmt)
stmt := r.db.BuildSelectStmt(channelPtr, channelPtr)
r.logger.Debugf("Executing query %q", stmt)

var channels []*channel.Channel
if err := tx.SelectContext(ctx, &channels, stmt); err != nil {
log.Println(err)
r.logger.Errorln(err)
return err
}

channelsByType := make(map[string]*channel.Channel)
for _, c := range channels {
channelLogger := logger.With(
channelLogger := r.logger.With(
zap.Int64("id", c.ID),
zap.String("name", c.Name),
zap.String("type", c.Type),
Expand Down Expand Up @@ -51,7 +48,7 @@ func (r *RuntimeConfig) fetchChannels(ctx context.Context, db *icingadb.DB, tx *
return nil
}

func (r *RuntimeConfig) applyPendingChannels(logger *logging.Logger) {
func (r *RuntimeConfig) applyPendingChannels() {
if r.Channels == nil {
r.Channels = make(map[string]*channel.Channel)
}
Expand Down
10 changes: 9 additions & 1 deletion internal/config/configfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ type ConfigFile struct {
DebugPassword string `yaml:"debug-password"`
Icingaweb2URL string `yaml:"icingaweb2-url"`
Database icingadbConfig.Database `yaml:"database"`
Logging icingadbConfig.Logging `yaml:"logging"`
}

func FromFile(path string) (*ConfigFile, error) {
Expand Down Expand Up @@ -40,5 +41,12 @@ func FromFile(path string) (*ConfigFile, error) {
}

func (c *ConfigFile) Validate() error {
return c.Database.Validate()
if err := c.Database.Validate(); err != nil {
return err
}
if err := c.Logging.Validate(); err != nil {
return err
}

return nil
}
15 changes: 6 additions & 9 deletions internal/config/contact.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,27 @@ package config

import (
"context"
"github.com/icinga/icingadb/pkg/icingadb"
"github.com/icinga/icingadb/pkg/logging"
"github.com/icinga/noma/internal/recipient"
"github.com/jmoiron/sqlx"
"go.uber.org/zap"
"log"
)

func (r *RuntimeConfig) fetchContacts(ctx context.Context, db *icingadb.DB, tx *sqlx.Tx, logger *logging.Logger) error {
func (r *RuntimeConfig) fetchContacts(ctx context.Context, tx *sqlx.Tx) error {
var contactPtr *recipient.Contact
stmt := db.BuildSelectStmt(contactPtr, contactPtr)
log.Println(stmt)
stmt := r.db.BuildSelectStmt(contactPtr, contactPtr)
r.logger.Debugf("Executing query %q", stmt)

var contacts []*recipient.Contact
if err := tx.SelectContext(ctx, &contacts, stmt); err != nil {
log.Println(err)
r.logger.Errorln(err)
return err
}

contactsByID := make(map[int64]*recipient.Contact)
for _, c := range contacts {
contactsByID[c.ID] = c

logger.Debugw("loaded contact config",
r.logger.Debugw("loaded contact config",
zap.Int64("id", c.ID),
zap.String("name", c.FullName))
}
Expand All @@ -44,7 +41,7 @@ func (r *RuntimeConfig) fetchContacts(ctx context.Context, db *icingadb.DB, tx *
return nil
}

func (r *RuntimeConfig) applyPendingContacts(logger *logging.Logger) {
func (r *RuntimeConfig) applyPendingContacts() {
if r.Contacts == nil {
r.Contacts = make(map[int64]*recipient.Contact)
}
Expand Down
37 changes: 17 additions & 20 deletions internal/config/contact_address.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,27 @@ package config

import (
"context"
"github.com/icinga/icingadb/pkg/icingadb"
"github.com/icinga/icingadb/pkg/logging"
"github.com/icinga/noma/internal/recipient"
"github.com/jmoiron/sqlx"
"go.uber.org/zap"
"golang.org/x/exp/slices"
"log"
)

func (r *RuntimeConfig) fetchContactAddresses(ctx context.Context, db *icingadb.DB, tx *sqlx.Tx, logger *logging.Logger) error {
func (r *RuntimeConfig) fetchContactAddresses(ctx context.Context, tx *sqlx.Tx) error {
var addressPtr *recipient.Address
stmt := db.BuildSelectStmt(addressPtr, addressPtr)
log.Println(stmt)
stmt := r.db.BuildSelectStmt(addressPtr, addressPtr)
r.logger.Debugf("Executing query %q", stmt)

var addresses []*recipient.Address
if err := tx.SelectContext(ctx, &addresses, stmt); err != nil {
log.Println(err)
r.logger.Errorln(err)
return err
}

addressesById := make(map[int64]*recipient.Address)
for _, a := range addresses {
addressesById[a.ID] = a
logger.Debugw("loaded contact_address config",
r.logger.Debugw("loaded contact_address config",
zap.Int64("id", a.ID),
zap.Int64("contact_id", a.ContactID),
zap.String("type", a.Type),
Expand All @@ -47,7 +44,7 @@ func (r *RuntimeConfig) fetchContactAddresses(ctx context.Context, db *icingadb.
return nil
}

func (r *RuntimeConfig) applyPendingContactAddresses(logger *logging.Logger) {
func (r *RuntimeConfig) applyPendingContactAddresses() {
if r.ContactAddresses == nil {
r.ContactAddresses = make(map[int64]*recipient.Address)
}
Expand All @@ -56,24 +53,24 @@ func (r *RuntimeConfig) applyPendingContactAddresses(logger *logging.Logger) {
currentAddress := r.ContactAddresses[id]

if pendingAddress == nil {
r.removeContactAddress(logger, currentAddress)
r.removeContactAddress(currentAddress)
} else if currentAddress != nil {
r.updateContactAddress(logger, currentAddress, pendingAddress)
r.updateContactAddress(currentAddress, pendingAddress)
} else {
r.addContactAddress(logger, pendingAddress)
r.addContactAddress(pendingAddress)
}
}

r.pending.ContactAddresses = nil
}

func (r *RuntimeConfig) addContactAddress(logger *logging.Logger, addr *recipient.Address) {
func (r *RuntimeConfig) addContactAddress(addr *recipient.Address) {
contact := r.Contacts[addr.ContactID]
if contact != nil {
if i := slices.Index(contact.Addresses, addr); i < 0 {
contact.Addresses = append(contact.Addresses, addr)

logger.Debugw("added new address to contact",
r.logger.Debugw("added new address to contact",
zap.Any("contact", contact),
zap.Any("address", addr))
}
Expand All @@ -82,30 +79,30 @@ func (r *RuntimeConfig) addContactAddress(logger *logging.Logger, addr *recipien
r.ContactAddresses[addr.ID] = addr
}

func (r *RuntimeConfig) updateContactAddress(logger *logging.Logger, addr, pending *recipient.Address) {
func (r *RuntimeConfig) updateContactAddress(addr, pending *recipient.Address) {
contactChanged := addr.ContactID != pending.ContactID

if contactChanged {
r.removeContactAddress(logger, addr)
r.removeContactAddress(addr)
}

addr.ContactID = pending.ContactID
addr.Type = pending.Type
addr.Address = pending.Address

if contactChanged {
r.addContactAddress(logger, addr)
r.addContactAddress(addr)
}

logger.Debugw("updated contact address", zap.Any("address", addr))
r.logger.Debugw("updated contact address", zap.Any("address", addr))
}

func (r *RuntimeConfig) removeContactAddress(logger *logging.Logger, addr *recipient.Address) {
func (r *RuntimeConfig) removeContactAddress(addr *recipient.Address) {
if contact := r.Contacts[addr.ContactID]; contact != nil {
if i := slices.Index(contact.Addresses, addr); i >= 0 {
contact.Addresses = slices.Delete(contact.Addresses, i, i+1)

logger.Debugw("removed address from contact",
r.logger.Debugw("removed address from contact",
zap.Any("contact", contact),
zap.Any("address", addr))
}
Expand Down
23 changes: 10 additions & 13 deletions internal/config/group.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,27 @@ package config

import (
"context"
"github.com/icinga/icingadb/pkg/icingadb"
"github.com/icinga/icingadb/pkg/logging"
"github.com/icinga/noma/internal/recipient"
"github.com/jmoiron/sqlx"
"go.uber.org/zap"
"log"
)

func (r *RuntimeConfig) fetchGroups(ctx context.Context, db *icingadb.DB, tx *sqlx.Tx, logger *logging.Logger) error {
func (r *RuntimeConfig) fetchGroups(ctx context.Context, tx *sqlx.Tx) error {
var groupPtr *recipient.Group
stmt := db.BuildSelectStmt(groupPtr, groupPtr)
log.Println(stmt)
stmt := r.db.BuildSelectStmt(groupPtr, groupPtr)
r.logger.Debugf("Executing query %q", stmt)

var groups []*recipient.Group
if err := tx.SelectContext(ctx, &groups, stmt); err != nil {
log.Println(err)
r.logger.Errorln(err)
return err
}

groupsById := make(map[int64]*recipient.Group)
for _, g := range groups {
groupsById[g.ID] = g

logger.Debugw("loaded group config",
r.logger.Debugw("loaded group config",
zap.Int64("id", g.ID),
zap.String("name", g.Name))
}
Expand All @@ -36,17 +33,17 @@ func (r *RuntimeConfig) fetchGroups(ctx context.Context, db *icingadb.DB, tx *sq
}

var memberPtr *ContactgroupMember
stmt = db.BuildSelectStmt(memberPtr, memberPtr)
log.Println(stmt)
stmt = r.db.BuildSelectStmt(memberPtr, memberPtr)
r.logger.Debugf("Executing query %q", stmt)

var members []*ContactgroupMember
if err := tx.SelectContext(ctx, &members, stmt); err != nil {
log.Println(err)
r.logger.Errorln(err)
return err
}

for _, m := range members {
memberLogger := logger.With(
memberLogger := r.logger.With(
zap.Int64("contact_id", m.ContactId),
zap.Int64("contactgroup_id", m.GroupId),
)
Expand Down Expand Up @@ -75,7 +72,7 @@ func (r *RuntimeConfig) fetchGroups(ctx context.Context, db *icingadb.DB, tx *sq
return nil
}

func (r *RuntimeConfig) applyPendingGroups(logger *logging.Logger) {
func (r *RuntimeConfig) applyPendingGroups() {
if r.Groups == nil {
r.Groups = make(map[int64]*recipient.Group)
}
Expand Down
Loading

0 comments on commit 17c2541

Please sign in to comment.