From 7d16ff22bbf518ec3f94c891c92c875cec4b01ac Mon Sep 17 00:00:00 2001 From: Nick Van Wiggeren Date: Tue, 23 Mar 2021 12:49:48 -0700 Subject: [PATCH 1/2] add open/read timeout kwargs Signed-off-by: Nick Van Wiggeren --- lib/prometheus/client/push.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/prometheus/client/push.rb b/lib/prometheus/client/push.rb index 03efb9f7..d5e14d80 100644 --- a/lib/prometheus/client/push.rb +++ b/lib/prometheus/client/push.rb @@ -20,7 +20,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 @@ -30,6 +30,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) From 546b5352610b4d26ce341264558749805ff3c0ee Mon Sep 17 00:00:00 2001 From: Nick Van Wiggeren Date: Wed, 12 May 2021 15:46:39 -0700 Subject: [PATCH 2/2] test open/read timeout Signed-off-by: Nick Van Wiggeren --- spec/prometheus/client/push_spec.rb | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/spec/prometheus/client/push_spec.rb b/spec/prometheus/client/push_spec.rb index f84748e3..a9a4020a 100644 --- a/spec/prometheus/client/push_spec.rb +++ b/spec/prometheus/client/push_spec.rb @@ -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 @@ -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) @@ -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) @@ -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) @@ -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)