fluent-plugin-switch 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: c6e9e0cdd83c2f9ea36224028c55e9e727e66685
4
+ data.tar.gz: f2f2ae33f058ddaf87fd9d2494f0d07ff30c40d4
5
+ SHA512:
6
+ metadata.gz: 737c520b6c1bbcef5edb047c6fc55670979ebec718e72b8fc81cc50a967b6c794b5b8301af38a329c208f3ef83a0484559c3e2a909d0dc511c7df8d6d830c482
7
+ data.tar.gz: 6a1ea1e64c3b8bdeb7abe6b74e5f578550ffd370417354388fe5c455a879e68602c099d03f6294741375d34f0a83382686ad4b9b0489dac4f657780191caf85f
data/README.md ADDED
@@ -0,0 +1,123 @@
1
+ ## Fluent::Plugin::Switch
2
+
3
+ Filter plugin for [Fluentd](http://fluentd.org), similar to switch statements in PLs.
4
+
5
+ ### Installation
6
+
7
+ $ gem install fluent-plugin-switch
8
+
9
+
10
+ ### Configuration Guide
11
+
12
+ <filter *>
13
+ @type switch
14
+ key_space # optional : add one or a list of comma sparated keys to be used for patttern matching, defaults to all
15
+ action # optional : append a new key or replace the value of an exisiting key, defaults to append
16
+ category_name # optional : if appending, add this key to the record, if replacing, replaces the value of this key after matching, defaults to 'category'
17
+ default_value # optional : if no matching condition found, uses this value, defaults to no action
18
+ <case>
19
+ condition # a regex condition to be used for matching
20
+ value # if matched use this value
21
+ </case>
22
+ </filter>
23
+
24
+
25
+
26
+ Example config (append):
27
+
28
+ <filter *>
29
+ @type switch
30
+ key_space couleur
31
+ action append
32
+ category_name english_color
33
+ default_value 'no matching cases'
34
+ <case>
35
+ condition '(couleur).+(rouge)'
36
+ value 'red'
37
+ </case>
38
+ <case>
39
+ condition '(couleur).+(jaune)'
40
+ value 'yellow'
41
+ </case>
42
+ <case>
43
+ condition '(couleur).+(bleu)'
44
+ value 'blue'
45
+ </case>
46
+ </filter>
47
+
48
+
49
+ Given the above config, if following JSON record is passed:
50
+
51
+ {"couleur":"La couleur est rouge"}
52
+
53
+ Then you get a new record like this :
54
+
55
+ {"couleur":"La couleur est rouge", "english_color" : "red"}
56
+
57
+
58
+ Example config (replace):
59
+
60
+ <filter *>
61
+ @type switch
62
+ key_space couleur
63
+ action replace
64
+ category_name couleur
65
+ default_value 'no matching cases'
66
+ <case>
67
+ condition '(couleur).+(rouge)'
68
+ value 'red'
69
+ </case>
70
+ <case>
71
+ condition '(couleur).+(jaune)'
72
+ value 'yellow'
73
+ </case>
74
+ <case>
75
+ condition '(couleur).+(bleu)'
76
+ value 'blue'
77
+ </case>
78
+ </filter>
79
+
80
+
81
+ Given the above config, if following JSON record is passed:
82
+
83
+ {"couleur":"La couleur est rouge"}
84
+
85
+ Then you get a new record like this :
86
+
87
+ {"couleur":"red"}
88
+
89
+
90
+
91
+ Example config (default_value):
92
+
93
+ <filter *>
94
+ @type switch
95
+ key_space couleur
96
+ action replace
97
+ category_name couleur
98
+ default_value 'no matching cases'
99
+ <case>
100
+ condition '(couleur).+(rouge)'
101
+ value 'red'
102
+ </case>
103
+ <case>
104
+ condition '(couleur).+(jaune)'
105
+ value 'yellow'
106
+ </case>
107
+ <case>
108
+ condition '(couleur).+(bleu)'
109
+ value 'blue'
110
+ </case>
111
+ </filter>
112
+
113
+
114
+ Given the above config, if following JSON record is passed:
115
+
116
+ {"couleur":"La couleur est blanche"}
117
+
118
+ Then you get a new record like this :
119
+
120
+ {"couleur":"no matching cases"}
121
+
122
+
123
+ ***Note** : if the default_value config parameter is not present, and there is no matched patterns, the filter will act as a pass-thru.*
@@ -0,0 +1,21 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.name = "fluent-plugin-switch"
6
+ gem.version = "0.0.2"
7
+ gem.date = '2017-08-08'
8
+ gem.authors = ["Arash Vatanpoor"]
9
+ gem.email = ["arash@a-venture.org"]
10
+ gem.summary = %q{Fluentd filter plugin to switch events}
11
+ gem.description = %q{Fluentd filter plugin to categorize events based on conditional or pattern matching of its value}
12
+ gem.homepage = 'https://github.intuit.com/avatanpoor/fluent-plugin-switch'
13
+ gem.license = 'MIT'
14
+ gem.files = `git ls-files`.split($\)
15
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.require_paths = ["lib"]
18
+
19
+ # dependency
20
+ gem.add_runtime_dependency "fluentd", '~> 0.12'
21
+ end
@@ -0,0 +1,80 @@
1
+ class Fluent::AddFilter < Fluent::Filter
2
+ Fluent::Plugin.register_filter('switch', self)
3
+
4
+ config_param :tag, :string, :default => 'switch.event'
5
+ config_param :key_space, :string, :default => 'all'
6
+ config_param :action, :string, :default => 'append'
7
+ config_param :category_name, :string, :default => 'category'
8
+ config_param :default_value, :string, :default => nil
9
+
10
+ def initialize
11
+ super
12
+ end
13
+
14
+ def configure(conf)
15
+ super
16
+ # config file validations
17
+ if !['append', 'replace'].include? action
18
+ raise Fluent::ConfigError, "undefined action, select: 'append' or 'replace'"
19
+ end
20
+
21
+ # do more config validation
22
+ ###
23
+
24
+ # create the condition / value array
25
+ @list = []
26
+ conf.elements.select { |element| element.name == 'case' }.each do |element|
27
+ element_hash = element.to_hash
28
+ if ['condition', 'value'].all? {|s| element_hash.key? s}
29
+ map = { 'condition' => element_hash['condition'], 'value' => element_hash['value']}
30
+ @list.push(map)
31
+ else
32
+ raise Fluent::ConfigError, "use 'condtion' key for pattern matching, and use 'value' key for replacement value"
33
+ end
34
+ end
35
+ # puts @list
36
+ end
37
+
38
+ # method to remap based the conditions in the config
39
+ def revalue(record)
40
+
41
+ # create the string to be compared against conditions
42
+ payload = ''
43
+ if key_space == 'all'
44
+ array = record.each{|key, value| value}
45
+ payload = array.join(',')
46
+ else
47
+ keys = key_space.split(',')
48
+ # puts keys
49
+ if keys.all? {|elem| record.key? elem}
50
+ keys.each do |key|
51
+ payload = payload + record[key]
52
+ end
53
+ else
54
+ raise Fluent::ConfigError, "one or more keys provided don't exist in the record"
55
+ end
56
+ end
57
+ @list.each do |hash|
58
+ regex_pattern = Regexp.new hash['condition']
59
+ if regex_pattern =~ (payload)
60
+ return hash['value']
61
+ end
62
+ end
63
+ return (default_value) ? default_value : record[category_name]
64
+ end
65
+
66
+
67
+ def filter(tag, time, record)
68
+ value = revalue(record)
69
+ if action == 'append'
70
+ record = record.merge!(category_name => value)
71
+ else
72
+ if record.has_key? category_name
73
+ record[category_name] = value
74
+ else
75
+ raise Fluent::ConfigError, "can't find the key in record to replace its value"
76
+ end
77
+ end
78
+ return record
79
+ end
80
+ end
data/reinstall.sh ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ # basic clean-up and reinstall
3
+ fluent-gem uninstall fluent-plugin-switch
4
+ rm -f ./fluent-plugin-switch-0.0.1.gem
5
+ gem build ./fluent-plugin-switch.gemspec
6
+ fluent-gem install ./fluent-plugin-switch-0.0.1.gem
7
+ fluentd -c ./config/switch.conf
8
+
metadata ADDED
@@ -0,0 +1,64 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: fluent-plugin-switch
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Arash Vatanpoor
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-08-08 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: fluentd
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0.12'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0.12'
27
+ description: Fluentd filter plugin to categorize events based on conditional or pattern
28
+ matching of its value
29
+ email:
30
+ - arash@a-venture.org
31
+ executables: []
32
+ extensions: []
33
+ extra_rdoc_files: []
34
+ files:
35
+ - ".gitignore"
36
+ - README.md
37
+ - fluent-plugin-switch.gemspec
38
+ - lib/fluent/plugin/filter_switch.rb
39
+ - reinstall.sh
40
+ homepage: https://github.intuit.com/avatanpoor/fluent-plugin-switch
41
+ licenses:
42
+ - MIT
43
+ metadata: {}
44
+ post_install_message:
45
+ rdoc_options: []
46
+ require_paths:
47
+ - lib
48
+ required_ruby_version: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: '0'
53
+ required_rubygems_version: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ version: '0'
58
+ requirements: []
59
+ rubyforge_project:
60
+ rubygems_version: 2.6.8
61
+ signing_key:
62
+ specification_version: 4
63
+ summary: Fluentd filter plugin to switch events
64
+ test_files: []