LoginSignup
14
13

More than 5 years have passed since last update.

fluentd で json に time を残したい

Posted at

fluentd の ChangeLog によると、0.10.34 以降、in_tail の format ltsv などで time が json に残らなくなったようです。out_s3 で format_json にしていると、時刻を特定する手段がなくなるので要注意です。

自分の場合は td-agent の設定を

<match nginx.access>
  type s3
  (中略)
  format_json
</match>
<source>
  type tail
  format ltsv
  path /var/log/nginx/access.log
  pos_file /var/log/td-agent/nginx.access.log.pos
  tag nginx.access
  time_format %FT%T%:z
</source>

というようにしておいて、nginx で出力するログを

    log_format ltsv "time:$time_iso8601"
        "\thost:$remote_addr"
        "\tforwardedfor:$http_x_forwarded_for"
        "\treq:$request"
        "\tstatus:$status"
        "\tsize:$body_bytes_sent"
        "\treferer:$http_referer"
        "\tua:$http_user_agent"
        "\treqtime:$request_time"
        "\tcache:$upstream_http_x_cache"
        "\truntime:$upstream_http_x_runtime"
        "\tvhost:$host";

というようにしていました。タイムスタンプが ISO 8601 ですが大きな意味は無いです。これを S3 に飛ばすと、time が無くなってしまいました。time:$time_local でも同じでした。

これを何とかするには、まずアクセスログにタイムスタンプをもう一つ追加します。

    log_format ltsv "time:$time_iso8601"
        "\tts:$iso8601"     # ←ここ追加
        "\thost:$remote_addr"
        "\tforwardedfor:$http_x_forwarded_for"
        "\treq:$request"
        "\tstatus:$status"
        "\tsize:$body_bytes_sent"
        "\treferer:$http_referer"
        "\tua:$http_user_agent"
        "\treqtime:$request_time"
        "\tcache:$upstream_http_x_cache"
        "\truntime:$upstream_http_x_runtime"
        "\tvhost:$host";

そして、新たに追加した ts に犠牲になってもらいます。

<source>
  type tail
  format ltsv
  path /var/log/nginx/access.log
  pos_file /var/log/td-agent/nginx.access.log.pos
  tag nginx.access
  time_format %FT%T%:z
  time_key ts   # ←ここ追加
</source>

こうすれば、ts が犠牲となり、time は生き残ります。

使い方がおかしいとかあれば指摘してもらえるとうれしいです。あと、なんで time が消されるようになったのか気になります。

14
13
2

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