Skip to content
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

Merged
merged 1 commit into from
May 3, 2017

Conversation

jcmfernandes
Copy link

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.

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.
@dwbutler dwbutler added the bug label Feb 18, 2017
@dwbutler
Copy link
Owner

dwbutler commented Feb 18, 2017

Nice catch. And good call on treating the input as immutable as well.

I dove into the source code of LogStash::Event and discovered that most of the "built in" fields are expected to use string keys. So it might make sense to convert some or all of the keys to strings.

Example of the problem:

> LogStash::Event.new(tags: ['foo']).tags
# => nil
> LogStash::Event.new('tags' => ['foo']).tags
# => ["foo"]

@jcmfernandes
Copy link
Author

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)
Copy link

@stereobooster stereobooster Feb 27, 2017

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]

Copy link
Owner

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.

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

Copy link

@stereobooster stereobooster Feb 28, 2017

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

Copy link
Owner

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.

@dwbutler
Copy link
Owner

There is a very specific set of keys that LogStash::Event expects to be strings. There wouldn't be any value in stringifying all keys, or deep converting keys.

Since this fixes an immediate bug, I'll merge it as-is and implement a more generic solution later.

@jcmfernandes
Copy link
Author

Can we get this merged @dwbutler or would you like to see some changes to it? Let me know 😉

@dwbutler dwbutler merged commit bf47b4e into dwbutler:master May 3, 2017
@jcmfernandes
Copy link
Author

Thank you!

@dwbutler
Copy link
Owner

dwbutler commented May 7, 2017

Fix released in 0.24.1. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants