Fluentd meetup dive into fluent plugin

73
Dive into Fluent Plugin 201224日土曜日

description

Fluentd meetup in Japan. I talked about "Dive into Fluent plugin"

Transcript of Fluentd meetup dive into fluent plugin

Page 1: Fluentd meetup dive into fluent plugin

Dive into Fluent Plugin

2012年2月4日土曜日

Page 2: Fluentd meetup dive into fluent plugin

Site:repeatedly.github.com

Company:Preferred Infrastructure, Inc.

Love plugins:input: tailbuffer: memoryoutput: mongo

2012年2月4日土曜日

Page 3: Fluentd meetup dive into fluent plugin

developed by

The missing log collector

What's Fluentd?

See keynote

2012年2月4日土曜日

Page 4: Fluentd meetup dive into fluent plugin

Fluentd is abufferroutercollectorconverteraggregatoretc...

2012年2月4日土曜日

Page 5: Fluentd meetup dive into fluent plugin

... but,Fluentd doesn’t have such features as a built-in.

2012年2月4日土曜日

Page 6: Fluentd meetup dive into fluent plugin

Instead,Fluentd has flexible plugin architecturewhich consists of Input, Output and Buffer.

2012年2月4日土曜日

Page 7: Fluentd meetup dive into fluent plugin

We can customize Fluentd using plugins :)

2012年2月4日土曜日

Page 8: Fluentd meetup dive into fluent plugin

Agenda

Yes, I talk about - an example of Fluentd plugins - Fluentd and libraries - how to develop a Fluentd plugins

No, I don’t talk about - the details of each plugin - the experience of production

2012年2月4日土曜日

Page 9: Fluentd meetup dive into fluent plugin

Examplebased on bit.ly/fluentd-with-mongo

2012年2月4日土曜日

Page 10: Fluentd meetup dive into fluent plugin

Install

Plugin name is ,and fluent-gem is included in Fluentd gem.

fluent-plugin-xxx

2012年2月4日土曜日

Page 11: Fluentd meetup dive into fluent plugin

Let’s type!

$ fluent-gem install fluent-plugin-mongo

2012年2月4日土曜日

Page 12: Fluentd meetup dive into fluent plugin

Me!

Many Many

ManyPlugins!

2012年2月4日土曜日

Page 13: Fluentd meetup dive into fluent plugin

<source> type tail format apache path /path/to/log tag mongo.apache</source>

<match mongo.**> type mongo database apache collection access host otherhost</match>

Input Output

fluentd.conf

2012年2月4日土曜日

Page 14: Fluentd meetup dive into fluent plugin

Start!

$ fluentd -c fluentd.conf2012-02-04 00:00:14 +0900: starting fluentd-0.10.82012-02-04 00:00:14 +0900: reading config file path="fluentd.conf"2012-02-04 00:00:14 +0900: adding source type="tail"2012-02-04 00:00:14 +0900: adding match pattern="mongo.**" type="mongo"█

2012年2月4日土曜日

Page 15: Fluentd meetup dive into fluent plugin

Attack!

$ ab -n 100 -c 10 http://localhost/

2012年2月4日土曜日

Page 16: Fluentd meetup dive into fluent plugin

$ mongo --host otherhost> use apache> db.access.find() { "type": "127.0.0.1", "method": "GET", "path": "/", "code": "200", "size": "44", "time": ISODate("2011-11-27T07:56:27Z") ... }has more...

2012年2月4日土曜日

Page 17: Fluentd meetup dive into fluent plugin

Mongo

Apache

Fluentd

write

tail

insert

I’m a log!

eventbuffering

2012年2月4日土曜日

Page 18: Fluentd meetup dive into fluent plugin

Warming up

2012年2月4日土曜日

Page 19: Fluentd meetup dive into fluent plugin

Ruby

Fluentd Stack

OS

Cool.io

MessagePack

BufferInput

Output

2012年2月4日土曜日

Page 20: Fluentd meetup dive into fluent plugin

Rubyruby-lang.org

2012年2月4日土曜日

Page 21: Fluentd meetup dive into fluent plugin

Fluentd and plugins are written in Ruby.

2012年2月4日土曜日

Page 22: Fluentd meetup dive into fluent plugin

... but note thatFluentd works on Ruby 1.9, goodbye 1.8!

2012年2月4日土曜日

Page 23: Fluentd meetup dive into fluent plugin

MessagePackmsgpack.org

2012年2月4日土曜日

Page 24: Fluentd meetup dive into fluent plugin

Serialization:JSON like fast and compact format.

RPC:Async and parallelism for high performance.

IDL:Easy to integrate and maintain the service.

2012年2月4日土曜日

Page 25: Fluentd meetup dive into fluent plugin

Binary format,Header + Body,andVariable length.

2012年2月4日土曜日

Page 26: Fluentd meetup dive into fluent plugin

Note thatRuby version can’t handle a Time object.

2012年2月4日土曜日

Page 27: Fluentd meetup dive into fluent plugin

So,we use an Integer object instead of a Time.

2012年2月4日土曜日

Page 28: Fluentd meetup dive into fluent plugin

Source:github.com/msgpack

Wiki:wiki.msgpack.org/display/MSGPACK

Mailing List:groups.google.com/group/msgpack

2012年2月4日土曜日

Page 29: Fluentd meetup dive into fluent plugin

Cool.iocoolio.github.com

2012年2月4日土曜日

Page 30: Fluentd meetup dive into fluent plugin

Event driven framework built on top of libev.

2012年2月4日土曜日

Page 31: Fluentd meetup dive into fluent plugin

Cool.io has Loop and Watchers withTransport wrappers.

2012年2月4日土曜日

Page 32: Fluentd meetup dive into fluent plugin

Fluentd has a default event loop.We can use @default_loop in the plugin.

2012年2月4日土曜日

Page 33: Fluentd meetup dive into fluent plugin

Configuration

2012年2月4日土曜日

Page 34: Fluentd meetup dive into fluent plugin

Fluentd loads plugins from $LOAD_PATH.

2012年2月4日土曜日

Page 35: Fluentd meetup dive into fluent plugin

Input:$LOAD_PATH/fluent/plugin/in_<type>.rb

Buffer:$LOAD_PATH/fluent/plugin/buf_<type>.rb

Output:$LOAD_PATH/fluent/plugin/out_<type>.rb

2012年2月4日土曜日

Page 36: Fluentd meetup dive into fluent plugin

We use ‘register_input’, ‘register_buffer’ and ‘register_output’ to register a plugin.

2012年2月4日土曜日

Page 37: Fluentd meetup dive into fluent plugin

We can load the plugin configuration usingconfig_param and configure method.config_param set config value to@<config name> automatically.

2012年2月4日土曜日

Page 38: Fluentd meetup dive into fluent plugin

class TailInput < Input Plugin.register_input(’tail’, self) config_param :path, :string ...end

<source> type tail path /path/to/log ...</source> fluentd.conf

in_tail.rb2012年2月4日土曜日

Page 39: Fluentd meetup dive into fluent plugin

One trick is here:

Fluentd’s configuration module does notverify a default value. So,we can use the nil like Tribool :)

config_param :tag, :string, :default => nil

Fluentd does not check the type

2012年2月4日土曜日

Page 40: Fluentd meetup dive into fluent plugin

Fluentd provides some useful mixins forinput and output plugins.

2012年2月4日土曜日

Page 41: Fluentd meetup dive into fluent plugin

SetTagKeyMixin:Provide ‘tag_key’ and ‘include_tag_key’.

SetTimeKeyMixin:Provide ‘time_key’ and ‘include_time_key’.

DetachMultiProcessMixin:Provide ‘detach_process’ andexecute an action in the multi-process.

2012年2月4日土曜日

Page 42: Fluentd meetup dive into fluent plugin

Code Flow

Mixin usage

class MongoOutput < BufferedOutput ... include SetTagKeyMixin config_set_default :include_tag_key, false ...end MongoOutput

SetTagKeyMixin

BufferedOutput

super

super

super

2012年2月4日土曜日

Page 43: Fluentd meetup dive into fluent plugin

Input

2012年2月4日土曜日

Page 44: Fluentd meetup dive into fluent plugin

Default 3rd party

Available plugins

execforwardhttpstreamsyslogtailetc...

mongo_tailscribemsgpackdstatzmqamqpetc...

2012年2月4日土曜日

Page 45: Fluentd meetup dive into fluent plugin

class NewInput < Input ... def configure(conf) # parse a configuration manually end

def start # invoke action end

def shutdown # cleanup resources endend

2012年2月4日土曜日

Page 46: Fluentd meetup dive into fluent plugin

In action method,we use Engine.emit to input data.

tag = "app.tag"time = Engine.nowrecord = {"key" => "value", ...}Engine.emit(tag, time, record)

Sample:

2012年2月4日土曜日

Page 47: Fluentd meetup dive into fluent plugin

How to read an input in an efficient way?We use a thread and an event loop.

2012年2月4日土曜日

Page 48: Fluentd meetup dive into fluent plugin

class ForwardInput < Fluent::Input ... def start ... @thread = Thread.new(&method(:run)) end

def run ... endend

Thread

2012年2月4日土曜日

Page 49: Fluentd meetup dive into fluent plugin

class ForwardInput < Fluent::Input ... def start @loop = Coolio::Loop.new @lsock = listen @loop.attach(@lsock) ... end ...end

Event loop

2012年2月4日土曜日

Page 50: Fluentd meetup dive into fluent plugin

Note thatWe must use Engine.now instead of Time.now

2012年2月4日土曜日

Page 51: Fluentd meetup dive into fluent plugin

Buffer

2012年2月4日土曜日

Page 52: Fluentd meetup dive into fluent plugin

Default 3rd party

Available plugins

memoryfilezfile (?)

2012年2月4日土曜日

Page 53: Fluentd meetup dive into fluent plugin

In most cases,Memory and File are enough.

2012年2月4日土曜日

Page 54: Fluentd meetup dive into fluent plugin

Memory type is default.It’s fast but can’t resume data.

2012年2月4日土曜日

Page 55: Fluentd meetup dive into fluent plugin

File type is persistent type.It can resume data from file.

2012年2月4日土曜日

Page 56: Fluentd meetup dive into fluent plugin

Output

2012年2月4日土曜日

Page 57: Fluentd meetup dive into fluent plugin

Default 3rd party

Available plugins

copyexecfileforwardnullstdoutetc...

mongos3scribecouchhoopsplunketc...

2012年2月4日土曜日

Page 58: Fluentd meetup dive into fluent plugin

class NewOutput < BufferedOutput # configure, start and shutdown # are same as input plugin

def format(tag, time, record) # convert event to raw string end

def write(chunk) # write chunk to target # chunk has multiple formatted data endend

2012年2月4日土曜日

Page 59: Fluentd meetup dive into fluent plugin

Output has 3 buffering modes.NoneBufferedTime sliced

2012年2月4日土曜日

Page 60: Fluentd meetup dive into fluent plugin

Buffered Time sliced

Buffering type

chunk

go out

chunk

chunk

from in

chunklimit

queuelimit

Buffer has an internal map to manage a chunk. A key is tag in Buffered, but a key is time slice in TimeSliced buffer.

def write(chunk) # chunk.key is time sliceend

2012年2月4日土曜日

Page 61: Fluentd meetup dive into fluent plugin

How to write an output in an efficient way?We can use multi-process (input too).

See: DetachMultiProcessMixin with detach_multi_process

2012年2月4日土曜日

Page 62: Fluentd meetup dive into fluent plugin

Test

2012年2月4日土曜日

Page 63: Fluentd meetup dive into fluent plugin

Input:Fluent::Test::InputTestDriver

Buffer:Fluent::Test::BufferedOutputTestDriver

Output:Fluent::Test::OutputTestDriver

2012年2月4日土曜日

Page 64: Fluentd meetup dive into fluent plugin

class MongoOutputTest < Test::Unit::TestCase def setup Fluent::Test.setup require 'fluent/plugin/out_mongo' end

def create_driver(conf = CONFIG) Fluent::Test::BufferedOutputTestDriver.new (Fluent::MongoOutput) { def start # prevent external access super end ... }.configure(conf) end

2012年2月4日土曜日

Page 65: Fluentd meetup dive into fluent plugin

...

def test_format # test format using emit and expect_format

end

def test_write d = create_driver t = emit_documents(d)

# return a result of write method collection_name, documents = d.run assert_equal([{...}, {...}, ...], documents) assert_equal('test', collection_name) end ...

end

2012年2月4日土曜日

Page 66: Fluentd meetup dive into fluent plugin

It’s a weak point in Fluentd... right?

2012年2月4日土曜日

Page 67: Fluentd meetup dive into fluent plugin

Release

2012年2月4日土曜日

Page 68: Fluentd meetup dive into fluent plugin

Gem Structure

Plugin root |-- lib/ | |-- fluent/ | |-- plugin/ | |- out_<name>.rb |- Gemfile |- fluent-plugin-<name>.gemspec |- Rakefile |- README.md(rdoc) |- VERSION

2012年2月4日土曜日

Page 69: Fluentd meetup dive into fluent plugin

Bundle with git

$ edit lib/fluent/plugin/out_<name>.rb

$ git add / commit

$ cat VERSION0.1.0

$ bunlde exec rake release

See: rubygems.org/gems/fluent-plugin-<name>

2012年2月4日土曜日

Page 70: Fluentd meetup dive into fluent plugin

See released pluginsfor more details about each file.

2012年2月4日土曜日

Page 71: Fluentd meetup dive into fluent plugin

Lastly...

2012年2月4日土曜日

Page 72: Fluentd meetup dive into fluent plugin

Help!2012年2月4日土曜日

Page 73: Fluentd meetup dive into fluent plugin

Question?

2012年2月4日土曜日