Skip to content

Commit 5baa9bf

Browse files
committed
Handle user-defined precision timestamp
1 parent 86e4a68 commit 5baa9bf

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

lib/fluent/plugin/filter_elasticsearch_timestamp_check.rb

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
module Fluent::Plugin
44
class ElasticsearchTimestampCheckFilter < Filter
5+
attr_reader :timestamp_digits
6+
57
Fluent::Plugin.register_filter('elasticsearch_timestamp_check', self)
68

79
config_param :subsecond_precision, :integer, default: 3
@@ -11,6 +13,15 @@ def configure(conf)
1113
require 'date'
1214
raise Fluent::ConfigError, "specify 1 or bigger number." if subsecond_precision < 1
1315
@strftime_format = "%Y-%m-%dT%H:%M:%S.%#{@subsecond_precision}N%z".freeze
16+
@timestamp_digits = configure_digits
17+
end
18+
19+
def configure_digits
20+
subepoch_precision = 10 + @subsecond_precision
21+
timestamp_digits = [10, 13]
22+
timestamp_digits << subepoch_precision
23+
timestamp_digits.uniq!
24+
timestamp_digits
1425
end
1526

1627
def start
@@ -29,10 +40,11 @@ def filter(tag, time, record)
2940
# all digit entry would be treated as epoch seconds or epoch millis
3041
if !!(timestamp =~ /\A[-+]?\d+\z/)
3142
num = timestamp.to_f
32-
# epoch second or epoch millis should be either 10 or 13 digits
43+
# By default, epoch second or epoch millis should be either 10 or 13 digits
3344
# other length should be considered invalid (until the next digit
3445
# rollover at 2286-11-20 17:46:40 Z
35-
next unless [10, 13].include?(Math.log10(num).to_i + 1)
46+
# For user-defined precision also should handle here.
47+
next unless @timestamp_digits.include?(Math.log10(num).to_i + 1)
3648
record['@timestamp'] = record['fluent_converted_timestamp'] =
3749
Time.at(
3850
num / (10 ** ((Math.log10(num).to_i + 1) - 10))

test/plugin/test_filter_elasticsearch_timestamp_check.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ def test_timestamp_with_digit_and_nano_precision(data)
7777
timekey = data
7878
precision = 9
7979
d = create_driver(%[subsecond_precision #{precision}])
80-
timestamp = '1505800348899'
80+
timestamp = '1517878172013770000'
8181
d.run(default_tag: 'test') do
8282
d.feed({'test' => 'notime'}.merge(timekey => timestamp))
8383
end
@@ -86,6 +86,7 @@ def test_timestamp_with_digit_and_nano_precision(data)
8686
formatted_time = Time.at(
8787
num / (10 ** ((Math.log10(num).to_i + 1) - 10))
8888
).strftime("%Y-%m-%dT%H:%M:%S.%#{precision}N%z")
89+
assert_equal([10, 13, 10 + precision], d.instance.timestamp_digits)
8990
assert_true(filtered.key?("@timestamp"))
9091
assert_true(filtered.key?("fluent_converted_timestamp"))
9192
assert_equal(formatted_time, filtered["fluent_converted_timestamp"])

0 commit comments

Comments
 (0)