Class: LogStash::Filters::ArrayOfMapsValueUpdate

Inherits:
Object
  • Object
show all
Defined in:
lib/logstash/filters/array_of_maps_value_update.rb

Instance Method Summary collapse

Constructor Details

#initialize(iterate_on, field, destination, fallback, lookup) ⇒ ArrayOfMapsValueUpdate

Returns a new instance of ArrayOfMapsValueUpdate.



5
6
7
8
9
10
11
12
# File 'lib/logstash/filters/array_of_maps_value_update.rb', line 5

def initialize(iterate_on, field, destination, fallback, lookup)
  @iterate_on = ensure_reference_format(iterate_on)
  @field = ensure_reference_format(field)
  @destination = ensure_reference_format(destination)
  @fallback = fallback
  @use_fallback = !fallback.nil? # fallback is not nil, the user set a value in the config
  @lookup = lookup
end

Instance Method Details

#ensure_reference_format(field) ⇒ Object



40
41
42
# File 'lib/logstash/filters/array_of_maps_value_update.rb', line 40

def ensure_reference_format(field)
  field.start_with?("[") && field.end_with?("]") ? field : "[#{field}]"
end

#test_for_inclusion(event, override) ⇒ Object



14
15
16
# File 'lib/logstash/filters/array_of_maps_value_update.rb', line 14

def test_for_inclusion(event, override)
  event.include?(@iterate_on)
end

#update(event) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/logstash/filters/array_of_maps_value_update.rb', line 18

def update(event)
  val = event.get(@iterate_on) # should be an array of hashes
  source = Array(val)
  matches = Array.new(source.size)
  source.size.times do |index|
    nested_field = "#{@iterate_on}[#{index}]#{@field}"
    nested_destination = "#{@iterate_on}[#{index}]#{@destination}"
    inner = event.get(nested_field)
    next if inner.nil?
    matched = [true, nil]
    @lookup.fetch_strategy.fetch(inner.to_s, matched)
    if matched.first
      event.set(nested_destination, matched.last)
      matches[index] = true
    elsif @use_fallback
      event.set(nested_destination, event.sprintf(@fallback))
      matches[index] = true
    end
  end
  return matches.any?
end