Class: Fluent::LookupOutput

Inherits:
Output
  • Object
show all
Includes:
HandleTagNameMixin
Defined in:
lib/fluent/plugin/out_lookup.rb

Instance Method Summary collapse

Instance Method Details

#configure(conf) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/fluent/plugin/out_lookup.rb', line 54

def configure(conf)
  super

  @assign_method = method(:assign)
  @assign_self_method = method(:assign_self)
  @return_method = method(:return)
  @rename_method = method(:rename)

  if (field.nil? || table_file.nil?) 
    raise ConfigError, "lookup: Both 'field', and 'table_file' are required to be set."
  end

  @lookup_table = create_lookup_table(table_file)
  @field = field.split(".")

  if (rename_key)
    @filter_method = method(:filter_rename_key)
    return
  end

  if (output_field.nil?)
    @filter_method = method(:filter_no_output)
  else
    @output_field = output_field.split(".")
    @filter_method = method(:filter_with_output)
  end


end

#create_lookup_table(file) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/fluent/plugin/out_lookup.rb', line 36

def create_lookup_table(file)
  lookup_table = {}
  CSV.foreach(file) do |row|
    handle_row(lookup_table, row)
  end

  if (strict && lookup_table.length == 0)
    raise ConfigError, "Lookup file is empty"
  end

  return lookup_table
rescue Errno::ENOENT => e
  handle_file_err(file, e)
rescue Errno::EACCES => e
  handle_file_err(file, e)
end

#emit(tag, es, chain) ⇒ Object



84
85
86
87
88
89
90
91
92
# File 'lib/fluent/plugin/out_lookup.rb', line 84

def emit(tag, es, chain)
  es.each { |time, record|
    t = tag.dup
    filter_record(t, time, record)
    Engine.emit(t, time, record)
  }

  chain.next
end

#handle_row(lookup_table, row) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/fluent/plugin/out_lookup.rb', line 16

def handle_row(lookup_table, row)
  if (row.length < 2)
    return handle_row_error(row, "Too few columns : #{row.length} instead of 2")
  end

  # If too much columns
  if (strict && row.length > 2)
    return handle_row_error(row, "Too much columns : #{row.length} instead of 2")
  end

  # If duplicates
  if (strict && lookup_table.has_key?(row[0]))
    return handle_row_error(row, "Duplicate entry")
  end

  lookup_table[row[0]] = row[1]

end