LoginSignup
8
3

More than 3 years have passed since last update.

fluentdのoutputプラグインの作成

Posted at

fluentdのoutputプラグインを作成する道のりです。

Ruby, gemはすでに入っているものとします。以下の手順で進めます。

  1. fluentdをインストール
  2. サンプルプラグイン作成
  3. fluentd設定ファイルを修正
  4. サンプルプラグインでfluentdを起動
  5. サンプルプラグインを修正する形で開発
  6. gem作成

fluentdをインストール

$ gem install -N fluentd
$ fluentd --version
fluentd 1.7.4

シンプルな設定ファイルとして以下のファイルを作成します。

sample.conf
<source>
  @type tail
  path input.txt
  pos_file input.pos
  tag test
  <parse>
    @type none
  </parse>
</source>
<match test>
  @type stdout
</match>

fluentdを起動してみます。

$ touch input.txt
$ fluentd -c sample.conf

別のターミナルで以下のようにすると、

echo hello >> input.txt

さきほどのfluentdから以下のように出力され、正常に動いていることを確認できます。

2019-10-28 12:57:50.817045876 +0900 test: {"message":"hello"}

サンプルプラグイン作成

fluent-plugin-generateというコマンドでプラグインのひな型を生成できます。このコマンドはfluentdをインストールしたときに同時にインストールされています。

$ fluent-plugin-generate output sample
License: Apache-2.0
        create Gemfile
        create README.md
        create Rakefile
        create fluent-plugin-sample.gemspec
        create lib/fluent/plugin/out_sample.rb
        create test/helper.rb
        create test/plugin/test_out_sample.rb

fluent-plugin-generateに渡しているパラメータのoutputはプラグインの種類、2つ目のパラメータのsampleはプラグインの名前です。名前はここではサンプルとしていますが、初めから作りたいプラグインの内容に応じた名前をここで付けておいたほうが早いです。fluent-plugin-で始まる名前が慣例の模様。

ディレクトリが生成され、その中でgit initまで済ました状態になっています。とりあえずそのままコミットするのがいいかもしれません。

$ cd fluent-plugin-sample
$ git add -A
$ git commit -m 'first commit'
$ cd ..

作成されたディレクトリの中の lib/fluent/plugin/out_sample.rb というファイルがプラグインの本体になります。最低限動かすためのひな型として以下のようにします。なにも定義しないとfluentdが起動しないです。(BUG: output plugin must implement some methods. see developer documents. というエラーメッセージになります)

fluent-plugin-sample/lib/fluent/plugin/out_sample.rb
module Fluent
  module Plugin
    class SampleOutput < Fluent::Plugin::Output
      Fluent::Plugin.register_output("sample", self)

      def format(tag, time, record)
        [tag, time, record].to_msgpack
      end

      def formatted_to_msgpack_binary?
        true
      end

      def write(chunk)
        chunk.msgpack_each do |tag, time, data|
          p tag
          p time
          p data
        end
      end

    end
  end
end

fluentd設定ファイルを修正

さきほど作ったsample.confの後半を以下のようにして、sampleプラグインが動くように変更します。

<match test>
  @type sample
  <buffer>
    chunk_limit_records 1
  </buffer>
</match>

@typeのところが使用するoutputプラグインの名前です。

chunk_limit_recordsの設定は、デバッグ目的でバッファリングの容量を1レコードのみにして実質バッファリングせずに入力されたらすぐに出力することを指示するものです。

Config: Buffer Section - Fluentd
https://docs.fluentd.org/configuration/buffer-section#buffering-parameters

サンプルプラグインでfluentdを起動

fluent-plugin-generateで作成したサンプルプラグインを含めてfluentdを起動し、別のターミナルで echo hello >> input.txt とするとサンプルプラグインが動いていることを確認できます。

$ fluentd -c sample.conf -p fluent-plugin-sample/lib/fluent/plugin
...
"test"
1572238328
{"message"=>"hello"}

サンプルプラグインを修正する形で開発

fluent-plugin-sample/lib/fluent/plugin/out_sample.rbを好きなように開発します。
依存するgemがあれば、のちにプラグインをgem化するのに備えてfluent-plugin-sample.gemspecに以下のように書いておきます。

spec.add_runtime_dependency "foolibrary"

gem作成

fluent-plugin-sample/fluent-plugin-sample.gemspecを編集します。少なくともTODOと書かれている3行を以下のように適当に変更します。

  spec.summary       = %q{sampe fluent plugin.}
  spec.description   = %q{sampe fluent plugin.}
  spec.homepage      = "http://www.example.com/"

以下のコマンドを実行するとパッケージが生成されます。

$ cd fluent-plugin-sample
$ rake build

fluent-plugin-sampleの中のpkg/fluent-plugin-sample-0.1.0.gemにファイルが生成されます。

あとはRubyGemsに登録するなどすれば完了です。

参考

8
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
3