LoginSignup
13
10

More than 5 years have passed since last update.

fluentdで@timestampフィールドをレコードに追加する

Last updated at Posted at 2015-01-19

やりたいこと

スクリーンショット 2015-01-19 17.56.34.png

上記のような構成で、fluent-plugin-elasticsearchを使ってログ収集する際に、web-server側のイベント時間を@timestampフィールド(fluent-plugin-elasticsearchがタイムスタンプとして利用するフィールド)としてレコードに追加したい。

fluentdの設定

<match oreno.access>
  type record_reformer
  enable_ruby true
  tag oreno.access.timestamp
  <record>
    @timestamp ${time.strftime('%Y-%m-%dT%H:%M:%S%z')}
  </record>
</match>

fluent-plugin-record-reformerを利用して、こんな感じの設定をweb-serverのfluentdに追加した。(正規表現でフォーマットしないと、スペース入りのtimestampになってしまって、Elasticsearch側で正しく処理されなかった)

何が嬉しいのか

@timestampフィールドはデフォルトだとfluent-plugin-elasticsearchが動作しているfluentdのtimeを適用するので、アグリゲーターとして動かしているfluentdがダウンしている時間があった場合などに、再起動後からのタイムスタンプを付与してしまう。送信側でバッファしていたログがあった場合は、再起動後ではなく、ログの出力時間を基準にmetricsを構築しないと、正しいアクセス数などが集計できないため、こんな感じの設定を入れた。

13
10
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
13
10