Skip to content

Commit 845440d

Browse files
committed
Replace benchmark gem dependency in production
The benchmark gem is no longer a default gem in Ruby 3.5, so we've removed its use by replacing the Benchmark.realtime method with our own implementation. We still rely on the gem in our tests, though. Signed-off-by: Juan Manuel Cuello <jcuello@fu.do>
1 parent 32b0e8f commit 845440d

File tree

2 files changed

+12
-7
lines changed

2 files changed

+12
-7
lines changed

lib/prometheus/middleware/collector.rb

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
# encoding: UTF-8
22

3-
require 'benchmark'
43
require 'prometheus/client'
54

65
module Prometheus
@@ -34,6 +33,12 @@ def call(env) # :nodoc:
3433

3534
protected
3635

36+
def realtime
37+
start_time = Process.clock_gettime(Process::CLOCK_MONOTONIC)
38+
yield
39+
Process.clock_gettime(Process::CLOCK_MONOTONIC) - start_time
40+
end
41+
3742
def init_request_metrics
3843
@requests = @registry.counter(
3944
:"#{@metrics_prefix}_requests_total",
@@ -58,7 +63,7 @@ def init_exception_metrics
5863

5964
def trace(env)
6065
response = nil
61-
duration = Benchmark.realtime { response = yield }
66+
duration = realtime { response = yield }
6267
record(env, response.first.to_s, duration)
6368
return response
6469
rescue => exception

spec/prometheus/middleware/collector_spec.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
end
4343

4444
it 'traces request information' do
45-
expect(Benchmark).to receive(:realtime).and_yield.and_return(0.2)
45+
expect(app).to receive(:realtime).and_yield.and_return(0.2)
4646

4747
get '/foo'
4848

@@ -68,7 +68,7 @@
6868
end
6969

7070
it 'normalizes paths containing numeric IDs by default' do
71-
expect(Benchmark).to receive(:realtime).and_yield.and_return(0.3)
71+
expect(app).to receive(:realtime).and_yield.and_return(0.3)
7272

7373
get '/foo/42/bars'
7474

@@ -82,7 +82,7 @@
8282
end
8383

8484
it 'normalizes paths containing UUIDs by default' do
85-
expect(Benchmark).to receive(:realtime).and_yield.and_return(0.3)
85+
expect(app).to receive(:realtime).and_yield.and_return(0.3)
8686

8787
get '/foo/5180349d-a491-4d73-af30-4194a46bdff3/bars'
8888

@@ -96,7 +96,7 @@
9696
end
9797

9898
it 'handles consecutive path segments containing IDs' do
99-
expect(Benchmark).to receive(:realtime).and_yield.and_return(0.3)
99+
expect(app).to receive(:realtime).and_yield.and_return(0.3)
100100

101101
get '/foo/42/24'
102102

@@ -110,7 +110,7 @@
110110
end
111111

112112
it 'handles consecutive path segments containing UUIDs' do
113-
expect(Benchmark).to receive(:realtime).and_yield.and_return(0.3)
113+
expect(app).to receive(:realtime).and_yield.and_return(0.3)
114114

115115
get '/foo/5180349d-a491-4d73-af30-4194a46bdff3/5180349d-a491-4d73-af30-4194a46bdff2'
116116

0 commit comments

Comments
 (0)