Class: LogStash::Filters::Units
- Inherits:
-
Base
- Object
- Base
- LogStash::Filters::Units
- Defined in:
- lib/logstash/filters/units.rb
Overview
The units filter is used for converting specified fields from one unit of measure to another (or many others).
Instance Method Summary collapse
- #_nested_hash_to_matrix(data, name) ⇒ Object
- #filter(event) ⇒ Object
-
#initialize(*args) ⇒ Units
constructor
Renames the resulting output fields according to a hash.
- #register ⇒ Object
Constructor Details
#initialize(*args) ⇒ Units
Renames the resulting output fields according to a hash. Keys should be the output fields and values should be their desired names, respectively. Unamed fields will persist. Example:
- source,ruby
-
filter { units { fields => [“file_size”] input_unit => “byte” output_units => [“kilobyte”, “megabyte”] rename_labels => { “kilobyte” => “kB” “megabyte” => “mB” } } }
139 140 141 |
# File 'lib/logstash/filters/units.rb', line 139 def initialize(*args) super(*args) end |
Instance Method Details
#_nested_hash_to_matrix(data, name) ⇒ Object
151 152 153 154 155 156 157 158 159 160 161 |
# File 'lib/logstash/filters/units.rb', line 151 def _nested_hash_to_matrix(data, name) data.each do |key, val| new_key = name + @sep + key.to_s if val.is_a?(Hash) and val != {} _nested_hash_to_matrix(val, new_key) else @output.push([new_key, val]) end end return @output end |
#filter(event) ⇒ Object
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 |
# File 'lib/logstash/filters/units.rb', line 224 def filter(event) return unless filter?(event) begin # flag for converting a value in place or expanding it into # a hash inplace = @output_units.is_a?(String) matrix = nested_hash_to_matrix(event.to_hash) # convert matching fields and remove rows that # do not contain leaf nodes that match @fields fields = field_handler(@fields) del = [] matrix.each do |row| if fields["exact"].include?(row[0]) if inplace row[1] = convert(row[1], @output_units) else row[1] = convert_to_dict(row[1]) end elsif fields["partial"].include?(row[0][-1]) if inplace row[1] = convert(row[1], @output_units) else row[1] = convert_to_dict(row[1]) end else del.push(row) end end # delete all rows that do not have leaf nodes listed # in fields for row in del matrix.delete(row) end new_hash = matrix_to_nested_hash(matrix) if @root_field new_hash = {@root_field => new_hash} end # merge new hash into event's hash # assignment of items from new_hash with a prior merge # will destroy unmatched branches data = event.to_hash().merge(new_hash) data.each do |key, value| event[key] = value end return filter_matched(event) rescue StandardError event.tag("_unitsparsefailure") return event rescue Logstash::ShutdownSignal event.cancel() @logger.debug(e) end end |
#register ⇒ Object
144 145 |
# File 'lib/logstash/filters/units.rb', line 144 def register() end |