Skip to content

Commit 1b947bd

Browse files
authored
Merge pull request #2 from ryan-dyer-sp/master
Support v14 of fluent and update to check for invalid timestamps
2 parents 94a63c5 + 7e0a6ba commit 1b947bd

File tree

2 files changed

+20
-6
lines changed

2 files changed

+20
-6
lines changed

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.1.2"
3+
spec.version = "0.2.0"
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: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
module Fluent
1+
require 'fluent/plugin/filter'
2+
3+
module Fluent::Plugin
24
class ElasticsearchTimestampCheckFilter < Filter
35
Fluent::Plugin.register_filter('elasticsearch_timestamp_check', self)
46

@@ -16,12 +18,24 @@ def shutdown
1618
end
1719

1820
def filter(tag, time, record)
19-
existing = record['@timestamp'] || record['timestamp'] || record['time']
20-
if existing
21+
timestamps = [ record['@timestamp'], record['timestamp'], record['time'] ]
22+
timestamps.each do |timestamp|
23+
begin
24+
DateTime.parse(timestamp)
25+
valid = timestamp
26+
break
27+
rescue ArgumentError
28+
next
29+
else
30+
valid = false
31+
end
32+
end
33+
34+
if valid
2135
record['@timestamp'] =
22-
DateTime.parse(existing).strftime('%Y-%m-%dT%H:%M:%S.%L%z')
36+
DateTime.parse(valid).strftime('%Y-%m-%dT%H:%M:%S.%L%z')
2337
record['fluent_converted_timestamp'] =
24-
DateTime.parse(existing).strftime('%Y-%m-%dT%H:%M:%S.%L%z')
38+
DateTime.parse(valid).strftime('%Y-%m-%dT%H:%M:%S.%L%z')
2539
$log.debug("Timestamp parsed: #{record['@timestamp']}")
2640
else
2741
record['@timestamp'] = Time.now.strftime('%Y-%m-%dT%H:%M:%S.%L%z')

0 commit comments

Comments
 (0)