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

Ability to route some metrics only for given adapter #39

Closed
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
2 changes: 1 addition & 1 deletion lib/yabeda.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def default_tags
def register_adapter(name, instance)
adapters[name] = instance
# NOTE: Pretty sure there is race condition
metrics.each do |_, metric|
metrics.each_value do |metric|
instance.register!(metric)
end
end
Expand Down
4 changes: 2 additions & 2 deletions lib/yabeda/counter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ class Counter < Metric
def increment(tags, by: 1)
all_tags = ::Yabeda::Tags.build(tags, group)
values[all_tags] += by
::Yabeda.adapters.each do |_, adapter|
adapter.perform_counter_increment!(self, all_tags, by)
::Yabeda.adapters.each do |adapter_name, adapter|
adapter.perform_counter_increment!(self, all_tags, by) if can_access_for_adapter?(adapter_name)
end
values[all_tags]
end
Expand Down
10 changes: 10 additions & 0 deletions lib/yabeda/dsl/class_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,16 @@ def group(group_name)
@group = nil
end

def adapter(adapter_name)
@adapter_name = adapter_name
yield if block_given?
Copy link
Member

Choose a reason for hiding this comment

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

Love that you thought about usage with groups and very curious about this yield. How you planned to use it?

It may worth to be cherry-picked into separate pull request!

Copy link
Author

Choose a reason for hiding this comment

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

I did it for this:

adapter :prometheus do
  include_group :sidekiq
end

Initialize the group configuration, before or after that declare that this is a group of only this adapter: sidekiq group only for prometheus adapter.

end

def include_group(group_name)
group = Yabeda.groups[group_name] || Yabeda::Group.new(group_name)
group.only_for_adapter = @adapter_name
end

# Register a growing-only counter
def counter(*args, **kwargs, &block)
metric = MetricBuilder.new(Counter).build(args, kwargs, @group, &block)
Expand Down
4 changes: 2 additions & 2 deletions lib/yabeda/gauge.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ class Gauge < Metric
def set(tags, value)
all_tags = ::Yabeda::Tags.build(tags, group)
values[all_tags] = value
::Yabeda.adapters.each do |_, adapter|
adapter.perform_gauge_set!(self, all_tags, value)
::Yabeda.adapters.each do |adapter_name, adapter|
adapter.perform_gauge_set!(self, all_tags, value) if can_access_for_adapter?(adapter_name)
end
value
end
Expand Down
2 changes: 2 additions & 0 deletions lib/yabeda/group.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ module Yabeda
class Group
extend Dry::Initializer

attr_accessor :only_for_adapter

param :name

def default_tags
Expand Down
4 changes: 2 additions & 2 deletions lib/yabeda/histogram.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ def measure(tags, value = nil)

all_tags = ::Yabeda::Tags.build(tags, group)
values[all_tags] = value
::Yabeda.adapters.each do |_, adapter|
adapter.perform_histogram_measure!(self, all_tags, value)
::Yabeda.adapters.each do |adapter_name, adapter|
adapter.perform_histogram_measure!(self, all_tags, value) if can_access_for_adapter?(adapter_name)
end
value
end
Expand Down
4 changes: 4 additions & 0 deletions lib/yabeda/metric.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,9 @@ def tags
def inspect
"#<#{self.class.name}: #{[@group, @name].compact.join('.')}>"
end

def can_access_for_adapter?(adapter_name)
[adapter_name, nil].include?(Yabeda.groups[group].only_for_adapter)
end
end
end
4 changes: 2 additions & 2 deletions lib/yabeda/summary.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ def observe(tags, value = nil)

all_tags = ::Yabeda::Tags.build(tags, group)
values[all_tags] = value
::Yabeda.adapters.each do |_, adapter|
adapter.perform_summary_observe!(self, all_tags, value)
::Yabeda.adapters.each do |adapter_name, adapter|
adapter.perform_summary_observe!(self, all_tags, value) if can_access_for_adapter?(adapter_name)
end
value
end
Expand Down
58 changes: 48 additions & 10 deletions spec/yabeda/counter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,57 @@
let(:built_tags) { { built_foo: "built_bar" } }
let(:adapter) { instance_double(Yabeda::BaseAdapter, perform_counter_increment!: true, register!: true) }

before do
Yabeda.configure do
counter :test_counter
before { Yabeda.configure! unless Yabeda.already_configured? }

context "when config has no group" do
before do
Yabeda.configure do
counter :test_counter
end
allow(Yabeda::Tags).to receive(:build).with(tags, anything).and_return(built_tags)
Yabeda.register_adapter(:test_adapter, adapter)
end

it { expect(increment_counter).to eq(metric_value) }

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)
end
Yabeda.configure! unless Yabeda.already_configured?
allow(Yabeda::Tags).to receive(:build).with(tags, anything).and_return(built_tags)
Yabeda.register_adapter(:test_adapter, adapter)
end

it { expect(increment_counter).to eq(metric_value) }
context "when config contains include_group" do
before do
Yabeda.configure do
group :mushrooms do
counter :champignon_counter
end

adapter :basket_adapter do
include_group :mushrooms
end
end
end

let(:tags) { { type: "champignon" } }
let(:counter) { Yabeda.mushrooms.champignon_counter }

context "when adapter_name is equal to only_for_adapter" do
before { Yabeda.register_adapter(:basket_adapter, adapter) }

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

context "when adapter_name is non equal to only_for_adapter" do
before { Yabeda.register_adapter(:test_adapter, adapter) }

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)
it "don't execute perform_counter_increment! method of adapter" do
increment_counter
expect(adapter).not_to have_received(:perform_counter_increment!).with(counter, tags, metric_value)
end
end
end
end
37 changes: 37 additions & 0 deletions spec/yabeda/dsl/class_methods_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -156,4 +156,41 @@
end
end
end

describe ".adapter" do
context "when block don't given" do
it "return nil" do
expect(Yabeda.adapter(:test_adapter_name)).to be_nil
end
end

context "when block given" do
it "return block result" do
expect(Yabeda.adapter(:test_adapter_name) { 1 + 1 }).to eq 2
end
end
end

describe ".include_group" do
let(:adapter_and_include_group) do
lambda do
Yabeda.configure do
group :test_group do
counter :test_counter
end

adapter :test_adapter_name do
include_group :test_group
end
end
Yabeda.configure! unless Yabeda.already_configured?
end
end

it "set only_for_adapter value for group" do
expect(Yabeda.groups[:test_group]).to be_nil
adapter_and_include_group.call
expect(Yabeda.groups[:test_group].only_for_adapter).to eq :test_adapter_name
end
end
end
118 changes: 78 additions & 40 deletions spec/yabeda/gauge_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,68 +9,106 @@
let(:built_tags) { { built_foo: "built_bar" } }
let(:adapter) { instance_double(Yabeda::BaseAdapter, perform_gauge_set!: true, register!: true) }

before do
Yabeda.configure do
gauge :test_gauge
before { Yabeda.configure! unless Yabeda.already_configured? }

context "when config has no group" do
before do
Yabeda.configure do
gauge :test_gauge
end
allow(Yabeda::Tags).to receive(:build).with(tags, anything).and_return(built_tags)
Yabeda.register_adapter(:test_adapter, adapter)
end
Yabeda.configure! unless Yabeda.already_configured?
allow(Yabeda::Tags).to receive(:build).with(tags, anything).and_return(built_tags)
Yabeda.register_adapter(:test_adapter, adapter)
end

it { expect(set_gauge).to eq(metric_value) }
it { expect(set_gauge).to eq(metric_value) }

it "execute perform_gauge_set! method of adapter" do
set_gauge
expect(adapter).to have_received(:perform_gauge_set!).with(gauge, built_tags, metric_value)
end
it "execute perform_gauge_set! method of adapter" do
set_gauge
expect(adapter).to have_received(:perform_gauge_set!).with(gauge, built_tags, metric_value)
end

describe "#increment" do
context "when gauge has no initial value" do
before { gauge.increment(tags) }
describe "#increment" do
context "when gauge has no initial value" do
before { gauge.increment(tags) }

it { expect(adapter).to have_received(:perform_gauge_set!).with(gauge, built_tags, 1) }
end
it { expect(adapter).to have_received(:perform_gauge_set!).with(gauge, built_tags, 1) }
end

context "when gauge has a value already" do
before do
set_gauge
gauge.increment(tags)
context "when gauge has a value already" do
before do
set_gauge
gauge.increment(tags)
end

it { expect(adapter).to have_received(:perform_gauge_set!).with(gauge, built_tags, metric_value + 1) }
end

it { expect(adapter).to have_received(:perform_gauge_set!).with(gauge, built_tags, metric_value + 1) }
context "when custom step specified" do
it "increases by value of custom step" do
set_gauge
gauge.increment(tags, by: 42)
expect(adapter).to have_received(:perform_gauge_set!).with(gauge, built_tags, metric_value + 42)
end
end
end

context "when custom step specified" do
it "increases by value of custom step" do
set_gauge
gauge.increment(tags, by: 42)
expect(adapter).to have_received(:perform_gauge_set!).with(gauge, built_tags, metric_value + 42)
describe "#decrement" do
context "when gauge has no initial value" do
before { gauge.decrement(tags) }

it { expect(adapter).to have_received(:perform_gauge_set!).with(gauge, built_tags, -1) }
end

context "when gauge has a value already" do
before do
set_gauge
gauge.decrement(tags)
end

it { expect(adapter).to have_received(:perform_gauge_set!).with(gauge, built_tags, metric_value - 1) }
end

context "when custom step specified" do
it "decreases by value of custom step" do
set_gauge
gauge.decrement(tags, by: 42)
expect(adapter).to have_received(:perform_gauge_set!).with(gauge, built_tags, metric_value - 42)
end
end
end
end

describe "#decrement" do
context "when gauge has no initial value" do
before { gauge.decrement(tags) }
context "when config contains include_group" do
before do
Yabeda.configure do
group :mushrooms do
gauge :champignon_gauge
end

it { expect(adapter).to have_received(:perform_gauge_set!).with(gauge, built_tags, -1) }
adapter :basket_adapter do
include_group :mushrooms
end
end
end

context "when gauge has a value already" do
before do
let(:tags) { { type: "champignon" } }
let(:gauge) { Yabeda.mushrooms.champignon_gauge }

context "when adapter_name is equal to only_for_adapter" do
before { Yabeda.register_adapter(:basket_adapter, adapter) }

it "execute perform_gauge_set! method of adapter" do
set_gauge
gauge.decrement(tags)
expect(adapter).to have_received(:perform_gauge_set!).with(gauge, tags, metric_value)
end

it { expect(adapter).to have_received(:perform_gauge_set!).with(gauge, built_tags, metric_value - 1) }
end

context "when custom step specified" do
it "decreases by value of custom step" do
context "when adapter_name is non equal to only_for_adapter" do
before { Yabeda.register_adapter(:test_adapter, adapter) }

it "don't execute perform_gauge_set! method of adapter" do
set_gauge
gauge.decrement(tags, by: 42)
expect(adapter).to have_received(:perform_gauge_set!).with(gauge, built_tags, metric_value - 42)
expect(adapter).not_to have_received(:perform_gauge_set!).with(gauge, tags, metric_value)
end
end
end
Expand Down
Loading