fluent-plugin-comparison-filter 0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: a16b1f2cc160691386727f66572c81575cc90c73
4
+ data.tar.gz: ddd9853bc3b0d7cbb5bb785813f6ab23427bb1e5
5
+ SHA512:
6
+ metadata.gz: 78feac7f5eb657b3e00a64b7f7670763b8e8e1c1b3dba49d1a4f1d6f675d40d8aa54c24750f0d7e746278b445df0361713ec979c3677754c2a370393f834794a
7
+ data.tar.gz: 2471defb2f6bb834b3a075416d63053f81f63d9a51c3d1fabb3c013c7a206d8e8fa7dee380941ce52a4e4cadd2d334885c3930eebe898da4bfccd59b617e3ef2
@@ -0,0 +1,13 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.gem
11
+
12
+ # rspec failure tracking
13
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1,10 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.3.3
4
+ - 2.4.0
5
+ - 2.4.1
6
+ before_install: gem update bundler
7
+ cache: bundler
8
+
9
+ script:
10
+ - bundle exec rake test
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 reizist
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.
@@ -0,0 +1,105 @@
1
+ # Fluent::Plugin::ComparisonFilter
2
+
3
+ [![Build Status](https://travis-ci.org/reizist/fluent-plugin-comparison-filter.svg?branch=master)](https://travis-ci.org/reizist/fluent-plugin-comparison-filter)
4
+
5
+ A Filter plugin of fluentd for filtering by comparing records between newest one and the others.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'fluent-plugin-comparison-filter'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install fluent-plugin-comparison-filter
22
+
23
+ ## Configuration
24
+
25
+ ### Comparison section
26
+ | name | type | required? | default | description |
27
+ | :------------- | :------------ | :----------- | :-------| :----------------------- |
28
+ | column_key | string | yes | nil | the column for using comparison
29
+ | column_key_type | enum | yes | time | time or numeric
30
+ | time_type | enum | no | float | type of time type, float or string or unixtime
31
+ | time_format | string | no | nil |
32
+ | localtime | bool | no | true | UTC if :localtime is false and :timezone is nil
33
+ | utc | bool | no | false |
34
+ | timezone | string | no | nli |
35
+
36
+ ## Usage
37
+
38
+ ### ComparisonFilter
39
+
40
+ Add comparison filter.
41
+
42
+ ```
43
+ <filter **>
44
+ @type comparison
45
+ <comparison>
46
+ column_key start_at
47
+ columm_key_type time
48
+ time_type string
49
+ time_format %Y-%m-%dT%H:%M:%S %z
50
+ </comparison>
51
+ </filter>
52
+ ```
53
+
54
+ In the case of incoming this records:
55
+
56
+ ```
57
+ {"message": "hogehoge", "start_at":"2017-08-28T03:45:03+00:00"}
58
+ {"message": "fugafuga", "start_at":"2017-08-28T03:45:05+00:00"}
59
+ {"message": "piyopiyo", "start_at":"2017-08-28T03:45:02+00:00"}
60
+ ```
61
+
62
+ Then output becomes as belows:
63
+
64
+ ```
65
+ {"message": "hogehoge", "start_at":"2017-08-28T03:45:03+00:00"}
66
+ {"message": "fugafuga", "start_at":"2017-08-28T03:45:05+00:00"}
67
+ ```
68
+
69
+ In the other case like this,
70
+
71
+ ```
72
+ <filter **>
73
+ @type comparison
74
+ <comparison>
75
+ column_key id
76
+ columm_key_type numeric
77
+ </comparison>
78
+ </filter>
79
+ ```
80
+
81
+ input:
82
+
83
+ ```
84
+ {"message": "hogehoge", "id": 1 }
85
+ {"message": "fugafuga", "id": 5 }
86
+ {"message": "piyopiyo", "id": 3 }
87
+ {"message": "mogemoge", "id": 8 }
88
+ ```
89
+
90
+ output:
91
+
92
+ ```
93
+ {"message": "hogehoge", "id": 1 }
94
+ {"message": "fugafuga", "id": 5 }
95
+ {"message": "mogemoge", "id": 8 }
96
+ ```
97
+
98
+ ## Contributing
99
+
100
+ Bug reports and pull requests are welcome on GitHub at https://github.com/reizist/fluent-plugin-comparison-filter. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
101
+
102
+ ## License
103
+
104
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
105
+
@@ -0,0 +1,18 @@
1
+ # encoding: utf-8
2
+ require "bundler/gem_tasks"
3
+
4
+ require 'rake/testtask'
5
+ desc 'Run test_unit based test'
6
+ Rake::TestTask.new(:test) do |t|
7
+ t.libs << "test"
8
+ t.test_files = Dir["test/**/test_*.rb"].sort
9
+ t.verbose = true
10
+ #t.warning = true
11
+ end
12
+ task :default => :test
13
+
14
+ desc 'Open an irb session preloaded with the gem library'
15
+ task :console do
16
+ sh 'irb -rubygems -I lib'
17
+ end
18
+ task :c => :console
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "fluent/plugin/filter_comparison"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,7 @@
1
+ type comparison
2
+ <comparison>
3
+ column_key time
4
+ column_key_type time
5
+ time_key time
6
+ time_type unixtime
7
+ </comparison>
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'fluent/plugin/filter_comparison/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "fluent-plugin-comparison-filter"
8
+ spec.version = Fluent::Plugin::FilterComparison::VERSION
9
+ spec.authors = ["reizist"]
10
+ spec.email = ["reizist@gmail.com"]
11
+
12
+ spec.summary = %q{A fluent filter plugin to filter by comparing records.}
13
+ spec.description = spec.summary
14
+ spec.homepage = "https://github.com/reizist/fluent-plugin-comparison-filter"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files`.split("\n")
18
+ spec.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
19
+ spec.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_runtime_dependency "fluentd", [">= 0.14", "< 2"]
23
+
24
+ spec.add_development_dependency "bundler", "~> 1.15"
25
+ spec.add_development_dependency "rake", "~> 12.0.0"
26
+ spec.add_development_dependency "test-unit", "~> 3.2.5"
27
+ spec.add_development_dependency "pry"
28
+ end
@@ -0,0 +1,93 @@
1
+ require 'fluent/plugin/filter'
2
+
3
+ module Fluent
4
+ module Plugin
5
+ class ComparisonFilter < Filter
6
+ class ConfigError < StandardError ; end
7
+ Fluent::Plugin.register_filter('comparison', self)
8
+ helpers :storage
9
+
10
+ config_section :comparison, required: true, multi: false, param_name: :comparison_config do
11
+ config_param :column_key, :string, default: nil
12
+ config_param :column_key_type, :enum, list: [:numeric, :time], default: :time
13
+
14
+ config_param :time_type, :enum, list: [:float, :unixtime, :string], default: :float
15
+
16
+ Fluent::TimeMixin::TIME_PARAMETERS.each do |name, type, opts|
17
+ config_param name, type, opts
18
+ end
19
+ end
20
+
21
+ DEFAULT_STORAGE_TYPE = 'local'
22
+
23
+ def initialize
24
+ super
25
+ @column_key = nil
26
+ @column_key_type = nil
27
+ @time_type = nil
28
+ end
29
+
30
+ def configure(conf)
31
+ super
32
+ @column_key = @comparison_config.column_key
33
+ @column_key_type = @comparison_config.column_key_type
34
+ @time_type = @comparison_config.time_type
35
+
36
+ if compare_by_time?
37
+ @time_parser = case @time_type
38
+ when :float then Fluent::NumericTimeParser.new(:float)
39
+ when :unixtime then Fluent::NumericTimeParser.new(:unixtime)
40
+ else
41
+ localtime = @comparison_config.localtime && !@comparison_config.utc
42
+ Fluent::TimeParser.new(@comparison_config.time_format, localtime, @comparison_config.timezone)
43
+ end
44
+ else
45
+ end
46
+
47
+ @storage = storage_create(usage: 'comparison', conf: nil, type: DEFAULT_STORAGE_TYPE)
48
+ end
49
+
50
+ def filter(tag, time, record)
51
+ result = nil
52
+ last_recorded = fetch_comparison_key
53
+ begin
54
+ comparison_key = extract_comparison_column_from_record(record)
55
+ if last_recorded.nil? || last_recorded < comparison_key
56
+ set_comparison_key(comparison_key)
57
+ result = record
58
+ end
59
+ rescue => e
60
+ log.warn "failed to filter records", error: e
61
+ log.warn_backtrace
62
+ end
63
+ result
64
+ end
65
+
66
+ private
67
+
68
+ def fetch_comparison_key
69
+ @storage.get(:last_recorded)
70
+ end
71
+
72
+ def set_comparison_key(key)
73
+ @storage.put(:last_recorded, key)
74
+ end
75
+
76
+ def compare_by_time?
77
+ @comparison_config.column_key_type == :time
78
+ end
79
+
80
+ def extract_comparison_column_from_record(record)
81
+
82
+ if compare_by_time?
83
+ if @column_key && record.has_key?(@column_key)
84
+ return @time_parser.call(record[@column_key]).to_i # treats time as unixtime
85
+ end
86
+ nil
87
+ else
88
+ record[@column_key]
89
+ end
90
+ end
91
+ end
92
+ end
93
+ end
@@ -0,0 +1,7 @@
1
+ module Fluent
2
+ module Plugin
3
+ module FilterComparison
4
+ VERSION = "0.2".freeze
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,98 @@
1
+ require 'test_helper'
2
+ require 'fluent/test/driver/filter'
3
+
4
+ class ComparisonFilterTest < Test::Unit::TestCase
5
+ include Fluent
6
+
7
+ def setup
8
+ Fluent::Test.setup
9
+ end
10
+
11
+ def create_driver(conf = nil)
12
+ Test::FilterTestDriver.new(Plugin::ComparisonFilter).configure(conf)
13
+ end
14
+
15
+ def filter(conf, msgs)
16
+ driver = create_driver(conf)
17
+ driver.run do
18
+ msgs.each do |msg|
19
+ driver.filter(msg, "time" => Time.now)
20
+ end
21
+ end
22
+ filtered = driver.filtered_as_array
23
+ filtered
24
+ end
25
+
26
+ sub_test_case 'filter' do
27
+ test 'execute filter by datetime string' do
28
+ CONFIG = <<CONF
29
+ type comparison
30
+ <comparison>
31
+ column_key time
32
+ column_key_type time
33
+ time_type string
34
+ time_format %Y-%m-%d %H:%M:%S %z
35
+ </comparison>
36
+ CONF
37
+ base_time = Time.now
38
+ future_time = base_time + 1
39
+ past_time = base_time - 1
40
+
41
+ d = create_driver(CONFIG)
42
+
43
+ d.run do
44
+ d.emit("time" => base_time.to_s, "message" => "initial message")
45
+ d.emit("time" => future_time.to_s, "message" => "future message")
46
+ d.emit("time" => past_time.to_s, "message" => "past message")
47
+ end
48
+
49
+ assert_equal(
50
+ [{"time" => base_time.to_s, "message" => "initial message"}, {"time" => future_time.to_s, "message" => "future message"}].sort_by{|h| h["time"]},
51
+ d.filtered.instance_variable_get(:@record_array).sort_by{|h| h["time"]}
52
+ )
53
+ end
54
+
55
+ test 'execute filter by unixtime' do
56
+ conf_file = IO.read("#{File.expand_path('../../../example.conf', __FILE__)}")
57
+ base_time = Time.now.to_i
58
+ future_time = base_time + 1
59
+ past_time = base_time - 1
60
+
61
+ d = create_driver(conf_file)
62
+
63
+ d.run do
64
+ d.emit("time" => base_time, "message" => "initial message")
65
+ d.emit("time" => future_time, "message" => "future message")
66
+ d.emit("time" => past_time, "message" => "past message")
67
+ end
68
+
69
+ assert_equal(
70
+ [{"time" => base_time, "message" => "initial message"}, {"time" => future_time, "message" => "future message"}].sort_by{|h| h["time"]},
71
+ d.filtered.instance_variable_get(:@record_array).sort_by{|h| h["time"]}
72
+ )
73
+ end
74
+
75
+ test 'execute filter by numeric' do
76
+ CONFIG = <<CONF
77
+ type comparison
78
+ <comparison>
79
+ column_key id
80
+ column_key_type numeric
81
+ </comparison>
82
+ CONF
83
+
84
+ d = create_driver(CONFIG)
85
+
86
+ d.run do
87
+ d.emit("id" => 1, "message" => "initial message")
88
+ d.emit("id" => 5, "message" => "future message")
89
+ d.emit("id" => 3, "message" => "past message")
90
+ end
91
+
92
+ assert_equal(
93
+ [{"id" => 1, "message" => "initial message"}, {"id" => 5, "message" => "future message"}].sort_by{|h| h["id"]},
94
+ d.filtered.instance_variable_get(:@record_array).sort_by{|h| h["id"]}
95
+ )
96
+ end
97
+ end
98
+ end
@@ -0,0 +1,29 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+
4
+ begin
5
+ Bundler.setup(:default, :development)
6
+ rescue Bundler::BundlerError => e
7
+ $stderr.puts e.message
8
+ $stderr.puts "Run `bundle install` to install missing gems"
9
+ exit e.status_code
10
+ end
11
+ require 'test/unit'
12
+
13
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
14
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
15
+ require 'fluent/test'
16
+ unless ENV.has_key?('VERBOSE')
17
+ nulllogger = Object.new
18
+ nulllogger.instance_eval {|obj|
19
+ def method_missing(method, *args)
20
+ # pass
21
+ end
22
+ }
23
+ $log = nulllogger
24
+ end
25
+
26
+ require 'fluent/plugin/filter_comparison'
27
+
28
+ class Test::Unit::TestCase
29
+ end
metadata ADDED
@@ -0,0 +1,139 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: fluent-plugin-comparison-filter
3
+ version: !ruby/object:Gem::Version
4
+ version: '0.2'
5
+ platform: ruby
6
+ authors:
7
+ - reizist
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-09-21 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.14'
20
+ - - "<"
21
+ - !ruby/object:Gem::Version
22
+ version: '2'
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ version: '0.14'
30
+ - - "<"
31
+ - !ruby/object:Gem::Version
32
+ version: '2'
33
+ - !ruby/object:Gem::Dependency
34
+ name: bundler
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - "~>"
38
+ - !ruby/object:Gem::Version
39
+ version: '1.15'
40
+ type: :development
41
+ prerelease: false
42
+ version_requirements: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - "~>"
45
+ - !ruby/object:Gem::Version
46
+ version: '1.15'
47
+ - !ruby/object:Gem::Dependency
48
+ name: rake
49
+ requirement: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - "~>"
52
+ - !ruby/object:Gem::Version
53
+ version: 12.0.0
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - "~>"
59
+ - !ruby/object:Gem::Version
60
+ version: 12.0.0
61
+ - !ruby/object:Gem::Dependency
62
+ name: test-unit
63
+ requirement: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - "~>"
66
+ - !ruby/object:Gem::Version
67
+ version: 3.2.5
68
+ type: :development
69
+ prerelease: false
70
+ version_requirements: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - "~>"
73
+ - !ruby/object:Gem::Version
74
+ version: 3.2.5
75
+ - !ruby/object:Gem::Dependency
76
+ name: pry
77
+ requirement: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ version: '0'
82
+ type: :development
83
+ prerelease: false
84
+ version_requirements: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ description: A fluent filter plugin to filter by comparing records.
90
+ email:
91
+ - reizist@gmail.com
92
+ executables:
93
+ - console
94
+ - setup
95
+ extensions: []
96
+ extra_rdoc_files: []
97
+ files:
98
+ - ".gitignore"
99
+ - ".rspec"
100
+ - ".travis.yml"
101
+ - Gemfile
102
+ - LICENSE.txt
103
+ - README.md
104
+ - Rakefile
105
+ - bin/console
106
+ - bin/setup
107
+ - example.conf
108
+ - fluent-plugin-comparison-filter.gemspec
109
+ - lib/fluent/plugin/filter_comparison.rb
110
+ - lib/fluent/plugin/filter_comparison/version.rb
111
+ - test/plugin/test_filter_comparison.rb
112
+ - test/test_helper.rb
113
+ homepage: https://github.com/reizist/fluent-plugin-comparison-filter
114
+ licenses:
115
+ - MIT
116
+ metadata: {}
117
+ post_install_message:
118
+ rdoc_options: []
119
+ require_paths:
120
+ - lib
121
+ required_ruby_version: !ruby/object:Gem::Requirement
122
+ requirements:
123
+ - - ">="
124
+ - !ruby/object:Gem::Version
125
+ version: '0'
126
+ required_rubygems_version: !ruby/object:Gem::Requirement
127
+ requirements:
128
+ - - ">="
129
+ - !ruby/object:Gem::Version
130
+ version: '0'
131
+ requirements: []
132
+ rubyforge_project:
133
+ rubygems_version: 2.6.11
134
+ signing_key:
135
+ specification_version: 4
136
+ summary: A fluent filter plugin to filter by comparing records.
137
+ test_files:
138
+ - test/plugin/test_filter_comparison.rb
139
+ - test/test_helper.rb