Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expand functionality: limit group for adapter #41

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,19 @@ Yabeda.configure do
end
```

Or use another DSL to describe this logic:

```ruby
Yabeda.configure do
group :mushrooms do
counter :champignon_counter
end

adapter :basket_adapter do
include_group :mushrooms
end
end
```

## Roadmap (aka TODO or Help wanted)

Expand Down
19 changes: 17 additions & 2 deletions lib/yabeda/dsl/class_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,25 @@ def temporary_tags
#
# @param adapter_names [Array<Symbol>] Names of adapters to use
def adapter(*adapter_names, group: @group)
raise ConfigurationError, "Adapter limitation can't be defined outside of group" unless group
raise ConfigurationError, "Adapter limitation can't be defined without adapter_names" if adapter_names.empty?

@adapter_names = adapter_names
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm a bit worried that this instance variable is being kept defined after adapter method has ended. While it should be harmless now, it can possibly create problems in the future. Let's clear it in the ensure block maybe?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

if group
include_group(group)
else
return yield if block_given?

raise ConfigurationError, "Yabeda.adapter should be called either inside group declaration " \
"or should have block provided with a call to include_group. No metric group provided."
end
ensure @adapter_names = nil
end

def include_group(group)
raise ConfigurationError, "Adapter limitation can't be defined without of group name" unless group

Yabeda.groups[group] ||= Yabeda::Group.new(group)
Yabeda.groups[group].adapter(*adapter_names)
Yabeda.groups[group].adapter(*@adapter_names)
end

private
Expand Down
98 changes: 88 additions & 10 deletions spec/yabeda/counter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,15 @@

let(:tags) { { foo: "bar" } }
let(:metric_value) { 10 }
let(:counter) { Yabeda.test_counter }
let(:built_tags) { { built_foo: "built_bar" } }
let(:adapter) { instance_double(Yabeda::BaseAdapter, perform_counter_increment!: true, register!: true) }
let(:counter) { Yabeda.test_counter }

before do
Yabeda.register_adapter(:test_adapter, adapter)
Yabeda.configure do
counter :test_counter
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) }
Expand All @@ -25,30 +22,111 @@
expect { counter.increment }.to change { counter.values[{}] }.by(1)
end

it "execute perform_counter_increment! method of adapter" do
it "execute perform_counter_increment!" do
increment_counter
expect(adapter).to have_received(:perform_counter_increment!).with(counter, built_tags, metric_value)
expect(adapter).to have_received(:perform_counter_increment!).with(counter, tags, metric_value)
end

context "with adapter option" do
let(:counter) { Yabeda.counter_with_adapter }
let(:another_adapter) { instance_double(Yabeda::BaseAdapter, perform_counter_increment!: true, register!: true) }
let(:counter) { Yabeda.counter_with_adapter }

before do
Yabeda.register_adapter(:another_adapter, another_adapter)

Yabeda.configure do
counter :counter_with_adapter, adapter: :test_adapter
end
Yabeda.configure! unless Yabeda.already_configured?
end

it "execute perform_counter_increment! method of adapter with name :test_adapter" do
it "execute perform_counter_increment! with name :test_adapter" do
increment_counter

aggregate_failures do
expect(adapter).to have_received(:perform_counter_increment!).with(counter, built_tags, metric_value)
expect(adapter).to have_received(:perform_counter_increment!).with(counter, tags, metric_value)
expect(another_adapter).not_to have_received(:perform_counter_increment!)
end
end
end

context "with call .adapter method" do
let(:tags) { { type: "champignon" } }
let(:counter) { Yabeda.mushrooms.champignon_counter }
let(:basket_adapter) { instance_double(Yabeda::BaseAdapter, perform_counter_increment!: true, register!: true) }

before do
Yabeda.register_adapter(:basket_adapter, basket_adapter)
end

it "raises an error using include_group construction without adapter_names args" do
expect do
Yabeda.configure do
group :mushrooms do
counter :champignon_counter
end

adapter { include_group :mushrooms }
end
Yabeda.configure! unless Yabeda.already_configured?
end.to raise_error(Yabeda::ConfigurationError, "Adapter limitation can't be defined without adapter_names")
end

it "raises an error using adapter construction into the group block without adapter_names args" do
expect do
Yabeda.configure do
group :mushrooms do
adapter
counter :champignon_counter
end
end
Yabeda.configure! unless Yabeda.already_configured?
end.to raise_error(Yabeda::ConfigurationError, "Adapter limitation can't be defined without adapter_names")
end

context "when call .adapter method in outside of group" do
before do
Yabeda.configure do
group :mushrooms do
counter :champignon_counter
end

adapter :basket_adapter do
include_group :mushrooms
end
end
Yabeda.configure! unless Yabeda.already_configured?
end

it "execute perform_counter_increment! with name :basket_adapter" do
increment_counter

aggregate_failures do
expect(basket_adapter).to have_received(:perform_counter_increment!).with(counter, tags, metric_value)
expect(adapter).not_to have_received(:perform_counter_increment!)
end
end
end

context "when call adapter method in inside of group" do
before do
Yabeda.configure do
group :mushrooms do
adapter :basket_adapter
counter :champignon_counter
end
end
Yabeda.configure! unless Yabeda.already_configured?
end

it "execute perform_counter_increment! with name :basket_adapter" do
increment_counter

aggregate_failures do
expect(basket_adapter).to have_received(:perform_counter_increment!).with(counter, tags, metric_value)
expect(adapter).not_to have_received(:perform_counter_increment!)
end
end
end
end
end
5 changes: 4 additions & 1 deletion spec/yabeda/dsl/class_methods_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -189,10 +189,13 @@
describe ".adapter" do
context "when group is not defined" do
it "raises an error" do
error_message = "Yabeda.adapter should be called either inside group declaration " \
"or should have block provided with a call to include_group. No metric group provided."

expect do
Yabeda.configure { adapter :test }
Yabeda.configure! unless Yabeda.already_configured?
end.to raise_error(Yabeda::ConfigurationError, /can't be defined outside of group/)
end.to raise_error(Yabeda::ConfigurationError, error_message)
end
end

Expand Down
7 changes: 1 addition & 6 deletions spec/yabeda/group_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,9 @@

RSpec.describe Yabeda::Group do
let(:name) { nil }

let(:group) { described_class.new(name) }

before do
Yabeda.groups[name] = group
end

after { Yabeda.reset! }
before { Yabeda.groups[name] = group }

describe "default tags" do
context "when on the top level group" do
Expand Down
2 changes: 2 additions & 0 deletions spec/yabeda_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# frozen_string_literal: true

RSpec.describe Yabeda do
before { described_class.reset! }

it "has a version number" do
expect(Yabeda::VERSION).not_to be_nil
end
Expand Down