Skip to content

Commit

Permalink
Remove zerolog dependency
Browse files Browse the repository at this point in the history
The zerolog functionality is not worth the effort to maintain the dependency
tree brought by this package from the packaging point of view. We can always
revert this is it turns out that we need performance capabilities of the library.
  • Loading branch information
paramite committed Aug 13, 2019
1 parent 2e7541e commit b31364d
Show file tree
Hide file tree
Showing 10 changed files with 441 additions and 76 deletions.
13 changes: 0 additions & 13 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 0 additions & 4 deletions Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,6 @@
name = "github.com/go-ini/ini"
version = "1.44.0"

[[constraint]]
name = "github.com/rs/zerolog"
version = "1.14.3"

[[constraint]]
branch = "master"
name = "github.com/streadway/amqp"
Expand Down
28 changes: 15 additions & 13 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"strings"

"github.com/go-ini/ini"
"github.com/rs/zerolog"
"github.com/paramite/collectd-sensubility/logging"
)

const (
Expand All @@ -34,7 +34,7 @@ type Section struct {
}

type Config struct {
log zerolog.Logger
log *logging.Logger
metadata map[string][]Parameter
Sections map[string]*Section
}
Expand Down Expand Up @@ -170,7 +170,7 @@ func validate(value string, validators []Validator) error {
return nil
}

func NewConfig(metadata map[string][]Parameter, logger zerolog.Logger) (*Config, error) {
func NewConfig(metadata map[string][]Parameter, logger *logging.Logger) (*Config, error) {
var conf Config
conf.metadata = metadata
conf.log = logger
Expand Down Expand Up @@ -204,17 +204,19 @@ func (conf *Config) Parse(path string) error {
return fmt.Errorf("Failed to validate parameter %s. %s", param.Name, err.Error())
}
conf.Sections[sectionName].Options[param.Name].value = paramData.Value()
conf.log.Debug().
Str("section", sectionName).
Str("option", param.Name).
Str("value", paramData.Value()).
Msg("Using parsed configuration value.")
conf.log.Metadata(map[string]interface{}{
"section": sectionName,
"option": param.Name,
"value": paramData.Value(),
})
conf.log.Debug("Using parsed configuration value.")
} else {
conf.log.Debug().
Str("section", sectionName).
Str("option", param.Name).
Str("value", conf.Sections[sectionName].Options[param.Name].value).
Msg("Using default configuration value.")
conf.log.Metadata(map[string]interface{}{
"section": sectionName,
"option": param.Name,
"value": conf.Sections[sectionName].Options[param.Name].value,
})
conf.log.Debug("Using default configuration value.")
}
}
}
Expand Down
131 changes: 131 additions & 0 deletions logging/logger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
package logging

import (
"fmt"
"os"
"strings"
"time"
)

type LogLevel int

const (
DEBUG LogLevel = iota
INFO
WARN
ERROR
)

func (self LogLevel) String() string {
return [...]string{"DEBUG", "INFO", "WARN", "ERROR"}[self]
}

type Logger struct {
Level LogLevel
Timestamp bool
metadata map[string]interface{}
logfile *os.File
}

func NewLogger(level LogLevel, path string) (*Logger, error) {
var logger Logger
logger.Level = level
logger.Timestamp = false
logger.metadata = make(map[string]interface{})

logfile, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
if err != nil {
return nil, err
}
logger.logfile = logfile

return &logger, nil
}

func (self *Logger) Destroy() error {
return self.logfile.Close()
}

func (self *Logger) Metadata(metadata map[string]interface{}) {
self.metadata = metadata
}

func (self *Logger) formatMetadata() (string, error) {
var build strings.Builder
if len(self.metadata) > 0 {
joiner := ""
for key, item := range self.metadata {
_, err := fmt.Fprintf(&build, "%s%s: %v", joiner, key, item)
if err != nil {
return build.String(), err
}
if len(joiner) == 0 {
joiner = ", "
}
}
}
// clear metadata for next use
self.metadata = make(map[string]interface{})
return build.String(), nil
}

func (self *Logger) writeRecord(level LogLevel, message string) error {
metadata, err := self.formatMetadata()
if err != nil {
return err
}

var build strings.Builder
if self.Timestamp {
_, err = build.WriteString(time.Now().Format("2006-01-02 15:04:05 "))
}

_, err = build.WriteString(fmt.Sprintf("[%s] ", level))
if err != nil {
return nil
}
_, err = build.WriteString(message)
if err != nil {
return nil
}
if len(metadata) > 0 {
_, err = build.WriteString(fmt.Sprintf(" [%s]", metadata))
if err != nil {
return nil
}
}
_, err = build.WriteString("\n")
if err != nil {
return nil
}
_, err = self.logfile.WriteString(build.String())
return err
}

func (self *Logger) Debug(message string) error {
if self.Level == DEBUG {
return self.writeRecord(DEBUG, message)
}
return nil
}

func (self *Logger) Info(message string) error {
if self.Level <= INFO {
return self.writeRecord(INFO, message)
}
return nil
}

func (self *Logger) Warn(message string) error {
if self.Level <= WARN {
return self.writeRecord(WARN, message)
}
return nil
}

func (self *Logger) Error(message string) error {
if self.Level <= ERROR {
return self.writeRecord(ERROR, message)
}
return nil
}
44 changes: 25 additions & 19 deletions main/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,38 +6,41 @@ import (
"os"

"github.com/paramite/collectd-sensubility/config"
"github.com/paramite/collectd-sensubility/logging"
"github.com/paramite/collectd-sensubility/sensu"
"github.com/rs/zerolog"
)

const DEFAULT_CONFIG_PATH = "/etc/collectd-sensubility.conf"

func main() {
debug := flag.Bool("debug", false, "enables debugging logs")
verbose := flag.Bool("verbose", false, "enables debugging logs")
verbose := flag.Bool("verbose", false, "enables informational logs")
silent := flag.Bool("silent", false, "disables all logs except fatal errors")
logpath := flag.String("log", "/var/log/collectd/sensubility.log", "path to log file")
flag.Parse()

// set logging
logfile, err := os.OpenFile(*logpath, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
defer logfile.Close()
level := logging.WARN
if *verbose {
level = logging.INFO
} else if *debug {
level = logging.DEBUG
} else if *silent {
level = logging.ERROR
}
log, err := logging.NewLogger(level, *logpath)
if err != nil {
fmt.Printf("Failed to open log file %s.\n", *logpath)
os.Exit(2)
}
zerolog.SetGlobalLevel(zerolog.WarnLevel)
if *verbose {
zerolog.SetGlobalLevel(zerolog.InfoLevel)
} else if *debug {
zerolog.SetGlobalLevel(zerolog.DebugLevel)
}
log := zerolog.New(logfile).With().Timestamp().Logger()
defer log.Destroy()

// spawn entities
metadata := config.GetAgentConfigMetadata()
cfg, err := config.NewConfig(metadata, log.With().Str("component", "config-parser").Logger())
cfg, err := config.NewConfig(metadata, *log)
if err != nil {
log.Fatal().Err(err).Msg("Failed to parse config file.")
log.Metadata(map[string]interface{}{"error": err})
log.Error("Failed to parse config file.")
os.Exit(2)
}
confPath := os.Getenv("COLLECTD_SENSUBILITY_CONFIG")
Expand All @@ -48,22 +51,25 @@ func main() {
if err != nil {
panic(err.Error())
}
sensuConnector, err := sensu.NewConnector(cfg, log.With().Str("component", "sensu-connector").Logger())
sensuConnector, err := sensu.NewConnector(cfg, *log)
if err != nil {
log.Fatal().Err(err).Msg("Failed to spawn RabbitMQ connector.")
log.Metadata(map[string]interface{}{"error": err})
log.Error("Failed to spawn RabbitMQ connector.")
os.Exit(2)
}
defer sensuConnector.Disconnect()

sensuScheduler, err := sensu.NewScheduler(cfg, log.With().Str("component", "sensu-scheduler").Logger())
sensuScheduler, err := sensu.NewScheduler(cfg, *log)
if err != nil {
log.Fatal().Err(err).Msg("Failed to spawn check scheduler.")
log.Metadata(map[string]interface{}{"error": err})
log.Error("Failed to spawn check scheduler.")
os.Exit(2)
}

sensuExecutor, err := sensu.NewExecutor(cfg, log.With().Str("component", "sensu-executor").Logger())
sensuExecutor, err := sensu.NewExecutor(cfg, log)
if err != nil {
log.Fatal().Err(err).Msg("Failed to spawn check executor.")
log.Metadata(map[string]interface{}{"error": err})
log.Error("Failed to spawn check executor.")
os.Exit(2)
}
defer sensuExecutor.Clean()
Expand Down
24 changes: 15 additions & 9 deletions sensu/connector.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"time"

"github.com/paramite/collectd-sensubility/config"
"github.com/rs/zerolog"
"github.com/paramite/collectd-sensubility/logging"
"github.com/streadway/amqp"
)

Expand Down Expand Up @@ -35,7 +35,7 @@ type Connector struct {
ClientName string
ClientAddress string
KeepaliveInterval int
log zerolog.Logger
log *logging.Logger
queueName string
exchangeName string
inConnection *amqp.Connection
Expand All @@ -46,7 +46,7 @@ type Connector struct {
consumer <-chan amqp.Delivery
}

func NewConnector(cfg *config.Config, logger zerolog.Logger) (*Connector, error) {
func NewConnector(cfg *config.Config, logger *logging.Logger) (*Connector, error) {
var connector Connector
connector.Address = cfg.Sections["sensu"].Options["connection"].GetString()
connector.Subscription = cfg.Sections["sensu"].Options["subscriptions"].GetStrings(",")
Expand Down Expand Up @@ -167,7 +167,8 @@ func (self *Connector) Start(outchan chan interface{}, inchan chan interface{})
if err == nil {
outchan <- request
} else {
self.log.Warn().Err(err).Bytes("request-body", req.Body).Msg("Failed to unmarshal request body.")
self.log.Metadata(map[string]interface{}{"error": err, "request-body": req.Body})
self.log.Warn("Failed to unmarshal request body.")
}
}
}()
Expand All @@ -179,7 +180,8 @@ func (self *Connector) Start(outchan chan interface{}, inchan chan interface{})
case Result:
body, err := json.Marshal(result)
if err != nil {
self.log.Error().Err(err).Msg("Failed to marshal execution result.")
self.log.Metadata(map[string]interface{}{"error": err})
self.log.Error("Failed to marshal execution result.")
continue
}
err = self.outChannel.Publish(
Expand All @@ -196,10 +198,12 @@ func (self *Connector) Start(outchan chan interface{}, inchan chan interface{})
Priority: 0, // 0-9
})
if err != nil {
self.log.Error().Err(err).Msg("Failed to publish execution result.")
self.log.Metadata(map[string]interface{}{"error": err})
self.log.Error("Failed to publish execution result.")
}
default:
self.log.Error().Str("type", fmt.Sprintf("%t", res)).Msg("Received execution result with invalid type.")
self.log.Metadata(map[string]interface{}{"type": fmt.Sprintf("%t", res)})
self.log.Error("Received execution result with invalid type.")
}
}
}()
Expand All @@ -215,7 +219,8 @@ func (self *Connector) Start(outchan chan interface{}, inchan chan interface{})
Timestamp: time.Now().Unix(),
})
if err != nil {
self.log.Error().Err(err).Msg("Failed to marshal keepalive body.")
self.log.Metadata(map[string]interface{}{"error": err})
self.log.Error("Failed to marshal keepalive body.")
continue
}
err = self.outChannel.Publish(
Expand All @@ -232,7 +237,8 @@ func (self *Connector) Start(outchan chan interface{}, inchan chan interface{})
Priority: 0, // 0-9
})
if err != nil {
self.log.Error().Err(err).Msg("Failed to publish keepalive body.")
self.log.Metadata(map[string]interface{}{"error": err})
self.log.Error("Failed to publish keepalive body.")
}
time.Sleep(time.Duration(self.KeepaliveInterval) * time.Second)
}
Expand Down
Loading

0 comments on commit b31364d

Please sign in to comment.