零からの

ゆるーく綴るブログ

Fluentdのtime_slice_formatの挙動確認

file outputのオプションについて、time_slice_formatがよくわからなかったので挙動を確認してみた。

file - Fluentd

デフォルトだと%Y%m%dだが、挙動確認のため意図的に%Y%m%d%H%Mに変更する。

※なお、%Y%m%d%H%M%Sまで指定可能

確認環境

構成

.
├── docker
│   └── fluentd
│       ├── Dockerfile
│       └── etc
│           └── fluent.conf
└── docker-compose.yml

設定ファイル

docker/fluentd/etc/fluent.conf

<source>
  @type  forward
  @id    input1
  @label @mainstream
  port  24224
</source>

<filter **>
  @type stdout
</filter>

<label @mainstream>
  <match nginx>
    @type             file
    @id               output_nginx
    path              /fluentd/log/nginx.*.log
    symlink_path      /fluentd/log/nginx.log
    append            true
    time_slice_format %Y%m%d%H%M # ココ!!
    time_slice_wait   1m
    time_format       %Y%m%dT%H%M%S%z
  </match>
  <match **>
    @type             file
    @id               output_other
    path              /fluentd/log/data.*.log
    symlink_path      /fluentd/log/data.log
    append            true
    time_slice_format %Y%m%d
    time_slice_wait   10m
    time_format       %Y%m%dT%H%M%S%z
  </match>
</label>

Dockerfile

docker/fluentd/Dockerfile

FROM fluent/fluentd:v1.11.2-1.0
COPY docker/fluentd/etc/fluent.conf /fluentd/etc/fluent.conf

docker-compose.yml

version: '3'
services:
  nginx:
    image: nginx:latest
    environment:
      - TZ=Asia/Tokyo
    ports:
      - '8080:80'
    links:
      - fluentd
    logging:
      driver: fluentd
      options:
        fluentd-address: localhost:24224
        tag: nginx
  fluentd:
    build:
      context: .
      dockerfile: docker/fluentd/Dockerfile
    environment:
      - TZ=Asia/Tokyo
    ports:
      - '24224:24224'
      - '24224:24224/udp'

確認結果

1分でバッファファイルからフォーマット通りのファイルに変更されることを確認

# time_slice_formatの指定時間内はバッファファイル
/fluentd/log $ ls -lha
total 20
drwxr-xr-x    1 fluent   fluent      4.0K Aug  8 05:11 .
drwxr-xr-x    1 fluent   fluent      4.0K Aug  5 22:19 ..
-rw-r--r--    1 fluent   nogroup      401 Aug  8 05:11 nginx.b5ac56c0fae21db227b1f693e482e1e51.log
-rw-r--r--    1 fluent   nogroup       79 Aug  8 05:11 nginx.b5ac56c0fae21db227b1f693e482e1e51.log.meta
lrwxrwxrwx    1 fluent   nogroup       56 Aug  8 05:11 nginx.log -> /fluentd/log/nginx.b5ac56c0fae21db227b1f693e482e1e51.log

# time_slice_formatの指定時間経過後はフォーマット通りのファイル名に更新される
/fluentd/log $ ls -lha
total 16
drwxr-xr-x    1 fluent   fluent      4.0K Aug  8 05:13 .
drwxr-xr-x    1 fluent   fluent      4.0K Aug  5 22:19 ..
-rw-r--r--    1 fluent   nogroup      401 Aug  8 05:13 nginx.202008080511.log
lrwxrwxrwx    1 fluent   nogroup       56 Aug  8 05:11 nginx.log -> /fluentd/log/nginx.b5ac56c0fae21db227b1f693e482e1e51.log

今回は極端な例なので分単位にしたが、デフォルト通り1時間単位で良さそう。