But sometimes the context.response field is sent as a string, which is a problem for Elasticsearch.
I want to make a Ruby filter to fix this, so if it's a string, pack it into context.response.body instead.
This is what I came up with initially (having never written any Ruby before):
if event.include? '[context][response]' && event.get('[context][response]').is_a? String
textval = event.get('[context][response]')
event.remove('[context][response]')
event.set('[context][response][body]', textval)
end
Logstash just crashes immediately, with the error:
I didn't realize it would just "do nothing" if run on a hash. Thank you!
EDIT: Sorry, was too quick.
The convert process can't turn a string into a hash, as far as I can see.
The issue is I need to check:
if context.response is type String:
move context.response into context.response.body
Moving it into a different field would also be acceptable, but most of the events contain context.response as a hash, so those need to be left untouched.
When context.response is an object is there any nested field that is always present? Like will you always have context.response.body or context.response.code?
If so, than you can fix this issue without the need to write any ruby, just combining some mutate filters.
if event.include? '[context][response]' and event.get('[context][response]').is_a? String
event.set('[context][response][body]', event.remove('[context][response]'))
end
If you always have a specific field when the field is an object you can text if this field exists, if it does not exist, than you do not have it as an object.
It tests if the field context.response.body exists, if it does not exists than it will rename context.response to context.response.body, but this only works if you can guarantee that when context.response is an object, it will always have the nested field body.
Apache, Apache Lucene, Apache Hadoop, Hadoop, HDFS and the yellow elephant
logo are trademarks of the
Apache Software Foundation
in the United States and/or other countries.