Skip to content

Commit

Permalink
Allow multiple adapters for metric, move more logic to metric class
Browse files Browse the repository at this point in the history
  • Loading branch information
Envek committed Oct 1, 2024
1 parent 6c9ee57 commit c0ce489
Show file tree
Hide file tree
Showing 7 changed files with 18 additions and 40 deletions.
7 changes: 4 additions & 3 deletions lib/yabeda.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,9 @@ def register_adapter(name, instance)
adapters[name] = instance
# NOTE: Pretty sure there is race condition
metrics.each_value do |metric|
next if metric.adapter && metric.adapter != name
next unless metric.adapters.key?(name)

instance.register!(metric)
metric.restrict_adapter! if metric.adapter == name
end
end

Expand Down Expand Up @@ -103,7 +102,9 @@ def configure!
# Register metrics in adapters after evaluating all configuration blocks
# to ensure that all global settings (like default tags) will be applied.
metrics.each_value do |metric|
register_metric_for_adapters(metric)
metric.adapters.each_value do |adapter|
adapter.register!(metric)
end
end

@configured_by = caller_locations(1, 1)[0].to_s
Expand Down
9 changes: 1 addition & 8 deletions lib/yabeda/dsl/class_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,7 @@ def register_metric(metric)
::Yabeda.define_singleton_method(name) { metric }
::Yabeda.metrics[name] = metric
register_group_for(metric) if metric.group
register_metric_for_adapters(metric) if ::Yabeda.configured?

metric.adapters.each_value { |adapter| adapter.register!(metric) } if ::Yabeda.configured?
metric
end

Expand All @@ -116,12 +115,6 @@ def register_group_for(metric)

group.register_metric(metric)
end

def register_metric_for_adapters(metric)
return ::Yabeda.adapters.each_value { |adapter| adapter.register!(metric) } if metric.adapter.nil?

metric.restrict_adapter!
end
end
end
end
19 changes: 10 additions & 9 deletions lib/yabeda/metric.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,18 +40,19 @@ def inspect
# Returns the metric adapters
# @return [Hash<Symbol, Yabeda::BaseAdapter>]
def adapters
@adapters ||= ::Yabeda.adapters
end
return ::Yabeda.adapters unless adapter

# @return [Hash<Symbol, Yabeda::BaseAdapter> | null]
def restrict_adapter!
return if adapter.nil?
@adapters ||= begin
adapter_names = Array(adapter)
unknown_adapters = adapter_names - ::Yabeda.adapters.keys

adapter_slice = ::Yabeda.adapters.slice(adapter)
raise ConfigurationError, "invalid adapter option in metric #{inspect}" if adapter_slice.empty?
if unknown_adapters.any?
raise ConfigurationError,
"invalid adapter option #{adapter.inspect} in metric #{inspect}"
end

adapter_slice[adapter].register!(self)
@adapters = adapter_slice
::Yabeda.adapters.slice(*Array(@adapter))
end
end
end
end
1 change: 1 addition & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

require "bundler/setup"
require "yabeda"
require "yabeda/base_adapter"
require "pry"

RSpec.configure do |config|
Expand Down
2 changes: 1 addition & 1 deletion spec/yabeda/dsl/class_methods_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@
context "when option is invalid" do
let(:block) { proc { histogram :invalid_test, buckets: [42], adapter: :invalid } }

it { expect { configure }.to raise_error(Yabeda::ConfigurationError, /invalid adapter option in metric/) }
it { expect { configure }.to raise_error(Yabeda::ConfigurationError, /invalid adapter option/) }
end
end
end
Expand Down
18 changes: 0 additions & 18 deletions spec/yabeda/metric_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,22 +61,4 @@
it { is_expected.to match_array(%i[foo bar baz]) }
end
end

describe "#restrict_adapter!" do
subject(:restrict_adapter!) { metric.restrict_adapter! }

let(:adapter) { instance_double(Yabeda::BaseAdapter, register!: true) }

before do
Yabeda.register_adapter(:test_adapter, adapter)
end

it { is_expected.to be_nil }

context "when adapter option is configured" do
let(:options) { { tags: %i[foo bar], adapter: :test_adapter } }

it { is_expected.to eq(Yabeda.adapters.slice(:test_adapter)) }
end
end
end
2 changes: 1 addition & 1 deletion spec/yabeda_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
described_class.register_adapter(:test_adapter, adapter)
end

it { expect { configure! }.to raise_error(Yabeda::ConfigurationError, /invalid adapter option in metric/) }
it { expect { configure! }.to raise_error(Yabeda::ConfigurationError, /invalid adapter option/) }
end
end

Expand Down

0 comments on commit c0ce489

Please sign in to comment.