fluentd-output-plugin 101

fluentdのoutputプラグインを書く

fluentdのoutput pluginをやっていく機会が来たので色々メモ

公式サイトのHow to

How to Write Input Plugin まずはここをざっと読む

fluent付属のpluginジェネレーターを使う

fluent-plugin-generateコマンドがfluentdをインストールする際についていくるので使う。 これでひな形が完成

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

pluginを実際に動作させる方法

コマンドでやる場合

$ fluentd -c td-agent.conf -p PATH_TO_PLUGIN/out_sample.rb

設定ファイルからやる場合

fluent-plugin-generateで作成されたrbファイルをpluginディレクトリに配置する

cp out_sample.rb FLUENT_PATH/lib/fluent/plugin/out_sample.rb

ファイルが有ると設定confファイルで有効化することが可能

<match *.*>
  @type uksolr
</match>

config ファイルとのデータ連携

こういうふうに書いたとき,

<match *.*>
  @type uksolr
  url http://localhost
  port 1919
</match>

pluginの中ではこういうふうに参照する

    config_param :url, :string, default: 'http://yahoo.co.jp' 
    config_param :port, :string, default: '80' 
    
    def configure(conf)
      super

      p @url  #標準出力に表示
      p @port #標準出力に表示
      log.info(solrurl: @solrurl) #log.***でデバッグログに表示する
      log.info(solr_core: @solr_core)
    end

デバッグログの設定方法はここを見る

適当に動作確認をする

ログにurlが入ってきていて、それをパースしてjsonにして出力するタイプのやつ。

require "fluent/plugin/output"
require 'uri'
require 'json'

module Fluent
  module Plugin
    class UksolrOutput < Fluent::Plugin::Output
      Fluent::Plugin.register_output("sample", self)
    
    config_param :url, :string, default: 'http://localhost' 
    config_param :port, :string, default: 'port' 
    
    def configure(conf)
      super

      #初回のみ、@urlと@portが表示される
      p @url
      p @port
      log.info(solrurl: @solrurl)
      log.info(solr_core: @solr_core)
    end
    
    def write(chunk)
      chunk.each do |time, record|
        #pathにurlのpathが入っている前提
        path=record["path"]
        puts "#{time} - #{path}"
        p ary = URI.decode_www_form(path)
        p Hash[ary]
        p JSON.dump(Hash[ary])
      end
    end
    end
  end
end

ざっくりと理解はできた感じ。次は

How to Write Output Plugin

Non-Buffered Mode(#process)
Sync-Buffered Mode(#write)
Async-Buffered Mode(#try_write)

の3つ挙動を確かめたいなー。

connvoi's Picture

About connvoi

肉とビールと料理と写真とゲーム たまに技術 python / Solr / PHP / ansible

アマゾンセール情報サイト アマセール管理人

Jp, Tokyo https://connvoi.com