Class: Fluent::TypecastOutput

Inherits:
Output
  • Object
show all
Defined in:
lib/fluent/plugin/out_typecast.rb

Constant Summary collapse

ITEM_TYPES =
['json', 'string', 'integer', 'float', 'bool', 'time', 'array']

Instance Method Summary collapse

Instance Method Details

#cast_proc(key) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/fluent/plugin/out_typecast.rb', line 50

def cast_proc(key)
  case key
  when 'json'
    Proc.new {|value| value.to_json }
  when 'string'
    Proc.new {|value| value.to_s }
  when 'integer'
    Proc.new {|value| value.to_i }
  when 'float'
    Proc.new {|value| value.to_f }
  when 'bool'
    Proc.new {|value| Config.bool_value(value) }
  when 'time'
    Proc.new {|value| Time.strptime(value, @time_format) }
  when 'array'
    Proc.new {|value| value.split(/\s*,\s*/) }
  else
    Proc.new {|value| value }
  end
end

#configure(conf) ⇒ Object

Raises:

  • (ConfigError)


22
23
24
25
26
27
28
29
30
# File 'lib/fluent/plugin/out_typecast.rb', line 22

def configure(conf)
  super
  raise ConfigError, "typecast: 'prefix' or 'tag' is required" unless @tag or @prefix

  @cast_procs = {}
  @item_types.map {|key, type|
    @cast_procs[key] = cast_proc(type)
  }
end

#emit(tag, es, chain) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/fluent/plugin/out_typecast.rb', line 32

def emit(tag, es, chain)
  tag = 
    if @tag
      @tag
    elsif @prefix
      "#{@prefix}.#{tag}"
    end
  es.each do |time, record|
    record.each_key do |key|
      if cast_proc = @cast_procs[key]
        record[key] = cast_proc.call(record[key])
      end
    end
    Fluent::Engine.emit(tag, time, record)
  end
  chain.next
end