yabeda-rack-ratelimit 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
+ SHA256:
3
+ metadata.gz: af2a7c5a6f86aba9c2dfde74aa7042732a7c1e6708837dd1f324177cee148e3e
4
+ data.tar.gz: ee0609741e37b5362955d8e3bc12340bbb7658bb500f51f7ffae2c9d0ad92d00
5
+ SHA512:
6
+ metadata.gz: fe569e5fe14eeebf4b2ef6d52a5fdbfdb0c0e9b8bdaa5f9b1731e3fd4511f11e69c915377dd3ccb23f7c389ed0f4d854d39bf50f1ebf2e680653769723db47b1
7
+ data.tar.gz: e618a108d82029b62f542f984254d6806c7834fd933f86429403a25d601fdd795df360df86d820d7ed7cfa9a645d846177133dde1e3a09012d7877972a26684d
@@ -0,0 +1,27 @@
1
+ name: CI
2
+ on: [push, pull_request]
3
+ jobs:
4
+ tests:
5
+ runs-on: ubuntu-latest
6
+
7
+ steps:
8
+ - uses: actions/checkout@v1
9
+
10
+ - name: Set up Ruby
11
+ uses: ruby/setup-ruby@v1
12
+ with:
13
+ ruby-version: 2.7.8
14
+ rubygems: latest
15
+
16
+ - name: Cache gem dependencies
17
+ uses: actions/cache@v1
18
+ with:
19
+ path: vendor/bundle
20
+ key: ${{ runner.os }}-bundler-${{ hashFiles('**/Gemfile.lock') }}
21
+ restore-keys: ${{ runner.os }}-bundler-
22
+
23
+ - name: Install dependencies
24
+ run: gem install bundler && bundle install --jobs 4 --retry 3 --path vendor/bundle
25
+
26
+ - name: Run tests
27
+ run: bundle exec rake test
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,33 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ yabeda-rack-ratelimit (0.1.0)
5
+ rack-ratelimit
6
+ yabeda
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ anyway_config (2.4.2)
12
+ ruby-next-core (>= 0.14.0)
13
+ concurrent-ruby (1.2.2)
14
+ dry-initializer (3.1.1)
15
+ minitest (5.18.1)
16
+ rack-ratelimit (1.2.1)
17
+ rake (13.0.6)
18
+ ruby-next-core (0.15.3)
19
+ yabeda (0.11.0)
20
+ anyway_config (>= 1.0, < 3)
21
+ concurrent-ruby
22
+ dry-initializer
23
+
24
+ PLATFORMS
25
+ arm64-darwin-22
26
+
27
+ DEPENDENCIES
28
+ minitest
29
+ rake
30
+ yabeda-rack-ratelimit!
31
+
32
+ BUNDLED WITH
33
+ 2.4.17
data/LICENSE.md ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2023 37signals
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,37 @@
1
+ # yabeda-rack-ratelimit
2
+
3
+ ![CI](https://github.com/basecamp/yabeda-rack-ratelimit/actions/workflows/ci.yml/badge.svg)
4
+ [![Gem Version](https://img.shields.io/gem/v/yabeda-rack-ratelimit.svg)](https://rubygems.org/gems/yabeda-rack-ratelimit)
5
+
6
+ [Yabeda] plugin to collect metrics for [Rack::Ratelimit].
7
+
8
+ ## Get started
9
+
10
+ To install the latest version using [Bundler][bundler]:
11
+
12
+ ```ruby
13
+ gem "yabeda-rack-ratelimit"
14
+ ```
15
+
16
+ ## Metrics
17
+
18
+ | Metric | Type | Tags | Description |
19
+ |-------------------|-------------|-----------------------------|--------------------------------------------------------------------|
20
+ | `requests_total` | counter | name | The total number of requests considered for rate limiting for a named ratelimit |
21
+ | `exceeded_requests_total` | counter | name | The total number of requests that exceeded a named rate limit |
22
+ | `clients_total` | counter | name | The total number of clients considered for rate limiting for a named ratelimit |
23
+ | `exceeded_clients_total` | counter | name | The total number of clients that exceeded a named rate limit |
24
+ | `quota_consumed_ratio` | gauge | name | The ratio of quota consumed to quota available for a named rate limit |
25
+
26
+ ## Acknowledgments
27
+
28
+ yabeda-rack-ratelimit is [MIT-licensed](LICENSE.md) open-source software from [37signals](https://37signals.com/), the creators of [Ruby on Rails](https://rubyonrails.org).
29
+
30
+ ---
31
+
32
+ © 2023 37signals, LLC.
33
+
34
+ [Yabeda]: https://github.com/yabeda-rb/yabeda
35
+ [Rack::Ratelimit]: https://github.com/jeremy/rack-ratelimit
36
+ [bundler]: https://bundler.io
37
+ [rubygems]: https://rubygems.org
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require "rubygems"
2
+ require "bundler/setup"
3
+ require "rake"
4
+ require "rake/testtask"
5
+
6
+ task default: :test
7
+
8
+ Rake::TestTask.new do |t|
9
+ t.pattern = "test/**/*_test.rb"
10
+ end
data/bin/release ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env bash
2
+
3
+ VERSION=$1
4
+
5
+ printf "module Yabeda\n module Rack\n module Ratelimit\n VERSION = \"$VERSION\"\n end\n end\nend\n" > ./lib/yabeda/rack/ratelimit/version.rb
6
+ bundle
7
+ git add Gemfile.lock lib/yabeda/rack/ratelimit/version.rb
8
+ git commit -m "Bump version for $VERSION"
9
+ git push
10
+ git tag v$VERSION
11
+ git push --tags
12
+ gem build yabeda-rack-ratelimit.gemspec
13
+ gem push "yabeda-rack-ratelimit-$VERSION.gem" --host https://rubygems.org
14
+ rm "yabeda-rack-ratelimit-$VERSION.gem"
@@ -0,0 +1,31 @@
1
+ module Yabeda
2
+ module Rack
3
+ module Ratelimit
4
+ module Instrumentation
5
+ private
6
+ def ratelimit_json(remaining, epoch)
7
+ report_stats(remaining)
8
+ super
9
+ end
10
+
11
+ def report_stats(remaining)
12
+ quota_consumed_ratio = (1 - remaining.to_f / @max)
13
+
14
+ Yabeda.rack_ratelimit.requests_total.increment({ name: @name })
15
+ Yabeda.rack_ratelimit.quota_consumed_ratio.set({ name: @name }, quota_consumed_ratio)
16
+
17
+ case remaining
18
+ when @max - 1 # On the first request within a rate-limiting period
19
+ Yabeda.rack_ratelimit.clients_total.increment({ name: @name })
20
+ when -1 # On the first request that exceeds the rate limit
21
+ Yabeda.rack_ratelimit.exceeded_clients_total.increment({ name: @name })
22
+ end
23
+
24
+ if remaining < 0
25
+ Yabeda.rack_ratelimit.exceeded_requests_total.increment({ name: @name })
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,7 @@
1
+ module Yabeda
2
+ module Rack
3
+ module Ratelimit
4
+ VERSION = "0.1.0"
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "yabeda"
4
+
5
+ Yabeda.configure do
6
+ group :rack_ratelimit
7
+
8
+ default_tag :name, "", group: :rack_ratelimit
9
+
10
+ counter :requests_total, comment: "The total number of requests considered for rate limiting"
11
+ counter :exceeded_requests_total, comment: "The total number of requests that exceeded rate limits"
12
+ counter :clients_total, comment: "The total number of clients considered for rate limiting"
13
+ counter :exceeded_clients_total, comment: "The total number of clients that exceeded rate limits"
14
+ gauge :quota_consumed_ratio, comment: "The ratio of quota consumed to quota available"
15
+ end
16
+
17
+ require_relative "ratelimit/instrumentation"
18
+ Rack::Ratelimit.prepend Yabeda::Rack::Ratelimit::Instrumentation
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/yabeda/rack/ratelimit/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "yabeda-rack-ratelimit"
7
+ spec.version = Yabeda::Rack::Ratelimit::VERSION
8
+ spec.authors = ["Lewis Buckley"]
9
+ spec.email = ["lewis@37signals.com"]
10
+
11
+ spec.summary = "Instrumentation for Rack::Ratelimit"
12
+ spec.homepage = "https://github.com/basecamp/yabeda-rack-ratelimit"
13
+ spec.license = "MIT"
14
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.7")
15
+
16
+ spec.metadata["homepage_uri"] = spec.homepage
17
+ spec.metadata["source_code_uri"] = "https://github.com/basecamp/yabeda-rack-ratelimit"
18
+
19
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
20
+ `git ls-files -z`.split("\x0").reject do |f|
21
+ f.match(%r{^(test|spec|features|example|docs)/})
22
+ end
23
+ end
24
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
25
+ spec.require_paths = ["lib"]
26
+
27
+ spec.add_runtime_dependency "yabeda"
28
+ spec.add_runtime_dependency "rack-ratelimit"
29
+
30
+ spec.add_development_dependency "rake"
31
+ spec.add_development_dependency "minitest"
32
+ end
metadata ADDED
@@ -0,0 +1,113 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: yabeda-rack-ratelimit
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Lewis Buckley
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2023-07-27 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: yabeda
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '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'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rack-ratelimit
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
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: rake
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
+ - !ruby/object:Gem::Dependency
56
+ name: minitest
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description:
70
+ email:
71
+ - lewis@37signals.com
72
+ executables:
73
+ - release
74
+ extensions: []
75
+ extra_rdoc_files: []
76
+ files:
77
+ - ".github/workflows/ci.yml"
78
+ - Gemfile
79
+ - Gemfile.lock
80
+ - LICENSE.md
81
+ - README.md
82
+ - Rakefile
83
+ - bin/release
84
+ - lib/yabeda/rack/ratelimit.rb
85
+ - lib/yabeda/rack/ratelimit/instrumentation.rb
86
+ - lib/yabeda/rack/ratelimit/version.rb
87
+ - yabeda-rack-ratelimit.gemspec
88
+ homepage: https://github.com/basecamp/yabeda-rack-ratelimit
89
+ licenses:
90
+ - MIT
91
+ metadata:
92
+ homepage_uri: https://github.com/basecamp/yabeda-rack-ratelimit
93
+ source_code_uri: https://github.com/basecamp/yabeda-rack-ratelimit
94
+ post_install_message:
95
+ rdoc_options: []
96
+ require_paths:
97
+ - lib
98
+ required_ruby_version: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - ">="
101
+ - !ruby/object:Gem::Version
102
+ version: '2.7'
103
+ required_rubygems_version: !ruby/object:Gem::Requirement
104
+ requirements:
105
+ - - ">="
106
+ - !ruby/object:Gem::Version
107
+ version: '0'
108
+ requirements: []
109
+ rubygems_version: 3.4.12
110
+ signing_key:
111
+ specification_version: 4
112
+ summary: Instrumentation for Rack::Ratelimit
113
+ test_files: []