
LogstashからElasticsearchへログ等の時系列データを送信する際に、インデックス数、シャード数の考慮により週次でのindexローテーションとすることがある。
単純にYYYY.MM.ddからYYYY.wwとするだけでは、年またぎのタイミングで想定外のindexが作成されることがあるので注意が必要となる。
結論サマリー
xxxx.ww とする。
Logstash DateFilter, Joda-Time仕様
※7.12の情報を抜粋しています。最新バージョンの情報を確認してください。
LogstashのDateFileterでは、Joda-Timeで日付処理を行っている。
https://www.elastic.co/guide/en/logstash/7.12/plugins-filters-date.html
Joda-Timeでの年・週・月・日の扱いは以下のようになっている。
http://www.joda.org/joda-time/key_format.html
Symbol Meaning Presentation Examples ------ ------- ------------ ------- x weekyear year 1996 w week of weekyear number 27 ... y year year 1996 D day of year number 189 M month of year month July; Jul; 07 d day of month number 10
記載の通り、wはweek of weekyearなので、x: weekyearと組合せるのが適切。
Elasticsearch output plugin
https://www.elastic.co/guide/en/logstash/7.12/plugins-outputs-elasticsearch.html
The index to write events to. This can be dynamic using the
%{foo}syntax. The default value will partition your indices by day so you can more easily delete old data or only search specific date ranges. Indexes may not contain uppercase characters. For weekly indexes ISO 8601 format is recommended, eg. logstash-%{+xxxx.ww}. Logstash uses Joda formats for the index pattern from event timestamp.
動作確認
jirbで各日付がどのようになるか動作確認する。
$ sudo apt install jruby
$ jirb -v
irb 0.9.6(09/06/30)
$ jirb
irb(main):004:0> org.joda.time.DateTime.new(2021, 1, 1,0,0,0).toString("YYYY.ww")
=> "2021.53"
...
irb(main):011:0> org.joda.time.DateTime.new(2021, 1, 1,0,0,0).toString("xxxx.ww")
=> "2020.53"
...
まとめると以下のような結果になった。
| 日時 | YYYY-ww | xxxx-ww | remarks |
|---|---|---|---|
| 2019/12/31 | 2019-01 | 2020-01 | YYYY過去index |
| 2020/1/1 | 2020-01 | 2020-01 | |
| 2020/12/31 | 2020-53 | 2020-53 | |
| 2021/1/1 | 2021-52 | 2020-53 | YYYY未来index |
| 2021/12/31 | 2021-52 | 2021-52 | |
| 2022/1/1 | 2022-52 | 2021-52 | YYYY未来index |
YYYYだと、各年のカレンダー(1/1曜日)次第で過去や未来のindexとなってしまう。
まとめ
Logstashで週次indexを使用する場合は、 xxxx.ww とする。