TL;DR
Receiving errors when using logstash split
filter after using ruby
filter to convert JSON dict (hash) to array.
The rest of the story...
I have Logstash polling an application for mbean metrics. The data set is coming to logstash in the following format:
{
"request": {
"mbean": "mbean_name",
"type": "read"
},
"value": {
"mbean_id1": {
"metric1": true,
"metric2": 0,
"metric3": 0.0
},
"mbean_id2": {
"metric1": true,
"metric2": 0,
"metric3": 0.0
}
}
}
I need to be able to split the values into individual records. The best way to do this appears to be the split
filter, which, unfortunately, doesn't work on a dict. So what I am attempting to do is convert the dict to an array, then split the subsequent array. My conf
file is:
input {
file {
path => "${CONF_D}/sample-data.json"
mode => "read"
}
}
filter {
# Convert JSON dict/hash into array, then add it as a new field.
ruby {
code => "
val_a = Array.new
event['value'].each do |k, v|
v['val_id'] = k
val_a.push(v)
end
event['val_a'] = val_a
"
}
split {
field => "val_a"
}
}
output {
stdout { codec => rubydebug }
}
When I run this rule set, I get multiple instances of the following errors:
[2019-11-11T16:10:40,666][ERROR][logstash.filters.ruby ] Ruby exception occurred: undefined method `[]' for #<LogStash::Event:0x18b864f7>
[2019-11-11T16:10:40,668][ERROR][logstash.filters.ruby ] Ruby exception occurred: undefined method `[]' for #<LogStash::Event:0x53dc61a9>
[2019-11-11T16:10:40,697][WARN ][logstash.filters.split ] Only String and Array types are splittable. field:val_a is of type = NilClass
[2019-11-11T16:10:40,703][WARN ][logstash.filters.split ] Only String and Array types are splittable. field:val_a is of type = NilClass
However, when I run the following Ruby script against the same data set, processes correctly and I get the following output:
#!/usr/bin/ruby
require 'json'
file = File.read('sample-data.json')
event = JSON.parse(file)
vals_a = Array.new
event['value'].each do |k, v|
v['val_id'] = k
vals_a.push(v)
end
print vals_a