fluent-plugin-time-sampling 0.1.1

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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 145704aa65c1245200718d754dbea24378f8dfe4
4
+ data.tar.gz: a7996a3de0b496bcd2478f97d43294ec36ae586d
5
+ SHA512:
6
+ metadata.gz: 572092b8d47eb567810d86638921e9cf4455e4d7edc0c5e29e320c5fc923686a72409bdae3365be764e310be08ed7c949c92605200af2ce29815e72a9f7488c1
7
+ data.tar.gz: b8550d42301a3ed1ee563892a17c75d92132499bd7bd2a0b49761cd2048444dc65f0e1a8f62372e2499801c9e7b66bff72d7bcf9b6fbe62c18ed6eeaefc5b4ef
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in fluent-plugin-facts.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2018 eramuk
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 all
13
+ 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 THE
21
+ SOFTWARE.
22
+
data/README.md ADDED
@@ -0,0 +1,41 @@
1
+ # fluent-plugin-time-sampling
2
+
3
+ ## Installation
4
+ ```
5
+ gem install fluent-plugin-time-sampling
6
+ ```
7
+
8
+ ## Usage
9
+ Example:
10
+ ```
11
+ <filter test.**>
12
+ @type time_sampling
13
+ unit ${tag}, hostname, sample_key1
14
+ interval 10
15
+ keep_keys hostname, sample_key2
16
+ </filter>
17
+ ```
18
+
19
+ Assume following input in 10 seconds:
20
+ ```
21
+ sample.tag { "hostname": "host1", "sample_key1": "foo", "sample_key2": "aaa" }
22
+ sample.tag { "hostname": "host1", "sample_key1": "foo", "sample_key2": "bbb" }
23
+ sample.tag { "hostname": "host2", "sample_key1": "bar", "sample_key2": "ccc" }
24
+ ```
25
+
26
+ then output is below:
27
+ ```
28
+ sample.tag { "hostname": "host1", "sample_key2": "aaa" }
29
+ sample.tag { "hostname": "host2", "sample_key2": "ccc" }
30
+ ```
31
+
32
+ ## Configuration
33
+
34
+ ### unit
35
+ Specify keys for grouping.
36
+
37
+ ### interval
38
+ Time of filtering interval. default is 60 second.
39
+
40
+ ### keep_keys
41
+ Specify output keys. default is all keys.
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+ Rake::TestTask.new(:test) do |test|
4
+ test.libs << 'lib' << 'test'
5
+ test.pattern = 'test/**/test_*.rb'
6
+ test.verbose = true
7
+ test.warning = false
8
+ end
9
+
10
+ task :default => :test
@@ -0,0 +1,24 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+
5
+ Gem::Specification.new do |gem|
6
+ gem.name = "fluent-plugin-time-sampling"
7
+ gem.version = "0.1.1"
8
+ gem.authors = ["Yuki Kuwabara"]
9
+ gem.email = ["eramuk@gmail.com"]
10
+ gem.summary = "filtering record at prescribed intervals"
11
+ gem.description = gem.summary
12
+ gem.homepage = "https://github.com/eramuk/fluent-plugin-time-sampling"
13
+ gem.licenses = ["MIT"]
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+
20
+ gem.add_runtime_dependency "fluentd", "~> 0.12.0"
21
+
22
+ gem.add_development_dependency "rake"
23
+ gem.add_development_dependency "test-unit"
24
+ end
@@ -0,0 +1,81 @@
1
+ require "fluent/output"
2
+ require "fluent/time"
3
+ require "fluent/timezone"
4
+ require "fluent/config/error"
5
+ require "date"
6
+
7
+ module Fluent
8
+ class TimeSamplingFilter < Filter
9
+ Plugin.register_filter('time_sampling', self)
10
+
11
+ config_param :unit, :array, value_type: :string
12
+ config_param :keep_keys, :array, value_type: :string, default: []
13
+ config_param :interval, :time, default: 60
14
+
15
+ def configure(conf)
16
+ super
17
+ @cache = {}
18
+ @cache_period = @interval * 2
19
+ @interval = -1 if @interval.zero?
20
+ @unused_cache_clear_interval = 300
21
+ @unused_cache_clear_time = Time.now
22
+ end
23
+
24
+ def start
25
+ super
26
+ end
27
+
28
+ def shutdown
29
+ super
30
+ end
31
+
32
+ def filter(tag, time, record)
33
+ cache_key = @unit.map do |unit|
34
+ if unit != "${tag}" && !record.has_key?(unit)
35
+ raise "#{unit}: unit not found"
36
+ end
37
+ unit != "${tag}" ? record[unit] : tag
38
+ end.join(":")
39
+
40
+ @keep_keys.each do |key|
41
+ unless record.has_key?(key)
42
+ raise "#{key}: keep_keys not found"
43
+ end
44
+ end
45
+
46
+ unless cache(cache_key)
47
+ cache(cache_key, "")
48
+ new_record = @keep_keys.empty? ?
49
+ record.dup : record.select { |k, v| @keep_keys.include?(k) }
50
+ end
51
+
52
+ if Time.now > @unused_cache_clear_time + @unused_cache_clear_interval
53
+ clear_unused_cache
54
+ @unused_cache_clear_time = Time.now
55
+ end
56
+
57
+ new_record ||= nil
58
+ end
59
+
60
+ def cache(key, data = nil)
61
+ if data.nil?
62
+ @cache[key] ||= { :time => Time.at(0) }
63
+ expired = Time.now > @cache[key][:time]
64
+ expired ? nil : @cache[key][:data]
65
+ else
66
+ @cache[key] = {
67
+ :data => data,
68
+ :time => Time.now + @interval
69
+ }
70
+ end
71
+ end
72
+
73
+ def clear_unused_cache
74
+ @cache.each_key do |key|
75
+ expired = Time.now > @cache[key][:time] + @cache_period
76
+ @cache.delete(key) if expired
77
+ end
78
+ end
79
+
80
+ end
81
+ end
data/test/helper.rb ADDED
@@ -0,0 +1,28 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ begin
4
+ Bundler.setup(:default, :development)
5
+ rescue Bundler::BundlerError => e
6
+ $stderr.puts e.message
7
+ $stderr.puts "Run `bundle install` to install missing gems"
8
+ exit e.status_code
9
+ end
10
+ require 'test/unit'
11
+
12
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
13
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
14
+ require 'fluent/test'
15
+ unless ENV.has_key?('VERBOSE')
16
+ nulllogger = Object.new
17
+ nulllogger.instance_eval {|obj|
18
+ def method_missing(method, *args)
19
+ # pass
20
+ end
21
+ }
22
+ $log = nulllogger
23
+ end
24
+
25
+ require 'fluent/plugin/filter_time_sampling'
26
+
27
+ class Test::Unit::TestCase
28
+ end
@@ -0,0 +1,33 @@
1
+ require "helper"
2
+
3
+ class TimeSamplingFilterTest < Test::Unit::TestCase
4
+ def setup
5
+ Fluent::Test.setup
6
+ end
7
+
8
+ CONFIG = %q{
9
+ unit ${tag}, sample_key1
10
+ keep_keys sample_key2
11
+ }
12
+
13
+ def create_driver(conf = CONFIG, tag = "test")
14
+ Fluent::Test::FilterTestDriver.new(Fluent::TimeSamplingFilter, tag).configure(conf)
15
+ end
16
+
17
+ def test_filter
18
+ sample_records = [
19
+ {"sample_key1" => "foo", "sample_key2" => "aaa"},
20
+ {"sample_key1" => "foo", "sample_key2" => "bbb"},
21
+ {"sample_key1" => "bar", "sample_key2" => "ccc"},
22
+ ]
23
+ d = create_driver
24
+ d.run {
25
+ sample_records.each do |records|
26
+ d.filter(records, Time.now)
27
+ end
28
+ }
29
+ filtered = d.filtered_as_array
30
+ p filtered.map {|m| m[2] }
31
+ end
32
+
33
+ end
metadata ADDED
@@ -0,0 +1,96 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: fluent-plugin-time-sampling
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Yuki Kuwabara
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-08-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.12.0
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.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '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: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: filtering record at prescribed intervals
56
+ email:
57
+ - eramuk@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - Gemfile
63
+ - LICENSE
64
+ - README.md
65
+ - Rakefile
66
+ - fluent-plugin-time-sampling.gemspec
67
+ - lib/fluent/plugin/filter_time_sampling.rb
68
+ - test/helper.rb
69
+ - test/plugin/test_filter_time_sampling.rb
70
+ homepage: https://github.com/eramuk/fluent-plugin-time-sampling
71
+ licenses:
72
+ - MIT
73
+ metadata: {}
74
+ post_install_message:
75
+ rdoc_options: []
76
+ require_paths:
77
+ - lib
78
+ required_ruby_version: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ required_rubygems_version: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - ">="
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
88
+ requirements: []
89
+ rubyforge_project:
90
+ rubygems_version: 2.6.14
91
+ signing_key:
92
+ specification_version: 4
93
+ summary: filtering record at prescribed intervals
94
+ test_files:
95
+ - test/helper.rb
96
+ - test/plugin/test_filter_time_sampling.rb