Skip to content

Commit ca2cd7d

Browse files
committed
Handle milliseconds in time formatting
1 parent 144d6fc commit ca2cd7d

File tree

2 files changed

+4
-4
lines changed

2 files changed

+4
-4
lines changed

lib/fluent/plugin/filter_elasticsearch_timestamp_check.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,14 @@ def filter(tag, time, record)
2424
begin
2525
# all digit entry would be treated as epoch seconds or epoch millis
2626
if !!(timestamp =~ /\A[-+]?\d+\z/)
27-
num = timestamp.to_i
27+
num = timestamp.to_f
2828
# epoch second or epoch millis should be either 10 or 13 digits
2929
# other length should be considered invalid (until the next digit
3030
# rollover at 2286-11-20 17:46:40 Z
31-
next unless [10, 13].include?(num.to_s.length)
31+
next unless [10, 13].include?(Math.log10(num).to_i + 1)
3232
record['@timestamp'] = record['fluent_converted_timestamp'] =
3333
Time.at(
34-
num / (10 ** (num.to_s.length - 10))
34+
num / (10 ** ((Math.log10(num).to_i + 1) - 10))
3535
).strftime('%Y-%m-%dT%H:%M:%S.%L%z')
3636
break
3737
end

test/plugin/test_filter_elasticsearch_timestamp_check.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ def test_timestamp_with_digit(data)
4848
d.feed({'test' => 'notime'}.merge(timekey => timestamp))
4949
end
5050
filtered = d.filtered.map{|e| e.last}.first
51-
num = timestamp.to_i
51+
num = timestamp.to_f
5252
formatted_time = Time.at(
5353
num / (10 ** ((Math.log10(num).to_i + 1) - 10))
5454
).strftime('%Y-%m-%dT%H:%M:%S.%L%z')

0 commit comments

Comments
 (0)