Skip to content

Commit

Permalink
Allow to increment counters and measure histograms without tags
Browse files Browse the repository at this point in the history
Using empty tags (`{}`) as default.

Resolves #26
  • Loading branch information
Envek committed Oct 2, 2024
1 parent 754c8a2 commit 7df0d8e
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 5 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/yabeda/counter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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|
Expand Down
4 changes: 2 additions & 2 deletions lib/yabeda/gauge.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/yabeda/histogram.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/yabeda/summary.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions spec/yabeda/counter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 6 additions & 0 deletions spec/yabeda/histogram_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down
6 changes: 6 additions & 0 deletions spec/yabeda/summary_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down

0 comments on commit 7df0d8e

Please sign in to comment.