Skip to content

Commit 16e42ef

Browse files
Merge branch 'dev' into skip_span_event_when_transaction_ignored
2 parents 987369a + d589560 commit 16e42ef

File tree

3 files changed

+19
-7
lines changed

3 files changed

+19
-7
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010

1111
Previously, the agent was incorrectly recording span events only on transactions that should be ignored. This fix will prevent any span events from being created for transactions using newrelic_ignore, or ignored through the `rules.ignore_url_regexes` configuration option.
1212

13+
* **Bugfix: Scrub non-unicode characters from DecoratingLogger**
14+
15+
To prevent `JSON::GeneratorErrors`, the DecoratingLogger replaces non-unicode characters with the replacement character: �. Thank you Jonathan del Strother (@jdelStrother) for bringing this to our attention!
1316

1417
## v8.1.0
1518

lib/new_relic/agent/logging.rb

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ class DecoratingFormatter < ::Logger::Formatter
3333
COLON = ':'.freeze
3434
COMMA = ','.freeze
3535
CLOSING_BRACE = '}'.freeze
36+
REPLACEMENT_CHAR = '�'
3637

3738
def initialize
3839
Agent.config.register_callback :app_name do
@@ -86,12 +87,10 @@ def add_key_value message, key, value
8687
message << QUOTE << key << QUOTE << COLON << QUOTE << value << QUOTE
8788
end
8889

89-
def escape message
90-
if String === message
91-
message.to_json
92-
else
93-
message.inspect.to_json
94-
end
90+
def escape(message)
91+
message = message.to_s unless message.is_a?(String)
92+
message = message.scrub(REPLACEMENT_CHAR) unless message.valid_encoding?
93+
message.to_json
9594
end
9695

9796
def clear_tags!

test/new_relic/agent/logging_test.rb

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ def test_constructor_arguments_shift_age
7878
'carriage_return' => "message with a carriage return \r",
7979
'tab' => "message with a tab \t ",
8080
'unicode' => "message with a unicode snowman ☃ ",
81-
'unicode_hex' => "message with a unicode snowman \u2603 "
81+
'unicode_hex' => "message with a unicode snowman \u2603 ",
8282
}
8383
messages_to_escape.each do |name, message|
8484
define_method "test_escape_message_#{name}" do
@@ -88,6 +88,16 @@ def test_constructor_arguments_shift_age
8888
end
8989
end
9090

91+
def test_to_replace_non_utf_8_chars
92+
message = 'message with a non-unicode code '
93+
input = "#{message} \xb3"
94+
expectation = "#{message} #{DecoratingFormatter::REPLACEMENT_CHAR}"
95+
logger = DecoratingLogger.new @output
96+
97+
logger.info input
98+
assert_equal expectation,
99+
last_message['message']
100+
end
91101

92102
if RUBY_VERSION >= '2.4.0'
93103
def test_constructor_arguments_level

0 commit comments

Comments
 (0)