Class: LogStash::Filters::SingleValueUpdate

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

Defined Under Namespace

Classes: CoerceArray, CoerceOther, CoerceString

Instance Method Summary collapse

Constructor Details

#initialize(field, destination, fallback, lookup) ⇒ SingleValueUpdate

Returns a new instance of SingleValueUpdate.



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/logstash/filters/single_value_update.rb', line 15

def initialize(field, destination, fallback, lookup)
  @field = field
  @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[String] = CoerceString.new
  @coercers_table[Array] = CoerceArray.new
end

Instance Method Details

#test_for_inclusion(event, override) ⇒ Object



27
28
29
30
31
# File 'lib/logstash/filters/single_value_update.rb', line 27

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

#update(event) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/logstash/filters/single_value_update.rb', line 33

def update(event)
  # If source field is array use first value and make sure source value is string
  # source = Array(event.get(@field)).first.to_s
  source = event.get(@field)
  source = @coercers_table[source.class].call(source)
  matched = [true, nil]
  @lookup.fetch_strategy.fetch(source, matched)
  if matched.first
    event.set(@destination, matched.last)
  elsif @use_fallback
    event.set(@destination, event.sprintf(@fallback))
    matched[0] = true
  end
  return matched.first
end