From 3d082bad1a08dc99d36fbcad2ad2da5b1be5e801 Mon Sep 17 00:00:00 2001 From: Mirac Kara <55501260+mirackara@users.noreply.github.com> Date: Wed, 10 Jul 2024 16:17:30 -0500 Subject: [PATCH] Fixed an issue where logrus would not return logs if the application was shut down (#934) --- v3/integrations/logcontext-v2/nrslog/example/main.go | 1 + v3/newrelic/internal_app.go | 3 ++- v3/newrelic/log_event.go | 6 ++++++ 3 files changed, 9 insertions(+), 1 deletion(-) diff --git a/v3/integrations/logcontext-v2/nrslog/example/main.go b/v3/integrations/logcontext-v2/nrslog/example/main.go index 2544fb3b6..f201b3184 100644 --- a/v3/integrations/logcontext-v2/nrslog/example/main.go +++ b/v3/integrations/logcontext-v2/nrslog/example/main.go @@ -12,6 +12,7 @@ import ( func main() { app, err := newrelic.NewApplication( + newrelic.ConfigAppName("slog example app"), newrelic.ConfigFromEnvironment(), newrelic.ConfigAppLogEnabled(true), ) diff --git a/v3/newrelic/internal_app.go b/v3/newrelic/internal_app.go index b85365a86..54b7b4fdc 100644 --- a/v3/newrelic/internal_app.go +++ b/v3/newrelic/internal_app.go @@ -255,7 +255,7 @@ func (app *app) process() { // Remove the run before merging any final data to // ensure a bounded number of receives from dataChan. - app.setState(nil, errors.New("application shut down")) + app.setState(nil, errApplicationShutDown) if obs := app.getObserver(); obs != nil { if err := obs.shutdown(timeout); err != nil { @@ -541,6 +541,7 @@ var ( errHighSecurityEnabled = errors.New("high security enabled") errCustomEventsDisabled = errors.New("custom events disabled") errCustomEventsRemoteDisabled = errors.New("custom events disabled by server") + errApplicationShutDown = errors.New("application shut down") ) // RecordCustomEvent implements newrelic.Application's RecordCustomEvent. diff --git a/v3/newrelic/log_event.go b/v3/newrelic/log_event.go index 700be9d24..ff7e46d01 100644 --- a/v3/newrelic/log_event.go +++ b/v3/newrelic/log_event.go @@ -186,6 +186,12 @@ func EnrichLog(buf *bytes.Buffer, opts EnricherOption) error { reply, err := app.app.getState() if err != nil { + app.app.Debug("cannot enrich logs, unable to reach application", map[string]interface{}{"error": err.Error()}) + // If the application is shut down, don't return an error so the log can still be written. + // If debug logging is enabled, the error will be logged there. + if err == errApplicationShutDown { + return nil + } return err }