Skip to content

Commit

Permalink
Improve logging and set exit code according to the exit status
Browse files Browse the repository at this point in the history
  • Loading branch information
kasia-kujawa committed Jan 3, 2025
1 parent 710d8e9 commit 185812d
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 7 deletions.
11 changes: 8 additions & 3 deletions cmd/agent/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,16 +97,21 @@ func run(ctx context.Context) error {
defer registrator.ReleaseWaiters()

castailog.SetupLogExporter(registrator, remoteLogger, localLog, castaiClient, &loggingConfig)
// to invoke exit handlers set up in castailog.SetupLogExporter
defer logrus.Exit(0)

clusterIDHandler := func(clusterID string) {
loggingConfig.ClusterID = clusterID
log.Data["cluster_id"] = clusterID
registrator.ReleaseWaiters()
}

return runAgentMode(ctx, castaiClient, log, cfg, clusterIDHandler)
err = runAgentMode(ctx, castaiClient, log, cfg, clusterIDHandler)
if err != nil {
// it is necessary to log error because invoking of logrus exit handlers will terminate the process using os.Exit()
// error handling in cobra ("github.com/spf13/cobra") won't be able to log this error
remoteLogger.Error(err)
}
defer castailog.InvokeLogrusExitHandlers(err)
return err
}

func runAgentMode(ctx context.Context, castaiclient castai.Client, log *logrus.Entry, cfg config.Config, clusterIDChanged func(clusterID string)) error {
Expand Down
12 changes: 8 additions & 4 deletions cmd/monitor/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,20 @@ func run(ctx context.Context) error {
defer registrator.ReleaseWaiters()

castailog.SetupLogExporter(registrator, remoteLogger, localLog, castaiClient, &loggingConfig)
// to invoke exit handlers set up in castailog.SetupLogExporter
defer logrus.Exit(0)

clusterIDHandler := func(clusterID string) {
loggingConfig.ClusterID = clusterID
log.Data["cluster_id"] = clusterID
registrator.ReleaseWaiters()
}

return runMonitorMode(ctx, log, cfg, clusterIDHandler)
err = runMonitorMode(ctx, log, cfg, clusterIDHandler)
if err != nil {
// it is necessary to log error because invoking of logrus exit handlers will terminate the process using os.Exit()
// error handling in cobra ("github.com/spf13/cobra") won't be able to log this error
remoteLogger.Error(err)
}
defer castailog.InvokeLogrusExitHandlers(err)
return err
}

func runMonitorMode(ctx context.Context, log *logrus.Entry, cfg config.Config, clusterIDChanged func(clusterID string)) error {
Expand Down
11 changes: 11 additions & 0 deletions pkg/log/exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,3 +105,14 @@ func (ex *exporter) sendLogEvent(clusterID string, e *logrus.Entry) {
ex.localLog.Errorf("failed to send logs: %v", err)
}
}

// InvokeLogrusExitHandlers invokes exit handlers set up in SetupLogExporter
// The handlers are also invoked when any Fatal log entry is made.
// logrus.Exit runs all the Logrus exit handlers and then terminates the program using os.Exit(code)
func InvokeLogrusExitHandlers(err error) {
if err != nil {
logrus.Exit(1)
} else {
logrus.Exit(0)
}
}

0 comments on commit 185812d

Please sign in to comment.