Skip to content

Commit

Permalink
Fixed an issue where logrus would not return logs if the application …
Browse files Browse the repository at this point in the history
…was shut down
  • Loading branch information
mirackara committed Jul 8, 2024
1 parent ad44bc6 commit 21437c9
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 1 deletion.
1 change: 1 addition & 0 deletions v3/integrations/logcontext-v2/nrslog/example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

func main() {
app, err := newrelic.NewApplication(
newrelic.ConfigAppName("slog example app"),
newrelic.ConfigFromEnvironment(),
newrelic.ConfigAppLogEnabled(true),
)
Expand Down
3 changes: 2 additions & 1 deletion v3/newrelic/internal_app.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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.
Expand Down
6 changes: 6 additions & 0 deletions v3/newrelic/log_event.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down

0 comments on commit 21437c9

Please sign in to comment.