-
Notifications
You must be signed in to change notification settings - Fork 120
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
Stringify key :message in the base formatter #116
Conversation
Doing the following (an example taken from the README file) logger.debug message: 'test', foo: 'bar' will fail for any logger whose the formatter's class inherits from LogStashLogger::Formatter::Base when LogStashLogger.configuration.max_message_size is set. This happens because event['message'.freeze].byteslice (previously on line 49) will fail as event['message'.freeze] will return nil.
Nice catch. And good call on treating the input as immutable as well. I dove into the source code of Example of the problem: > LogStash::Event.new(tags: ['foo']).tags
# => nil
> LogStash::Event.new('tags' => ['foo']).tags
# => ["foo"] |
Thanks! 😺 I agree. Should we deep stringify hashes or just root keys? |
@@ -25,7 +25,9 @@ def build_event(message, severity, time) | |||
when LogStash::Event | |||
data.clone | |||
when Hash | |||
event_data = data.merge("@timestamp".freeze => time) | |||
event_data = data.clone | |||
event_data['message'.freeze] = event_data.delete(:message) if event_data.key?(:message) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Constanize strings as well e.g.
MESSAGE_FIELD = 'message'.freeze
...
event_data[MESSAGE_FIELD]
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My recollection is that Ruby will optimize this sort of code anyway, and therefore extracting to a constant isn't necessary. Can't find a source for this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep. I missed this optimisation somehow. Here is the proof (at least for ruby 2.2.6p396 (2016-11-15 revision 56800)
):
irb(main):001:0> "asd".object_id
=> 70178538223500
irb(main):002:0> "asd".object_id
=> 70178538189060
irb(main):003:0> "asd".freeze.object_id
=> 70178538223620
irb(main):004:0> "asd".freeze.object_id
=> 70178538223620
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
but for ruby 2.0.0p247 (2013-06-27 revision 41674)
irb(main):001:0> "asd".object_id
=> 70313257930780
irb(main):002:0> "asd".object_id
=> 70313241344880
irb(main):003:0> "asd".freeze.object_id
=> 70313241338520
irb(main):004:0> "asd".freeze.object_id
=> 70313241348500
irb(main):005:0> "asd".freeze.object_id
And taking into account that library claims to support ruby 2.0 constanization still makes sense
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the legwork. And good point, LogStashLogger support goes back to 1.9, so it should definitely do whatever optimizations are necessary.
There is a very specific set of keys that Since this fixes an immediate bug, I'll merge it as-is and implement a more generic solution later. |
Can we get this merged @dwbutler or would you like to see some changes to it? Let me know 😉 |
Thank you! |
Fix released in 0.24.1. Thanks! |
Doing the following (an example taken from the README
file)
logger.debug message: 'test', foo: 'bar'
will fail for any logger whose the formatter's class
inherits from LogStashLogger::Formatter::Base when
LogStashLogger.configuration.max_message_size is
set. This happens because event['message'.freeze].byteslice
(previously on line 49) will fail as
event['message'.freeze] will return nil.