Class: LogStash::Filters::SumNumbers
- Defined in:
- lib/logstash/filters/sumnumbers.rb
Overview
This filter automatically sum all numbers found inside a string
The sum is returned in a new field, “sumTotal”. The total numbers summed will be in a new field, “sumNums”
The fields produced by this filter are extra useful used in combination with kibana number plotting features.
If the field is an array, all of the numbers in it will be summed. If the field is a hash, all of the values of the top-level keys will be summed. If the field is a string, it will be split, numbers extracted, and summed.
Constant Summary
Constants inherited from Base
Constants included from Config::Mixin
Instance Attribute Summary
Attributes included from Config::Mixin
Attributes inherited from Plugin
Instance Method Summary collapse
- #filter(event) ⇒ Object
- #register ⇒ Object
- #str_as_float(str) ⇒ Object
- #str_as_integer(str) ⇒ Object
Methods inherited from Base
#execute, #initialize, #threadsafe?
Methods included from Config::Mixin
Methods inherited from Plugin
#eql?, #finished, #finished?, #hash, #initialize, #inspect, lookup, #reload, #running?, #shutdown, #teardown, #terminating?, #to_s
Constructor Details
This class inherits a constructor from LogStash::Filters::Base
Instance Method Details
#filter(event) ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/logstash/filters/sumnumbers.rb', line 28 def filter(event) msg = event[@source] sumnums = 0 sumtotal = 0 if not msg return end # If for some reason the field is an array of values, take the first only. if msg.is_a?(Array) fields = msg.first.split # If msg is json, get an array from the values elsif msg.is_a?(Hash) fields = msg.values # Else, we have a string. Split it. else fields = msg.split end for elem in fields int = str_as_integer(elem) if int != nil sumtotal += int sumnums += 1 next end f = str_as_float(elem) if f != nil sumtotal += f sumnums += 1 end end event["sumNums"] = sumnums event["sumTotal"] = sumtotal end |
#register ⇒ Object
24 25 |
# File 'lib/logstash/filters/sumnumbers.rb', line 24 def register end |
#str_as_float(str) ⇒ Object
70 71 72 |
# File 'lib/logstash/filters/sumnumbers.rb', line 70 def str_as_float(str) Float(str) rescue nil end |
#str_as_integer(str) ⇒ Object
66 67 68 |
# File 'lib/logstash/filters/sumnumbers.rb', line 66 def str_as_integer(str) Integer(str) rescue nil end |