Skip to content

Commit 15a57e8

Browse files
authored
Merge pull request #10 from cosmo0920/add-arbitrary-precision-timestamp
Add an ability to specify arbitrary precision timestamp
2 parents 00ca651 + 77d532b commit 15a57e8

File tree

2 files changed

+42
-4
lines changed

2 files changed

+42
-4
lines changed

lib/fluent/plugin/filter_elasticsearch_timestamp_check.rb

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,13 @@ module Fluent::Plugin
44
class ElasticsearchTimestampCheckFilter < Filter
55
Fluent::Plugin.register_filter('elasticsearch_timestamp_check', self)
66

7+
config_param :subsecond_precision, :integer, default: 3
8+
79
def configure(conf)
810
super
911
require 'date'
12+
raise Fluent::ConfigError, "specify 1 or bigger number." if subsecond_precision < 1
13+
@strftime_format = "%Y-%m-%dT%H:%M:%S.%#{@subsecond_precision}N%z".freeze
1014
end
1115

1216
def start
@@ -32,13 +36,13 @@ def filter(tag, time, record)
3236
record['@timestamp'] = record['fluent_converted_timestamp'] =
3337
Time.at(
3438
num / (10 ** ((Math.log10(num).to_i + 1) - 10))
35-
).strftime('%Y-%m-%dT%H:%M:%S.%L%z')
39+
).strftime(@strftime_format)
3640
break
3741
end
3842

3943
# normal timestamp string processing
4044
record['@timestamp'] = record['fluent_converted_timestamp'] =
41-
DateTime.parse(timestamp).strftime('%Y-%m-%dT%H:%M:%S.%L%z')
45+
DateTime.parse(timestamp).strftime(@strftime_format)
4246
$log.debug("Timestamp parsed: #{record['@timestamp']}")
4347
break
4448
rescue ArgumentError
@@ -47,7 +51,7 @@ def filter(tag, time, record)
4751

4852
unless record['fluent_converted_timestamp']
4953
record['@timestamp'] = record['fluent_added_timestamp'] =
50-
Time.now.strftime('%Y-%m-%dT%H:%M:%S.%L%z')
54+
Time.now.strftime(@strftime_format)
5155
$log.debug("Timestamp added: #{record['@timestamp']}")
5256
end
5357

test/plugin/test_filter_elasticsearch_timestamp_check.rb

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,18 @@ def create_driver(conf='')
99
Fluent::Test::Driver::Filter.new(Fluent::Plugin::ElasticsearchTimestampCheckFilter).configure(conf)
1010
end
1111

12+
def test_configure
13+
assert_raise(Fluent::ConfigError) do
14+
create_driver(%[subsecond_precision -3])
15+
end
16+
assert_raise(Fluent::ConfigError) do
17+
create_driver(%[subsecond_precision 0])
18+
end
19+
assert_nothing_raised do
20+
create_driver(%[subsecond_precision 1])
21+
end
22+
end
23+
1224
def test_added_timestamp
1325
d = create_driver
1426
d.run(default_tag: 'test') do
@@ -51,7 +63,29 @@ def test_timestamp_with_digit(data)
5163
num = timestamp.to_f
5264
formatted_time = Time.at(
5365
num / (10 ** ((Math.log10(num).to_i + 1) - 10))
54-
).strftime('%Y-%m-%dT%H:%M:%S.%L%z')
66+
).strftime('%Y-%m-%dT%H:%M:%S.%3N%z')
67+
assert_true(filtered.key?("@timestamp"))
68+
assert_true(filtered.key?("fluent_converted_timestamp"))
69+
assert_equal(formatted_time, filtered["fluent_converted_timestamp"])
70+
end
71+
72+
data('@timestamp' => '@timestamp',
73+
'timestamp' => 'timestamp',
74+
'time' => 'time',
75+
'syslog_timestamp' => 'syslog_timestamp')
76+
def test_timestamp_with_digit_and_nano_precision(data)
77+
timekey = data
78+
precision = 9
79+
d = create_driver(%[subsecond_precision #{precision}])
80+
timestamp = '1505800348899'
81+
d.run(default_tag: 'test') do
82+
d.feed({'test' => 'notime'}.merge(timekey => timestamp))
83+
end
84+
filtered = d.filtered.map{|e| e.last}.first
85+
num = timestamp.to_f
86+
formatted_time = Time.at(
87+
num / (10 ** ((Math.log10(num).to_i + 1) - 10))
88+
).strftime("%Y-%m-%dT%H:%M:%S.%#{precision}N%z")
5589
assert_true(filtered.key?("@timestamp"))
5690
assert_true(filtered.key?("fluent_converted_timestamp"))
5791
assert_equal(formatted_time, filtered["fluent_converted_timestamp"])

0 commit comments

Comments
 (0)