Skip to content

Do not use benchmark gem in production #324

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions lib/prometheus/middleware/collector.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# encoding: UTF-8

require 'benchmark'
require 'prometheus/client'

module Prometheus
Expand Down Expand Up @@ -34,6 +33,12 @@ def call(env) # :nodoc:

protected

def realtime
start_time = Process.clock_gettime(Process::CLOCK_MONOTONIC)
yield
Process.clock_gettime(Process::CLOCK_MONOTONIC) - start_time
end

def init_request_metrics
@requests = @registry.counter(
:"#{@metrics_prefix}_requests_total",
Expand All @@ -58,7 +63,7 @@ def init_exception_metrics

def trace(env)
response = nil
duration = Benchmark.realtime { response = yield }
duration = realtime { response = yield }
record(env, response.first.to_s, duration)
return response
rescue => exception
Expand Down
1 change: 1 addition & 0 deletions prometheus-client.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ Gem::Specification.new do |s|

s.add_dependency 'base64'

s.add_development_dependency 'benchmark'
s.add_development_dependency 'benchmark-ips'
s.add_development_dependency 'concurrent-ruby'
s.add_development_dependency 'timecop'
Expand Down
10 changes: 5 additions & 5 deletions spec/prometheus/middleware/collector_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
end

it 'traces request information' do
expect(Benchmark).to receive(:realtime).and_yield.and_return(0.2)
expect(app).to receive(:realtime).and_yield.and_return(0.2)

get '/foo'

Expand All @@ -68,7 +68,7 @@
end

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

get '/foo/42/bars'

Expand All @@ -82,7 +82,7 @@
end

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

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

Expand All @@ -96,7 +96,7 @@
end

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

get '/foo/42/24'

Expand All @@ -110,7 +110,7 @@
end

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

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

Expand Down