Skip to content

Commit

Permalink
Log config values on startup (prebid#33)
Browse files Browse the repository at this point in the history
* Logged all the config values on app startup.

* Renamed some stuff, and fixed a missing validation step.
  • Loading branch information
dbemiller authored May 30, 2018
1 parent 5297936 commit 88a7001
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 5 deletions.
39 changes: 39 additions & 0 deletions config/backends.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package config

import log "github.com/Sirupsen/logrus"

type Backend struct {
Type BackendType `mapstructure:"type"`
Aerospike Aerospike `mapstructure:"aerospike"`
Expand All @@ -8,6 +10,23 @@ type Backend struct {
Memcache Memcache `mapstructure:"memcache"`
}

func (cfg *Backend) validateAndLog() {
log.Infof("config.backend.type: %s", cfg.Type)
switch cfg.Type {
case BackendAerospike:
cfg.Aerospike.validateAndLog()
case BackendAzure:
cfg.Azure.validateAndLog()
case BackendCassandra:
cfg.Cassandra.validateAndLog()
case BackendMemcache:
cfg.Memcache.validateAndLog()
case BackendMemory:
default:
log.Fatalf(`invalid config.backend.type: %s. It must be "aerospike", "azure", "cassandra", "memcache", or "memory".`, cfg.Type)
}
}

type BackendType string

const (
Expand All @@ -24,16 +43,36 @@ type Aerospike struct {
Namespace string `mapstructure:"namespace"`
}

func (cfg *Aerospike) validateAndLog() {
log.Infof("config.backend.aerospike.host: %s", cfg.Host)
log.Infof("config.backend.aerospike.port: %d", cfg.Port)
log.Infof("config.backend.aerospike.namespace: %s", cfg.Namespace)
}

type Azure struct {
Account string `mapstructure:"account"`
Key string `mapstructure:"key"`
}

func (cfg *Azure) validateAndLog() {
log.Infof("config.backend.azure.account: %s", cfg.Account)
log.Infof("config.backend.azure.key: %s", cfg.Key)
}

type Cassandra struct {
Hosts string `mapstructure:"hosts"`
Keyspace string `mapstructure:"keyspace"`
}

func (cfg *Cassandra) validateAndLog() {
log.Infof("config.backend.cassandra.hosts: %s", cfg.Hosts)
log.Infof("config.backend.cassandra.keyspace: %s", cfg.Keyspace)
}

type Memcache struct {
Hosts []string `mapstructure:"hosts"`
}

func (cfg *Memcache) validateAndLog() {
log.Infof("config.backend.memcache.hosts: %v", cfg.Hosts)
}
56 changes: 56 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,27 @@ type Configuration struct {
Metrics Metrics `mapstructure:"metrics"`
}

// ValidateAndLog validates the config, terminating the program on any errors.
// It also logs the config values that it used.
func (cfg *Configuration) ValidateAndLog() {
log.Infof("config.port: %d", cfg.Port)
log.Infof("config.admin_port: %d", cfg.AdminPort)
cfg.Log.validateAndLog()
cfg.RateLimiting.validateAndLog()
cfg.RequestLimits.validateAndLog()
cfg.Backend.validateAndLog()
cfg.Compression.validateAndLog()
cfg.Metrics.validateAndLog()
}

type Log struct {
Level LogLevel `mapstructure:"level"`
}

func (cfg *Log) validateAndLog() {
log.Infof("config.log.level: %s", cfg.Level)
}

type LogLevel string

const (
Expand All @@ -75,18 +92,40 @@ type RateLimiting struct {
MaxRequestsPerSecond int64 `mapstructure:"num_requests"`
}

func (cfg *RateLimiting) validateAndLog() {
log.Infof("config.rate_limiter.enabled: %t", cfg.Enabled)
log.Infof("config.rate_limiter.num_requests: %d", cfg.MaxRequestsPerSecond)
}

type RequestLimits struct {
MaxSize int `mapstructure:"max_size_bytes"`
MaxNumValues int `mapstructure:"max_num_values"`
}

func (cfg *RequestLimits) validateAndLog() {
log.Infof("config.request_limits.max_size_bytes: %d", cfg.MaxSize)
log.Infof("config.request_limits.max_num_values: %d", cfg.MaxNumValues)
}

type Compression struct {
Type CompressionType
}

func (cfg *Compression) validateAndLog() {
switch cfg.Type {
case CompressionNone:
fallthrough
case CompressionSnappy:
log.Infof("config.compression.type: %s", cfg.Type)
default:
log.Fatalf(`invalid config.compression.type: %s. It must be "none" or "snappy"`, cfg.Type)
}
}

type CompressionType string

const (
CompressionNone CompressionType = "none"
CompressionSnappy CompressionType = "snappy"
)

Expand All @@ -95,6 +134,17 @@ type Metrics struct {
Influx Influx `mapstructure:"influx"`
}

func (cfg *Metrics) validateAndLog() {
log.Infof("config.metrics.type: %s", cfg.Type)
switch cfg.Type {
case MetricsNone:
case MetricsInflux:
cfg.Influx.validateAndLog()
default:
log.Fatalf(`invalid config.metrics.type: %s. It must be "none" or "influx"`, cfg.Type)
}
}

type MetricsType string

const (
Expand All @@ -108,3 +158,9 @@ type Influx struct {
Username string `mapstructure:"username"`
Password string `mapstructure:"password"`
}

func (cfg *Influx) validateAndLog() {
log.Infof("config.metrics.influx.host: %s", cfg.Host)
log.Infof("config.metrics.influx.database: %s", cfg.Database)
// This intentionally skips username and password for security reasons.
}
11 changes: 6 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,22 @@ import (
)

func main() {
log.SetOutput(os.Stdout)
cfg := config.NewConfig()
initLogging(cfg)
setLogLevel(cfg.Log.Level)
cfg.ValidateAndLog()

appMetrics := metrics.CreateMetrics()
backend := backendConfig.NewBackend(cfg, appMetrics)
handler := routing.NewHandler(cfg, backend, appMetrics)
go appMetrics.Export(cfg.Metrics)
server.Listen(cfg, handler, appMetrics.Connections)
}

func initLogging(cfg config.Configuration) {
level, err := log.ParseLevel(string(cfg.Log.Level))
func setLogLevel(logLevel config.LogLevel) {
level, err := log.ParseLevel(string(logLevel))
if err != nil {
log.Fatalf("Invalid logrus level: %v", err)
}
log.SetOutput(os.Stdout)
log.SetLevel(level)
log.Info("Log level set to: ", log.GetLevel())
}

0 comments on commit 88a7001

Please sign in to comment.