Hello,
I am trying to apply KV filter to a field that does not have "key:value key:value" format. The format is "value(key) value(key) ...". Below are the details.
-
2021-06-28 19:06:15.848 [0000-XYZ] [appLog ] partition 100 in 4 buckets: [ 10(0) 20(1) 20(0) 50(1) ]
-
I pass the log line through a grok filer and create a filed called [bucket_data].
[bucket_data]: 10(0) 20(1) 20(0) 50(1) -
Goal : To create fields as below :
bucket.0 : 30 (which is the sum of all values in bucket 0 i.e 10+20=30)
bucket.1 : 70 (which is the sum of all values in bucket 1 i.e 20+50=70) -
How I think we can achieve this is by passing the [bucket_data] into a KV filter and then to a ruby filter to compute the sum.
Questions:
-
How do I use the KV filter in this case ? The [bucket_data] format is value(key) value(key) .... What should be the field_split, value_split, trim_key, trim_value ?
-
How to I apply ruby filter after this ? Please see the code below.
-
Is there a better approach? Please let me know.
if [bucket_data] {
kv {
source => "[bucket_data]"
field_split => ""
value_split => ""
trim_key => "\s"
trim_value => "\s"
target => "[bucket]"
}
if [bucket] {
ruby {
code => "
kv = event.get('[bucket]')
kv.to_hash.each { |key,values|
sum = 0
values.each { |val| sum+=val };
event.set('[bucket][' + key + ']', sum.to_f);
}
"
}
}
}