fluent-plugin-filter-single_key 0.1.0

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: 708465fbc3e9c986d6fd1dbaa9e30ac5d1a6ec3f
4
+ data.tar.gz: 45e958e5e96d36df05d39f702b9c1bb0b0f3113d
5
+ SHA512:
6
+ metadata.gz: 2d0f117455700b25de7a4ca66513a879849e749a8f07f92965871696bc882f230638b34eab302905123c241499e96537aa2e695a6ffc5c250407f1b86fab6d47
7
+ data.tar.gz: 205d1b058a4a7c53ce787d5a839f0febd010db9ee1c9b40d96b9208976e17de3b83b72e90b22f8c8817229934f1cad2c16d72ef799f98d949687c92b1654545b
data/.gitignore ADDED
@@ -0,0 +1,2 @@
1
+ .bundle
2
+ Gemfile.lock
data/.travis.yml ADDED
@@ -0,0 +1,6 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.8
4
+ - 2.3.5
5
+ - 2.4.2
6
+ before_install: gem install bundler
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source "https://rubygems.org"
2
+
3
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 joker1007
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,73 @@
1
+ # fluent-plugin-filter-single_key
2
+
3
+ [Fluentd](https://fluentd.org/) filter plugin that Explode record to single key record.
4
+
5
+ ## Installation
6
+
7
+ ### RubyGems
8
+
9
+ ```
10
+ $ gem install fluent-plugin-filter-single_key
11
+ ```
12
+
13
+ ### Bundler
14
+
15
+ Add following line to your Gemfile:
16
+
17
+ ```ruby
18
+ gem "fluent-plugin-filter-single_key"
19
+ ```
20
+
21
+ And then execute:
22
+
23
+ ```
24
+ $ bundle
25
+ ```
26
+
27
+ ## Configuration
28
+
29
+ ### key_pattern (string) (required)
30
+
31
+ regexp pattern for target key
32
+
33
+ ### keep_key_pattern (string) (optional)
34
+
35
+ regexp pattern for keep key
36
+
37
+ ### new_key (string) (optional)
38
+
39
+ If this param is set, replace this value as new key
40
+
41
+ You can copy and paste generated documents here.
42
+
43
+ ## Sample
44
+
45
+ ### Config
46
+
47
+ ```
48
+ <filter sample.tag>
49
+ key_pattern foo(\d)
50
+ new_key bar\1
51
+ keep_key_pattern other1
52
+ </filter>
53
+ ```
54
+
55
+ ### Incoming Record
56
+
57
+ ```
58
+ {"foo1" => 1, "foo2" => 2, "other1" => 99, "other2" => 100}
59
+ ```
60
+
61
+ ### Outgoing Record
62
+
63
+ ```
64
+ {"bar1" => 1, "other1" => 99}
65
+ {"bar2" => 2, "other1" => 99}
66
+ {"bar3" => 3, "other1" => 99}
67
+ ```
68
+
69
+ ## Copyright
70
+
71
+ * Copyright(c) 2017- joker1007
72
+ * License
73
+ * MIT
data/Rakefile ADDED
@@ -0,0 +1,13 @@
1
+ require "bundler"
2
+ Bundler::GemHelper.install_tasks
3
+
4
+ require "rake/testtask"
5
+
6
+ Rake::TestTask.new(:test) do |t|
7
+ t.libs.push("lib", "test")
8
+ t.test_files = FileList["test/**/test_*.rb"]
9
+ t.verbose = true
10
+ t.warning = true
11
+ end
12
+
13
+ task default: [:test]
@@ -0,0 +1,27 @@
1
+ lib = File.expand_path("../lib", __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+
4
+ Gem::Specification.new do |spec|
5
+ spec.name = "fluent-plugin-filter-single_key"
6
+ spec.version = "0.1.0"
7
+ spec.authors = ["joker1007"]
8
+ spec.email = ["kakyoin.hierophant@gmail.com"]
9
+
10
+ spec.summary = %q{Fluentd filter plugin that Explode record to single key record.}
11
+ spec.description = %q{Fluentd filter plugin that Explode record to single key record.}
12
+ spec.homepage = "https://github.com/joker1007/fluent-plugin-filter-single_key"
13
+ spec.license = "MIT"
14
+
15
+ test_files, files = `git ls-files -z`.split("\x0").partition do |f|
16
+ f.match(%r{^(test|spec|features)/})
17
+ end
18
+ spec.files = files
19
+ spec.executables = files.grep(%r{^bin/}) { |f| File.basename(f) }
20
+ spec.test_files = test_files
21
+ spec.require_paths = ["lib"]
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.14"
24
+ spec.add_development_dependency "rake", "~> 12.0"
25
+ spec.add_development_dependency "test-unit", "~> 3.0"
26
+ spec.add_runtime_dependency "fluentd", [">= 0.14.10", "< 2"]
27
+ end
@@ -0,0 +1,54 @@
1
+ require "fluent/plugin/filter"
2
+
3
+ module Fluent
4
+ module Plugin
5
+ class SingleValueFilter < Fluent::Plugin::Filter
6
+ Fluent::Plugin.register_filter("single_key", self)
7
+
8
+ desc "regexp pattern for target key"
9
+ config_param :key_pattern, :string
10
+
11
+ desc "regexp pattern for keep key"
12
+ config_param :keep_key_pattern, :string, default: nil
13
+
14
+ desc "If this param is set, replace this value as new key"
15
+ config_param :new_key, :string, default: nil
16
+
17
+ def configure(conf)
18
+ super
19
+
20
+ @key_pattern = Regexp.new(@key_pattern)
21
+ @keep_key_pattern = Regexp.new(@keep_key_pattern) if @keep_key_pattern
22
+ end
23
+
24
+ def filter_stream(tag, es)
25
+ new_es = MultiEventStream.new
26
+ es.each do |time, record|
27
+ begin
28
+ record.each do |k, v|
29
+ if k =~ @key_pattern
30
+ new_record =
31
+ if @new_key
32
+ {k.gsub(@key_pattern, @new_key) => v}
33
+ else
34
+ {k => v}
35
+ end
36
+
37
+ if @keep_key_pattern
38
+ keep_record = record.select { |k2, _| k2 =~ @keep_key_pattern }
39
+ unless keep_record.empty?
40
+ new_record = keep_record.merge(new_record)
41
+ end
42
+ end
43
+ new_es.add(time, new_record)
44
+ end
45
+ end
46
+ rescue => e
47
+ router.emit_error_event(tag, time, record, e)
48
+ end
49
+ end
50
+ new_es
51
+ end
52
+ end
53
+ end
54
+ end
data/test/helper.rb ADDED
@@ -0,0 +1,8 @@
1
+ $LOAD_PATH.unshift(File.expand_path("../../", __FILE__))
2
+ require "test-unit"
3
+ require "fluent/test"
4
+ require "fluent/test/driver/filter"
5
+ require "fluent/test/helpers"
6
+
7
+ Test::Unit::TestCase.include(Fluent::Test::Helpers)
8
+ Test::Unit::TestCase.extend(Fluent::Test::Helpers)
@@ -0,0 +1,61 @@
1
+ require "helper"
2
+ require "fluent/plugin/filter_single_key.rb"
3
+
4
+ class SingleValueFilterTest < Test::Unit::TestCase
5
+ setup do
6
+ Fluent::Test.setup
7
+ end
8
+
9
+ test "#configure" do
10
+ d = create_driver
11
+
12
+ assert { d.instance.key_pattern == /foo(\d)/ }
13
+ assert { d.instance.new_key == 'bar\1' }
14
+ end
15
+
16
+ test "#filter_stream" do
17
+ d = create_driver
18
+
19
+ d.run do
20
+ d.feed("tag", Time.now.to_i, {"foo1" => 1, "foo2" => 2, "no_match" => 99})
21
+ end
22
+
23
+ assert { d.filtered_records == [{"bar1" => 1}, {"bar2" => 2}] }
24
+ end
25
+
26
+ test "#filter_stream without new_key" do
27
+ d = create_driver(%q{
28
+ key_pattern foo(\d)
29
+ })
30
+
31
+ d.run do
32
+ d.feed("tag", Time.now.to_i, {"foo1" => 1, "foo2" => 2, "no_match" => 99})
33
+ end
34
+
35
+ assert { d.filtered_records == [{"foo1" => 1}, {"foo2" => 2}] }
36
+ end
37
+
38
+ test "#filter_stream wit keep_key_pattern" do
39
+ d = create_driver(%q{
40
+ key_pattern foo(\d)
41
+ keep_key_pattern no_match
42
+ })
43
+
44
+ d.run do
45
+ d.feed("tag", Time.now.to_i, {"foo1" => 1, "foo2" => 2, "no_match" => 99})
46
+ end
47
+
48
+ assert { d.filtered_records == [{"foo1" => 1, "no_match" => 99}, {"foo2" => 2, "no_match" => 99}] }
49
+ end
50
+
51
+ private
52
+
53
+ DEFAULT_CONF = %q{
54
+ key_pattern foo(\d)
55
+ new_key bar\1
56
+ }
57
+
58
+ def create_driver(conf = DEFAULT_CONF)
59
+ Fluent::Test::Driver::Filter.new(Fluent::Plugin::SingleValueFilter).configure(conf)
60
+ end
61
+ end
metadata ADDED
@@ -0,0 +1,118 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: fluent-plugin-filter-single_key
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - joker1007
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-11-13 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.14'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.14'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '12.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '12.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: test-unit
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: fluentd
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: 0.14.10
62
+ - - "<"
63
+ - !ruby/object:Gem::Version
64
+ version: '2'
65
+ type: :runtime
66
+ prerelease: false
67
+ version_requirements: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - ">="
70
+ - !ruby/object:Gem::Version
71
+ version: 0.14.10
72
+ - - "<"
73
+ - !ruby/object:Gem::Version
74
+ version: '2'
75
+ description: Fluentd filter plugin that Explode record to single key record.
76
+ email:
77
+ - kakyoin.hierophant@gmail.com
78
+ executables: []
79
+ extensions: []
80
+ extra_rdoc_files: []
81
+ files:
82
+ - ".gitignore"
83
+ - ".travis.yml"
84
+ - Gemfile
85
+ - LICENSE
86
+ - README.md
87
+ - Rakefile
88
+ - fluent-plugin-filter-single_key.gemspec
89
+ - lib/fluent/plugin/filter_single_key.rb
90
+ - test/helper.rb
91
+ - test/plugin/test_filter_single_key.rb
92
+ homepage: https://github.com/joker1007/fluent-plugin-filter-single_key
93
+ licenses:
94
+ - MIT
95
+ metadata: {}
96
+ post_install_message:
97
+ rdoc_options: []
98
+ require_paths:
99
+ - lib
100
+ required_ruby_version: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - ">="
103
+ - !ruby/object:Gem::Version
104
+ version: '0'
105
+ required_rubygems_version: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - ">="
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ requirements: []
111
+ rubyforge_project:
112
+ rubygems_version: 2.6.13
113
+ signing_key:
114
+ specification_version: 4
115
+ summary: Fluentd filter plugin that Explode record to single key record.
116
+ test_files:
117
+ - test/helper.rb
118
+ - test/plugin/test_filter_single_key.rb