駄文型

プログラミングとか英語とかの話題を中心にした至極ちゃらんぽらんな日記です。

fluentdコンテナをDockerで立ててRailsのログをTreasureData by IDCFに保存する

参考

fluent.configの作成

<source>
  type forward
  bind 0.0.0.0
</source>

<match td.*.*>
  @type tdlog
  endpoint api.ybi.idcfcloud.net
  apikey $API_KEY
  auto_create_table
  use_ssl true
  buffer_type memory
  flush_interval 60s
</match>

例によってendpoint api.ybi.idcfcloud.netのところが重要で、これがないとログインエラーになる。

ローカルで動作確認

まずfluent-plugin-tdをインストール

$ gem install fluent-plugin-td

fluentd を起動。

$ fluentd -c fluent.config

JSONを投げる。TreasureDataにテーブルができていればOK。DBは事前につくっておく。td.<db_name>.<table_name>

$ echo '{"app":"command", "id":1, "message":"Hi!"}' | fluent-cat td.fluent.test

Docker

FROM fluent/fluentd
COPY fluent.conf /fluentd/etc/
RUN gem install fluent-plugin-td

copyはfluent/fluentdでやっているので必要ないはずだが、なぜか僕のビルド先(OpenShift)で動かなかったので明示的に入れた。docker runで起動すれば動く

$ docker build . -t $CONTAINER
$ docker run -p 24224:24224 $CONTAINER

Rails

fluent-loggerをインストール。

$ gem install fluent-logger

application_conraller.rb に下記を追加。requestの内容をログに含めることもできる。あとはRailsを起動してアクセスすればログが溜まっていく。はず。

Fluent::Logger::FluentLogger.open(nil, :host=>ENV['FLUENTD_SERVICE_HOST'], :port=>ENV['FLUENTD_SERVICE_PORT'])
before_action :fluentpost
def fluentpost
  Fluent::Logger.post("td.#{db_name}.#{table_name}",{
    path:   request.fullpath,
    method: request.request_method,
    user:   user_name,
    body:   request.body,
    time:   Time.current.to_s
  })
end