Skip to content

Commit

Permalink
FIX: kwargs patching (#313)
Browse files Browse the repository at this point in the history
Amends method profiler patching to use Ruby 3.0 patterns and enforces Ruby 3.0 and up.
  • Loading branch information
simpl1g committed Apr 3, 2024
1 parent d172482 commit e23bac5
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 11 deletions.
1 change: 1 addition & 0 deletions .rubocop
@@ -0,0 +1 @@
--ignore-unrecognized-cops
9 changes: 8 additions & 1 deletion .rubocop.yml
Expand Up @@ -4,4 +4,11 @@ inherit_gem:
AllCops:
Exclude:
- 'gemfiles/**/*'
- 'vendor/**/*'
- 'vendor/**/*'

Discourse/Plugins/NoMonkeyPatching:
Enabled: false

Discourse/Plugins/NamespaceMethods:
Exclude:
- bin/prometheus_exporter
4 changes: 2 additions & 2 deletions README.md
Expand Up @@ -40,7 +40,7 @@ To learn more see [Instrumenting Rails with Prometheus](https://samsaffron.com/a

## Requirements

Minimum Ruby of version 2.6.0 is required, Ruby 2.5.0 is EOL as of March 31st 2021.
Minimum Ruby of version 3.0.0 is required, Ruby 2.7 is EOL as of March 31st 2023.

## Migrating from v0.x

Expand Down Expand Up @@ -884,7 +884,7 @@ prometheus_exporter -p 8080 \
--prefix 'foo_'
```

You can use `-b` option to bind the `prometheus_exporter` web server to any IPv4 interface with `-b 0.0.0.0`,
You can use `-b` option to bind the `prometheus_exporter` web server to any IPv4 interface with `-b 0.0.0.0`,
any IPv6 interface with `-b ::`, or `-b ANY` to any IPv4/IPv6 interfaces available on your host system.

#### Enabling Basic Authentication
Expand Down
2 changes: 2 additions & 0 deletions gemfiles/ar_70.gemfile
Expand Up @@ -2,4 +2,6 @@

source "https://rubygems.org"

gem "activerecord", "~> 7.0.0"

gemspec path: "../"
8 changes: 4 additions & 4 deletions lib/prometheus_exporter/instrumentation/method_profiler.rb
Expand Up @@ -44,7 +44,7 @@ def self.define_methods_on_module(klass, methods, name)
patch_source_line = __LINE__ + 3
patches = methods.map do |method_name|
<<~RUBY
def #{method_name}(*args, &blk)
def #{method_name}(...)
unless prof = Thread.current[:_method_profiler]
return super
end
Expand Down Expand Up @@ -75,13 +75,13 @@ def self.patch_using_alias_method(klass, methods, name)
<<~RUBY
unless defined?(#{method_name}__mp_unpatched)
alias_method :#{method_name}__mp_unpatched, :#{method_name}
def #{method_name}(*args, &blk)
def #{method_name}(...)
unless prof = Thread.current[:_method_profiler]
return #{method_name}__mp_unpatched(*args, &blk)
return #{method_name}__mp_unpatched(...)
end
begin
start = Process.clock_gettime(Process::CLOCK_MONOTONIC)
#{method_name}__mp_unpatched(*args, &blk)
#{method_name}__mp_unpatched(...)
ensure
data = (prof[:#{name}] ||= {duration: 0.0, calls: 0})
data[:duration] += Process.clock_gettime(Process::CLOCK_MONOTONIC) - start
Expand Down
4 changes: 2 additions & 2 deletions prometheus_exporter.gemspec
Expand Up @@ -34,13 +34,13 @@ Gem::Specification.new do |spec|
spec.add_development_dependency "oj", "~> 3.0"
spec.add_development_dependency "rack-test", "~> 0.8.3"
spec.add_development_dependency "minitest-stub-const", "~> 0.6"
spec.add_development_dependency "rubocop-discourse", ">2"
spec.add_development_dependency "rubocop-discourse", ">= 3"
spec.add_development_dependency "appraisal", "~> 2.3"
spec.add_development_dependency "activerecord", "~> 6.0.0"
spec.add_development_dependency "redis", "> 5"
spec.add_development_dependency "m"
if !RUBY_ENGINE == 'jruby'
spec.add_development_dependency "raindrops", "~> 0.19"
end
spec.required_ruby_version = '>= 2.6.0'
spec.required_ruby_version = '>= 3.0.0'
end
4 changes: 2 additions & 2 deletions test/instrumentation/method_profiler_test.rb
Expand Up @@ -22,7 +22,7 @@ def some_method
def test_alias_method_source_location
file, line = SomeClassPatchedUsingAliasMethod.instance_method(:some_method).source_location
source = File.read(file).lines[line - 1].strip
assert_equal 'def #{method_name}(*args, &blk)', source
assert_equal 'def #{method_name}(...)', source
end

def test_alias_method_preserves_behavior
Expand All @@ -32,7 +32,7 @@ def test_alias_method_preserves_behavior
def test_prepend_source_location
file, line = SomeClassPatchedUsingPrepend.instance_method(:some_method).source_location
source = File.read(file).lines[line - 1].strip
assert_equal 'def #{method_name}(*args, &blk)', source
assert_equal 'def #{method_name}(...)', source
end

def test_prepend_preserves_behavior
Expand Down

0 comments on commit e23bac5

Please sign in to comment.