Skip to content

Instantly share code, notes, and snippets.

@harukasan
Last active December 21, 2015 18:49
Show Gist options
  • Save harukasan/6350381 to your computer and use it in GitHub Desktop.
Save harukasan/6350381 to your computer and use it in GitHub Desktop.
#
# Auto typecast
# =============
#
# Ref:: http://y-ken.hatenablog.com/entry/fluentd-parser-auto-type-conversion
#
# ちなみにこの方法だとApacheとかNginxのログで、"-"とかが流れたときにIntegerとStringが混ざって死ぬ。
#
module Fluent
class AutoTypecastOutput < Output
Fluent::Plugin.register_output('auto_typecast', self)
config_param :add_prefix, :string, default: 'typed'
def emit(tag, es, chain)
if @add_prefix
tag = "#{@add_prefix}.#{tag}"
end
es.each do |time, record|
record.keys.each do |key|
record[key] = typecast(key, record[key])
end
Fluent::Engine.emit(tag, time, record)
end
chain.next
end
def typecast(key, value)
if value == value.to_i.to_s
value = value.to_i
elsif value == value.to_f.to_s
value = value.to_f
end
value
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment