Last active
December 21, 2015 18:49
-
-
Save harukasan/6350381 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# 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