fluent-plugin-amplifier-filter 0.1.6 → 0.2.0

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: 4c418acc02e375c36620d6731b7d65e95435ee36
4
- data.tar.gz: d5958fe64969a439d70019f67af14918a8fe073e
3
+ metadata.gz: 5c9755678e4245819f34011b36969ea35245d75c
4
+ data.tar.gz: ace2c6968786f5ec90a9b9579043de1105430430
5
5
  SHA512:
6
- metadata.gz: 2373ab79ff181e64694f0b2118234a451db8be96bcb4b2e755d2e3a86228e978798072978523d2b70cd05d3d007a49b32d5f23dd0e065d044f626044e56a4774
7
- data.tar.gz: 044566f36afe08bfa9e3ada239c863ed7153d638ee1b8d87a1d76395f91d0d9b33b642167f2de96dc8119ce0bafba2a29e96372215c199fa140dbc49547958c0
6
+ metadata.gz: 3b6916158e1612720492b56b1b570bf7f799255f6ac0ca664d70c8c2199ad23f0a24f15ae31e6372aad2fb11c977458be9c0a91e87a079c756c018898f02eafe
7
+ data.tar.gz: 8ed7db05d43c4a0c09b5a2dff4a045c4d1e7eac745d8112809a9afbf9ccd80f1cda68fa526864c357384b9e4cffc50dbb5022eb985f4fda74b39ad5f8ceb9624
data/README.md CHANGED
@@ -39,9 +39,46 @@ To do x10 for messages 1/10 sampled, and to do x100 for messages 1/100 sampled:
39
39
  add_prefix summary
40
40
  key_pattern .*_(count|rate)$
41
41
  </match>
42
-
42
+
43
43
  <match summary.**>
44
44
  # output configurations where to send original/modified messages...
45
+ </match>
46
+
47
+ ### AmplifierFilter
48
+
49
+ Filter version of AmplifierFilterOutput plugin.
50
+ It depends on Fluentd 0.12 or later.
51
+
52
+ <filter sampled_10.**>
53
+ type amplifier_filter
54
+ ratio 10
55
+ key_names counts,rates
56
+ </filter>
57
+
58
+ <filter sampled_100.**>
59
+ type amplifier_filter
60
+ ratio 100
61
+ key_names counts,rates
62
+ </filter>
63
+
64
+ <match sampled_10.**>
65
+ # output configurations where to send original/modified messages...
66
+ </match>
67
+
68
+ <match sampled_100.**>
69
+ # output configurations where to send original/modified messages...
70
+ </match>
71
+
72
+ `key_pattern`(regexp) useful insted of `key_names`, and `add_prefix` is also useful:
73
+
74
+ <filter sampled_10.**>
75
+ type amplifier_filter
76
+ ratio 10
77
+ key_pattern .*_(count|rate)$
78
+ </filter>
79
+
80
+ <match sampled_10.**>
81
+ # output configurations where to send original/modified messages...
45
82
  </match>
46
83
 
47
84
  ## TODO
@@ -1,7 +1,7 @@
1
1
  # -*- encoding: utf-8 -*-
2
2
  Gem::Specification.new do |gem|
3
3
  gem.name = "fluent-plugin-amplifier-filter"
4
- gem.version = "0.1.6"
4
+ gem.version = "0.2.0"
5
5
  gem.authors = ["TAGOMORI Satoshi"]
6
6
  gem.email = ["tagomoris@gmail.com"]
7
7
  gem.summary = %q{plugin to re-emit messages with amplified values}
@@ -17,4 +17,5 @@ Gem::Specification.new do |gem|
17
17
  gem.add_runtime_dependency "fluentd"
18
18
  gem.add_development_dependency "bundler"
19
19
  gem.add_development_dependency "rake"
20
+ gem.add_development_dependency "test-unit", "~> 3.1.0"
20
21
  end
@@ -0,0 +1,86 @@
1
+ class Fluent::AmplifierFilter < Fluent::Filter
2
+ Fluent::Plugin.register_filter('amplifier_filter', self)
3
+
4
+ config_param :ratio, :float
5
+
6
+ config_param :key_names, :string, default: nil
7
+ config_param :key_pattern, :string, default: nil
8
+
9
+ config_param :floor, :bool, default: false
10
+
11
+ # Define `log` method for v0.10.42 or earlier
12
+ unless method_defined?(:log)
13
+ define_method("log") { $log }
14
+ end
15
+
16
+ def configure(conf)
17
+ super
18
+
19
+ if @key_names.nil? and @key_pattern.nil?
20
+ raise Fluent::ConfigError, "missing both of key_names and key_pattern"
21
+ end
22
+ if not @key_names.nil? and not @key_pattern.nil?
23
+ raise Fluent::ConfigError, "cannot specify both of key_names and key_pattern"
24
+ end
25
+ if @key_names
26
+ @key_names = @key_names.split(',')
27
+ end
28
+ if @key_pattern
29
+ @key_pattern = Regexp.new(@key_pattern)
30
+ end
31
+
32
+ amp = if @floor
33
+ method(:amp_with_floor)
34
+ else
35
+ method(:amp_without_floor)
36
+ end
37
+ (class << self; self; end).module_eval do
38
+ define_method(:amp, amp)
39
+ end
40
+ end
41
+
42
+ def amp_without_floor(value)
43
+ value.to_f * @ratio
44
+ end
45
+
46
+ def amp_with_floor(value)
47
+ (value.to_f * @ratio).floor
48
+ end
49
+
50
+ def filter_stream(tag, es)
51
+ new_es = Fluent::MultiEventStream.new
52
+ if @key_names
53
+ es.each {|time,record|
54
+ updated = {}
55
+ @key_names.each {|key|
56
+ val = record[key]
57
+ next unless val
58
+ updated[key] = amp(val)
59
+ }
60
+ log.debug "amplifier tag:#{tag} floor:#{@floor} ratio:#{@ratio} updated:#{updated.to_json} record:#{record.to_json}"
61
+ if updated.size > 0
62
+ new_es.add(time, record.merge(updated))
63
+ else
64
+ new_es.add(time, record.dup)
65
+ end
66
+ }
67
+ else @key_pattern
68
+ es.each {|time,record|
69
+ updated = {}
70
+ record.keys.each {|key|
71
+ val = record[key]
72
+ next unless val
73
+ next unless @key_pattern.match(key)
74
+ updated[key] = amp(val)
75
+ }
76
+ log.debug "amplifier tag:#{tag} floor:#{@floor} ratio:#{@ratio} updated:#{updated.to_json} record:#{record.to_json}"
77
+ if updated.size > 0
78
+ new_es.add(time, record.merge(updated))
79
+ else
80
+ new_es.add(time, record.dup)
81
+ end
82
+ }
83
+ end
84
+ new_es
85
+ end
86
+ end if defined?(Fluent::Filter)
@@ -107,7 +107,7 @@ class Fluent::AmplifierFilterOutput < Fluent::Output
107
107
  end
108
108
  }
109
109
  end
110
- Fluent::Engine.emit_array(tag, pairs)
110
+ router.emit_array(tag, pairs)
111
111
 
112
112
  chain.next
113
113
  end
data/test/helper.rb CHANGED
@@ -25,6 +25,7 @@ else
25
25
  end
26
26
 
27
27
  require 'fluent/plugin/out_amplifier_filter'
28
+ require 'fluent/plugin/filter_amplifier'
28
29
 
29
30
  class Test::Unit::TestCase
30
31
  end
@@ -0,0 +1,118 @@
1
+ require 'helper'
2
+
3
+ class AmplifierFilterTest < Test::Unit::TestCase
4
+ def setup
5
+ if not defined?(Fluent::Filter)
6
+ omit("Fluent::Filter is not defined. Use fluentd 0.12 or later.")
7
+ end
8
+
9
+ Fluent::Test.setup
10
+ end
11
+
12
+ # config_param :ratio, :float
13
+ # config_param :key_names, :string, :default => nil
14
+ # config_param :key_pattern, :string, :default => nil
15
+ # config_param :floor, :bool, :default => false
16
+ # config_param :remove_prefix, :string, :default => nil
17
+ # config_param :add_prefix, :string, :default => nil
18
+
19
+ CONFIG = %[
20
+ ratio 1.5
21
+ key_names foo,bar,baz
22
+ ]
23
+ CONFIG2 = %[
24
+ ratio 0.75
25
+ floor yes
26
+ key_pattern field.*
27
+ ]
28
+
29
+ def create_driver(conf = CONFIG, tag='test')
30
+ Fluent::Test::FilterTestDriver.new(Fluent::AmplifierFilter, tag).configure(conf)
31
+ end
32
+
33
+ def test_configure
34
+ assert_raise(Fluent::ConfigError) {
35
+ d = create_driver('')
36
+ }
37
+ assert_raise(Fluent::ConfigError) {
38
+ d = create_driver(%[
39
+ ratio 1
40
+ ])
41
+ }
42
+ assert_nothing_thrown {
43
+ d = create_driver(%[
44
+ ratio 1
45
+ key_names field1
46
+ ])
47
+ }
48
+ assert_nothing_raised {
49
+ d = create_driver(%[
50
+ ratio 1
51
+ key_pattern field\d+
52
+ ])
53
+ }
54
+
55
+ d = create_driver
56
+ assert_equal false, d.instance.floor
57
+ assert_equal ['foo', 'bar', 'baz'], d.instance.key_names
58
+ end
59
+
60
+ def test_filter_1
61
+ # CONFIG = %[
62
+ # ratio 1.5
63
+ # key_names foo,bar,baz
64
+ # ]
65
+ d1 = create_driver(CONFIG, 'test.service')
66
+ d1.run do
67
+ d1.filter({'name' => 'first', 'foo' => 10, 'bar' => 1, 'baz' => 20, 'zap' => 50})
68
+ d1.filter({'name' => 'second', 'foo' => 10, 'bar' => 2, 'baz' => 40, 'zap' => 50})
69
+ end
70
+ filtered = d1.filtered_as_array
71
+ assert_equal 2, filtered.length
72
+ assert_equal 'test.service', filtered[0][0] # tag
73
+
74
+ first = filtered[0][2]
75
+ assert_equal 'first', first['name']
76
+ assert_equal 15 , first['foo']
77
+ assert_equal 1.5 , first['bar']
78
+ assert_equal 30 , first['baz']
79
+ assert_equal 50 , first['zap']
80
+
81
+ second = filtered[1][2]
82
+ assert_equal 'second', second['name']
83
+ assert_equal 15 , second['foo']
84
+ assert_equal 3 , second['bar']
85
+ assert_equal 60 , second['baz']
86
+ assert_equal 50 , second['zap']
87
+ end
88
+
89
+ def test_filter_2
90
+ # CONFIG2 = %[
91
+ # ratio 0.75
92
+ # floor yes
93
+ # key_pattern field.*
94
+ # ]
95
+ d3 = create_driver(CONFIG2, 'test.service')
96
+ d3.run do
97
+ d3.filter({'name' => 'first', 'fieldfoo' => 10, 'fieldbar' => 1, 'fieldbaz' => 20, 'zap' => 50})
98
+ d3.filter({'name' => 'second', 'fieldfoo' => '10', 'fieldbar' => '2', 'fieldbaz' => '40', 'zap' => '50'})
99
+ end
100
+ filtered = d3.filtered_as_array
101
+ assert_equal 2, filtered.length
102
+ assert_equal 'test.service', filtered[0][0] # tag
103
+
104
+ first = filtered[0][2]
105
+ assert_equal 'first', first['name']
106
+ assert_equal 7 , first['fieldfoo']
107
+ assert_equal 0 , first['fieldbar']
108
+ assert_equal 15 , first['fieldbaz']
109
+ assert_equal 50 , first['zap']
110
+
111
+ second = filtered[1][2]
112
+ assert_equal 'second', second['name']
113
+ assert_equal 7 , second['fieldfoo']
114
+ assert_equal 1 , second['fieldbar']
115
+ assert_equal 30 , second['fieldbaz']
116
+ assert_equal '50' , second['zap']
117
+ end
118
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fluent-plugin-amplifier-filter
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.6
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - TAGOMORI Satoshi
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-05-16 00:00:00.000000000 Z
11
+ date: 2015-10-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: fluentd
@@ -52,6 +52,20 @@ dependencies:
52
52
  - - ">="
53
53
  - !ruby/object:Gem::Version
54
54
  version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: test-unit
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 3.1.0
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 3.1.0
55
69
  description: plugin to increase/decrease values by specified ratio (0-1 or 1-)
56
70
  email:
57
71
  - tagomoris@gmail.com
@@ -66,8 +80,10 @@ files:
66
80
  - README.md
67
81
  - Rakefile
68
82
  - fluent-plugin-amplifier-filter.gemspec
83
+ - lib/fluent/plugin/filter_amplifier.rb
69
84
  - lib/fluent/plugin/out_amplifier_filter.rb
70
85
  - test/helper.rb
86
+ - test/plugin/test_filter_amplifier.rb
71
87
  - test/plugin/test_out_amplifier_filter.rb
72
88
  homepage: http://github.com/tagomoris/fluent-plugin-amplifier-filter
73
89
  licenses:
@@ -89,10 +105,12 @@ required_rubygems_version: !ruby/object:Gem::Requirement
89
105
  version: '0'
90
106
  requirements: []
91
107
  rubyforge_project:
92
- rubygems_version: 2.2.2
108
+ rubygems_version: 2.4.5
93
109
  signing_key:
94
110
  specification_version: 4
95
111
  summary: plugin to re-emit messages with amplified values
96
112
  test_files:
97
113
  - test/helper.rb
114
+ - test/plugin/test_filter_amplifier.rb
98
115
  - test/plugin/test_out_amplifier_filter.rb
116
+ has_rdoc: