Skip to content

Commit 9d43e35

Browse files
committed
Add an ability to specify arbitrary precision timestamp
1 parent 00ca651 commit 9d43e35

File tree

2 files changed

+29
-4
lines changed

2 files changed

+29
-4
lines changed

lib/fluent/plugin/filter_elasticsearch_timestamp_check.rb

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,12 @@ 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+
@strftime_format = "%Y-%m-%dT%H:%M:%S.%#{@subsecond_precision}N%z".freeze
1013
end
1114

1215
def start
@@ -32,13 +35,13 @@ def filter(tag, time, record)
3235
record['@timestamp'] = record['fluent_converted_timestamp'] =
3336
Time.at(
3437
num / (10 ** ((Math.log10(num).to_i + 1) - 10))
35-
).strftime('%Y-%m-%dT%H:%M:%S.%L%z')
38+
).strftime(@strftime_format)
3639
break
3740
end
3841

3942
# normal timestamp string processing
4043
record['@timestamp'] = record['fluent_converted_timestamp'] =
41-
DateTime.parse(timestamp).strftime('%Y-%m-%dT%H:%M:%S.%L%z')
44+
DateTime.parse(timestamp).strftime(@strftime_format)
4245
$log.debug("Timestamp parsed: #{record['@timestamp']}")
4346
break
4447
rescue ArgumentError
@@ -47,7 +50,7 @@ def filter(tag, time, record)
4750

4851
unless record['fluent_converted_timestamp']
4952
record['@timestamp'] = record['fluent_added_timestamp'] =
50-
Time.now.strftime('%Y-%m-%dT%H:%M:%S.%L%z')
53+
Time.now.strftime(@strftime_format)
5154
$log.debug("Timestamp added: #{record['@timestamp']}")
5255
end
5356

test/plugin/test_filter_elasticsearch_timestamp_check.rb

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,29 @@ def test_timestamp_with_digit(data)
5151
num = timestamp.to_f
5252
formatted_time = Time.at(
5353
num / (10 ** ((Math.log10(num).to_i + 1) - 10))
54-
).strftime('%Y-%m-%dT%H:%M:%S.%L%z')
54+
).strftime('%Y-%m-%dT%H:%M:%S.%3N%z')
55+
assert_true(filtered.key?("@timestamp"))
56+
assert_true(filtered.key?("fluent_converted_timestamp"))
57+
assert_equal(formatted_time, filtered["fluent_converted_timestamp"])
58+
end
59+
60+
data('@timestamp' => '@timestamp',
61+
'timestamp' => 'timestamp',
62+
'time' => 'time',
63+
'syslog_timestamp' => 'syslog_timestamp')
64+
def test_timestamp_with_digit_and_nano_precision(data)
65+
timekey = data
66+
precision = 9
67+
d = create_driver(%[subsecond_precision #{precision}])
68+
timestamp = '1505800348899'
69+
d.run(default_tag: 'test') do
70+
d.feed({'test' => 'notime'}.merge(timekey => timestamp))
71+
end
72+
filtered = d.filtered.map{|e| e.last}.first
73+
num = timestamp.to_f
74+
formatted_time = Time.at(
75+
num / (10 ** ((Math.log10(num).to_i + 1) - 10))
76+
).strftime("%Y-%m-%dT%H:%M:%S.%#{precision}N%z")
5577
assert_true(filtered.key?("@timestamp"))
5678
assert_true(filtered.key?("fluent_converted_timestamp"))
5779
assert_equal(formatted_time, filtered["fluent_converted_timestamp"])

0 commit comments

Comments
 (0)