Skip to content

Commit dca6002

Browse files
authored
Merge pull request #324 from Juanmcuello/add-benchmark-to-gemspec
Do not use benchmark gem in production
2 parents 32b0e8f + ddfae02 commit dca6002

File tree

3 files changed

+13
-7
lines changed

3 files changed

+13
-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

prometheus-client.gemspec

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ Gem::Specification.new do |s|
1717

1818
s.add_dependency 'base64'
1919

20+
s.add_development_dependency 'benchmark'
2021
s.add_development_dependency 'benchmark-ips'
2122
s.add_development_dependency 'concurrent-ruby'
2223
s.add_development_dependency 'timecop'

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)