Class: LogStash::Filters::ArrayOfValuesUpdate

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

Defined Under Namespace

Classes: CoerceArray, CoerceOther

Instance Method Summary collapse

Constructor Details

#initialize(iterate_on, destination, fallback, lookup) ⇒ ArrayOfValuesUpdate

Returns a new instance of ArrayOfValuesUpdate.



12
13
14
15
16
17
18
19
20
21
# File 'lib/logstash/filters/array_of_values_update.rb', line 12

def initialize(iterate_on, destination, fallback, lookup)
  @iterate_on = iterate_on
  @destination = destination
  @fallback = fallback
  @use_fallback = !fallback.nil? # fallback is not nil, the user set a value in the config
  @lookup = lookup
  @coercers_table = {}
  @coercers_table.default = CoerceOther.new
  @coercers_table[Array] = CoerceArray.new
end

Instance Method Details

#test_for_inclusion(event, override) ⇒ Object



23
24
25
26
27
# File 'lib/logstash/filters/array_of_values_update.rb', line 23

def test_for_inclusion(event, override)
  # Skip translation in case @destination iterate_on already exists and @override is disabled.
  return false if !override && event.include?(@destination)
  event.include?(@iterate_on)
end

#update(event) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/logstash/filters/array_of_values_update.rb', line 29

def update(event)
  val = event.get(@iterate_on)
  source = @coercers_table[val.class].call(val)
  target = Array.new(source.size)
  if @use_fallback
    target.fill(event.sprintf(@fallback))
  end
  source.each_with_index do |inner, index|
    matched = [true, nil]
    @lookup.fetch_strategy.fetch(inner.to_s, matched)
    if matched.first
      target[index] = matched.last
    end
  end
  event.set(@destination, target)
  return target.any?
end