From 6921b470792bbed9a4322570758779ded8511aa9 Mon Sep 17 00:00:00 2001 From: Kenji Okimoto Date: Thu, 5 Jul 2018 09:33:31 +0900 Subject: [PATCH] Consider timezone when calculate timekey Because the timestamp of the midnight cannot be divisible by 86400 in localtime (JST). The timestamp of the midnight is divisible by 86400 only in UTC. Therefore we must consider offset from UTC in localtime. For example, at `2018-07-04 01:23:23 +0900`: If timekey is 86400 and path template is `/log/%Y%m%d.log`. In previous version, extract path template to `/log/20180703.log`. In this version, extract path template to `/log/20180704.log`. Fix #1986 Signed-off-by: Kenji Okimoto --- lib/fluent/plugin/output.rb | 19 +++++++++++++------ test/plugin/test_out_file.rb | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 6 deletions(-) diff --git a/lib/fluent/plugin/output.rb b/lib/fluent/plugin/output.rb index 3d64839264..feaa8b0c20 100644 --- a/lib/fluent/plugin/output.rb +++ b/lib/fluent/plugin/output.rb @@ -803,20 +803,17 @@ def metadata(tag, time, record) if !@chunk_key_time && !@chunk_key_tag @buffer.metadata() elsif @chunk_key_time && @chunk_key_tag - time_int = time.to_i - timekey = (time_int - (time_int % @buffer_config.timekey)).to_i + timekey = calculate_timekey(time) @buffer.metadata(timekey: timekey, tag: tag) elsif @chunk_key_time - time_int = time.to_i - timekey = (time_int - (time_int % @buffer_config.timekey)).to_i + timekey = calculate_timekey(time) @buffer.metadata(timekey: timekey) else @buffer.metadata(tag: tag) end else timekey = if @chunk_key_time - time_int = time.to_i - (time_int - (time_int % @buffer_config.timekey)).to_i + calculate_timekey(time) else nil end @@ -825,6 +822,16 @@ def metadata(tag, time, record) end end + def calculate_timekey(time) + time_int = time.to_i + if @buffer_config.timekey_use_utc + (time_int - (time_int % @buffer_config.timekey)).to_i + else + offset = Time.at(time_int).utc_offset + (time_int - ((time_int + offset)% @buffer_config.timekey)).to_i + end + end + def chunk_for_test(tag, time, record) require 'fluent/plugin/buffer/memory_chunk' diff --git a/test/plugin/test_out_file.rb b/test/plugin/test_out_file.rb index f68624effb..59d3080e42 100644 --- a/test/plugin/test_out_file.rb +++ b/test/plugin/test_out_file.rb @@ -557,6 +557,39 @@ def parse_system(text) check_gzipped_result(path, formatted_lines * 3) end + test 'append when JST' do + time = event_time("2011-01-02 03:14:15+09:00") + formatted_lines = %[2011-01-02T03:14:15+09:00\ttest\t{"a":1}\n] + %[2011-01-02T03:14:15+09:00\ttest\t{"a":2}\n] + + write_once = ->(){ + d = create_driver %[ + path #{TMP_DIR}/out_file_test + compress gz + append true + + timekey_use_utc false + + ] + d.run(default_tag: 'test'){ + d.feed(time, {"a"=>1}) + d.feed(time, {"a"=>2}) + } + d.instance.last_written_path + } + + path = write_once.call + assert_equal "#{TMP_DIR}/out_file_test.20110102.log.gz", path + check_gzipped_result(path, formatted_lines) + + path = write_once.call + assert_equal "#{TMP_DIR}/out_file_test.20110102.log.gz", path + check_gzipped_result(path, formatted_lines * 2) + + path = write_once.call + assert_equal "#{TMP_DIR}/out_file_test.20110102.log.gz", path + check_gzipped_result(path, formatted_lines * 3) + end + test '${chunk_id}' do time = event_time("2011-01-02 13:14:15 UTC") formatted_lines = %[2011-01-02T13:14:15Z\ttest\t{"a":1}\n] + %[2011-01-02T13:14:15Z\ttest\t{"a":2}\n]