メタプログラミングruby勉強会#7(fluentプラグイン)
TRANSCRIPT
メタプログラミング Ruby勉強会 #7( fluentのプラグイン)
2013/10/27@aegif
テーマ
fluentのプラグインを少し見ていじってみる
2
対象のプラグイン
Amazon S3 output plugin
3
• 現在出力内容のログレコード部分が JSONになるのをタブ区切りにしたい
どう変更する?
4
# 変更前2013-10-27T07:40:41Z s3.apache.access {"host":"::1","user":null,"method":"GET","path":"/","code":200,"size":44,"referer":null,"agent":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,"agent":null,"tag":"s3.apache.access","time":"2013-10-27T07:40:41Z"}
# 変更後200 ::1 GET / 44 s3.apache.access 2013-10-27T07:50:47Z 200 ::1 GET / 44 s3.apache.access 2013-10-27T07:50:47Z 200 ::1 GET / 44 s3.apache.access 2013-10-27T07:50:47Z
元のリポジトリを clone• $ git clone [email protected]: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を少し変更すればよい
6
プラグインを動作させる方法の例・ /etc/fluent/plugin/に rubyスクリプトを置く・ lib/fluent/plugin/<TYPE>_<NAME>.rbを持つ Gemを作ってインストール
diff• 変更したもの( out_s3yoshio.rb)とオリジナル( out_s3.rb)の diff
7
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> end110c113< if @include_time_key---> if @include_time_key || !@format_json
diff(続き)• 変更したのはパラメータ指定, configureメソッドおよび formatメソッドを少々
8
122,123c125< case @out_format< when 'json'---> if @format_json125,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"
所感
簡単に書ける。自分用の Redshiftプラグインも多分簡単に書け
る
9
参考• http://docs.fluentd.org/articles/plugin-development• http://fluentd.org/plugin/• https://github.com/fluent/fluent-plugin-s3
10
おわり
11