fluent-plugin-measure_time 0.1.2 → 0.2.0

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: e19251ed1f95f372fe500788206264184ceaf759
4
- data.tar.gz: d7c8c6cf65cd9799d1f35ddfd1598669d1b4254e
3
+ metadata.gz: 5a8d172be4d254d2c2d37283bb71b50ec5f6a804
4
+ data.tar.gz: 40825b213be411cae4ea76b21665cf28876f07f9
5
5
  SHA512:
6
- metadata.gz: f5fdb0a564b5129be907d4a17ed6f0764c88fdd51224f62664d729e5ea6ac2065b72f1b2207a34d51abf4e757b6150e9b795a97c5263e627ce6eb7b52f88c415
7
- data.tar.gz: 2fce2f754b0023f3fbe81c9368392166eac41233d0a2e6e895dbfceb884b1f2aaa6d73e45a33a0b4e6f78ae4e9a88a277c289d5dfd5a39ccc73eb47f9f18aa6f
6
+ metadata.gz: 8b0acf5b99ca783344045e15f1b3bfbaca61321b3221b5196021b27c8cf3fc69043bcedcec6ad2d1234b9492c8c0f1ed7d456f896ee2bef9e941f85be0a92961
7
+ data.tar.gz: 9154958570ea846abcc7a8e116139cdfe6e6f6b484d78ead00677d467a270be02d4ddf7fffb6fbd167d236a637f2dcf43862f384a9bef242810e62db98927d07
data/.gitignore CHANGED
@@ -13,3 +13,4 @@ coverage
13
13
  pkg/
14
14
  .ruby-version
15
15
  *.log
16
+ Gemfile.fluentd.v10.lock
data/.travis.yml CHANGED
@@ -1,6 +1,11 @@
1
1
  rvm:
2
- - 1.9.2
3
2
  - 1.9.3
4
- - 2.0.0
3
+ - 2.0
4
+ - 2.1
5
+ - 2.2
6
+ - 2.3.0
7
+ before_install:
8
+ - gem update bundler
5
9
  gemfile:
10
+ - Gemfile.fluentd.v10
6
11
  - Gemfile
data/CHANGELOG.md CHANGED
@@ -1,3 +1,9 @@
1
+ ## 0.2.0 (2016/04/09)
2
+
3
+ Enhancements:
4
+
5
+ * Make it work with Fluentd v0.12
6
+
1
7
  ## 0.1.2 (2014/09/22)
2
8
 
3
9
  Changes:
data/Gemfile CHANGED
@@ -1,3 +1,6 @@
1
1
  source "http://rubygems.org"
2
2
 
3
3
  gemspec
4
+ gem 'test-unit'
5
+ gem 'fluent-plugin-grep'
6
+ gem 'pry-nav'
@@ -0,0 +1,6 @@
1
+ source "http://rubygems.org"
2
+
3
+ gemspec
4
+ gem 'fluentd', '~> 0.10.0'
5
+ gem 'test-unit'
6
+ gem 'fluent-plugin-grep'
data/README.md CHANGED
@@ -31,10 +31,12 @@ Use RubyGems:
31
31
  As an example, let's profile how long the [emit](https://github.com/sonots/fluent-plugin-grep/blob/master/lib/fluent/plugin/out_grep.rb#L56) method of [fluent-plugin-grep](https://github.com/sonots/fluent-plugin-grep) is taking.
32
32
  Configure fluentd.conf as below:
33
33
 
34
+ For Fluentd v0.10:
35
+
34
36
  ```apache
35
37
  <source>
36
38
  type measure_time
37
- # This makes available the `masure_time` directive for all plugins
39
+ # This makes available the `measure_time` directive for all plugins
38
40
  </source>
39
41
 
40
42
  <source>
@@ -59,7 +61,43 @@ Configure fluentd.conf as below:
59
61
  tag measure_time
60
62
  hook emit
61
63
  </measure_time>
64
+ </match>
65
+ ```
66
+
67
+ For Fluentd v0.12:
68
+
69
+ ```apache
70
+ <label @measure_time>
71
+ <match>
72
+ @type measure_time
73
+ # This makes available the `measure_time` directive for all plugins
74
+ </match>
75
+ </label>
76
+
77
+ <source>
78
+ @type dummy
79
+ tag raw.dummy
80
+ dummy {"message":"foo"}
62
81
  </source>
82
+
83
+ # measure_time plugin output comes here
84
+ <match measure_time>
85
+ @type stdout
86
+ </match>
87
+
88
+ # Whatever you want to do
89
+ <match greped.**>
90
+ @type stdout
91
+ </match>
92
+
93
+ <match **>
94
+ @type grep
95
+ add_tag_prefix greped
96
+ <measure_time>
97
+ tag measure_time
98
+ hook emit
99
+ </measure_time>
100
+ </match>
63
101
  ```
64
102
 
65
103
  The output of fluent-plugin-measure_time will be as below:
@@ -114,10 +152,12 @@ This profiling is very useful to investigate when you have a suspicion that thro
114
152
 
115
153
  The configuration will be as follows:
116
154
 
155
+ For Fluentd v0.10:
156
+
117
157
  ```apache
118
158
  <source>
119
159
  type measure_time
120
- # This makes available the `masure_time` directive for all plugins
160
+ # This makes available the `measure_time` directive for all plugins
121
161
  </source>
122
162
 
123
163
  <source>
@@ -139,6 +179,35 @@ The configuration will be as follows:
139
179
  </match>
140
180
  ```
141
181
 
182
+ For Fluentd v0.12:
183
+
184
+ ```apache
185
+ <label @measure_time>
186
+ <match>
187
+ @type measure_time
188
+ # This makes available the `measure_time` directive for all plugins
189
+ </match>
190
+ </match>
191
+
192
+ <source>
193
+ @type forward
194
+ port 24224
195
+ <measure_time>
196
+ tag measure_time
197
+ hook on_message
198
+ </measure_time>
199
+ </source>
200
+
201
+ <match measure_time>
202
+ @type stdout
203
+ </match>
204
+
205
+ # whatever you want
206
+ <match **>
207
+ @type stdout
208
+ </match>
209
+ ```
210
+
142
211
  Output becomes as below:
143
212
 
144
213
  ```
@@ -0,0 +1,29 @@
1
+ <source>
2
+ type measure_time
3
+ # This makes available the `measure_time` directive for all plugins
4
+ </source>
5
+
6
+ <source>
7
+ type dummy
8
+ tag raw.dummy
9
+ dummy {"message":"test"}
10
+ </source>
11
+
12
+ # measure_time plugin output comes here
13
+ <match measure_time>
14
+ type stdout
15
+ </match>
16
+
17
+ # Whatever you want to do
18
+ <match greped.**>
19
+ type stdout
20
+ </match>
21
+
22
+ <match **>
23
+ type grep
24
+ add_tag_prefix greped
25
+ <measure_time>
26
+ tag measure_time
27
+ hook emit
28
+ </measure_time>
29
+ </match>
@@ -0,0 +1,31 @@
1
+ <label @measure_time>
2
+ <match>
3
+ @type measure_time
4
+ # This makes available the `measure_time` directive for all plugins
5
+ </match>
6
+ </label>
7
+
8
+ <source>
9
+ @type dummy
10
+ tag raw.dummy
11
+ dummy {"message":"foo"}
12
+ </source>
13
+
14
+ # measure_time plugin output comes here
15
+ <match measure_time>
16
+ @type stdout
17
+ </match>
18
+
19
+ # Whatever you want to do
20
+ <match greped.**>
21
+ @type stdout
22
+ </match>
23
+
24
+ <match **>
25
+ @type grep
26
+ add_tag_prefix greped
27
+ <measure_time>
28
+ tag measure_time
29
+ hook emit
30
+ </measure_time>
31
+ </match>
@@ -3,7 +3,7 @@ $:.push File.expand_path('../lib', __FILE__)
3
3
 
4
4
  Gem::Specification.new do |gem|
5
5
  gem.name = "fluent-plugin-measure_time"
6
- gem.version = "0.1.2"
6
+ gem.version = "0.2.0"
7
7
  gem.authors = ["Naotoshi Seo"]
8
8
  gem.email = "sonots@gmail.com"
9
9
  gem.homepage = "https://github.com/sonots/fluent-plugin-measure_time"
@@ -1,135 +1,16 @@
1
- require 'fluent/input'
1
+ require 'fluent/plugin/measure_timable'
2
2
 
3
3
  module Fluent
4
4
  class MeasureTimeInput < Input
5
5
  Plugin.register_input('measure_time', self)
6
6
 
7
- def configure(conf)
8
- ::Fluent::Input.__send__(:include, MeasureTimable)
9
- ::Fluent::Output.__send__(:include, MeasureTimable)
10
- end
11
- end
12
-
13
- module MeasureTimable
14
- def self.included(klass)
15
- unless klass.method_defined?(:configure_without_measure_time)
16
- klass.__send__(:alias_method, :configure_without_measure_time, :configure)
17
- klass.__send__(:alias_method, :configure, :configure_with_measure_time)
18
- end
19
- end
20
-
21
- attr_reader :measure_time
22
-
23
- def configure_with_measure_time(conf)
24
- configure_without_measure_time(conf)
25
- if element = conf.elements.select { |element| element.name == 'measure_time' }.first
26
- @measure_time = MeasureTime.new(self, log)
27
- @measure_time.configure(element)
28
- end
29
- end
30
- end
31
-
32
- class MeasureTime
33
- attr_reader :plugin, :log, :times, :mutex, :thread, :tag, :interval, :hook
34
- def initialize(plugin, log)
35
- @plugin = plugin
36
- @klass = @plugin.class
37
- @log = log
38
- @times = []
39
- @mutex = Mutex.new
7
+ unless method_defined?(:router)
8
+ define_method(:router) { ::Fluent::Engine }
40
9
  end
41
10
 
42
11
  def configure(conf)
43
- @tag = conf['tag'] || 'measure_time'
44
- unless @hook = conf['hook']
45
- raise Fluent::ConfigError, '`hook` option must be specified in <measure_time></measure_time> directive'
46
- end
47
- @hook_msg = {:class => @klass.to_s, :hook => @hook.to_s, :object_id => @plugin.object_id.to_s}
48
- @interval = conf['interval'].to_i if conf['interval']
49
- @add_or_emit_proc =
50
- if @interval
51
- # add to calculate statistics in each interval
52
- Proc.new {|elapsed|
53
- @mutex.synchronize { @times << elapsed }
54
- }
55
- else
56
- # emit information immediately
57
- Proc.new {|elapsed|
58
- msg = {:time => elapsed}.merge(@hook_msg)
59
- ::Fluent::Engine.emit(@tag, ::Fluent::Engine.now, msg)
60
- }
61
- end
62
- apply_hook
63
- end
64
-
65
- def apply_hook
66
- @plugin.instance_eval <<EOF
67
- def #{@hook}(*args)
68
- measure_time.measure_time do
69
- super
70
- end
71
- end
72
- def start
73
- super
74
- measure_time.start
75
- end
76
- def stop
77
- super
78
- measure_time.stop
79
- end
80
- EOF
81
- end
82
-
83
- def measure_time
84
- started = Time.now
85
- output = yield
86
- elapsed = (Time.now - started).to_f
87
- log.debug "elapsed time at #{@klass}##{@hook} is #{elapsed} sec"
88
- @add_or_emit_proc.call(elapsed)
89
- output
90
- end
91
-
92
- def start
93
- return unless @interval
94
- @thread = Thread.new(&method(:run))
95
- end
96
-
97
- def stop
98
- return unless @interval
99
- @thread.terminate
100
- @thread.join
101
- end
102
-
103
- def run
104
- @last_checked ||= Engine.now
105
- while (sleep 0.5)
106
- begin
107
- now = Engine.now
108
- if now - @last_checked >= @interval
109
- flush(now)
110
- @last_checked = now
111
- end
112
- rescue => e
113
- log.warn "in_measure_time: hook #{@klass}##{@hook} #{e.class} #{e.message} #{e.backtrace.first}"
114
- end
115
- end
116
- end
117
-
118
- def flush(now)
119
- times = []
120
- @mutex.synchronize do
121
- times = @times.dup
122
- @times.clear
123
- end
124
- triple = nil
125
- unless times.empty?
126
- num = times.size
127
- max = num == 0 ? 0 : times.max
128
- avg = num == 0 ? 0 : times.map(&:to_f).inject(:+) / num.to_f
129
- triple = [@tag, now, {:max => max, :avg => avg, :num => num}.merge(@hook_msg)]
130
- Engine.emit(*triple)
131
- end
132
- triple
12
+ ::Fluent::Input.__send__(:include, MeasureTimable)
13
+ ::Fluent::Output.__send__(:include, MeasureTimable)
133
14
  end
134
15
  end
135
16
  end
@@ -0,0 +1,129 @@
1
+ module Fluent
2
+ module MeasureTimable
3
+ def self.included(klass)
4
+ unless klass.method_defined?(:configure_without_measure_time)
5
+ klass.__send__(:alias_method, :configure_without_measure_time, :configure)
6
+ klass.__send__(:alias_method, :configure, :configure_with_measure_time)
7
+ end
8
+
9
+ unless klass.method_defined?(:router)
10
+ define_method(:router) { ::Fluent::Engine }
11
+ end
12
+ end
13
+
14
+ attr_reader :measure_time
15
+
16
+ def configure_with_measure_time(conf)
17
+ configure_without_measure_time(conf)
18
+ if element = conf.elements.select { |element| element.name == 'measure_time' }.first
19
+ @measure_time = MeasureTime.new(self, log, router)
20
+ @measure_time.configure(element)
21
+ end
22
+ end
23
+ end
24
+
25
+ class MeasureTime
26
+ attr_reader :plugin, :log, :router, :times, :mutex, :thread, :tag, :interval, :hook
27
+ def initialize(plugin, log, router)
28
+ @plugin = plugin
29
+ @klass = @plugin.class
30
+ @log = log
31
+ @router = router
32
+ @times = []
33
+ @mutex = Mutex.new
34
+ end
35
+
36
+ def configure(conf)
37
+ @tag = conf['tag'] || 'measure_time'
38
+ unless @hook = conf['hook']
39
+ raise Fluent::ConfigError, '`hook` option must be specified in <measure_time></measure_time> directive'
40
+ end
41
+ @hook_msg = {:class => @klass.to_s, :hook => @hook.to_s, :object_id => @plugin.object_id.to_s}
42
+ @interval = conf['interval'].to_i if conf['interval']
43
+ @add_or_emit_proc =
44
+ if @interval
45
+ # add to calculate statistics in each interval
46
+ Proc.new {|elapsed|
47
+ @mutex.synchronize { @times << elapsed }
48
+ }
49
+ else
50
+ # emit information immediately
51
+ Proc.new {|elapsed|
52
+ msg = {:time => elapsed}.merge(@hook_msg)
53
+ router.emit(@tag, ::Fluent::Engine.now, msg)
54
+ }
55
+ end
56
+ apply_hook
57
+ end
58
+
59
+ def apply_hook
60
+ @plugin.instance_eval <<EOF
61
+ def #{@hook}(*args)
62
+ measure_time.measure_time do
63
+ super
64
+ end
65
+ end
66
+ def start
67
+ super
68
+ measure_time.start
69
+ end
70
+ def stop
71
+ super
72
+ measure_time.stop
73
+ end
74
+ EOF
75
+ end
76
+
77
+ def measure_time
78
+ started = Time.now
79
+ output = yield
80
+ elapsed = (Time.now - started).to_f
81
+ log.debug "elapsed time at #{@klass}##{@hook} is #{elapsed} sec"
82
+ @add_or_emit_proc.call(elapsed)
83
+ output
84
+ end
85
+
86
+ def start
87
+ return unless @interval
88
+ @thread = Thread.new(&method(:run))
89
+ end
90
+
91
+ def stop
92
+ return unless @interval
93
+ @thread.terminate
94
+ @thread.join
95
+ end
96
+
97
+ def run
98
+ @last_checked ||= ::Fluent::Engine.now
99
+ while (sleep 0.5)
100
+ begin
101
+ now = ::Fluent::Engine.now
102
+ if now - @last_checked >= @interval
103
+ flush(now)
104
+ @last_checked = now
105
+ end
106
+ rescue => e
107
+ log.warn "in_measure_time: hook #{@klass}##{@hook} #{e.class} #{e.message} #{e.backtrace.first}"
108
+ end
109
+ end
110
+ end
111
+
112
+ def flush(now)
113
+ times = []
114
+ @mutex.synchronize do
115
+ times = @times.dup
116
+ @times.clear
117
+ end
118
+ triple = nil
119
+ unless times.empty?
120
+ num = times.size
121
+ max = num == 0 ? 0 : times.max
122
+ avg = num == 0 ? 0 : times.map(&:to_f).inject(:+) / num.to_f
123
+ triple = [@tag, now, {:max => max, :avg => avg, :num => num}.merge(@hook_msg)]
124
+ router.emit(*triple)
125
+ end
126
+ triple
127
+ end
128
+ end
129
+ end
@@ -0,0 +1,19 @@
1
+ require 'fluent/plugin/measure_timable'
2
+
3
+ module Fluent
4
+ class MeasureTimeOutput < Output
5
+ Plugin.register_output('measure_time', self)
6
+
7
+ unless method_defined?(:router)
8
+ define_method(:router) { ::Fluent::Engine }
9
+ end
10
+
11
+ def configure(conf)
12
+ ::Fluent::Input.__send__(:include, MeasureTimable)
13
+ ::Fluent::Output.__send__(:include, MeasureTimable)
14
+ end
15
+
16
+ def emit(tag, time, msg)
17
+ end
18
+ end
19
+ end
@@ -7,7 +7,10 @@ describe Fluent::MeasureTimeInput do
7
7
  before { Fluent::Test.setup }
8
8
 
9
9
  def create_driver(conf=%[])
10
- Fluent::Test::InputTestDriver.new(Fluent::MeasureTimeInput).configure(conf)
10
+ d = Fluent::Test::InputTestDriver.new(Fluent::MeasureTimeInput).configure(conf)
11
+ unless d.respond_to?(:router)
12
+ d.singleton_class.send(:define_method, :router) { ::Fluent::Engine }
13
+ end
11
14
  end
12
15
 
13
16
  describe 'test configure' do
@@ -78,8 +81,8 @@ describe "extends Fluent::ForwardInput" do
78
81
  data = ['tag1', 0, {'a'=>1}].to_msgpack
79
82
  d.__send__(:on_message, data, data.bytesize, "hi, yay!")
80
83
  triple = d.measure_time.flush(0)
81
- triple[0].should == 'measure_time'
82
- triple[2].keys.should =~ [:num, :max, :avg, :class, :hook, :object_id]
84
+ expect(triple[0]).to eql('measure_time')
85
+ expect(triple[2].keys).to eql([:max, :avg, :num, :class, :hook, :object_id])
83
86
  end
84
87
  end
85
88
  end
@@ -119,9 +122,9 @@ describe "extends Fluent::StdoutOutput" do
119
122
  ]}
120
123
  it 'should flush' do
121
124
  time = Fluent::Engine.now
122
- Fluent::Engine.stub(:now).and_return(time)
123
- Fluent::Engine.should_receive(:emit) # .with("measure_time", time, {})
125
+ allow(Fluent::Engine).to receive(:now) { time }
124
126
  d = driver.instance
127
+ expect(d.router).to receive(:emit) # .with("measure_time", time, {})
125
128
  d.emit('tag1', Fluent::OneEventStream.new(0, {'a'=>1}), Fluent::NullOutputChain.instance)
126
129
  end
127
130
  end
@@ -138,8 +141,8 @@ describe "extends Fluent::StdoutOutput" do
138
141
  d = driver.instance
139
142
  d.emit('tag1', Fluent::OneEventStream.new(0, {'a'=>1}), Fluent::NullOutputChain.instance)
140
143
  triple = d.measure_time.flush(0)
141
- triple[0].should == 'measure_time'
142
- triple[2].keys.should =~ [:num, :max, :avg, :class, :hook, :object_id]
144
+ expect(triple[0]).to eql('measure_time')
145
+ expect(triple[2].keys).to eql([:max, :avg, :num, :class, :hook, :object_id])
143
146
  end
144
147
  end
145
148
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fluent-plugin-measure_time
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Naotoshi Seo
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-09-21 00:00:00.000000000 Z
11
+ date: 2016-04-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: fluentd
@@ -105,6 +105,7 @@ files:
105
105
  - ".travis.yml"
106
106
  - CHANGELOG.md
107
107
  - Gemfile
108
+ - Gemfile.fluentd.v10
108
109
  - LICENSE
109
110
  - README.md
110
111
  - Rakefile
@@ -116,8 +117,12 @@ files:
116
117
  - benchmark/in_forward.conf
117
118
  - benchmark/patched_in_forward.conf
118
119
  - benchmark/plugin/in_forward.rb
120
+ - example/v0.10.conf
121
+ - example/v0.12.conf
119
122
  - fluent-plugin-measure_time.gemspec
120
123
  - lib/fluent/plugin/in_measure_time.rb
124
+ - lib/fluent/plugin/measure_timable.rb
125
+ - lib/fluent/plugin/out_measure_time.rb
121
126
  - spec/in_measure_time_spec.rb
122
127
  - spec/spec_helper.rb
123
128
  homepage: https://github.com/sonots/fluent-plugin-measure_time
@@ -140,7 +145,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
140
145
  version: '0'
141
146
  requirements: []
142
147
  rubyforge_project:
143
- rubygems_version: 2.2.2
148
+ rubygems_version: 2.5.1
144
149
  signing_key:
145
150
  specification_version: 4
146
151
  summary: Fluentd plugin to measure elapsed time to process messages