From 645492b90f0c04872b50c8b5813334dd27588024 Mon Sep 17 00:00:00 2001 From: Hannah Ramadan <76922290+hannahramadan@users.noreply.github.com> Date: Fri, 6 Dec 2024 16:57:40 -0800 Subject: [PATCH] Do not attempt to decorate nil logs (#2986) * Do not attempt to decorate nil logs * Add CHANGELOG * Apply suggestions from code review Co-authored-by: Kayla Reopelle <87386821+kaylareopelle@users.noreply.github.com> --------- Co-authored-by: Kayla Reopelle <87386821+kaylareopelle@users.noreply.github.com> --- CHANGELOG.md | 4 ++++ lib/new_relic/agent/local_log_decorator.rb | 2 +- test/new_relic/agent/local_log_decorator_test.rb | 7 +++++++ 3 files changed, 12 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ce0c9a1b85..310ffab922 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ ## dev +- **Bugfix: Do not attempt to decorate logs with `nil` messages** + + The agent no longer attempts to add New Relic linking metadata to logs with `nil` messages. Thank you, [@arlando](https://github.com/arlando) for bringing this to our attention! [Issue#2985](https://github.com/newrelic/newrelic-ruby-agent/issues/2985) [PR#2986](https://github.com/newrelic/newrelic-ruby-agent/pull/2986) + - **Bugfix: Stop renaming final Grape segment** Previously, the agent renamed the final segment in Grape transactions to `"Middleware/Grape/#{class_name}/call"`. This was a part of an old instrumentation pattern that is no longer relevant. Many thanks to [@seriousdev-gh](https://github.com/seriousdev-gh) for bringing this issue to our attention and along with a great reproduction and suggested fix. [PR#2987](https://github.com/newrelic/newrelic-ruby-agent/pull/2987). diff --git a/lib/new_relic/agent/local_log_decorator.rb b/lib/new_relic/agent/local_log_decorator.rb index e5a5abe60e..1085c788ee 100644 --- a/lib/new_relic/agent/local_log_decorator.rb +++ b/lib/new_relic/agent/local_log_decorator.rb @@ -9,7 +9,7 @@ module LocalLogDecorator extend self def decorate(message) - return message unless decorating_enabled? + return message if !decorating_enabled? || message.nil? metadata = NewRelic::Agent.linking_metadata diff --git a/test/new_relic/agent/local_log_decorator_test.rb b/test/new_relic/agent/local_log_decorator_test.rb index a3208fedd2..7a0a2c23f2 100644 --- a/test/new_relic/agent/local_log_decorator_test.rb +++ b/test/new_relic/agent/local_log_decorator_test.rb @@ -77,6 +77,13 @@ def test_decorates_if_enabled assert_equal decorated_message, "#{MESSAGE} #{METADATA_STRING}" end + def test_does_not_decorate_if_message_is_nil + metadata_stubs + decorated_message = LocalLogDecorator.decorate(nil) + + assert_nil(decorated_message) + end + def test_decorate_puts_metadata_at_end_of_first_newline metadata_stubs message = "This is a test of the Emergency Alert System\n this is only a test...."