fluent-plugin-event-tail 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # gem.dependencies
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Mario Freitas (imkira@gmail.com)
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,123 @@
1
+ # fluent-plugin-event-tail
2
+
3
+ event-tail is an input plugin for [fluentd](http://fluentd.org) based on
4
+ [in_tail](http://docs.fluentd.org/articles/in_tail) but for reading
5
+ [tag, time, record] JSON messages from a file.
6
+
7
+ If you use ```fluent-logger-ruby```, ```fluent-logger-node```, and so on, you
8
+ are probably connecting to 'localhost' (or worse, to a remote host) and sending
9
+ messages via TCP sockets.
10
+
11
+ That's all fine but what happens if fluentd is down for some reason?
12
+ Well, most (if not all) logger implementations keep a list of pending failed
13
+ messages and try sending them periodically. But what if your application dies
14
+ before having the chance to flush everything to fluentd? Well, you will
15
+ probably lose those pending messages.
16
+
17
+ The reason I made this plugin is to allow me (and hopefully you too) to rather
18
+ send those messages, not via TCP sockets, but directly to a file and have
19
+ fluentd read them. The obvious advantage is that you always have backups of
20
+ your logs in your hard disks in case fluentd is not running or the DB where you
21
+ are aggregating them died for some reason. Another reason is, your logs are
22
+ still human readable and easily transformable (they are just newline separated
23
+ JSON strings).
24
+
25
+ The format of the messages is based on
26
+ [in_forward](http://docs.fluentd.org/articles/in_forward) plugin:
27
+
28
+ ```
29
+ stream:
30
+ message...
31
+
32
+ message:
33
+ [tag, time, record]
34
+ or
35
+ [tag, [[time,record], [time,record], ...]]
36
+
37
+ example:
38
+ ["myapp.access", [1308466941, {"a"=>1}], [1308466942, {"b"=>2}]]
39
+ ```
40
+
41
+ This plugin is mostly based on
42
+ [in_tail](http://docs.fluentd.org/articles/in_tail),
43
+ and therefore you are expected to append newline separated JSON strings of
44
+ messages in the above format. Also note that this plugin supports formatted
45
+ time strings via the ```time_format``` config parameter, not just numeric UNIX
46
+ timestamps.
47
+
48
+ ## Installation
49
+
50
+ Add this line to your application's Gemfile:
51
+
52
+ gem 'fluent-plugin-event-tail'
53
+
54
+ And then execute:
55
+
56
+ $ bundle
57
+
58
+ Or install it yourself as:
59
+
60
+ $ gem install fluent-plugin-event-tail
61
+
62
+ ## Configuration
63
+
64
+ This plugin has the same configuration as
65
+ [in_tail](http://docs.fluentd.org/articles/in_tail),
66
+ with the exception of ```tag``` and ```format``` that were removed.
67
+
68
+ ```
69
+ # fluent.conf
70
+ <match prefix.**>
71
+ type event_tail
72
+ path /var/log/your_app.log
73
+ pos_file /var/log/your_app.log.pos
74
+ # disable next line to enable custom time formatting
75
+ # time_format %d %b %Y %H:%M:%S
76
+ </match>
77
+ ```
78
+
79
+ ## Usage
80
+
81
+ After running fluentd, you can emit events by appending to the file:
82
+
83
+ ```
84
+ # time 0 means "current time on server"
85
+ echo '["prefix.debug",0,{"foo":"bar"}]' >> /var/log/your_app.log
86
+ # time passed as UNIX timestamp (2013-02-20 01:18:31 +0900)
87
+ echo '["prefix.debug1",1361290711,{"foo1":"bar1"}]' >> /var/log/your_app.log
88
+ # same time but passed as string
89
+ echo '["prefix.debug2","2009-03-26 22:33:12 +0900",{"foo2":"bar2"}]' >> /var/log/your_app.log
90
+ # group multiple events in one line by tag
91
+ echo '["prefix.debug3",[[1361290714,{"foo3":"bar3"}],[1361290716,{"foo4":"bar4"}]]]' >> /var/log/your_app.log
92
+ ```
93
+
94
+ fluentd will report something like:
95
+ ```
96
+ prefix.debug: {"foo":"bar"}
97
+ prefix.debug1: {"foo1":"bar1"}
98
+ prefix.debug2: {"foo2":"bar2"}
99
+ prefix.debug3: {"foo3":"bar3"}
100
+ prefix.debug3: {"foo4":"bar4"}
101
+ ````
102
+
103
+ Please note that contrarily to ```in_forward``` that only accepts UNIX
104
+ timestamps, fluent-plugin-event-tail supports the original in_tail
105
+ ```time_format``` parameter, so you can also pass strings as event times.
106
+ The default value of ```time_format``` is ```'%Y-%m-%d %H:%M:%S %z```.
107
+ If you pass a numeric UNIX timestamp then ```time_format``` will be ignored.
108
+
109
+ ## Contributing
110
+
111
+ You are very welcome to submit patches or improve this plugin.
112
+ Just make sure you send me a pull request.
113
+
114
+ ## License
115
+
116
+ fluent-plugin-event-tail is licensed under the MIT license:
117
+
118
+ www.opensource.org/licenses/MIT
119
+
120
+ ## Copyright
121
+
122
+ Copyright (c) 2013 Mario Freitas. See
123
+ [LICENSE.txt](http://github.com/imkira/fluent-plugin-event-tail/blob/master/LICENSE.txt) for further details.
@@ -0,0 +1,7 @@
1
+ require 'bundler/gem_tasks'
2
+
3
+ require 'rake/testtask'
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs = ['lib']
6
+ t.test_files = FileList['test/**/*.rb']
7
+ end
@@ -0,0 +1,20 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+
5
+ Gem::Specification.new do |gem|
6
+ gem.name = "fluent-plugin-event-tail"
7
+ gem.version = "0.0.1"
8
+ gem.authors = ["Mario Freitas"]
9
+ gem.email = ["imkira@gmail.com"]
10
+ gem.description = %q{fluentd input plugin derived from in_tail and inspired by in_forward for reading [tag, time, record] messages from a file}
11
+ gem.summary = %q{fluentd input plugin for reading [tag, time, record] messages from a file}
12
+ gem.homepage = "https://github.com/imkira/fluent-plugin-event-tail"
13
+
14
+ gem.files = `git ls-files`.split($/)
15
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
16
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
17
+ gem.require_paths = ["lib"]
18
+ gem.add_development_dependency "fluentd"
19
+ gem.add_runtime_dependency "fluentd"
20
+ end
@@ -0,0 +1,102 @@
1
+
2
+ # -*- encoding: utf-8 -*-
3
+
4
+ # Copyright (c) 2013 Mario Freitas (imkira@gmail.com)
5
+ #
6
+ # MIT License
7
+ #
8
+ # Permission is hereby granted, free of charge, to any person obtaining
9
+ # a copy of this software and associated documentation files (the
10
+ # "Software"), to deal in the Software without restriction, including
11
+ # without limitation the rights to use, copy, modify, merge, publish,
12
+ # distribute, sublicense, and/or sell copies of the Software, and to
13
+ # permit persons to whom the Software is furnished to do so, subject to
14
+ # the following conditions:
15
+ #
16
+ # The above copyright notice and this permission notice shall be
17
+ # included in all copies or substantial portions of the Software.
18
+ #
19
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21
+ # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22
+ # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
23
+ # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
24
+ # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
25
+ # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26
+
27
+ require 'fluent/plugin/in_tail'
28
+
29
+ module Fluent
30
+ class EventTailInput < Fluent::TailInput
31
+ Fluent::Plugin.register_input('event_tail', self)
32
+
33
+ config_param :time_format, :string, :default => '%Y-%m-%d %H:%M:%S %z'
34
+
35
+ # don't need tag parameter
36
+ config_set_default(:tag, '')
37
+
38
+ # don't need format parameter
39
+ config_set_default(:format, '')
40
+
41
+ def configure_parser(conf)
42
+ # just disable the default parser
43
+ end
44
+
45
+ def receive_lines(lines)
46
+ es = MultiEventStream.new
47
+ tag = nil
48
+ lines.each do |line|
49
+ begin
50
+ line.chomp!
51
+ tag = parse_line(line) do |time, record|
52
+ es.add(time, record)
53
+ end
54
+ rescue
55
+ $log.warn line.dump, :error=>$!.to_s
56
+ $log.debug_backtrace
57
+ end
58
+ end
59
+
60
+ unless tag.nil? || es.empty?
61
+ begin
62
+ Engine.emit_stream(tag, es)
63
+ rescue => e
64
+ # ignore errors. Engine shows logs and backtraces.
65
+ end
66
+ end
67
+ end
68
+
69
+ def parse_line(line, &block)
70
+ msg = Yajl.load(line)
71
+ tag = msg[0].to_s
72
+ entries = msg[1]
73
+
74
+ # [tag, [[time,record], [time,record], ...]]
75
+ if entries.is_a? Array
76
+ entries.each do |e|
77
+ time = parse_time(e[0])
78
+ record = e[1]
79
+ block.call(time, record)
80
+ end
81
+
82
+ # [tag, time, record]
83
+ else
84
+ time = parse_time(msg[1])
85
+ record = msg[2]
86
+ block.call(time, record)
87
+ end
88
+
89
+ tag
90
+ end
91
+
92
+ def parse_time(time)
93
+ if !@time_format.nil? and time.is_a? String
94
+ Time.strptime(time, @time_format).to_i
95
+ else
96
+ time = time.to_i
97
+ time = Engine.now if time == 0
98
+ time
99
+ end
100
+ end
101
+ end
102
+ end
@@ -0,0 +1,131 @@
1
+ require 'fluent/test'
2
+ require 'fluent/plugin/in_event_tail'
3
+
4
+ class EventTailInputTest < Test::Unit::TestCase
5
+ def setup
6
+ Fluent::Test.setup
7
+ FileUtils.rm_rf(TMP_DIR)
8
+ FileUtils.mkdir_p(TMP_DIR)
9
+ end
10
+
11
+ TMP_DIR = File.dirname(__FILE__) + "/../tmp"
12
+
13
+ CONFIG = %[
14
+ path #{TMP_DIR}/tail.log
15
+ tag t1
16
+ rotate_wait 2s
17
+ pos_file #{TMP_DIR}/tail.pos
18
+ ]
19
+
20
+ def create_driver(conf = CONFIG)
21
+ Fluent::Test::InputTestDriver.new(Fluent::EventTailInput).configure(conf)
22
+ end
23
+
24
+ def test_configure
25
+ d = create_driver
26
+ assert_equal ["#{TMP_DIR}/tail.log"], d.instance.paths
27
+ assert_equal "t1", d.instance.tag
28
+ assert_equal 2, d.instance.rotate_wait
29
+ assert_equal "#{TMP_DIR}/tail.pos", d.instance.pos_file
30
+ end
31
+
32
+ def test_simple_emit
33
+ File.open("#{TMP_DIR}/tail.log", "w") {|f|
34
+ f.puts '["foo",123,{"bar":"hoge"}]'
35
+ }
36
+
37
+ d = create_driver
38
+
39
+ d.run do
40
+ sleep 1
41
+
42
+ File.open("#{TMP_DIR}/tail.log", "a") {|f|
43
+ f.puts '["foo3",789,{"bar3":"hoge3"}]'
44
+ }
45
+ sleep 1
46
+ end
47
+
48
+ emits = d.emits
49
+ assert_equal(emits.length, 1)
50
+ assert_equal("foo3", emits[0][0])
51
+ assert_equal(789, emits[0][1])
52
+ assert_equal({"bar3"=>"hoge3"}, emits[0][2])
53
+ end
54
+
55
+ def test_default_time_format
56
+ File.open("#{TMP_DIR}/tail.log", "w") {|f|
57
+ f.puts '["foo","10/Oct/2010:20:57:59 -0700",{"bar":"hoge"}]'
58
+ }
59
+
60
+ d = create_driver
61
+
62
+ d.run do
63
+ sleep 1
64
+
65
+ File.open("#{TMP_DIR}/tail.log", "a") {|f|
66
+ f.puts '["foo4","2012-10-22 11:57:59 -0100",{"bar4":"hoge4"}]'
67
+ }
68
+ sleep 1
69
+ end
70
+
71
+ emits = d.emits
72
+ assert_equal(emits.length, 1)
73
+ assert_equal("foo4", emits[0][0])
74
+ assert_equal(1350910679, emits[0][1])
75
+ assert_equal({"bar4"=>"hoge4"}, emits[0][2])
76
+ end
77
+
78
+ def test_composed_emit
79
+ File.open("#{TMP_DIR}/tail.log", "w") {|f|
80
+ f.puts '["foo",123,{"bar":"hoge"}]'
81
+ }
82
+
83
+ d = create_driver
84
+
85
+ d.run do
86
+ sleep 1
87
+
88
+ File.open("#{TMP_DIR}/tail.log", "a") {|f|
89
+ f.puts '["foo5",[[91011,{"bar5":"hoge5"}],' +
90
+ '["2011-10-24 12:30:20 -0400",{"bar6":"hoge6"}]]]'
91
+ }
92
+ sleep 1
93
+ end
94
+
95
+ emits = d.emits
96
+ assert_equal(emits.length, 2)
97
+ assert_equal("foo5", emits[0][0])
98
+ assert_equal(91011, emits[0][1])
99
+ assert_equal({"bar5"=>"hoge5"}, emits[0][2])
100
+ assert_equal("foo5", emits[1][0])
101
+ assert_equal(1319473820, emits[1][1])
102
+ assert_equal({"bar6"=>"hoge6"}, emits[1][2])
103
+ end
104
+
105
+ def test_custom_time_format
106
+ File.open("#{TMP_DIR}/tail.log", "w") {|f|
107
+ f.puts '["foo",123,{"bar":"hoge"}]'
108
+ }
109
+
110
+ conf = CONFIG + %[
111
+ time_format %d %b %Y %H:%M:%S
112
+ ]
113
+
114
+ d = create_driver(conf)
115
+
116
+ d.run do
117
+ sleep 1
118
+
119
+ File.open("#{TMP_DIR}/tail.log", "a") {|f|
120
+ f.puts '["foo7","6 Dec 2001 12:33:45",{"bar7":"hoge7"}]'
121
+ }
122
+ sleep 1
123
+ end
124
+
125
+ emits = d.emits
126
+ assert_equal(emits.length, 1)
127
+ assert_equal("foo7", emits[0][0])
128
+ assert_equal(1007609625, emits[0][1])
129
+ assert_equal({"bar7"=>"hoge7"}, emits[0][2])
130
+ end
131
+ end
metadata ADDED
@@ -0,0 +1,87 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: fluent-plugin-event-tail
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Mario Freitas
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-02-19 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: fluentd
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: fluentd
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ description: fluentd input plugin derived from in_tail and inspired by in_forward
47
+ for reading [tag, time, record] messages from a file
48
+ email:
49
+ - imkira@gmail.com
50
+ executables: []
51
+ extensions: []
52
+ extra_rdoc_files: []
53
+ files:
54
+ - .gitignore
55
+ - Gemfile
56
+ - LICENSE.txt
57
+ - README.md
58
+ - Rakefile
59
+ - fluent-plugin-event-tail.gemspec
60
+ - lib/fluent/plugin/in_event_tail.rb
61
+ - test/fluent/plugin/in_event_tail.rb
62
+ homepage: https://github.com/imkira/fluent-plugin-event-tail
63
+ licenses: []
64
+ post_install_message:
65
+ rdoc_options: []
66
+ require_paths:
67
+ - lib
68
+ required_ruby_version: !ruby/object:Gem::Requirement
69
+ none: false
70
+ requirements:
71
+ - - ! '>='
72
+ - !ruby/object:Gem::Version
73
+ version: '0'
74
+ required_rubygems_version: !ruby/object:Gem::Requirement
75
+ none: false
76
+ requirements:
77
+ - - ! '>='
78
+ - !ruby/object:Gem::Version
79
+ version: '0'
80
+ requirements: []
81
+ rubyforge_project:
82
+ rubygems_version: 1.8.24
83
+ signing_key:
84
+ specification_version: 3
85
+ summary: fluentd input plugin for reading [tag, time, record] messages from a file
86
+ test_files:
87
+ - test/fluent/plugin/in_event_tail.rb