http://docs.fluentd.org/v0.12/articles/filter_record_transformer
追加したり、編集したり、削除したりできるフィルタ。
<source>
@type forward
port 24224
bind 0.0.0.0
</source>
<filter hoge.log>
@type record_transformer
enable_ruby
auto_typecast true
remove_keys b,d
<record>
what_is_tag ${tag}
what_is_a ${record["a"]}
what_is_c_of_b_add_1 ${record["b"]["c"] + 1}
</record>
</filter>
<match hoge.log>
@type stdout
</match>
この例だとタグを値に持つ"what_is_tag"、aを値に持つ"what_is_a"、b.cの値に1を足す"what_is_c_of_b_add_1"が追加され、 bとdが削除される。一旦まっさらにして入れるものだけを指定することもできる。
auto_typecastをtrueにしないと"what_is_c_of_b_add_1"の値がstringになる。
$ echo '{"a": "hoge", "b": {"c": 1}, "d": "fuga"}' | /opt/td-agent/embedded/bin/fluent-cat hoge.log
$ tail /var/log/td-agent/td-agent.log
hoge.log: {"a":"hoge","what_is_tag":"hoge.log","what_is_a":"hoge","what_is_c_of_b_add_1":2}
エラーが起きるとnullになるが、それ以外の処理はされる。
$ echo '{"a": "hoge", "b": {"c": "error!"}, "d": "fuga"}' | /opt/td-agent/embedded/bin/fluent-cat hoge.log
$ tail /var/log/td-agent/td-agent.log
[warn]: failed to expand `record["b"]["c"] + 1` error_class=TypeError error="no implicit conversion of Fixnum into String"
...
hoge.log: {"a":"hoge","what_is_tag":"hoge.log","what_is_a":"hoge","what_is_c_of_b_add_1":null}
フィルタ適用前と後をそれぞれoutputしてみる。
<match hoge.log>
@type copy
<store>
@type stdout
</store>
<store>
@type relabel
@label @fuga
</store>
</match>
<label @fuga>
<filter hoge.log>
@type record_transformer
enable_ruby
auto_typecast true
remove_keys b,d
<record>
what_is_tag ${tag}
what_is_a ${record["a"]}
what_is_c_of_b_add_1 ${record["b"]["c"] + 1}
</record>
</filter>
<match hoge.log>
@type stdout
</match>
</label>
hoge.log: {"a":"hoge","b":{"c":1},"d":"fuga"}
hoge.log: {"a":"hoge","what_is_tag":"hoge.log","what_is_a":"hoge","what_is_c_of_b_add_1":2}