fluent-plugin-stats 0.3.3 → 0.3.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 28926898a6e0a00aad103bafd93aa8372cd22055
4
- data.tar.gz: 810624bf7e711d5cb30377545c3516edb0c4a5de
3
+ metadata.gz: ec8019dd61e1e3a0f5b57c1710666d827965bb83
4
+ data.tar.gz: 2800e919c9c44957a0ff57e81fb4ee57cc4c959d
5
5
  SHA512:
6
- metadata.gz: 929e982bb1c14ae5eeb7a8ff26be6196cc002128fd78921e474b9883d842b0d6a2719a6a1b2f6090713c84f252fece52270c0c175ccc4f31efefc02331b24955
7
- data.tar.gz: 0a9a65241a2b21d2ec3c1267be1e73853c69463085d0fb9ed6c7ea45ba83f766e91910efa39827ea9332bf42e53a07c0fb50c718fe05b999bb0fc49ee15f31a0
6
+ metadata.gz: bb176774a693586253f5e4d2020abcf74f513ccead4c17fc856be0f9560cab3c02ba909bad95d770da2f0198a75f6ac3962aa5c80fe197101f3a6324c76fe4ee
7
+ data.tar.gz: 8fdf07f4e8e953ea71b1df81f908ab500df303c5a47144e4b93f7af89e36e1d96fe7486e3a505115bf0f5af194ccc643a0fbf8afe5b3828bff1c6b69eeca2659
@@ -1,3 +1,9 @@
1
+ ## 0.3.4 (2014/04/12)
2
+
3
+ Enhancements:
4
+
5
+ * Add `add_tag_suffix`, and `remove_tag_suffix` options
6
+
1
7
  ## 0.3.3 (2014/02/04)
2
8
 
3
9
  Enhancement:
data/README.md CHANGED
@@ -88,6 +88,14 @@ then output bocomes as belows:
88
88
 
89
89
  Remove tag prefix for output message.
90
90
 
91
+ - add_tag_suffix
92
+
93
+ Add tag suffix for output message.
94
+
95
+ - remove_tag_suffix
96
+
97
+ Remove tag suffix for output message.
98
+
91
99
  - aggragate
92
100
 
93
101
  Calculate by each `tag` or `all`. The default value is `tag`.
@@ -3,7 +3,7 @@ $:.push File.expand_path("../lib", __FILE__)
3
3
 
4
4
  Gem::Specification.new do |s|
5
5
  s.name = "fluent-plugin-stats"
6
- s.version = "0.3.3"
6
+ s.version = "0.3.4"
7
7
  s.authors = ["Naotoshi Seo"]
8
8
  s.email = ["sonots@gmail.com"]
9
9
  s.homepage = "https://github.com/sonots/fluent-plugin-stats"
@@ -28,6 +28,8 @@ class Fluent::StatsOutput < Fluent::Output
28
28
  config_param :tag, :string, :default => nil
29
29
  config_param :add_tag_prefix, :string, :default => nil
30
30
  config_param :remove_tag_prefix, :string, :default => nil
31
+ config_param :add_tag_suffix, :string, :default => nil
32
+ config_param :remove_tag_suffix, :string, :default => nil
31
33
  config_param :aggregate, :string, :default => 'tag'
32
34
  config_param :store_file, :string, :default => nil
33
35
  config_param :zero_emit, :bool, :default => false
@@ -59,24 +61,10 @@ class Fluent::StatsOutput < Fluent::Output
59
61
  raise Fluent::ConfigError, "tag must be specified for aggregate all" if @tag.nil?
60
62
  end
61
63
 
62
- if @tag.nil? and @add_tag_prefix.nil? and @remove_tag_prefix.nil?
64
+ if @tag.nil? and @add_tag_prefix.nil? and @remove_tag_prefix.nil? and @add_tag_suffix.nil? and @remove_tag_suffix.nil?
63
65
  @add_tag_prefix = 'stats' # not ConfigError for lower version compatibility
64
66
  end
65
-
66
- @tag_prefix = "#{@add_tag_prefix}." if @add_tag_prefix
67
- @tag_prefix_match = "#{@remove_tag_prefix}." if @remove_tag_prefix
68
- @tag_proc =
69
- if @tag
70
- Proc.new {|tag| @tag }
71
- elsif @tag_prefix and @tag_prefix_match
72
- Proc.new {|tag| "#{@tag_prefix}#{lstrip(tag, @tag_prefix_match)}" }
73
- elsif @tag_prefix_match
74
- Proc.new {|tag| lstrip(tag, @tag_prefix_match) }
75
- elsif @tag_prefix
76
- Proc.new {|tag| "#{@tag_prefix}#{tag}" }
77
- else
78
- Proc.new {|tag| tag }
79
- end
67
+ @tag_proc = tag_proc
80
68
 
81
69
  @matches = {}
82
70
  @mutex = Mutex.new
@@ -115,6 +103,7 @@ class Fluent::StatsOutput < Fluent::Output
115
103
 
116
104
  # Called when new line comes. This method actually does not emit
117
105
  def emit(tag, es, chain)
106
+ _tag = tag
118
107
  tag = 'all' if @aggregate == 'all'
119
108
  # stats
120
109
  matches = { :count => 0, :sum => {}, :max => {}, :min => {}, :avg => {} }
@@ -173,23 +162,27 @@ class Fluent::StatsOutput < Fluent::Output
173
162
  @matches[tag][:count] += matches[:count]
174
163
  end
175
164
 
165
+ log.trace "out_stats: tag:#{_tag} @matches:#{@matches}"
166
+
176
167
  chain.next
177
168
  rescue => e
178
- log.warn "#{e.class} #{e.message} #{e.backtrace.first}"
169
+ log.warn "out_stats: #{e.class} #{e.message} #{e.backtrace.first}"
179
170
  end
180
171
 
181
172
  # thread callback
182
173
  def watcher
183
174
  # instance variable, and public accessable, for test
184
175
  @last_checked ||= Fluent::Engine.now
185
- while (sleep 0.1)
176
+ while (sleep 0.5)
186
177
  begin
187
178
  if Fluent::Engine.now - @last_checked >= @interval
188
- @last_checked = Fluent::Engine.now
189
- flush_emit
179
+ report_time do
180
+ @last_checked = Fluent::Engine.now
181
+ flush_emit
182
+ end
190
183
  end
191
184
  rescue => e
192
- log.warn "#{e.class} #{e.message} #{e.backtrace.first}"
185
+ log.warn "out_stats: #{e.class} #{e.message} #{e.backtrace.first}"
193
186
  end
194
187
  end
195
188
  end
@@ -197,13 +190,18 @@ class Fluent::StatsOutput < Fluent::Output
197
190
  # This method is the real one to emit
198
191
  def flush_emit
199
192
  time = Fluent::Engine.now
200
- flushed_matches, @matches = @matches, initial_matches(@matches)
193
+ flushed_matches = {}
194
+ @mutex.synchronize do
195
+ flushed_matches, @matches = @matches, initial_matches(@matches)
196
+ end
197
+ log.trace("out_stats: flushed_matches:#{flushed_matches} @matches:#{@matches}") unless flushed_matches.empty?
201
198
 
202
- flushed_matches.keys.each do |tag|
203
- matches = flushed_matches[tag]
204
- output = generate_output(matches)
199
+ flushed_matches.each do |tag, matches|
205
200
  emit_tag = @tag_proc.call(tag)
206
- Fluent::Engine.emit(emit_tag, time, output) if output and !output.empty?
201
+ report_time(" emit_tag:#{emit_tag} matches:#{matches}") do
202
+ output = generate_output(matches)
203
+ Fluent::Engine.emit(emit_tag, time, output) if output and !output.empty?
204
+ end
207
205
  end
208
206
  end
209
207
 
@@ -321,8 +319,31 @@ class Fluent::StatsOutput < Fluent::Output
321
319
  transform_keys(hash) { |key| key.to_s }
322
320
  end
323
321
 
324
- def lstrip(string, substring)
325
- string.index(substring) == 0 ? string[substring.size..-1] : string
322
+ def tag_proc
323
+ rstrip = Proc.new {|str, substr| str.chomp(substr) }
324
+ lstrip = Proc.new {|str, substr| str.start_with?(substr) ? str[substr.size..-1] : str }
325
+ tag_prefix = "#{rstrip.call(@add_tag_prefix, '.')}." if @add_tag_prefix
326
+ tag_suffix = ".#{lstrip.call(@add_tag_suffix, '.')}" if @add_tag_suffix
327
+ tag_prefix_match = "#{rstrip.call(@remove_tag_prefix, '.')}." if @remove_tag_prefix
328
+ tag_suffix_match = ".#{lstrip.call(@remove_tag_suffix, '.')}" if @remove_tag_suffix
329
+ tag_fixed = @tag if @tag
330
+ if tag_fixed
331
+ Proc.new {|tag| tag_fixed }
332
+ elsif tag_prefix_match and tag_suffix_match
333
+ Proc.new {|tag| "#{tag_prefix}#{rstrip.call(lstrip.call(tag, tag_prefix_match), tag_suffix_match)}#{tag_suffix}" }
334
+ elsif tag_prefix_match
335
+ Proc.new {|tag| "#{tag_prefix}#{lstrip.call(tag, tag_prefix_match)}#{tag_suffix}" }
336
+ elsif tag_suffix_match
337
+ Proc.new {|tag| "#{tag_prefix}#{rstrip.call(tag, tag_suffix_match)}#{tag_suffix}" }
338
+ else
339
+ Proc.new {|tag| "#{tag_prefix}#{tag}#{tag_suffix}" }
340
+ end
326
341
  end
327
342
 
343
+ def report_time(msg = nil, &blk)
344
+ t = Time.now
345
+ output = yield
346
+ log.debug sprintf("out_stats: elapsed_time:%.2f thread_id:%s%s caller:%s", (Time.now - t).to_f, Thread.current.object_id, msg, caller()[0])
347
+ output
348
+ end
328
349
  end
@@ -190,6 +190,7 @@ describe Fluent::StatsOutput do
190
190
  sum _count$
191
191
  ]
192
192
  end
193
+ let(:tag) { 'foo.bar' }
193
194
  before do
194
195
  Fluent::Engine.stub(:now).and_return(time)
195
196
  Fluent::Engine.should_receive(:emit).with("foo.#{tag}", time, {
@@ -206,6 +207,7 @@ describe Fluent::StatsOutput do
206
207
  sum _count$
207
208
  ]
208
209
  end
210
+ let(:tag) { 'foo.bar' }
209
211
  before do
210
212
  Fluent::Engine.stub(:now).and_return(time)
211
213
  Fluent::Engine.should_receive(:emit).with("bar", time, {
@@ -215,6 +217,40 @@ describe Fluent::StatsOutput do
215
217
  it { emit }
216
218
  end
217
219
 
220
+ context 'add_tag_suffix' do
221
+ let(:config) do
222
+ CONFIG + %[
223
+ add_tag_suffix foo
224
+ sum _count$
225
+ ]
226
+ end
227
+ let(:tag) { 'foo.bar' }
228
+ before do
229
+ Fluent::Engine.stub(:now).and_return(time)
230
+ Fluent::Engine.should_receive(:emit).with("#{tag}.foo", time, {
231
+ "4xx_count"=>6,"5xx_count"=>6
232
+ })
233
+ end
234
+ it { emit }
235
+ end
236
+
237
+ context 'remove_tag_suffix' do
238
+ let(:config) do
239
+ CONFIG + %[
240
+ remove_tag_suffix bar
241
+ sum _count$
242
+ ]
243
+ end
244
+ let(:tag) { 'foo.bar' }
245
+ before do
246
+ Fluent::Engine.stub(:now).and_return(time)
247
+ Fluent::Engine.should_receive(:emit).with("foo", time, {
248
+ "4xx_count"=>6,"5xx_count"=>6
249
+ })
250
+ end
251
+ it { emit }
252
+ end
253
+
218
254
  context 'aggregate' do
219
255
  let(:emit) do
220
256
  driver.run { messages.each {|message| driver.emit_with_tag(message, time, 'foo.bar') } }
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fluent-plugin-stats
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.3
4
+ version: 0.3.4
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-02-04 00:00:00.000000000 Z
11
+ date: 2014-04-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: fluentd