2
2
3
3
module Fluent ::Plugin
4
4
class ElasticsearchTimestampCheckFilter < Filter
5
+ attr_reader :timestamp_digits
6
+
5
7
Fluent ::Plugin . register_filter ( 'elasticsearch_timestamp_check' , self )
6
8
7
9
config_param :subsecond_precision , :integer , default : 3
@@ -11,6 +13,15 @@ def configure(conf)
11
13
require 'date'
12
14
raise Fluent ::ConfigError , "specify 1 or bigger number." if subsecond_precision < 1
13
15
@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
14
25
end
15
26
16
27
def start
@@ -29,10 +40,11 @@ def filter(tag, time, record)
29
40
# all digit entry would be treated as epoch seconds or epoch millis
30
41
if !!( timestamp =~ /\A [-+]?\d +\z / )
31
42
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
33
44
# other length should be considered invalid (until the next digit
34
45
# 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 )
36
48
record [ '@timestamp' ] = record [ 'fluent_converted_timestamp' ] =
37
49
Time . at (
38
50
num / ( 10 ** ( ( Math . log10 ( num ) . to_i + 1 ) - 10 ) )
0 commit comments