Skip to content

Commit

Permalink
add logging for initializations
Browse files Browse the repository at this point in the history
  • Loading branch information
derrick-dacosta committed May 8, 2024
1 parent 4550fba commit 9f1f695
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 13 deletions.
36 changes: 27 additions & 9 deletions cmd/fishymetrics/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,10 +243,26 @@ func main() {
VectorEndpoint: *vectorEndpoint,
}

logger.Initialize(app, hostname, logConfig)
err = logger.Initialize(app, hostname, logConfig)
if err != nil {
panic(fmt.Errorf("error initializing logger - log_method=%s vector_endpoint=%s log_file_path=%s log_file_max_size=%d log_file_max_backups=%d log_file_max_age=%d - err=%s",
*logMethod, *vectorEndpoint, *logFilePath, logfileMaxSize, logfileMaxBackups, logfileMaxAge, err.Error()))
}

log = zap.L()
defer logger.Flush()

if *logMethod == "vector" {
log.Info("successfully initialized logger", zap.String("log_method", *logMethod),
zap.String("vector_endpoint", *vectorEndpoint))
} else if *logMethod == "file" {
log.Info("successfully initialized logger", zap.String("log_method", *logMethod),
zap.String("log_file_path", *logFilePath),
zap.Int("log_file_max_size", logfileMaxSize),
zap.Int("log_file_max_backups", logfileMaxBackups),
zap.Int("log_file_max_age", logfileMaxAge))
}

// configure vault client if vaultRoleId & vaultSecretId are set
if *vaultRoleId != "" && *vaultSecretId != "" {
var err error
Expand All @@ -259,15 +275,17 @@ func main() {
},
)
if err != nil {
log.Error("failed initializing vault client", zap.Error(err))
}

// we add this here so we can update credentials once we detect they are rotated
common.ChassisCreds.Vault = vault
log.Error("failed initializing vault client", zap.Error(err),
zap.String("vault_address", *vaultAddr),
zap.String("vault_role_id", *vaultRoleId))
} else {
// we add this here so we can update credentials once we detect they are rotated
common.ChassisCreds.Vault = vault

// start go routine to continuously renew vault token
wg.Add(1)
go vault.RenewToken(ctx, doneRenew, tokenLifecycle, &wg)
// start go routine to continuously renew vault token
wg.Add(1)
go vault.RenewToken(ctx, doneRenew, tokenLifecycle, &wg)
}
}

config.NewConfig(&config.Config{
Expand Down
10 changes: 6 additions & 4 deletions logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func (lumberjackSink) Sync() error {
return nil
}

func Initialize(svc, hostname string, config LoggerConfig) {
func Initialize(svc, hostname string, config LoggerConfig) error {

atomicLevel = zap.NewAtomicLevel()

Expand Down Expand Up @@ -98,17 +98,17 @@ func Initialize(svc, hostname string, config LoggerConfig) {
if config.LogMethod == "vector" {
url, err := url.Parse(config.VectorEndpoint)
if err != nil {
panic(err)
return err
}

err = zap.RegisterSink(url.Scheme, initVectorSink)
if err != nil {
panic(err)
return err
}

vecWriteSyncer, _, err := zap.Open(url.String())
if err != nil {
panic(err)
return err
}

ws := zapcore.Lock(vecWriteSyncer)
Expand Down Expand Up @@ -137,6 +137,8 @@ func Initialize(svc, hostname string, config LoggerConfig) {
atomicLevel.SetLevel(parseLevel(config.LogLevel))

zap.ReplaceGlobals(logger)

return nil
}

func Flush() {
Expand Down

0 comments on commit 9f1f695

Please sign in to comment.