fluent-plugin-script_append 0.0.1 → 0.1.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: 27280a3d62cd0c8ceeb6e9f548bc5fa1ef5b4ad1
4
- data.tar.gz: e34fd0d9a34b70de1d56843593b8b4b2be377992
3
+ metadata.gz: 4de353b594ee8b985c00c7a4e00f007aec9b8ade
4
+ data.tar.gz: 59e139968b6ef81edeeac4062b400526b29a275b
5
5
  SHA512:
6
- metadata.gz: 741ce22c7fba57d85f4874d5b59fcbdda8cc68f5fb1b8a7329e58cd8c8ebd39c1042f9b47926b7e0f55c0801f40e4583e1e140d11955930c807c11d7835a4fac
7
- data.tar.gz: 6f00de3e9a025ba1ebb251df249d1126d6893e00f26aeac485c34e6155555df5dddc20c9c3dcd8e9719d8a5a8de210d1a427d8771f6c4fd82e8179e653a8dd37
6
+ metadata.gz: 28cb5c8fd6e11e1e67fd86f3a8b43d4ece148b1b15d708fd61f03951fc5460bc76fed0e8a40443209934d5c2e756dc6b39f43ed909f67614e2641ca3cfebea91
7
+ data.tar.gz: e5eb2819657bd7038f0e7a4c4f99a4af537420d4ab9324ba6d28d33c5c00d42475c69b3fd7a042df65c91f5420fa91aa0dc092acd58e5519d7c4434f3bdd6671
data/README.md CHANGED
@@ -1,26 +1,60 @@
1
1
  # Fluent::Plugin::ScriptAppend
2
2
 
3
- TODO: Write a gem description
3
+ A fluent plugin to add script-run result to existing json data
4
+
5
+ [![wercker status](https://app.wercker.com/status/56186aa7c9f166ffea49aba97971e40d/m "wercker status")](https://app.wercker.com/project/bykey/56186aa7c9f166ffea49aba97971e40d)
4
6
 
5
7
  ## Installation
6
8
 
7
- Add this line to your application's Gemfile:
9
+ Install it yourself as:
10
+
11
+ $ fluent-gem install fluent-plugin-script_append
8
12
 
9
- ```ruby
10
- gem 'fluent-plugin-script_append'
13
+ ## Usage
14
+
15
+ ```conf
16
+ <match access.foo>
17
+ type script_append
18
+ # currently only ruby supported
19
+ language ruby
20
+ run_script record['one'].to_i + record['two'].to_i
21
+ key three
22
+ </match>
11
23
  ```
12
24
 
13
- And then execute:
25
+ Input:
14
26
 
15
- $ bundle
27
+ ```json
28
+ {
29
+ "one" : 1,
30
+ "two" : 2
31
+ }
32
+ ```
16
33
 
17
- Or install it yourself as:
34
+ Then get emitted:
18
35
 
19
- $ gem install fluent-plugin-script_append
36
+ ```json
37
+ {
38
+ "one" : 1,
39
+ "two" : 2,
40
+ "three" : 3
41
+ }
42
+ ```
20
43
 
21
- ## Usage
44
+ ## Parameters
45
+
46
+ * `key`, A key for added record to use in json. Required
47
+ * `language`, A language of script, default to ruby, available: `ruby, sh(ell)`
48
+ * In `language ruby`, the record to add is the value of specified expression
49
+ * In `language shell`, the record to add is the stdout of specified shell script
50
+ * `run_script`, A script for generating data. Required
51
+ * `record_var_name`, A variable name for original json data in `ruby` script. Default to `record`
52
+ * `new_tag`, A tag name to use in new emissions
53
+ * `prefix`, A tag prefix to add original tag in new emissions. `new_tag` or `prefix` is required
54
+
55
+ ## See also
22
56
 
23
- TODO: Write usage instructions here
57
+ * https://github.com/ephemeralsnow/fluent-plugin-eval-filter
24
58
 
25
59
  ## Contributing
26
60
 
@@ -4,11 +4,13 @@ class Fluent::ScriptAppendOutput < Fluent::Output
4
4
 
5
5
  config_param :key, :string, :default => nil
6
6
  config_param :language, :string, :default => 'ruby'
7
- config_param :run_script, :string, :default => ''
7
+ config_param :run_script, :string, :default => nil
8
8
  config_param :record_var_name, :string, :default => 'record'
9
9
  config_param :new_tag, :string, :default => nil
10
10
  config_param :prefix, :string, :default => nil
11
11
 
12
+ SUPPORTED_SCRIPT_NAME = %w(ruby sh shell)
13
+
12
14
  def configure(conf)
13
15
  super
14
16
  ensure_param_set!(:key, @key)
@@ -17,16 +19,26 @@ class Fluent::ScriptAppendOutput < Fluent::Output
17
19
 
18
20
  @script_runner = Object.new
19
21
 
20
- # TODO multiple script language support
21
- if @language != 'ruby'
22
+ unless SUPPORTED_SCRIPT_NAME.include? @language
22
23
  warn "Plugin out_script_append would not accept 'language' value other than 'ruby'. Ignoring."
24
+ @language = 'ruby'
23
25
  end
24
26
 
25
- eval <<-RUBY
26
- def @script_runner.run(#{@record_var_name})
27
- #{@run_script}
28
- end
29
- RUBY
27
+ case @language
28
+ when 'ruby'
29
+ eval <<-RUBY
30
+ def @script_runner.run(#{@record_var_name})
31
+ #{@run_script}
32
+ end
33
+ RUBY
34
+ when 'sh', 'shell'
35
+ script = @run_script.gsub(/`/, '\\\`')
36
+ eval <<-RUBY
37
+ def @script_runner.run(*)
38
+ `#{@run_script}`
39
+ end
40
+ RUBY
41
+ end
30
42
  end
31
43
 
32
44
  def emit(tag, event_stream, chain)
@@ -49,7 +61,7 @@ class Fluent::ScriptAppendOutput < Fluent::Output
49
61
 
50
62
  def ensure_param_set!(name, value)
51
63
  unless value
52
- raise "#{name} must be set"
64
+ raise Fluent::ConfigError, "#{name} must be set"
53
65
  end
54
66
  end
55
67
 
@@ -1,7 +1,7 @@
1
1
  module Fluent
2
2
  module Plugin
3
3
  module ScriptAppend
4
- VERSION = "0.0.1"
4
+ VERSION = "0.1.0"
5
5
  end
6
6
  end
7
7
  end
@@ -1,7 +1,7 @@
1
1
  require 'helper'
2
2
 
3
3
  class ScriptAppendOutputTest < Test::Unit::TestCase
4
- def create_driver(conf ,tag='test')
4
+ def create_driver(conf, tag='test')
5
5
  Fluent::Test::OutputTestDriver.new(Fluent::ScriptAppendOutput, tag).configure(conf)
6
6
  end
7
7
 
@@ -16,20 +16,54 @@ class ScriptAppendOutputTest < Test::Unit::TestCase
16
16
  new_tag test.result
17
17
  FLUENTD
18
18
 
19
+ def config_with_run_script(script)
20
+ CONFIG_DEFAULT.sub(
21
+ /^ +run_script.*$/,
22
+ "run_script #{script}"
23
+ )
24
+ end
25
+
19
26
  test 'configure' do
20
27
  d = create_driver CONFIG_DEFAULT
21
28
  assert_equal "sample", d.instance.config['key']
22
29
  assert_equal '"Hello, World"', d.instance.config['run_script']
23
30
  end
24
31
 
25
- test 'appends a result' do
26
- config = <<-FLUENTD
27
- key sample
28
- language ruby
29
- run_script "Hello, World"
30
- new_tag test.result
31
- FLUENTD
32
+ test 'invalid config' do
33
+ assert_raise Fluent::ConfigError do
34
+ create_driver <<-FLUENTD
35
+ language ruby
36
+ run_script "Hello, World"
37
+ new_tag test.result
38
+ FLUENTD
39
+ end
40
+
41
+ assert_raise Fluent::ConfigError do
42
+ create_driver <<-FLUENTD
43
+ key invakid
44
+ language ruby
45
+ new_tag test.result
46
+ FLUENTD
47
+ end
48
+
49
+ assert_raise Fluent::ConfigError do
50
+ create_driver <<-FLUENTD
51
+ key invakid
52
+ language ruby
53
+ run_script "Hello, World"
54
+ FLUENTD
55
+ end
56
+
57
+ assert_nothing_raised do
58
+ create_driver <<-FLUENTD
59
+ key invalid
60
+ run_script "Hello, World"
61
+ new_tag test.result
62
+ FLUENTD
63
+ end
64
+ end
32
65
 
66
+ test 'appends a result' do
33
67
  d = create_driver CONFIG_DEFAULT, 'input.access'
34
68
  d.run do
35
69
  d.emit({'domain' => 'www.google.com', 'path' => '/foo/bar?key=value', 'agent' => 'Googlebot', 'response_time' => 1000000})
@@ -39,4 +73,51 @@ class ScriptAppendOutputTest < Test::Unit::TestCase
39
73
  assert_equal 'test.result', emits[0][0]
40
74
  assert_equal 'Hello, World', emits[0][2]['sample']
41
75
  end
76
+
77
+ test 'appends a result using record' do
78
+ d = create_driver config_with_run_script("record['one'].to_i + record['two'].to_i")
79
+ d.run do
80
+ d.emit({'one' => 1, 'two' => 2})
81
+ end
82
+
83
+ emits = d.emits
84
+ assert_equal 'test.result', emits[0][0]
85
+ assert_equal 3, emits[0][2]['sample']
86
+ end
87
+
88
+ test 'appends with prefix' do
89
+ d = create_driver CONFIG_DEFAULT.sub(/new_tag.*$/m, 'prefix the_prefix'), 'with_suffix'
90
+ d.run do
91
+ d.emit({'domain' => 'www.google.com', 'path' => '/foo/bar?key=value', 'agent' => 'Googlebot', 'response_time' => 1000000})
92
+ end
93
+
94
+ emits = d.emits
95
+ assert_equal 'the_prefix.with_suffix', emits[0][0]
96
+ assert_equal 'Hello, World', emits[0][2]['sample']
97
+ end
98
+
99
+ test 'customizes variable name in script' do
100
+ d = create_driver \
101
+ config_with_run_script("data['one'].to_i + data['two'].to_i * 10").
102
+ sub(/\z/, "\n record_var_name data")
103
+ d.run do
104
+ d.emit({'one' => 1, 'two' => 2})
105
+ end
106
+
107
+ emits = d.emits
108
+ assert_equal 21, emits[0][2]['sample']
109
+ end
110
+
111
+ test 'runs shell via Kernel#`' do
112
+ d = create_driver \
113
+ config_with_run_script(%{echo "Hello world via shell"}).
114
+ sub(/language.*$/, "language shell")
115
+
116
+ d.run do
117
+ d.emit({'domain' => 'www.google.com', 'path' => '/foo/bar?key=value', 'agent' => 'Googlebot', 'response_time' => 1000000})
118
+ end
119
+
120
+ emits = d.emits
121
+ assert_equal "Hello world via shell\n", emits[0][2]['sample']
122
+ end
42
123
  end
@@ -0,0 +1,16 @@
1
+ box: wercker/rvm
2
+ build:
3
+ steps:
4
+ - rvm-use:
5
+ version: ruby-2.1.5
6
+ - bundle-install
7
+ - script:
8
+ name: run test-unit
9
+ code: bundle exec rake test
10
+
11
+ - rvm-use:
12
+ version: ruby-2.0.0-p598
13
+ - bundle-install
14
+ - script:
15
+ name: run test-unit
16
+ code: bundle exec rake test
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fluent-plugin-script_append
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Uchio KONDO
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-12-26 00:00:00.000000000 Z
11
+ date: 2015-01-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: fluentd
@@ -126,6 +126,7 @@ files:
126
126
  - lib/fluent/plugin/script_append/version.rb
127
127
  - test/helper.rb
128
128
  - test/plugin/test_out_script_append.rb
129
+ - wercker.yml
129
130
  homepage: https://github.com/udzura/fluent-plugin-script_append
130
131
  licenses:
131
132
  - Apache 2.0