-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Consider timezone when calculate timekey #2054
Changes from 4 commits
6921b47
a5cf3b0
34fb65a
92225ca
1e9d52d
72339a2
f178b87
54bda48
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should consider There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe yes. How about the following function? diff --git a/lib/fluent/timezone.rb b/lib/fluent/timezone.rb
index bb6fa868..0e79dd03 100644
--- a/lib/fluent/timezone.rb
+++ b/lib/fluent/timezone.rb
@@ -139,5 +139,17 @@ module Fluent
return nil
end
+
+ def self.utc_offset(time, timezone)
+ return 0 if timezone.nil?
+
+ case timezone
+ when NUMERIC_PATTERN
+ Time.zone_offset(timezone)
+ when NAME_PATTERN
+ tz = TZInfo::Timezone.get(timezone)
+ tz.period_for_utc(time).utc_total_offset
+ end
+ end
end
end There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. looks good |
||
(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' | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -557,6 +557,42 @@ def parse_system(text) | |
check_gzipped_result(path, formatted_lines * 3) | ||
end | ||
|
||
test 'append when JST' do | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you add more test with different time and timezone, e.g. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. OK, I will add more test. |
||
with_timezone(Fluent.windows? ? "JST-9" : "Asia/Tokyo") 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 | ||
<buffer> | ||
timekey_use_utc false | ||
timekey_zone Asia/Tokyo | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you use "+0900" like value? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just commit the fix :) |
||
</buffer> | ||
] | ||
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 | ||
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] | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
accessing object member is slower than accessing instance variable.
@timekey_use_utc = @buffer_config.timekey_use_utc
inconfigure
is better.