diff --git a/lib/network_resiliency.rb b/lib/network_resiliency.rb index ff37f68..295e5d0 100644 --- a/lib/network_resiliency.rb +++ b/lib/network_resiliency.rb @@ -135,7 +135,7 @@ def record(adapter:, action:, destination:, duration:, error:, timeout:, attempt adapter: adapter, destination: destination, }, - ) + ) if timeout && timeout > 0 if error NetworkResiliency.statsd&.distribution( @@ -145,7 +145,7 @@ def record(adapter:, action:, destination:, duration:, error:, timeout:, attempt adapter: adapter, destination: destination, }, - ) if timeout + ) if timeout && timeout > 0 else # track successful retries NetworkResiliency.statsd&.increment( diff --git a/lib/network_resiliency/adapter/http.rb b/lib/network_resiliency/adapter/http.rb index e9a5d79..ca0279f 100644 --- a/lib/network_resiliency/adapter/http.rb +++ b/lib/network_resiliency/adapter/http.rb @@ -55,7 +55,7 @@ def connect destination: address, error: error, duration: ts, - timeout: self.open_timeout * 1_000, + timeout: self.open_timeout.to_f * 1_000, attempts: attempts, ) end diff --git a/lib/network_resiliency/adapter/mysql.rb b/lib/network_resiliency/adapter/mysql.rb index ccc96e0..a333b6f 100644 --- a/lib/network_resiliency/adapter/mysql.rb +++ b/lib/network_resiliency/adapter/mysql.rb @@ -36,7 +36,7 @@ def connect(_, _, host, *args) destination: host, error: e&.class, duration: ts, - timeout: query_options[:connect_timeout] * 1_000, + timeout: query_options[:connect_timeout].to_f * 1_000, ) end end diff --git a/lib/network_resiliency/adapter/redis.rb b/lib/network_resiliency/adapter/redis.rb index 6b31046..093b620 100644 --- a/lib/network_resiliency/adapter/redis.rb +++ b/lib/network_resiliency/adapter/redis.rb @@ -78,7 +78,7 @@ def establish_connection destination: host, duration: ts, error: error, - timeout: @options[:connect_timeout] * 1_000, + timeout: @options[:connect_timeout].to_f * 1_000, attempts: attempts, ) end diff --git a/spec/mysql_spec.rb b/spec/mysql_spec.rb index 8803a96..67ba509 100644 --- a/spec/mysql_spec.rb +++ b/spec/mysql_spec.rb @@ -46,7 +46,7 @@ before do described_class.patch - allow(NetworkResiliency).to receive(:record) + allow(NetworkResiliency).to receive(:record).and_call_original end it "can not connect to a mysql server" do @@ -60,10 +60,30 @@ destination: host, duration: be_a(Integer), error: nil, - timeout: be_a(Integer), + timeout: be_a(Numeric), ) end + it "logs timeout" do + subject + + expect(NetworkResiliency.statsd).to have_received(:gauge).with( + "network_resiliency.connect.timeout", + timeout * 1_000, + anything, + ) + end + + context "when connect timeout is nil" do + let(:timeout) { nil } + + it "does not log timeout" do + subject + + expect(NetworkResiliency.statsd).not_to have_received(:gauge) + end + end + context "when server connection times out" do let(:klass_mock) do Class.new(Mysql2::Client) do diff --git a/spec/network_resiliency_spec.rb b/spec/network_resiliency_spec.rb index 185e1fa..8b0f27f 100644 --- a/spec/network_resiliency_spec.rb +++ b/spec/network_resiliency_spec.rb @@ -286,6 +286,28 @@ def expect_enabled ) end + context "when timeout is nil" do + let(:timeout) { nil } + + it "does not track timeout" do + is_expected.not_to have_received(:gauge).with( + "network_resiliency.#{action}.timeout", + any_args, + ) + end + end + + context "when timeout is 0" do + let(:timeout) { 0 } + + it "does not track timeout" do + is_expected.not_to have_received(:gauge).with( + "network_resiliency.#{action}.timeout", + any_args, + ) + end + end + it "does not track attempts for first time successes" do is_expected.to have_received(:distribution).with( "network_resiliency.#{action}", diff --git a/spec/postgres_spec.rb b/spec/postgres_spec.rb index 3568174..1070bf5 100644 --- a/spec/postgres_spec.rb +++ b/spec/postgres_spec.rb @@ -60,7 +60,7 @@ destination: host, duration: be_a(Integer), error: nil, - timeout: be_a(Integer), + timeout: be_a(Numeric), ) end