Skip to content

Commit

Permalink
Merge pull request #3358 from ferbar/add_get_default_label
Browse files Browse the repository at this point in the history
Add ability to get the root-router
  • Loading branch information
ashie committed Jul 30, 2021
2 parents 2e324c5 + 4dc5e81 commit c6b2fe2
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 1 deletion.
6 changes: 5 additions & 1 deletion lib/fluent/plugin_helper/event_emitter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,11 @@ def event_emitter_used_actually?

def event_emitter_router(label_name)
if label_name
Engine.root_agent.find_label(label_name).event_router
if label_name == "@ROOT"
Engine.root_agent.event_router
else
Engine.root_agent.find_label(label_name).event_router
end
elsif self.respond_to?(:as_secondary) && self.as_secondary
if @primary_instance.has_router?
@_event_emitter_lazy_init = true
Expand Down
1 change: 1 addition & 0 deletions lib/fluent/root_agent.rb
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ def configure(conf)
end
name = e.arg
raise ConfigError, "Missing symbol argument on <label> directive" if name.empty?
raise ConfigError, "@ROOT for <label> is not permitted, reserved for getting root router" if name == '@ROOT'

if name == ERROR_LABEL
error_label_config = e
Expand Down
29 changes: 29 additions & 0 deletions test/plugin_helper/test_event_emitter.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require_relative '../helper'
require 'fluent/plugin_helper/event_emitter'
require 'fluent/plugin/base'
require 'flexmock/test_unit'

class EventEmitterTest < Test::Unit::TestCase
setup do
Expand Down Expand Up @@ -48,4 +49,32 @@ class Dummy < Fluent::Plugin::TestBase

d1.terminate
end

test 'should not have event_emitter_router' do
d0 = Dummy0.new
assert !d0.respond_to?(:event_emitter_router)
end

test 'should have event_emitter_router' do
d = Dummy.new
assert d.respond_to?(:event_emitter_router)
end

test 'get router' do
router_mock = flexmock('mytest')
label_mock = flexmock('mylabel')
label_mock.should_receive(:event_router).twice.and_return(router_mock)
Fluent::Engine.root_agent.labels['@mytest'] = label_mock

d = Dummy.new
d.configure(config_element('ROOT', '', {'@label' => '@mytest'}))
router = d.event_emitter_router("@mytest")
assert_equal router_mock, router
end

test 'get root router' do
d = Dummy.new
router = d.event_emitter_router("@ROOT")
assert_equal Fluent::Engine.root_agent.event_router, router
end
end
28 changes: 28 additions & 0 deletions test/test_root_agent.rb
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,34 @@ def configure_ra(conf_str)
end
end

test 'raises configuration error for label without name' do
conf = <<-EOC
<label>
@type test_out
</label>
EOC
errmsg = "Missing symbol argument on <label> directive"
assert_raise Fluent::ConfigError.new(errmsg) do
configure_ra(conf)
end
end

test 'raises configuration error for <label @ROOT>' do
conf = <<-EOC
<source>
@type test_in
@label @ROOT
</source>
<label @ROOT>
@type test_out
</label>
EOC
errmsg = "@ROOT for <label> is not permitted, reserved for getting root router"
assert_raise Fluent::ConfigError.new(errmsg) do
configure_ra(conf)
end
end

test 'raises configuration error if there are not match sections in label section' do
conf = <<-EOC
<source>
Expand Down

0 comments on commit c6b2fe2

Please sign in to comment.