Last active
August 29, 2015 14:10
-
-
Save kiyoto/fc736d38394f35ed6a87 to your computer and use it in GitHub Desktop.
DevNullParserで始めるFluentd パーサプラグイン入門 ref: http://qiita.com/kiyoto/items/9903718147adde4d6c32
This file contains hidden or 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
<source> | |
type tail | |
format json | |
path /path/to/file | |
tag aoi.yu | |
</source> |
This file contains hidden or 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
<source> | |
type tcp | |
format none | |
tag akai.yu | |
</source> |
This file contains hidden or 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
2014-12-01 03:31:44 +0000 something: {"hello":"world"} |
This file contains hidden or 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
vagrant@precise64:~$ echo 'this is not JSON' | nc localhost 13337 |
This file contains hidden or 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
2014-12-01 03:33:49 +0000 something: {"message":"this is not JSON"} |
This file contains hidden or 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
module Fluent | |
class TextParser | |
#パーサープラグインの名前 | |
class DevNullParser < Parser | |
# このプラグインをパーサプラグインとして登録する | |
Plugin.register_parser('dev_null', self) | |
def initialize | |
super | |
# いろいろとここで初期化 | |
end | |
def configure(conf={}) | |
super | |
# 設定ファイルによるものはここで | |
end | |
def parse(text) | |
# これが肝となるメソッドで、textをパースする。 | |
# parser.parse(text) {|time, record| ... } みたいに使う。 | |
yield Engine.now, {} | |
end | |
end | |
end | |
end |
This file contains hidden or 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
<source> | |
type tcp | |
format dev_null | |
port 13337 | |
tag nothing | |
</source> | |
<match nothing> | |
type stdout | |
</match> |
This file contains hidden or 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
vagrant@precise64:~$ cat t.conf | |
<source> | |
type tcp | |
format dev_null | |
port 13337 | |
tag nothing | |
</source> | |
<match nothing> | |
type stdout | |
</match> | |
vagrant@precise64:~$ ls my_plugins/ | |
parser_dev_null.rb | |
vagrant@precise64:~$ fluentd -c t.conf -p my_plugins/ | |
2014-12-01 02:46:36 +0000 [info]: starting fluentd-0.10.57 | |
2014-12-01 02:46:36 +0000 [info]: reading config file path="t.conf" | |
2014-12-01 02:46:37 +0000 [info]: gem 'fluent-plugin-record-reformer' version '0.4.0' | |
2014-12-01 02:46:37 +0000 [info]: gem 'fluentd' version '0.10.57' | |
2014-12-01 02:46:37 +0000 [info]: using configuration file: <ROOT> | |
<source> | |
type tcp | |
format dev_null | |
port 13337 | |
tag nothing | |
</source> | |
<match nothing> | |
type stdout | |
</match> | |
</ROOT> | |
2014-12-01 02:46:37 +0000 [info]: adding source type="tcp" | |
2014-12-01 02:46:37 +0000 [info]: adding match pattern="nothing" type="stdout" |
This file contains hidden or 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
vagrant@precise64:~$ echo 'this is a loooooooooooooooooooooooooooooooooooong message' | nc localhost 13337 |
This file contains hidden or 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
2014-12-01 02:46:43 +0000 nothing: {} |
This file contains hidden or 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
module Fluent | |
class TextParser | |
#パーサープラグインの名前 | |
class MaybeParser < Parser | |
Plugin.register_parser('maybe', self) | |
config_param :parser, :string | |
# フォールバックするNoneParserでのキーの名前。 | |
config_param :fallback_message_field_key, :string, :default => nil | |
def initialize | |
super | |
# いろいろとここで初期化 | |
end | |
def configure(conf={}) | |
super | |
conf['format'] = conf.delete('parser') # parserフィールドがformatの値なので、書き換えてパーサを初期化 | |
@parser = TextParser.new | |
@parser.configure(conf) | |
@fallback_parser = NoneParser.new | |
@fallback_parser.configure("message_key" => @fallback_message_field_key) | |
end | |
def parse(text, &block) | |
# パースに失敗したらfallback_parserを呼ぶだけ | |
@parser.parse(text) do |time, record| | |
if time.nil? or record.nil? | |
@fallback_parser.call(text, &block) | |
else | |
yield time, record | |
end | |
end | |
end | |
end | |
end | |
end |
This file contains hidden or 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
vagrant@precise64:~$ cat t.conf | |
<source> | |
type tcp | |
format maybe | |
parser json | |
port 13337 | |
tag something | |
</source> | |
<match something> | |
type stdout | |
</match> | |
vagrant@precise64:~$ ls my_plugins/ | |
parser_devnull.rb parser_maybe.rb | |
vagrant@precise64:~$ fluentd -c t.conf -p my_plugins/ | |
2014-12-01 03:30:59 +0000 [info]: starting fluentd-0.10.57 | |
2014-12-01 03:30:59 +0000 [info]: reading config file path="t.conf" | |
2014-12-01 03:30:59 +0000 [info]: gem 'fluent-plugin-record-reformer' version '0.4.0' | |
2014-12-01 03:30:59 +0000 [info]: gem 'fluentd' version '0.10.57' | |
2014-12-01 03:30:59 +0000 [info]: using configuration file: <ROOT> | |
<source> | |
type tcp | |
format maybe | |
parser json | |
port 13337 | |
tag something | |
</source> | |
<match something> | |
type stdout | |
</match> | |
</ROOT> | |
2014-12-01 03:30:59 +0000 [info]: adding source type="tcp" | |
2014-12-01 03:30:59 +0000 [info]: adding match pattern="something" type="stdout" |
This file contains hidden or 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
vagrant@precise64:~$ echo '{"hello":"world"}' | nc localhost 13337 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment