From ca858925de7bc4e150eea3d5023d37462da5dbe9 Mon Sep 17 00:00:00 2001 From: Dmitry Tsepelev Date: Sun, 26 Feb 2023 12:47:35 +0300 Subject: [PATCH] Handle zero payload (#22) --- CHANGELOG.md | 1 + lib/io_monitor/publishers/base_publisher.rb | 6 +++--- spec/io_monitor/publishers/shared_examples.rb | 11 +++++++++++ 3 files changed, 15 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 18088db..f7da057 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,6 @@ # Change log +- [PR#22](https://github.com/DmitryTsepelev/io_monitor/pull/22) Handle zero payload ([@DmitryTsepelev]) - [PR#17](https://github.com/DmitryTsepelev/io_monitor/pull/17) Prometheus publisher ([@maxshend]) ## main diff --git a/lib/io_monitor/publishers/base_publisher.rb b/lib/io_monitor/publishers/base_publisher.rb index 12585cb..2c85676 100644 --- a/lib/io_monitor/publishers/base_publisher.rb +++ b/lib/io_monitor/publishers/base_publisher.rb @@ -16,15 +16,15 @@ def process_action(payload) (payload.keys - [:response]).each do |source| ratio = ratio(payload[:response], payload[source]) - if ratio < IoMonitor.config.warn_threshold - publish(source, ratio) - end + publish(source, ratio) if ratio < IoMonitor.config.warn_threshold end end private def ratio(response_size, io_size) + return 0 if io_size.to_f.zero? + response_size.to_f / io_size.to_f end end diff --git a/spec/io_monitor/publishers/shared_examples.rb b/spec/io_monitor/publishers/shared_examples.rb index 4559910..cecceb8 100644 --- a/spec/io_monitor/publishers/shared_examples.rb +++ b/spec/io_monitor/publishers/shared_examples.rb @@ -52,6 +52,17 @@ publisher.process_action(payload) end + + context "when io_payload_size is zero" do + let(:io_payload_size) { 0 } + let(:ratio) { 0 } + + it "calls .publish method with source and ratio" do + expect(publisher).to receive(:publish).with(io_kind, ratio) + + publisher.process_action(payload) + end + end end end end