From 7df0d8eba880cd5d191f583d1112046cc856ba53 Mon Sep 17 00:00:00 2001 From: Andrey Novikov Date: Wed, 2 Oct 2024 19:12:15 +0900 Subject: [PATCH] Allow to increment counters and measure histograms without tags Using empty tags (`{}`) as default. Resolves https://github.com/yabeda-rb/yabeda/issues/26 --- CHANGELOG.md | 10 ++++++++++ lib/yabeda/counter.rb | 2 +- lib/yabeda/gauge.rb | 4 ++-- lib/yabeda/histogram.rb | 2 +- lib/yabeda/summary.rb | 2 +- spec/yabeda/counter_spec.rb | 5 +++++ spec/yabeda/histogram_spec.rb | 6 ++++++ spec/yabeda/summary_spec.rb | 6 ++++++ 8 files changed, 32 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a44b201..924da6e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -32,6 +32,16 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. ) ``` +### Changed + +- Don't require to provide tags for counters and histograms, use empty tags (`{}`) by default. See discussion at [#26](https://github.com/yabeda-rb/yabeda/issues/26). [@Envek] + + ```ruby + Yabeda.foo.increment + # same as + Yabeda.foo.increment({}, by: 1) + ``` + ### Fixed - Railtie loading to prevent calling methods that have not yet been defined diff --git a/lib/yabeda/counter.rb b/lib/yabeda/counter.rb index c9b52a1..9911912 100644 --- a/lib/yabeda/counter.rb +++ b/lib/yabeda/counter.rb @@ -3,7 +3,7 @@ module Yabeda # Growing-only counter class Counter < Metric - def increment(tags, by: 1) + def increment(tags = {}, by: 1) all_tags = ::Yabeda::Tags.build(tags, group) values[all_tags] += by adapters.each_value do |adapter| diff --git a/lib/yabeda/gauge.rb b/lib/yabeda/gauge.rb index 5f76c7b..64f323e 100644 --- a/lib/yabeda/gauge.rb +++ b/lib/yabeda/gauge.rb @@ -12,11 +12,11 @@ def set(tags, value) value end - def increment(tags, by: 1) + def increment(tags = {}, by: 1) set(tags, get(tags).to_i + by) end - def decrement(tags, by: 1) + def decrement(tags = {}, by: 1) set(tags, get(tags).to_i - by) end end diff --git a/lib/yabeda/histogram.rb b/lib/yabeda/histogram.rb index bf7cda7..d19b7fe 100644 --- a/lib/yabeda/histogram.rb +++ b/lib/yabeda/histogram.rb @@ -7,7 +7,7 @@ class Histogram < Metric option :buckets # rubocop: disable Metrics/MethodLength - def measure(tags, value = nil) + def measure(tags = {}, value = nil) if value.nil? ^ block_given? raise ArgumentError, "You must provide either numeric value or block for Yabeda::Histogram#measure!" end diff --git a/lib/yabeda/summary.rb b/lib/yabeda/summary.rb index b84d1d1..b0b8b6c 100644 --- a/lib/yabeda/summary.rb +++ b/lib/yabeda/summary.rb @@ -5,7 +5,7 @@ module Yabeda # calculate averages, percentiles, and so on. class Summary < Metric # rubocop: disable Metrics/MethodLength - def observe(tags, value = nil) + def observe(tags = {}, value = nil) if value.nil? ^ block_given? raise ArgumentError, "You must provide either numeric value or block for Yabeda::Summary#observe!" end diff --git a/spec/yabeda/counter_spec.rb b/spec/yabeda/counter_spec.rb index 1b404b7..fdb4b02 100644 --- a/spec/yabeda/counter_spec.rb +++ b/spec/yabeda/counter_spec.rb @@ -15,11 +15,16 @@ end Yabeda.configure! unless Yabeda.already_configured? allow(Yabeda::Tags).to receive(:build).with(tags, anything).and_return(built_tags) + allow(Yabeda::Tags).to receive(:build).with({}, anything).and_return({}) Yabeda.register_adapter(:test_adapter, adapter) end it { expect(increment_counter).to eq(metric_value) } + it "increments counter with empty tags if tags are not provided" do + expect { counter.increment }.to change { counter.values[{}] }.by(1) + end + it "execute perform_counter_increment! method of adapter" do increment_counter expect(adapter).to have_received(:perform_counter_increment!).with(counter, built_tags, metric_value) diff --git a/spec/yabeda/histogram_spec.rb b/spec/yabeda/histogram_spec.rb index 8900a48..6afe4d6 100644 --- a/spec/yabeda/histogram_spec.rb +++ b/spec/yabeda/histogram_spec.rb @@ -16,6 +16,7 @@ end Yabeda.configure! unless Yabeda.already_configured? allow(Yabeda::Tags).to receive(:build).with(tags, anything).and_return(built_tags) + allow(Yabeda::Tags).to receive(:build).with({}, anything).and_return({}) Yabeda.register_adapter(:test_adapter, adapter) end @@ -39,6 +40,11 @@ measure_histogram expect(adapter).to have_received(:perform_histogram_measure!).with(histogram, built_tags, be_between(0.01, 0.05)) end + + it "measures with empty tags if tags are not provided", :aggregate_failures do + histogram.measure(&block) + expect(adapter).to have_received(:perform_histogram_measure!).with(histogram, {}, be_between(0.01, 0.05)) + end end context "with both value and block provided" do diff --git a/spec/yabeda/summary_spec.rb b/spec/yabeda/summary_spec.rb index 9cc2f81..bc06642 100644 --- a/spec/yabeda/summary_spec.rb +++ b/spec/yabeda/summary_spec.rb @@ -14,6 +14,7 @@ Yabeda.configure { summary :test_summary } Yabeda.configure! unless Yabeda.already_configured? allow(Yabeda::Tags).to receive(:build).with(tags, anything).and_return(built_tags) + allow(Yabeda::Tags).to receive(:build).with({}, anything).and_return({}) Yabeda.register_adapter(:test_adapter, adapter) end @@ -37,6 +38,11 @@ observe_summary expect(adapter).to have_received(:perform_summary_observe!).with(summary, built_tags, be_between(0.01, 0.05)) end + + it "observes with empty tags if tags are not provided", :aggregate_failures do + summary.observe(&block) + expect(adapter).to have_received(:perform_summary_observe!).with(summary, {}, be_between(0.01, 0.05)) + end end context "with both value and block provided" do