fluent-plugin-config-expander 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -5,6 +5,9 @@
5
5
  ConfigExpanderInput and ConfigExpanderOutput plugins provide simple configuration template to write items repeatedly.
6
6
  In <config> section, you can write actual configuration for actual input/output plugin, with special directives for loop controls.
7
7
 
8
+ And also supports built-in placeholders below:
9
+ * hostname (ex: \_\_HOSTNAME\_\_, \_\_hostname\_\_, ${hostname}, ${HOSTNAME})
10
+
8
11
  ## Configuration
9
12
 
10
13
  For both of input and output (for <source> and <match>), you can use 'config_expander' and its 'for' directive like below:
@@ -42,6 +45,22 @@ Configuration above is equal to below:
42
45
  </server>
43
46
  </match>
44
47
 
48
+ As placeholder, you can use '${varname}' style:
49
+
50
+ <match example.**>
51
+ type config_expander
52
+ <config>
53
+ type forward
54
+ flush_interval 30s
55
+ <for node in 01 02 03>
56
+ <server>
57
+ host worker${node}.local
58
+ port 24224
59
+ </server>
60
+ </for>
61
+ </config>
62
+ </match>
63
+
45
64
  Nested 'for' directive is valid:
46
65
 
47
66
  <match example.**>
@@ -60,6 +79,18 @@ Nested 'for' directive is valid:
60
79
  </config>
61
80
  </match>
62
81
 
82
+ Set hostname into tag in 'tail' input plugin:
83
+
84
+ <source>
85
+ type config_expander
86
+ <config>
87
+ type tail
88
+ format /..../
89
+ path /var/log/access.log
90
+ tag access.log.${hostname}
91
+ </config>
92
+ </source>
93
+
63
94
  ## TODO
64
95
 
65
96
  * more tests
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |gem|
4
4
  gem.name = "fluent-plugin-config-expander"
5
- gem.version = "0.1.0"
5
+ gem.version = "0.1.1"
6
6
  gem.authors = ["TAGOMORI Satoshi"]
7
7
  gem.email = ["tagomoris@gmail.com"]
8
8
  gem.description = %q{This plugin provides directives for loop extraction}
@@ -2,7 +2,7 @@ require 'fluent/config'
2
2
 
3
3
  module Fluent::Config::Expander
4
4
  def self.replace(str, mapping)
5
- mapping.reduce(str){|r,p| str.gsub(p[0], p[1])}
5
+ mapping.reduce(str){|r,p| r.gsub(p[0], p[1])}
6
6
  end
7
7
 
8
8
  def self.expand(element, mapping)
@@ -15,10 +15,13 @@ module Fluent::Config::Expander
15
15
  unless e.arg =~ /^([a-zA-Z0-9]+) in (.+)$/
16
16
  raise Fluent::ConfigError, "invalid for tag syntax: <for NAME in ARG1 ARG2 ...>"
17
17
  end
18
- vname = '__' + $1 + '__'
18
+ vkey = $1.dup
19
19
  vargs = $2.split(/ +/).select{|v| v.size > 0}
20
+
21
+ vname = '__' + vkey + '__'
22
+ vname2 = '${' + vkey + '}'
20
23
  vargs.each do |v|
21
- expanded = expand(e, mapping.merge({vname => v}))
24
+ expanded = expand(e, mapping.merge({vname => v, vname2 => v}))
22
25
  attrs.update(expanded)
23
26
  elements += expanded.elements.map{|xe| expand(xe, mapping)}
24
27
  end
@@ -3,6 +3,7 @@ require_relative 'expander'
3
3
  class Fluent::ConfigExpanderInput < Fluent::Input
4
4
  Fluent::Plugin.register_input('config_expander', self)
5
5
 
6
+ config_param :hostname, :string, :default => `hostname`.chomp
6
7
  attr_accessor :plugin
7
8
 
8
9
  def mark_used(conf)
@@ -10,8 +11,12 @@ class Fluent::ConfigExpanderInput < Fluent::Input
10
11
  conf.elements.each{|e| mark_used(e)}
11
12
  end
12
13
 
14
+ def builtin_mapping
15
+ {'__hostname__' => @hostname, '__HOSTNAME__' => @hostname, '${hostname}' => @hostname, '${HOSTNAME}' => @hostname}
16
+ end
17
+
13
18
  def expand_config(conf)
14
- ex = Fluent::Config::Expander.expand(conf, {})
19
+ ex = Fluent::Config::Expander.expand(conf, builtin_mapping())
15
20
  ex.name = ''
16
21
  ex.arg = ''
17
22
  ex
@@ -3,6 +3,7 @@ require_relative 'expander'
3
3
  class Fluent::ConfigExpanderOutput < Fluent::Output
4
4
  Fluent::Plugin.register_output('config_expander', self)
5
5
 
6
+ config_param :hostname, :string, :default => `hostname`.chomp
6
7
  attr_accessor :plugin
7
8
 
8
9
  def mark_used(conf)
@@ -10,8 +11,12 @@ class Fluent::ConfigExpanderOutput < Fluent::Output
10
11
  conf.elements.each{|e| mark_used(e)}
11
12
  end
12
13
 
14
+ def builtin_mapping
15
+ {'__hostname__' => @hostname, '__HOSTNAME__' => @hostname, '${hostname}' => @hostname, '${HOSTNAME}' => @hostname}
16
+ end
17
+
13
18
  def expand_config(conf)
14
- ex = Fluent::Config::Expander.expand(conf, {})
19
+ ex = Fluent::Config::Expander.expand(conf, builtin_mapping())
15
20
  ex.name = ''
16
21
  ex.arg = ''
17
22
  ex
@@ -13,6 +13,7 @@ class ConfigExpanderTest < Test::Unit::TestCase
13
13
  assert_equal "foofoo", @m.replace("foobar", {'bar' => 'foo'})
14
14
  assert_equal "foobar", @m.replace("foobar", {'hoge' => 'moge'})
15
15
  assert_equal "xxbar", @m.replace("foofoobar", {'foo' => 'x'})
16
+ assert_equal "xxy", @m.replace("foofoobar", {'foo' => 'x', 'bar' => 'y'})
16
17
  end
17
18
 
18
19
  def test_expand
@@ -128,5 +129,69 @@ EOL
128
129
  </config>
129
130
  EOL
130
131
  assert_equal exconf3, @m.expand(conf3, {}).to_s
132
+
133
+ nonexconf4 = <<EOL
134
+ <config>
135
+ type forward
136
+ flush_interval 1s
137
+ <for nodenum in 01>
138
+ <for portnum in 24221 24222 24223 24224>
139
+ <server>
140
+ host node__nodenum__.local
141
+ port ${portnum}
142
+ </server>
143
+ </for>
144
+ </for>
145
+ <for nodenum in 02>
146
+ <for portnum in 24221 24222 24223 24224>
147
+ <server>
148
+ host node${nodenum}.local
149
+ port __portnum__
150
+ </server>
151
+ </for>
152
+ </for>
153
+ </config>
154
+ EOL
155
+ conf4 = Fluent::Config.parse(nonexconf4, 'hoge').elements.first
156
+ assert_equal nonexconf4, conf4.to_s
157
+ exconf4 = <<EOL
158
+ <config>
159
+ type forward
160
+ flush_interval 1s
161
+ <server>
162
+ host node01.local
163
+ port 24221
164
+ </server>
165
+ <server>
166
+ host node01.local
167
+ port 24222
168
+ </server>
169
+ <server>
170
+ host node01.local
171
+ port 24223
172
+ </server>
173
+ <server>
174
+ host node01.local
175
+ port 24224
176
+ </server>
177
+ <server>
178
+ host node02.local
179
+ port 24221
180
+ </server>
181
+ <server>
182
+ host node02.local
183
+ port 24222
184
+ </server>
185
+ <server>
186
+ host node02.local
187
+ port 24223
188
+ </server>
189
+ <server>
190
+ host node02.local
191
+ port 24224
192
+ </server>
193
+ </config>
194
+ EOL
195
+ assert_equal exconf4, @m.expand(conf4, {}).to_s
131
196
  end
132
197
  end
@@ -18,6 +18,7 @@ type config_expander
18
18
  </for>
19
19
  </config>
20
20
  ]
21
+
21
22
  def create_driver(conf=CONFIG)
22
23
  Fluent::Test::InputTestDriver.new(Fluent::ConfigExpanderInput).configure(conf)
23
24
  end
@@ -39,4 +40,26 @@ type config_expander
39
40
  assert_equal true, d.instance.plugin.stopped
40
41
  end
41
42
 
43
+ CONFIG2 = %[
44
+ type config_expander
45
+ hostname testing.node.local
46
+ <config>
47
+ type config_expander_test
48
+ tag baz
49
+ <node>
50
+ attr1 ${hostname}
51
+ attr2 ${HOSTNAME}
52
+ attr3 __hostname__
53
+ attr4 __HOSTNAME__
54
+ </node>
55
+ ]
56
+ def test_configure_hostname
57
+ d = create_driver CONFIG2
58
+ assert_equal 1, d.instance.plugin.nodes.size
59
+ assert_equal 'testing.node.local', d.instance.plugin.nodes.first['attr1']
60
+ assert_equal 'testing.node.local', d.instance.plugin.nodes.first['attr2']
61
+ assert_equal 'testing.node.local', d.instance.plugin.nodes.first['attr3']
62
+ assert_equal 'testing.node.local', d.instance.plugin.nodes.first['attr4']
63
+ end
64
+
42
65
  end
@@ -39,6 +39,28 @@ type config_expander
39
39
  assert_equal true, d.instance.plugin.stopped
40
40
  end
41
41
 
42
+ CONFIG2 = %[
43
+ type config_expander
44
+ hostname testing.node.local
45
+ <config>
46
+ type config_expander_test
47
+ tag baz
48
+ <node>
49
+ attr1 ${hostname}
50
+ attr2 ${HOSTNAME}
51
+ attr3 __hostname__
52
+ attr4 __HOSTNAME__
53
+ </node>
54
+ ]
55
+ def test_configure_hostname
56
+ d = create_driver CONFIG2
57
+ assert_equal 1, d.instance.plugin.nodes.size
58
+ assert_equal 'testing.node.local', d.instance.plugin.nodes.first['attr1']
59
+ assert_equal 'testing.node.local', d.instance.plugin.nodes.first['attr2']
60
+ assert_equal 'testing.node.local', d.instance.plugin.nodes.first['attr3']
61
+ assert_equal 'testing.node.local', d.instance.plugin.nodes.first['attr4']
62
+ end
63
+
42
64
  def test_emit
43
65
  d = create_driver
44
66
  d.run do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fluent-plugin-config-expander
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-08-02 00:00:00.000000000 Z
12
+ date: 2012-08-06 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rake