Skip to content

Instantly share code, notes, and snippets.

@kiyoto
Last active August 29, 2015 14:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kiyoto/fc736d38394f35ed6a87 to your computer and use it in GitHub Desktop.
Save kiyoto/fc736d38394f35ed6a87 to your computer and use it in GitHub Desktop.
DevNullParserで始めるFluentd パーサプラグイン入門 ref: http://qiita.com/kiyoto/items/9903718147adde4d6c32
<source>
type tail
format json
path /path/to/file
tag aoi.yu
</source>
<source>
type tcp
format none
tag akai.yu
</source>
2014-12-01 03:31:44 +0000 something: {"hello":"world"}
vagrant@precise64:~$ echo 'this is not JSON' | nc localhost 13337
2014-12-01 03:33:49 +0000 something: {"message":"this is not JSON"}
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
<source>
type tcp
format dev_null
port 13337
tag nothing
</source>
<match nothing>
type stdout
</match>
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"
vagrant@precise64:~$ echo 'this is a loooooooooooooooooooooooooooooooooooong message' | nc localhost 13337
2014-12-01 02:46:43 +0000 nothing: {}
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
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"
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