fluent-plugin-combiner 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,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ MjI2MTFmNDQxOTgxNjNjZDg2ZTZmNGNiMzljYzhjNTZlNDU2MmExOQ==
5
+ data.tar.gz: !binary |-
6
+ ZGYzZWU1NjExZjAyYTlhZTIyNzVlZWVmOWZkZjU1NmFhNDlkYWY3Mg==
7
+ SHA512:
8
+ metadata.gz: !binary |-
9
+ ZGU5NjM4N2NhZjBmNTdiODFhYzhjZjgxZmRjY2E4YWY3YmE5YzNkMzVmYjc1
10
+ NjhjMjJkOWQ2MTc2Yzc1OTRjY2NkZDQxYzdhMWQzMmE0N2ZhMjIwNzdkNTRk
11
+ YjI0NzZkNDhhNDdhYTcwY2Q2MWMxMTIwYmE2ZDNmNGI1YjE3Mjk=
12
+ data.tar.gz: !binary |-
13
+ MzM2ZjQ5ZWY5ZGViMjAxY2M0NGY3ZjM2ZWYwOGIzY2U1OTE1ZTk0ZTMyYmM1
14
+ MmVkNGQxMGQ5YjEzYThkNDBhOTBmNDRjMzMwOWM2MjBhOWQyODc4YTc2OTE5
15
+ YzQwNmY4MjUzY2E5OTI0MGJlNDYyMTdjNTU3ZGMwMGFhOWJkYTM=
@@ -0,0 +1,20 @@
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
18
+ *.swp
19
+ .conf
20
+ .idea
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in fluent-plugin-combiner.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 karahiyo
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,27 @@
1
+ # fluent-plugin-combiner
2
+
3
+ ## Installation
4
+
5
+ Add this line to your application's Gemfile:
6
+
7
+ gem 'fluent-plugin-combiner'
8
+
9
+ And then execute:
10
+
11
+ $ bundle
12
+
13
+ Or install it yourself as:
14
+
15
+ $ gem install fluent-plugin-combiner
16
+
17
+ ## Usage
18
+
19
+ TODO: Write usage instructions here
20
+
21
+ ## Contributing
22
+
23
+ 1. Fork it
24
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
25
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
26
+ 4. Push to the branch (`git push origin my-new-feature`)
27
+ 5. Create new Pull Request
@@ -0,0 +1,11 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ require 'rake/testtask'
4
+ Rake::TestTask.new(:test) do |test|
5
+ test.libs << 'lib' << 'test'
6
+ test.pattern = 'test/**/test_*.rb'
7
+ test.verbose = true
8
+ end
9
+
10
+ task :default => true
11
+
@@ -0,0 +1,25 @@
1
+ # coding: 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 |spec|
6
+ spec.name = "fluent-plugin-combiner"
7
+ spec.version = "0.0.1"
8
+ spec.authors = ["karahiyo"]
9
+ spec.email = ["a.ryuklnm@gmail.com"]
10
+ spec.summary = "Combine buffer output data to cut-down net-i/o load"
11
+ spec.description = "Combine buffer output data to cut-down net-i/o load"
12
+ spec.homepage = "https://github.com/karahiyo/fluent-plugin-combiner"
13
+
14
+ spec.rubyforge_project = "fluent-plugin-combiner"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+
24
+ spec.add_runtime_dependency "fluentd"
25
+ end
@@ -0,0 +1,144 @@
1
+ module Fluent
2
+ class CombinerOutput < Fluent::Output
3
+ Fluent::Plugin.register_output('combiner', self)
4
+
5
+ # config_param :hoge, :string, :default => 'hoge'
6
+ config_param :tag, :string, :default => 'combined'
7
+ config_param :tag_prefix, :string, :default => nil
8
+ config_param :input_tag_remove_prefix, :string, :default => nil
9
+ config_param :count_interval, :time, :default => 60
10
+ config_param :count_key, :string, :default => 'keys'
11
+
12
+ attr_accessor :hist
13
+ attr_accessor :tick, :last_checked
14
+
15
+ def initialize
16
+ super
17
+ require 'pathname'
18
+ end
19
+
20
+ def configure(conf)
21
+ super
22
+
23
+ @tick = @count_interval.to_i if @count_interval
24
+ @tag_prefix_string = @tag_prefix ? @tag_prefix + '.' : @tag + '.'
25
+ if @input_tag_remove_prefix
26
+ @remove_prefix_string = @input_tag_remove_prefix + '.'
27
+ @remove_prefix_length = @remove_prefix_string.length
28
+ end
29
+
30
+ @hist = initialize_hist
31
+ end
32
+
33
+ ## Fluent::Output main methods
34
+ def start
35
+ super
36
+ start_watch
37
+ end
38
+
39
+ def shutdown
40
+ super
41
+ @watcher.terminate
42
+ @watcher.join
43
+ end
44
+
45
+ def flush
46
+ flushed, @hist = @hist, initialize_hist(@hist.keys.dup)
47
+ generate_output(flushed)
48
+ end
49
+
50
+ def flush_emit
51
+ flushed = flush
52
+ Fluent::Engine.emit(@tag, Fluent::Engine.now, flushed)
53
+ end
54
+
55
+ def generate_output(data)
56
+ output = {}
57
+ data.each do |tag, hist|
58
+ output[add_prefix(stripped_tag(tag))] = hist
59
+ end
60
+ output
61
+ end
62
+
63
+ def emit(tag, es, chain)
64
+
65
+ es.each do |time, record|
66
+ keys = record[@count_key]
67
+ countup(tag, keys)
68
+ end
69
+
70
+ chain.next
71
+ end
72
+
73
+
74
+ ## Combiner's main methods
75
+ def initialize_hist(tags=nil)
76
+ hist = {}
77
+ if tags
78
+ tags.each do |tag|
79
+ hist[tag] = {:hist => {}, :sum => 0, :len => 0}
80
+ end
81
+ end
82
+ hist
83
+ end
84
+
85
+
86
+ def increment(tag, key)
87
+ @hist[tag] ||= {:hist => {}, :sum => 0, :len => 0}
88
+ if @hist[tag][:hist].key? key
89
+ @hist[tag][:hist][key] += 1
90
+ @hist[tag][:sum] += 1
91
+ else
92
+ @hist[tag][:hist][key] = 1
93
+ @hist[tag][:sum] += 1
94
+ @hist[tag][:len] += 1
95
+ end
96
+ @hist
97
+ end
98
+
99
+ def countup(tag, keys)
100
+ if keys.is_a?(Array)
101
+ keys.each {|k| increment(tag, k)}
102
+ elsif keys.is_a?(String)
103
+ increment(tag, keys)
104
+ end
105
+ end
106
+
107
+ def clear
108
+ @hist = initialize_hist(@hist.keys.dup)
109
+ end
110
+
111
+ ## Utils
112
+ def add_prefix(tag="")
113
+ return @tag_prefix if tag.empty?
114
+ return @tag_prefix_string + tag
115
+ end
116
+
117
+ def stripped_tag(tag)
118
+ return tag unless @input_tag_remove_prefix
119
+ return tag[@remove_prefix_length..-1] if tag.start_with?(@remove_prefix_string) && tag.length > @remove_prefix_length
120
+ return "" if tag == @input_tag_remove_prefix
121
+ return tag
122
+ end
123
+
124
+ private
125
+
126
+ ## watcher
127
+ def start_watch
128
+ @watcher = Thread.new(&method(:watch))
129
+ end
130
+
131
+ def watch
132
+ @last_checked = Fluent::Engine.now
133
+ while true
134
+ sleep 0.5
135
+ if Fluent::Engine.now - @last_checked >= @tick
136
+ now = Fluent::Engine.now
137
+ flush_emit
138
+ @last_checked = now
139
+ end
140
+ end
141
+ end
142
+
143
+ end
144
+ end
@@ -0,0 +1,30 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+
4
+ begin
5
+ Bundler.setup(:default, :development)
6
+ rescue Bundler::BundlerError => e
7
+ $stderr.puts e.message
8
+ $stderr.puts "Run `bundle install` to install missing gems"
9
+ exit e.status_code
10
+ end
11
+ require 'test/unit'
12
+
13
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '/../lib', ))
14
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
15
+ require 'fluent/test'
16
+ unless ENV.has_key? 'VERBOSE'
17
+ nulllogger = Object.new
18
+ nulllogger.instance_eval {|logj|
19
+ def method_missing(methos, *args)
20
+ # pass
21
+ end
22
+ }
23
+ $log = nulllogger
24
+ end
25
+
26
+ require 'fluent/plugin/out_combiner'
27
+
28
+ class Test::Unit::TestCase
29
+
30
+ end
@@ -0,0 +1,56 @@
1
+ require 'helper'
2
+
3
+ class CombinerOutputTest < Test::Unit::TestCase
4
+ def setup
5
+ Fluent::Test.setup
6
+ end
7
+
8
+ CONFIG = %[
9
+ count_key keys
10
+ count_interval 5s
11
+ tag_prefix combined
12
+ input_tag_remove_prefix test.input
13
+ ]
14
+
15
+ def create_driver(conf = CONFIG, tag='test')
16
+ Fluent::Test::OutputTestDriver.new(Fluent::CombinerOutput, tag).configure(conf)
17
+ end
18
+
19
+ def test_configure
20
+ d = create_driver
21
+ assert_equal 5, d.instance.tick
22
+ assert_equal 'combined', d.instance.tag
23
+ assert_equal 'keys', d.instance.count_key
24
+ assert_equal 'combined', d.instance.tag_prefix
25
+ assert_equal 'test.input', d.instance.input_tag_remove_prefix
26
+ end
27
+
28
+ def test_increment
29
+ f = create_driver
30
+ f.instance.increment("test.input", 'a')
31
+ expected = f.instance.flush
32
+ assert_equal({"combined" => {:hist => {"a" => 1}, :sum => 1, :len => 1}}, expected)
33
+ expected = f.instance.flush
34
+ assert_equal({"combined" => {:hist => {}, :sum => 0, :len => 0}}, expected)
35
+ end
36
+
37
+ def test_emit
38
+ f = create_driver
39
+ f.run do
40
+ 60.times do
41
+ f.emit({"keys" => ["A", "B", "C"]})
42
+ end
43
+ end
44
+ out = f.instance.flush
45
+ assert_equal({"combined.test" => {:hist => {"A" => 60, "B" => 60, "C" => 60},
46
+ :sum => 180, :len => 3}}, out)
47
+ end
48
+
49
+ def test_clear
50
+ f = create_driver
51
+ assert_equal({}, f.instance.hist)
52
+ f.instance.increment("test.input", 'A')
53
+ f.instance.clear
54
+ assert_equal({"combined" => {:hist => {}, :sum => 0, :len => 0}}, f.instance.flush)
55
+ end
56
+ end
metadata ADDED
@@ -0,0 +1,96 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: fluent-plugin-combiner
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - karahiyo
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-12-16 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ! '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ! '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: fluentd
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ! '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: Combine buffer output data to cut-down net-i/o load
56
+ email:
57
+ - a.ryuklnm@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - .gitignore
63
+ - Gemfile
64
+ - LICENSE.txt
65
+ - README.md
66
+ - Rakefile
67
+ - fluent-plugin-combiner.gemspec
68
+ - lib/fluent/plugin/out_combiner.rb
69
+ - test/helper.rb
70
+ - test/plugin/test_out_combiner.rb
71
+ homepage: https://github.com/karahiyo/fluent-plugin-combiner
72
+ licenses: []
73
+ metadata: {}
74
+ post_install_message:
75
+ rdoc_options: []
76
+ require_paths:
77
+ - lib
78
+ required_ruby_version: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ! '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ required_rubygems_version: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - ! '>='
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
88
+ requirements: []
89
+ rubyforge_project: fluent-plugin-combiner
90
+ rubygems_version: 2.1.9
91
+ signing_key:
92
+ specification_version: 4
93
+ summary: Combine buffer output data to cut-down net-i/o load
94
+ test_files:
95
+ - test/helper.rb
96
+ - test/plugin/test_out_combiner.rb