fluent-plugin-throttle 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 96baca37d87b547aadf4ef128d973f5605477242
4
- data.tar.gz: bb0ec1c1d81b4db2048dafac99d7b795caf2fc68
2
+ SHA256:
3
+ metadata.gz: 6b2e1a36b7725f3104af98d1fb78418cda39bbd485a4c934652a9e01a179bb23
4
+ data.tar.gz: 55a63790bcf7b60f27fdf6a886791feb57a7f8245c7607a1c182890ea95122e6
5
5
  SHA512:
6
- metadata.gz: a85cdcbdae70af12c549012a58a2e640727c8fb9dbb12e0753499ee0091a124bd8ac90097bf89193293849de9aea6cdab4e0e3b986856aab6396f18759dfe88b
7
- data.tar.gz: 21482c46242a78e2d7d2150cead76734afdaf937be5c2d714c1d2c6ac8afa79472a89ced7cffcea7a44ab0e6807c6d17bd9b4b5c5dab6adc54a851243a2dfacd
6
+ metadata.gz: 0ed4d59ec171b432ceb4d0f7bfd95842dbba2deef99d8fa81c12aa6531b3a72a87668f26df08a75189d203c5b9e31192d6cc02b3c60f70fbb7865a311ea35bc2
7
+ data.tar.gz: f6fddde539b046be640c99327276e3f965c605f87a1949e25e29e2339d109b29cc7f874608bdd164bc6a1c7a6b4d34f7f0441e83432f4a6a41ee77e7270b1a95
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ os: linux
3
+ sudo: false
4
+ script: bundle exec rake test
data/CONTRIBUTING.md ADDED
@@ -0,0 +1 @@
1
+ Contributions via GitHub pull requests are gladly accepted from their original author. Along with any pull requests, please state that the contribution is your original work and that you license the work to the project under the project's open source license. Whether or not you state this explicitly, by submitting any copyrighted material via pull request, email, or other means you agree to license the material under the project's open source license and warrant that you have the legal authority to do so.
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
4
+
5
+ gem 'pry'
6
+ gem 'pry-byebug'
data/Gemfile.lock ADDED
@@ -0,0 +1,89 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ fluent-plugin-throttle (0.0.3)
5
+ fluentd (~> 1.1)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ addressable (2.5.2)
11
+ public_suffix (>= 2.0.2, < 4.0)
12
+ appraisal (2.2.0)
13
+ bundler
14
+ rake
15
+ thor (>= 0.14.0)
16
+ byebug (10.0.2)
17
+ coderay (1.1.2)
18
+ cool.io (1.5.3)
19
+ crack (0.4.3)
20
+ safe_yaml (~> 1.0.0)
21
+ dig_rb (1.0.1)
22
+ fluentd (1.2.3)
23
+ cool.io (>= 1.4.5, < 2.0.0)
24
+ dig_rb (~> 1.0.0)
25
+ http_parser.rb (>= 0.5.1, < 0.7.0)
26
+ msgpack (>= 0.7.0, < 2.0.0)
27
+ serverengine (>= 2.0.4, < 3.0.0)
28
+ sigdump (~> 0.2.2)
29
+ strptime (>= 0.2.2, < 1.0.0)
30
+ tzinfo (~> 1.0)
31
+ tzinfo-data (~> 1.0)
32
+ yajl-ruby (~> 1.0)
33
+ hashdiff (0.3.7)
34
+ http_parser.rb (0.6.0)
35
+ maxitest (3.1.0)
36
+ minitest (>= 5.0.0, < 5.12.0)
37
+ metaclass (0.0.4)
38
+ method_source (0.9.0)
39
+ minitest (5.11.3)
40
+ mocha (1.6.0)
41
+ metaclass (~> 0.0.1)
42
+ msgpack (1.2.4)
43
+ power_assert (1.1.3)
44
+ pry (0.11.3)
45
+ coderay (~> 1.1.0)
46
+ method_source (~> 0.9.0)
47
+ pry-byebug (3.6.0)
48
+ byebug (~> 10.0)
49
+ pry (~> 0.10)
50
+ public_suffix (3.0.2)
51
+ rake (12.3.1)
52
+ safe_yaml (1.0.4)
53
+ serverengine (2.0.7)
54
+ sigdump (~> 0.2.2)
55
+ sigdump (0.2.4)
56
+ single_cov (1.1.0)
57
+ strptime (0.2.3)
58
+ test-unit (3.2.8)
59
+ power_assert
60
+ thor (0.20.0)
61
+ thread_safe (0.3.6)
62
+ tzinfo (1.2.5)
63
+ thread_safe (~> 0.1)
64
+ tzinfo-data (1.2018.5)
65
+ tzinfo (>= 1.0.0)
66
+ webmock (3.4.2)
67
+ addressable (>= 2.3.6)
68
+ crack (>= 0.3.2)
69
+ hashdiff
70
+ yajl-ruby (1.4.1)
71
+
72
+ PLATFORMS
73
+ ruby
74
+
75
+ DEPENDENCIES
76
+ appraisal (~> 2.2)
77
+ bundler (~> 1.16)
78
+ fluent-plugin-throttle!
79
+ maxitest
80
+ mocha
81
+ pry
82
+ pry-byebug
83
+ rake (~> 12.3)
84
+ single_cov
85
+ test-unit (~> 3.2)
86
+ webmock (~> 3.3)
87
+
88
+ BUNDLED WITH
89
+ 1.16.2
data/LICENSE ADDED
@@ -0,0 +1,201 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "[]"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright [yyyy] [name of copyright owner]
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
data/README.md CHANGED
@@ -1,5 +1,7 @@
1
1
  # fluent-plugin-throttle
2
2
 
3
+ [![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://github.com/rubrikinc/fluent-plugin-throttle/blob/master/LICENSE)
4
+
3
5
  A sentry pluging to throttle logs. Logs are grouped by a configurable key. When
4
6
  a group exceeds a configuration rate, logs are dropped for this group.
5
7
 
@@ -38,7 +40,11 @@ the group key resolve to "random":
38
40
  {"level": "error", "msg": "plugin test", "kubernetes": { "container_name": "random" } }
39
41
  ```
40
42
 
41
- If the group cannot be resolved, am anonymous group is used.
43
+ Multiple groups can be specified using the fluentd config array syntax,
44
+ e.g. `kubernetes.container_name,kubernetes.pod_name`, in which case each unique pair
45
+ of key values are rate limited independently.
46
+
47
+ If the group cannot be resolved, an anonymous (`nil`) group is used for rate limiting.
42
48
 
43
49
  #### group\_bucket\_period\_s
44
50
 
@@ -69,6 +75,16 @@ logs per minute. However `60/60s` will readily emit 60 logs within the first
69
75
  second then nothing for the remaining 59 seconds. While the `1/1s` will only
70
76
  emit the first log of every second.
71
77
 
78
+ #### group\_drop\_logs
79
+
80
+ Default: `true`.
81
+
82
+ When a group reaches its limit, logs will be dropped from further processing
83
+ if this value is true (set by default). To prevent the logs from being dropped
84
+ and only receive a warning message when rate limiting would have occurred, set
85
+ this value for false. This can be useful to fine-tune your group bucket limits
86
+ before dropping any logs.
87
+
72
88
  #### group\_reset\_rate\_s
73
89
 
74
90
  Default: `group_bucket_limit/group_bucket_period_s` (logs per `group_bucket_period_s`).
@@ -113,10 +129,10 @@ When a group reaches its limit and as long as it is not reset, a warning
113
129
  message with the current log rate of the group is emitted repeatedly. This is
114
130
  the delay between every repetition.
115
131
 
116
- ## Copyright
117
-
118
- Copyright © 2018 ([Rubrik Inc.](https://www.rubrik.com))
119
-
120
132
  ## License
121
133
 
122
134
  Apache License, Version 2.0
135
+
136
+ ## Copyright
137
+
138
+ Copyright © 2018 ([Rubrik Inc.](https://www.rubrik.com))
data/Rakefile ADDED
@@ -0,0 +1,9 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rake/testtask'
3
+
4
+ task default: [:test]
5
+ Rake::TestTask.new do |test|
6
+ test.libs << 'lib' << 'test'
7
+ test.test_files = FileList['test/**/*_test.rb']
8
+ test.options = '-v'
9
+ end
@@ -4,7 +4,7 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
4
 
5
5
  Gem::Specification.new do |spec|
6
6
  spec.name = "fluent-plugin-throttle"
7
- spec.version = "0.0.3"
7
+ spec.version = "0.0.4"
8
8
  spec.authors = ["François-Xavier Bourlet"]
9
9
  spec.email = ["fx.bourlet@rubrik.com"]
10
10
  spec.summary = %q{Fluentd filter for throttling logs based on a configurable key.}
@@ -21,6 +21,9 @@ Gem::Specification.new do |spec|
21
21
  spec.add_development_dependency "webmock", "~> 3.3"
22
22
  spec.add_development_dependency "test-unit", "~> 3.2"
23
23
  spec.add_development_dependency "appraisal", "~> 2.2"
24
+ spec.add_development_dependency "mocha"
25
+ spec.add_development_dependency "maxitest"
26
+ spec.add_development_dependency "single_cov"
24
27
 
25
28
  spec.add_runtime_dependency "fluentd", "~> 1.1"
26
29
  end
@@ -1,13 +1,38 @@
1
- require 'fluent/filter'
1
+ # frozen_string_literal: true
2
+ require 'fluent/plugin/filter'
2
3
 
3
- module Fluent
4
+ module Fluent::Plugin
4
5
  class ThrottleFilter < Filter
5
6
  Fluent::Plugin.register_filter('throttle', self)
6
7
 
7
- config_param :group_key, :string, :default => 'kubernetes.container_name'
8
+ desc "Used to group logs. Groups are rate limited independently"
9
+ config_param :group_key, :array, :default => ['kubernetes.container_name']
10
+
11
+ desc <<~DESC
12
+ This is the period of of time over which group_bucket_limit applies
13
+ DESC
8
14
  config_param :group_bucket_period_s, :integer, :default => 60
15
+
16
+ desc <<~DESC
17
+ Maximum number logs allowed per groups over the period of
18
+ group_bucket_period_s
19
+ DESC
9
20
  config_param :group_bucket_limit, :integer, :default => 6000
21
+
22
+ desc "Whether to drop logs that exceed the bucket limit or not"
23
+ config_param :group_drop_logs, :bool, :default => true
24
+
25
+ desc <<~DESC
26
+ After a group has exceeded its bucket limit, logs are dropped until the
27
+ rate per second falls below or equal to group_reset_rate_s.
28
+ DESC
10
29
  config_param :group_reset_rate_s, :integer, :default => nil
30
+
31
+ desc <<~DESC
32
+ When a group reaches its limit and as long as it is not reset, a warning
33
+ message with the current log rate of the group is emitted repeatedly.
34
+ This is the delay between every repetition.
35
+ DESC
11
36
  config_param :group_warning_delay_s, :integer, :default => 10
12
37
 
13
38
  Group = Struct.new(
@@ -21,7 +46,7 @@ module Fluent
21
46
  def configure(conf)
22
47
  super
23
48
 
24
- @group_key_path = group_key.split(".")
49
+ @group_key_paths = group_key.map { |key| key.split(".") }
25
50
 
26
51
  raise "group_bucket_period_s must be > 0" \
27
52
  unless @group_bucket_period_s > 0
@@ -46,21 +71,19 @@ module Fluent
46
71
  def start
47
72
  super
48
73
 
49
- @counters = Hash.new()
74
+ @counters = {}
50
75
  end
51
76
 
52
77
  def shutdown
53
- $log.info("counters summary: #{@counters}")
78
+ log.debug("counters summary: #{@counters}")
54
79
  super
55
80
  end
56
81
 
57
82
  def filter(tag, time, record)
58
83
  now = Time.now
84
+ rate_limit_exceeded = @group_drop_logs ? nil : record # return nil on rate_limit_exceeded to drop the record
59
85
  group = extract_group(record)
60
- counter = @counters.fetch(group, nil)
61
- counter = @counters[group] = Group.new(
62
- 0, now, 0, 0, now, nil) if counter == nil
63
-
86
+ counter = (@counters[group] ||= Group.new(0, now, 0, 0, now, nil))
64
87
  counter.rate_count += 1
65
88
 
66
89
  since_last_rate_reset = now - counter.rate_last_reset
@@ -81,7 +104,7 @@ module Fluent
81
104
  log_rate_back_down(now, group, counter)
82
105
  else
83
106
  log_rate_limit_exceeded(now, group, counter)
84
- return nil
107
+ return rate_limit_exceeded
85
108
  end
86
109
  end
87
110
 
@@ -92,7 +115,7 @@ module Fluent
92
115
  # if current time period credit is exhausted, drop the record.
93
116
  if counter.bucket_count == -1
94
117
  log_rate_limit_exceeded(now, group, counter)
95
- return nil
118
+ return rate_limit_exceeded
96
119
  end
97
120
  end
98
121
 
@@ -102,32 +125,36 @@ module Fluent
102
125
  if counter.bucket_count > @group_bucket_limit
103
126
  log_rate_limit_exceeded(now, group, counter)
104
127
  counter.bucket_count = -1
105
- return nil
128
+ return rate_limit_exceeded
106
129
  end
107
130
 
108
131
  record
109
132
  end
110
133
 
134
+ private
135
+
111
136
  def extract_group(record)
112
- record.dig(*@group_key_path)
137
+ @group_key_paths.map do |key_path|
138
+ record.dig(*key_path) || record.dig(*key_path.map(&:to_sym))
139
+ end
113
140
  end
114
141
 
115
142
  def log_rate_limit_exceeded(now, group, counter)
116
143
  emit = counter.last_warning == nil ? true \
117
144
  : (now - counter.last_warning) >= @group_warning_delay_s
118
145
  if emit
119
- $log.warn("rate exceeded", log_items(now, group, counter))
146
+ log.warn("rate exceeded", log_items(now, group, counter))
120
147
  counter.last_warning = now
121
148
  end
122
149
  end
123
150
 
124
151
  def log_rate_back_down(now, group, counter)
125
- $log.info("rate back down", log_items(now, group, counter))
152
+ log.info("rate back down", log_items(now, group, counter))
126
153
  end
127
154
 
128
155
  def log_items(now, group, counter)
129
156
  since_last_reset = now - counter.bucket_last_reset
130
- rate = (counter.bucket_count / since_last_reset).round()
157
+ rate = since_last_reset > 0 ? (counter.bucket_count / since_last_reset).round : Float::INFINITY
131
158
  aprox_rate = counter.aprox_rate
132
159
  rate = aprox_rate if aprox_rate > rate
133
160
 
@@ -0,0 +1,182 @@
1
+ # frozen_string_literal: true
2
+ require_relative '../../helper'
3
+ require 'fluent/plugin/filter_throttle'
4
+
5
+ SingleCov.covered!
6
+
7
+ describe Fluent::Plugin::ThrottleFilter do
8
+ include Fluent::Test::Helpers
9
+
10
+ before do
11
+ Fluent::Test.setup
12
+ end
13
+
14
+ after do
15
+ if instance_variable_defined?(:@driver)
16
+ assert @driver.error_events.empty?, "Errors detected: " + @driver.error_events.map(&:inspect).join("\n")
17
+ end
18
+ end
19
+
20
+ def create_driver(conf='')
21
+ @driver = Fluent::Test::Driver::Filter.new(Fluent::Plugin::ThrottleFilter).configure(conf)
22
+ end
23
+
24
+ describe '#configure' do
25
+ it 'raises on invalid group_bucket_limit' do
26
+ assert_raises { create_driver("group_bucket_limit 0") }
27
+ end
28
+
29
+ it 'raises on invalid group_bucket_period_s' do
30
+ assert_raises { create_driver("group_bucket_period_s 0") }
31
+ end
32
+
33
+ it 'raises on invalid group_reset_rate_s' do
34
+ assert_raises { create_driver("group_bucket_limit 10\ngroup_bucket_period_s 10\ngroup_reset_rate_s 2") }
35
+ end
36
+
37
+ it 'raises on invalid group_reset_rate_s' do
38
+ assert_raises { create_driver("group_bucket_limit 10\ngroup_bucket_period_s 10\ngroup_reset_rate_s -2") }
39
+ end
40
+
41
+ it 'raises on invalid group_warning_delay_s' do
42
+ assert_raises { create_driver("group_warning_delay_s 0") }
43
+ end
44
+ end
45
+
46
+ describe '#filter' do
47
+ it 'throttles per group key' do
48
+ driver = create_driver <<~CONF
49
+ group_key "group"
50
+ group_bucket_period_s 1
51
+ group_bucket_limit 5
52
+ CONF
53
+
54
+ driver.run(default_tag: "test") do
55
+ driver.feed([[event_time, {"msg": "test", "group": "a"}]] * 10)
56
+ driver.feed([[event_time, {"msg": "test", "group": "b"}]] * 10)
57
+ end
58
+
59
+ groups = driver.filtered_records.group_by { |r| r[:group] }
60
+ assert_equal(5, groups["a"].size)
61
+ assert_equal(5, groups["b"].size)
62
+ end
63
+
64
+ it 'allows composite group keys' do
65
+ driver = create_driver <<~CONF
66
+ group_key "group1,group2"
67
+ group_bucket_period_s 1
68
+ group_bucket_limit 5
69
+ CONF
70
+
71
+ driver.run(default_tag: "test") do
72
+ driver.feed([[event_time, {"msg": "test", "group1": "a", "group2": "b"}]] * 10)
73
+ driver.feed([[event_time, {"msg": "test", "group1": "b", "group2": "b"}]] * 10)
74
+ driver.feed([[event_time, {"msg": "test", "group1": "c"}]] * 10)
75
+ driver.feed([[event_time, {"msg": "test", "group2": "c"}]] * 10)
76
+ end
77
+
78
+ groups = driver.filtered_records.group_by { |r| r[:group1] }
79
+ groups.each { |k, g| groups[k] = g.group_by { |r| r[:group2] } }
80
+
81
+ assert_equal(5, groups["a"]["b"].size)
82
+ assert_equal(5, groups["b"]["b"].size)
83
+ assert_equal(5, groups["c"][nil].size)
84
+ assert_equal(5, groups[nil]["c"].size)
85
+ end
86
+
87
+ it 'drops until rate drops below group_reset_rate_s' do
88
+ driver = create_driver <<~CONF
89
+ group_bucket_period_s 60
90
+ group_bucket_limit 180
91
+ group_reset_rate_s 2
92
+ CONF
93
+
94
+ logs_per_sec = [4, 4, 2, 1, 2]
95
+
96
+ driver.run(default_tag: "test") do
97
+ (0...logs_per_sec.size*60).each do |i|
98
+ Time.stubs(now: Time.at(i))
99
+ min = i / 60
100
+
101
+ driver.feed([[event_time, min: min]] * logs_per_sec[min])
102
+ end
103
+ end
104
+
105
+ groups = driver.filtered_records.group_by { |r| r[:min] }
106
+ messages_per_minute = []
107
+ (0...logs_per_sec.size).each do |min|
108
+ messages_per_minute[min] = groups.fetch(min, []).size
109
+ end
110
+
111
+ assert_equal [
112
+ 180, # hits the limit in the first minute
113
+ 0, # still >= group_reset_rate_s
114
+ 0, # still >= group_reset_rate_s
115
+ 59, # now < group_reset_rate_s, stop dropping logs (except the first log is dropped)
116
+ 120 # > group_reset_rate_s is okay now because haven't hit the limit
117
+ ], messages_per_minute
118
+ end
119
+
120
+
121
+ it 'does not throttle when in log only mode' do
122
+ driver = create_driver <<~CONF
123
+ group_bucket_period_s 2
124
+ group_bucket_limit 4
125
+ group_reset_rate_s 2
126
+ group_drop_logs false
127
+ CONF
128
+
129
+ records_expected = 0
130
+ driver.run(default_tag: "test") do
131
+ (0...10).each do |i|
132
+ Time.stubs(now: Time.at(i))
133
+ count = [1,8 - i].max
134
+ records_expected += count
135
+ driver.feed((0...count).map { |j| [event_time, msg: "test#{i}-#{j}"] }) # * count)
136
+ end
137
+ end
138
+
139
+ assert_equal records_expected, driver.filtered_records.size
140
+ assert driver.logs.any? { |log| log.include?('rate exceeded') }
141
+ assert driver.logs.any? { |log| log.include?('rate back down') }
142
+ end
143
+ end
144
+
145
+ describe 'logging' do
146
+ it 'logs when rate exceeded once per group_warning_delay_s' do
147
+ driver = create_driver <<~CONF
148
+ group_bucket_period_s 2
149
+ group_bucket_limit 2
150
+ group_warning_delay_s 3
151
+ CONF
152
+
153
+ logs_per_sec = 4
154
+
155
+ driver.run(default_tag: "test") do
156
+ (0...10).each do |i|
157
+ Time.stubs(now: Time.at(i))
158
+ driver.feed([[event_time, msg: "test"]] * logs_per_sec)
159
+ end
160
+ end
161
+
162
+ assert_equal 4, driver.logs.select { |log| log.include?('rate exceeded') }.size
163
+ end
164
+
165
+ it 'logs when rate drops below group_reset_rate_s' do
166
+ driver = create_driver <<~CONF
167
+ group_bucket_period_s 2
168
+ group_bucket_limit 4
169
+ group_reset_rate_s 2
170
+ CONF
171
+
172
+ driver.run(default_tag: "test") do
173
+ (0...10).each do |i|
174
+ Time.stubs(now: Time.at(i))
175
+ driver.feed([[event_time, msg: "test"]] * [1,8 - i].max)
176
+ end
177
+ end
178
+
179
+ assert driver.logs.any? { |log| log.include?('rate back down') }
180
+ end
181
+ end
182
+ end
data/test/helper.rb ADDED
@@ -0,0 +1,12 @@
1
+ require 'bundler/setup'
2
+ require 'single_cov'
3
+
4
+ SingleCov.setup :minitest
5
+
6
+ require 'fluent/test'
7
+ require 'fluent/test/driver/input'
8
+ require 'fluent/test/driver/filter'
9
+ require 'fluent/test/driver/output'
10
+ require 'fluent/test/helpers'
11
+ require 'maxitest/autorun'
12
+ require 'mocha/minitest'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fluent-plugin-throttle
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - François-Xavier Bourlet
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-03-27 00:00:00.000000000 Z
11
+ date: 2019-01-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -80,6 +80,48 @@ dependencies:
80
80
  - - "~>"
81
81
  - !ruby/object:Gem::Version
82
82
  version: '2.2'
83
+ - !ruby/object:Gem::Dependency
84
+ name: mocha
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: maxitest
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: single_cov
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
83
125
  - !ruby/object:Gem::Dependency
84
126
  name: fluentd
85
127
  requirement: !ruby/object:Gem::Requirement
@@ -102,9 +144,17 @@ extensions: []
102
144
  extra_rdoc_files: []
103
145
  files:
104
146
  - ".gitignore"
147
+ - ".travis.yml"
148
+ - CONTRIBUTING.md
149
+ - Gemfile
150
+ - Gemfile.lock
151
+ - LICENSE
105
152
  - README.md
153
+ - Rakefile
106
154
  - fluent-plugin-throttle.gemspec
107
155
  - lib/fluent/plugin/filter_throttle.rb
156
+ - test/fluent/plugin/filter_throttle_test.rb
157
+ - test/helper.rb
108
158
  homepage: https://github.com/rubrikinc/fluent-plugin-throttle
109
159
  licenses:
110
160
  - Apache-2.0
@@ -125,8 +175,10 @@ required_rubygems_version: !ruby/object:Gem::Requirement
125
175
  version: '0'
126
176
  requirements: []
127
177
  rubyforge_project:
128
- rubygems_version: 2.5.2.1
178
+ rubygems_version: 2.7.6
129
179
  signing_key:
130
180
  specification_version: 4
131
181
  summary: Fluentd filter for throttling logs based on a configurable key.
132
- test_files: []
182
+ test_files:
183
+ - test/fluent/plugin/filter_throttle_test.rb
184
+ - test/helper.rb