Fluentd meetup dive into fluent plugin

Post on 08-May-2015

25669 Views

Category:

Technology

3 Downloads

Preview:

Click to see full reader

DESCRIPTION

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

Transcript

Dive into Fluent Plugin

2012年2月4日土曜日

Site:repeatedly.github.com

Company:Preferred Infrastructure, Inc.

Love plugins:input: tailbuffer: memoryoutput: mongo

2012年2月4日土曜日

developed by

The missing log collector

What's Fluentd?

See keynote

2012年2月4日土曜日

Fluentd is abufferroutercollectorconverteraggregatoretc...

2012年2月4日土曜日

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

2012年2月4日土曜日

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

2012年2月4日土曜日

We can customize Fluentd using plugins :)

2012年2月4日土曜日

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日土曜日

Examplebased on bit.ly/fluentd-with-mongo

2012年2月4日土曜日

Install

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

fluent-plugin-xxx

2012年2月4日土曜日

Let’s type!

$ fluent-gem install fluent-plugin-mongo

2012年2月4日土曜日

Me!

Many Many

ManyPlugins!

2012年2月4日土曜日

<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日土曜日

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日土曜日

Attack!

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

2012年2月4日土曜日

$ 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日土曜日

Mongo

Apache

Fluentd

write

tail

insert

I’m a log!

eventbuffering

2012年2月4日土曜日

Warming up

2012年2月4日土曜日

Ruby

Fluentd Stack

OS

Cool.io

MessagePack

BufferInput

Output

2012年2月4日土曜日

Rubyruby-lang.org

2012年2月4日土曜日

Fluentd and plugins are written in Ruby.

2012年2月4日土曜日

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

2012年2月4日土曜日

MessagePackmsgpack.org

2012年2月4日土曜日

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日土曜日

Binary format,Header + Body,andVariable length.

2012年2月4日土曜日

Note thatRuby version can’t handle a Time object.

2012年2月4日土曜日

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

2012年2月4日土曜日

Source:github.com/msgpack

Wiki:wiki.msgpack.org/display/MSGPACK

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

2012年2月4日土曜日

Cool.iocoolio.github.com

2012年2月4日土曜日

Event driven framework built on top of libev.

2012年2月4日土曜日

Cool.io has Loop and Watchers withTransport wrappers.

2012年2月4日土曜日

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

2012年2月4日土曜日

Configuration

2012年2月4日土曜日

Fluentd loads plugins from $LOAD_PATH.

2012年2月4日土曜日

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日土曜日

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

2012年2月4日土曜日

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

2012年2月4日土曜日

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日土曜日

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日土曜日

Fluentd provides some useful mixins forinput and output plugins.

2012年2月4日土曜日

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日土曜日

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日土曜日

Input

2012年2月4日土曜日

Default 3rd party

Available plugins

execforwardhttpstreamsyslogtailetc...

mongo_tailscribemsgpackdstatzmqamqpetc...

2012年2月4日土曜日

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

def start # invoke action end

def shutdown # cleanup resources endend

2012年2月4日土曜日

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日土曜日

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

2012年2月4日土曜日

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

def run ... endend

Thread

2012年2月4日土曜日

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

Event loop

2012年2月4日土曜日

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

2012年2月4日土曜日

Buffer

2012年2月4日土曜日

Default 3rd party

Available plugins

memoryfilezfile (?)

2012年2月4日土曜日

In most cases,Memory and File are enough.

2012年2月4日土曜日

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

2012年2月4日土曜日

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

2012年2月4日土曜日

Output

2012年2月4日土曜日

Default 3rd party

Available plugins

copyexecfileforwardnullstdoutetc...

mongos3scribecouchhoopsplunketc...

2012年2月4日土曜日

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日土曜日

Output has 3 buffering modes.NoneBufferedTime sliced

2012年2月4日土曜日

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日土曜日

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日土曜日

Test

2012年2月4日土曜日

Input:Fluent::Test::InputTestDriver

Buffer:Fluent::Test::BufferedOutputTestDriver

Output:Fluent::Test::OutputTestDriver

2012年2月4日土曜日

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日土曜日

...

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日土曜日

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

2012年2月4日土曜日

Release

2012年2月4日土曜日

Gem Structure

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

2012年2月4日土曜日

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日土曜日

See released pluginsfor more details about each file.

2012年2月4日土曜日

Lastly...

2012年2月4日土曜日

Help!2012年2月4日土曜日

Question?

2012年2月4日土曜日

top related