Skip to content

Commit f7eca3c

Browse files
committed
Code clean up; syslog_timestamp support
1 parent a28fc0c commit f7eca3c

File tree

3 files changed

+15
-22
lines changed

3 files changed

+15
-22
lines changed

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,11 @@ elasticsearch.
1616
* If *@timestamp* field already exists, it will ensure the format is correct
1717
by parse and convert to format '%Y-%m-%dT%H:%M:%S%z'.
1818

19-
* If a field named *timestamp* exists, it will parse that field and conver it to
20-
format '%Y-%m-%dT%H:%M:%S%z' then store it in *@timestamp* field.
19+
* If a field named *timestamp* or *time* or *syslog_timestamp* exists, it will
20+
parse that field and conver it to format '%Y-%m-%dT%H:%M:%S%z' then store it
21+
in *@timestamp* field.
2122

22-
* If neither of the above field exists, it will insert current time in
23+
* If none of the above field exists, it will insert current time in
2324
'%Y-%m-%dT%H:%M:%S%z' format as the *@timestamp* field.
2425

2526
## Usage

fluent-plugin-elasticsearch-timestamp-check.gemspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Gem::Specification.new do |spec|
22
spec.name = "fluent-plugin-elasticsearch-timestamp-check"
3-
spec.version = "0.2.1"
3+
spec.version = "0.2.3"
44
spec.authors = ["Richard Li"]
55
spec.email = ["[email protected]"]
66
spec.description = %q{fluent filter plugin to ensure @timestamp is in proper format}

lib/fluent/plugin/filter_elasticsearch_timestamp_check.rb

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,32 +18,24 @@ def shutdown
1818
end
1919

2020
def filter(tag, time, record)
21-
timestamps = [ record['@timestamp'], record['timestamp'], record['time'] ]
22-
valid = false
23-
timestamps.each do |timestamp|
21+
%w{@timestamp timestamp time syslog_timestamp}.map do |field|
22+
record[field]
23+
end.compact.each do |timestamp|
2424
begin
25-
if timestamp then
26-
DateTime.parse(timestamp)
27-
valid = timestamp
28-
break
29-
end
25+
record['@timestamp'] = record['fluent_converted_timestamp'] =
26+
DateTime.parse(timestamp).strftime('%Y-%m-%dT%H:%M:%S.%L%z')
27+
$log.debug("Timestamp parsed: #{record['@timestamp']}")
28+
break
3029
rescue ArgumentError
31-
next
3230
end
3331
end
3432

35-
if valid
36-
record['@timestamp'] =
37-
DateTime.parse(valid).strftime('%Y-%m-%dT%H:%M:%S.%L%z')
38-
record['fluent_converted_timestamp'] =
39-
DateTime.parse(valid).strftime('%Y-%m-%dT%H:%M:%S.%L%z')
40-
$log.debug("Timestamp parsed: #{record['@timestamp']}")
41-
else
42-
record['@timestamp'] = Time.now.strftime('%Y-%m-%dT%H:%M:%S.%L%z')
43-
record['fluent_added_timestamp'] =
33+
unless record['fluent_converted_timestamp']
34+
record['@timestamp'] = record['fluent_added_timestamp'] =
4435
Time.now.strftime('%Y-%m-%dT%H:%M:%S.%L%z')
4536
$log.debug("Timestamp added: #{record['@timestamp']}")
4637
end
38+
4739
record
4840
end
4941
end

0 commit comments

Comments
 (0)