Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve logging and set exit code according to the exit status #210

Merged
merged 1 commit into from
Jan 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
}
}
Loading