メタプログラミングRuby勉強
会#7
(fluentのプラグイン)

2013/10/27
@aegif
テーマ

fluentのプラグインを少
し見ていじってみる
2
対象のプラグイン

Amazon S3 output plugin

3
どう変更する?
• 現在出力内容のログレコード部分がJSONになるの
をタブ区切りにしたい
# 変更前
2013-10-27T07:40:41Z s3.apache.access
{"host":"::1","user":null,"method":"GET","path":"/","code":200,"size":44,"referer":null,"ag
ent":null,"tag":"s3.apache.access","time":"2013-10-27T07:40:41Z"}
2013-10-27T07:40:41Z s3.apache.access
{"host":"::1","user":null,"method":"GET","path":"/","code":200,"size":44,"referer":null,"ag
ent":null,"tag":"s3.apache.access","time":"2013-10-27T07:40:41Z"}

# 変更後
200 ::1
200 ::1
200 ::1

GET
GET
GET

/
/
/

44
44
44

s3.apache.access
s3.apache.access
s3.apache.access

2013-10-27T07:50:47Z
2013-10-27T07:50:47Z
2013-10-27T07:50:47Z

4
元のリポジトリをclone
• $ git clone git@github.com:fluent/fluent-plugin-s3.git

5
プラグイン用ディレクトリへコピー
• $ cp ./fluent-plugin-s3/lib/fluent/plugin/out_s3.rb 
/etc/fluent/plugin/out_s3yoshio.rb
• あとは,out_s3yoshio.rbを少し変更すればよい
プラグインを動作させる方法の例
・/etc/fluent/plugin/にrubyスクリプトを置く
・lib/fluent/plugin/<TYPE>_<NAME>.rbを持つGemを作ってインストー
ル

6
diff
• 変更したもの(out_s3yoshio.rb)とオリジナル
(out_s3.rb)のdiff
6c6
< Fluent::Plugin.register_output('s3yoshio', self)
--> Fluent::Plugin.register_output('s3', self)
38d37
< config_param :out_format, :string, :default => 'tab'
51c50,54
< @out_format = conf['out_format']
--> if format_json = conf['format_json']
>
@format_json = true
> else
>
@format_json = false
> end
110c113
< if @include_time_key
--> if @include_time_key || !@format_json

7
diff(続き)
• 変更したのはパラメータ指定,configureメソッド
およびformatメソッドを少々
122,123c125
< case @out_format
< when 'json'
--> if @format_json
125,126d126
< when 'csv'
<
record.keys.sort.map {|k| record[k] }.join(",") + "n"
128c128
<
record.keys.sort.map {|k| record[k] }.join("t") + "n"
-->
"#{time_str}t#{tag}t#{Yajl.dump(record)}n"

8
所感

簡単に書ける。
自分用のRedshiftプラグ
インも多分簡単に書ける
9
参考
• http://docs.fluentd.org/articles/plugin-development
• http://fluentd.org/plugin/
• https://github.com/fluent/fluent-plugin-s3

10
おわり

11

メタプログラミングRuby勉強会#7(fluentプラグイン)