fluent-plugin-belated-record-filter 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +13 -0
- data/.rspec +2 -0
- data/.travis.yml +10 -0
- data/Gemfile +5 -0
- data/LICENSE.txt +21 -0
- data/README.md +63 -0
- data/Rakefile +18 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/example.conf +6 -0
- data/fluent-plugin-belated-record-filter.gemspec +28 -0
- data/lib/fluent/plugin/filter_belated_record.rb +49 -0
- data/lib/fluent/plugin/filter_belated_record/version.rb +7 -0
- data/test/plugin/test_filter_belated_record.rb +76 -0
- data/test/test_helper.rb +29 -0
- metadata +139 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: b9e6581a21052f43f4595acecaf7770e30728a5d
|
4
|
+
data.tar.gz: 14018391cfc9af8b130b5fb78d48ea2f95a881ea
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: b8df9e0af6653ffffb60fd9ba62072a5cb8e8f91233daf2726517fd97a3a4bec66c4a0cc65b2669927773a938ed2fad1db613e9c23621c840c46e1f5a09d9130
|
7
|
+
data.tar.gz: 17a5f10d070bf13f5d400024ddaa5307e88d08abde104ec25220443d5386fb817a99da3a59771df75140385a69b25dfa9488aa82546eaa82852b73469bdda0e0
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -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.
|
data/README.md
ADDED
@@ -0,0 +1,63 @@
|
|
1
|
+
# Fluent::Plugin::BelatedRecordFilter
|
2
|
+
|
3
|
+
[![Build Status](https://travis-ci.org/reizist/fluent-plugin-belated-record-filter.svg?branch=master)](https://travis-ci.org/reizist/fluent-plugin-belated-record-filter)
|
4
|
+
|
5
|
+
A Filter plugin of fluentd for filtering older records than newest one.
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'fluent-plugin-belated-record-filter'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install fluent-plugin-belated-record-filter
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
### BelatedRecordFilter
|
26
|
+
|
27
|
+
Add belated_record filter.
|
28
|
+
|
29
|
+
```
|
30
|
+
<filter **>
|
31
|
+
@type belated_record
|
32
|
+
<extract>
|
33
|
+
time_key start_at
|
34
|
+
time_type string
|
35
|
+
time_format %Y-%m-%dT%H:%M:%S %z
|
36
|
+
keep_time_key true
|
37
|
+
</extract>
|
38
|
+
</filter>
|
39
|
+
```
|
40
|
+
|
41
|
+
In the case of incoming this records:
|
42
|
+
|
43
|
+
```
|
44
|
+
{"message": "hogehoge", "start_at":"2017-08-28T03:45:03+00:00"}
|
45
|
+
{"message": "fugafuga", "start_at":"2017-08-28T03:45:05+00:00"}
|
46
|
+
{"message": "piyopiyo", "start_at":"2017-08-28T03:45:02+00:00"}
|
47
|
+
```
|
48
|
+
|
49
|
+
Then output becomes as belows:
|
50
|
+
|
51
|
+
```
|
52
|
+
{"message": "hogehoge", "start_at":"2017-08-28T03:45:03+00:00"}
|
53
|
+
{"message": "fugafuga", "start_at":"2017-08-28T03:45:05+00:00"}
|
54
|
+
```
|
55
|
+
|
56
|
+
## Contributing
|
57
|
+
|
58
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/reizist/fluent-plugin-belated-record-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.
|
59
|
+
|
60
|
+
## License
|
61
|
+
|
62
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
63
|
+
|
data/Rakefile
ADDED
@@ -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
|
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "fluent/plugin/filter_belated_record"
|
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__)
|
data/bin/setup
ADDED
data/example.conf
ADDED
@@ -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_belated_record/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "fluent-plugin-belated-record-filter"
|
8
|
+
spec.version = Fluent::Plugin::FilterBelatedRecord::VERSION
|
9
|
+
spec.authors = ["reizist"]
|
10
|
+
spec.email = ["reizist@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{A fluent filter plugin to filter belated records.}
|
13
|
+
spec.description = spec.summary
|
14
|
+
spec.homepage = "https://github.com/reizist/fluent-plugin-belated-record-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,49 @@
|
|
1
|
+
require 'fluent/plugin/filter'
|
2
|
+
|
3
|
+
module Fluent
|
4
|
+
module Plugin
|
5
|
+
class BelatedRecordFilter < Filter
|
6
|
+
class ConfigError < StandardError ; end
|
7
|
+
Fluent::Plugin.register_filter('belated_record', self)
|
8
|
+
|
9
|
+
helpers :extract, :storage
|
10
|
+
config_param :time_key, :string, default: nil
|
11
|
+
DEFAULT_STORAGE_TYPE = 'local'
|
12
|
+
|
13
|
+
def initialize
|
14
|
+
super
|
15
|
+
end
|
16
|
+
|
17
|
+
def configure(conf)
|
18
|
+
super
|
19
|
+
@storage = storage_create(usage: 'belated_record', conf: nil, type: DEFAULT_STORAGE_TYPE)
|
20
|
+
end
|
21
|
+
|
22
|
+
def filter(tag, time, record)
|
23
|
+
result = nil
|
24
|
+
last_recorded = fetch_timestamp
|
25
|
+
begin
|
26
|
+
record_time = extract_time_from_record(record).to_i # treats time as unixtime
|
27
|
+
if last_recorded.nil? || last_recorded < record_time
|
28
|
+
set_timestamp(record_time)
|
29
|
+
result = record
|
30
|
+
end
|
31
|
+
rescue => e
|
32
|
+
log.warn "failed to filter records", error: e
|
33
|
+
log.warn_backtrace
|
34
|
+
end
|
35
|
+
result
|
36
|
+
end
|
37
|
+
|
38
|
+
private
|
39
|
+
|
40
|
+
def fetch_timestamp
|
41
|
+
@storage.get(:last_recorded)
|
42
|
+
end
|
43
|
+
|
44
|
+
def set_timestamp(time)
|
45
|
+
@storage.put(:last_recorded, time)
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
@@ -0,0 +1,76 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
require 'fluent/test/driver/filter'
|
3
|
+
|
4
|
+
class BelatedRecordFilterTest < 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::BelatedRecordFilter).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 belated_record
|
30
|
+
<extract>
|
31
|
+
time_key time
|
32
|
+
time_type string
|
33
|
+
time_format %Y-%m-%d %H:%M:%S %z
|
34
|
+
keep_time_key true
|
35
|
+
|
36
|
+
</extract>
|
37
|
+
CONF
|
38
|
+
base_time = Time.now
|
39
|
+
future_time = base_time + 1
|
40
|
+
past_time = base_time - 1
|
41
|
+
|
42
|
+
d = create_driver(CONFIG)
|
43
|
+
|
44
|
+
d.run do
|
45
|
+
d.emit("time" => base_time.to_s, "message" => "initial message")
|
46
|
+
d.emit("time" => future_time.to_s, "message" => "future message")
|
47
|
+
d.emit("time" => past_time.to_s, "message" => "past message")
|
48
|
+
end
|
49
|
+
|
50
|
+
assert_equal(
|
51
|
+
[{"time" => base_time.to_s, "message" => "initial message"}, {"time" => future_time.to_s, "message" => "future message"}].sort_by{|h| h["time"]},
|
52
|
+
d.filtered.instance_variable_get(:@record_array).sort_by{|h| h["time"]}
|
53
|
+
)
|
54
|
+
end
|
55
|
+
|
56
|
+
test 'execute filter by unixtime' do
|
57
|
+
conf_file = IO.read("#{File.expand_path('../../../example.conf', __FILE__)}")
|
58
|
+
base_time = Time.now.to_i
|
59
|
+
future_time = base_time + 1
|
60
|
+
past_time = base_time - 1
|
61
|
+
|
62
|
+
d = create_driver(conf_file)
|
63
|
+
|
64
|
+
d.run do
|
65
|
+
d.emit("time" => base_time, "message" => "initial message")
|
66
|
+
d.emit("time" => future_time, "message" => "future message")
|
67
|
+
d.emit("time" => past_time, "message" => "past message")
|
68
|
+
end
|
69
|
+
|
70
|
+
assert_equal(
|
71
|
+
[{"time" => base_time, "message" => "initial message"}, {"time" => future_time, "message" => "future message"}].sort_by{|h| h["time"]},
|
72
|
+
d.filtered.instance_variable_get(:@record_array).sort_by{|h| h["time"]}
|
73
|
+
)
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
data/test/test_helper.rb
ADDED
@@ -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_belated_record'
|
27
|
+
|
28
|
+
class Test::Unit::TestCase
|
29
|
+
end
|
metadata
ADDED
@@ -0,0 +1,139 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: fluent-plugin-belated-record-filter
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- reizist
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-08-30 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 belated 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-belated-record-filter.gemspec
|
109
|
+
- lib/fluent/plugin/filter_belated_record.rb
|
110
|
+
- lib/fluent/plugin/filter_belated_record/version.rb
|
111
|
+
- test/plugin/test_filter_belated_record.rb
|
112
|
+
- test/test_helper.rb
|
113
|
+
homepage: https://github.com/reizist/fluent-plugin-belated-record-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 belated records.
|
137
|
+
test_files:
|
138
|
+
- test/plugin/test_filter_belated_record.rb
|
139
|
+
- test/test_helper.rb
|