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

Allow configure more than one publisher #15

Merged
merged 1 commit into from
Jun 1, 2022
Merged
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## main

- [PR#15](https://github.com/DmitryTsepelev/io_monitor/pull/15) Allow configure more than one publisher ([@DmitryTsepelev])
- [PR#9](https://github.com/DmitryTsepelev/io_monitor/pull/9) Restrict minimum Rails version to 6.1, adjust test matrix, and related changes ([@Envek])

## 0.2.0 (2022-05-29)
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ Change configuration in an initializer if you need:

```ruby
IoMonitor.configure do |config|
config.publish = :notifications # defaults to :logs
config.publish = [:logs, :notifications] # defaults to :logs
config.warn_threshold = 0.8 # defaults to 0
config.adapters = [:active_record, :net_http, :redis] # defaults to [:active_record]
end
Expand Down
26 changes: 15 additions & 11 deletions lib/io_monitor/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,15 @@ class Configuration
DEFAULT_WARN_THRESHOLD = 0.0

def initialize
@publisher = LogsPublisher.new
@publishers = [LogsPublisher.new]
@adapters = [ActiveRecordAdapter.new]
@warn_threshold = DEFAULT_WARN_THRESHOLD
end

attr_reader :publisher, :adapters, :warn_threshold
attr_reader :publishers, :adapters, :warn_threshold

def publish=(value)
if value.is_a?(BasePublisher)
@publisher = value
elsif (publisher_type = resolve(IoMonitor::PUBLISHERS, value))
@publisher = publisher_type.new
else
supported = IoMonitor::PUBLISHERS.map(&:kind)
raise ArgumentError, "Only the following publishers are supported: #{supported}."
end
def publish=(values)
@publishers = [*values].map { |value| value_to_publisher(value) }
end

def adapters=(value)
Expand Down Expand Up @@ -50,5 +43,16 @@ def warn_threshold=(value)
def resolve(list, kind)
list.find { |p| p.kind == kind }
end

def value_to_publisher(value)
if value.is_a?(BasePublisher)
value
elsif (publisher_type = resolve(IoMonitor::PUBLISHERS, value))
publisher_type.new
else
supported = IoMonitor::PUBLISHERS.map(&:kind)
raise ArgumentError, "Only the following publishers are supported: #{supported}."
end
end
end
end
2 changes: 1 addition & 1 deletion lib/io_monitor/patches/action_controller_base_patch.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module IoMonitor
module ActionControllerBasePatch
def log_process_action(payload)
super.tap do |messages|
next unless IoMonitor.config.publisher.is_a?(LogsPublisher)
next unless IoMonitor.config.publishers.any? { |publisher| publisher.is_a?(LogsPublisher) }

data = payload[IoMonitor::NAMESPACE]
next unless data
Expand Down
2 changes: 1 addition & 1 deletion lib/io_monitor/railtie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class Railtie < Rails::Railtie
payload = args.last[IoMonitor::NAMESPACE]
next unless payload

IoMonitor.config.publisher.process_action(payload)
IoMonitor.config.publishers.each { |publisher| publisher.process_action(payload) }
end
end
end
Expand Down
14 changes: 11 additions & 3 deletions spec/io_monitor/configuration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
describe ".publish" do
it "resolves publisher by kind" do
config.publish = :notifications
expect(config.publisher).to be_a(IoMonitor::NotificationsPublisher)
expect(config.publishers.first).to be_a(IoMonitor::NotificationsPublisher)
end

context "when kind is unknown" do
Expand All @@ -18,11 +18,19 @@
it "allows custom publishers" do
custom = Class.new(IoMonitor::BasePublisher) {}
config.publish = custom.new
expect(config.publisher).to be_a(custom)
expect(config.publishers.first).to be_a(custom)
end

it "equals to :logs by default" do
expect(config.publisher).to be_a(IoMonitor::LogsPublisher)
expect(config.publishers.first).to be_a(IoMonitor::LogsPublisher)
end

context "when multiple publishers are set" do
it "assigns all publishers" do
config.publish = [:notifications, :logs]
expect(config.publishers.first).to be_a(IoMonitor::NotificationsPublisher)
expect(config.publishers.last).to be_a(IoMonitor::LogsPublisher)
end
end
end

Expand Down