Skip to content

Commit

Permalink
Merge pull request #222 from nickvanw/master
Browse files Browse the repository at this point in the history
Enable configuring `Net::HTTP` timeouts
  • Loading branch information
Sinjo authored May 12, 2021
2 parents a32180a + 546b535 commit f4ce120
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
4 changes: 3 additions & 1 deletion lib/prometheus/client/push.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class Push

attr_reader :job, :instance, :gateway, :path

def initialize(job, instance = nil, gateway = nil)
def initialize(job, instance = nil, gateway = nil, **kwargs)
@mutex = Mutex.new
@job = job
@instance = instance
Expand All @@ -31,6 +31,8 @@ def initialize(job, instance = nil, gateway = nil)

@http = Net::HTTP.new(@uri.host, @uri.port)
@http.use_ssl = (@uri.scheme == 'https')
@http.open_timeout = kwargs[:open_timeout] if kwargs[:open_timeout]
@http.read_timeout = kwargs[:read_timeout] if kwargs[:read_timeout]
end

def add(registry)
Expand Down
10 changes: 9 additions & 1 deletion spec/prometheus/client/push_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
describe Prometheus::Client::Push do
let(:gateway) { 'http://localhost:9091' }
let(:registry) { Prometheus::Client.registry }
let(:push) { Prometheus::Client::Push.new('test-job', nil, gateway) }
let(:push) { Prometheus::Client::Push.new('test-job', nil, gateway, open_timeout: 5, read_timeout: 30) }

describe '.new' do
it 'returns a new push instance' do
Expand Down Expand Up @@ -91,6 +91,8 @@

http = double(:http)
expect(http).to receive(:use_ssl=).with(false)
expect(http).to receive(:open_timeout=).with(5)
expect(http).to receive(:read_timeout=).with(30)
expect(http).to receive(:request).with(request)
expect(Net::HTTP).to receive(:new).with('localhost', 9091).and_return(http)

Expand All @@ -104,6 +106,8 @@

http = double(:http)
expect(http).to receive(:use_ssl=).with(false)
expect(http).to receive(:open_timeout=).with(5)
expect(http).to receive(:read_timeout=).with(30)
expect(http).to receive(:request).with(request)
expect(Net::HTTP).to receive(:new).with('localhost', 9091).and_return(http)

Expand All @@ -121,6 +125,8 @@

http = double(:http)
expect(http).to receive(:use_ssl=).with(true)
expect(http).to receive(:open_timeout=).with(5)
expect(http).to receive(:read_timeout=).with(30)
expect(http).to receive(:request).with(request)
expect(Net::HTTP).to receive(:new).with('localhost', 9091).and_return(http)

Expand All @@ -140,6 +146,8 @@

http = double(:http)
expect(http).to receive(:use_ssl=).with(true)
expect(http).to receive(:open_timeout=).with(5)
expect(http).to receive(:read_timeout=).with(30)
expect(http).to receive(:request).with(request)
expect(Net::HTTP).to receive(:new).with('localhost', 9091).and_return(http)

Expand Down

0 comments on commit f4ce120

Please sign in to comment.