From bb811c450bafb37ce1fdbfadfb7b5858a3e2fdd2 Mon Sep 17 00:00:00 2001 From: sarankumar1325 <120080287+sarankumar1325@users.noreply.github.com> Date: Wed, 11 Nov 2026 09:10:47 +0530 Subject: [PATCH 1/2] fix: keep runtime label lookups on the plugin's own configuration during reload event_emitter_router looked up labels in Engine.root_agent, which reload_config replaces before the old plugins stop. A runtime lookup (as rewrite_tag_filter and concat do) could then emit into a new label before its was added, cache that tag as unmatched, and drop events of that tag until the next reload. Look up labels through the plugin's context_router, and clear EventRouter's MatchCache when a rule is added. Fixes #5594 Signed-off-by: sarankumar1325 <120080287+sarankumar1325@users.noreply.github.com> --- lib/fluent/event_router.rb | 9 +++ lib/fluent/plugin_helper/event_emitter.rb | 23 +++++- test/plugin_helper/test_event_emitter.rb | 36 ++++++++++ test/test_engine.rb | 86 +++++++++++++++++++++++ test/test_event_router.rb | 18 +++++ 5 files changed, 170 insertions(+), 2 deletions(-) diff --git a/lib/fluent/event_router.rb b/lib/fluent/event_router.rb index bfc192ff0a..83f8e087b9 100644 --- a/lib/fluent/event_router.rb +++ b/lib/fluent/event_router.rb @@ -83,6 +83,10 @@ def suppress_missing_match! # called by Agent to add new match pattern and collector def add_rule(pattern, collector) @match_rules << Rule.new(pattern, collector) + # A tag may already be cached as unmatched (or as an earlier rule) + # if an event arrived before this rule was added, e.g. during a + # graceful reload. Drop the cache so the new rule can match. + @match_cache.clear end def add_metric_callbacks(caller_plugin_id, callback) @@ -158,6 +162,11 @@ def get(key) @keys << key collector end + + def clear + @map.clear + @keys.clear + end end private diff --git a/lib/fluent/plugin_helper/event_emitter.rb b/lib/fluent/plugin_helper/event_emitter.rb index 23bc15ce53..840940ee83 100644 --- a/lib/fluent/plugin_helper/event_emitter.rb +++ b/lib/fluent/plugin_helper/event_emitter.rb @@ -59,12 +59,31 @@ def event_emitter_cancel_source_only @_event_emitter_force_source_only_router = false end + # The root agent of this plugin's configuration. + # During a graceful reload, Engine.root_agent is already the new + # configuration, but context_router still belongs to the old one. + # Looking labels up there keeps runtime lookups (rewrite_tag_filter, + # concat timeout_label, and similar) on the same configuration as + # @label and relabel, which are resolved once in configure. + def event_emitter_root_agent + router = self.context_router + handler = router.respond_to?(:emit_error_handler) ? router.emit_error_handler : nil + if handler.respond_to?(:find_label) + handler + elsif handler.respond_to?(:root_agent) && handler.root_agent + handler.root_agent + else + Engine.root_agent + end + end + def event_emitter_router(label_name) if label_name + root_agent = event_emitter_root_agent if label_name == "@ROOT" - Engine.root_agent.event_router + root_agent.event_router else - Engine.root_agent.find_label(label_name).event_router + root_agent.find_label(label_name).event_router end elsif self.respond_to?(:as_secondary) && self.as_secondary if @primary_instance.has_router? diff --git a/test/plugin_helper/test_event_emitter.rb b/test/plugin_helper/test_event_emitter.rb index 26580f0087..157ed46abd 100644 --- a/test/plugin_helper/test_event_emitter.rb +++ b/test/plugin_helper/test_event_emitter.rb @@ -1,6 +1,7 @@ require_relative '../helper' require 'fluent/plugin_helper/event_emitter' require 'fluent/plugin/base' +require 'fluent/event_router' require 'flexmock/test_unit' class EventEmitterTest < Test::Unit::TestCase @@ -106,4 +107,39 @@ class Dummy < Fluent::Plugin::TestBase [router_when_source_only, router_after_canceled] ) end + + test 'event_emitter_router looks up labels in the plugin configuration, not Engine.root_agent' do + old_router_mock = flexmock('old_router') + old_label_mock = flexmock('old_label') + old_label_mock.should_receive(:event_router).once.and_return(old_router_mock) + old_root = flexmock('old_root') + old_root.should_receive(:find_label).with('@L').once.and_return(old_label_mock) + + d = Dummy.new + d.context_router = Fluent::EventRouter.new(nil, old_root) + + stub(Fluent::Engine.root_agent).find_label { raise 'looked up in Engine.root_agent' } + + assert_equal old_router_mock, d.event_emitter_router('@L') + end + + test 'event_emitter_router @ROOT uses the plugin configuration event_router' do + old_router = Object.new + old_root = Class.new do + attr_reader :event_router + def initialize(router) + @event_router = router + end + def find_label(_name) + raise 'find_label should not be used for @ROOT' + end + end.new(old_router) + + d = Dummy.new + d.context_router = Fluent::EventRouter.new(nil, old_root) + + stub(Fluent::Engine.root_agent).event_router { raise 'looked up in Engine.root_agent' } + + assert_equal old_router, d.event_emitter_router('@ROOT') + end end diff --git a/test/test_engine.rb b/test/test_engine.rb index 5b9bf1cffd..88eb8ba3a1 100644 --- a/test/test_engine.rb +++ b/test/test_engine.rb @@ -3,6 +3,20 @@ require 'fluent/config' require 'fluent/input' require 'fluent/system_config' +require 'fluent/plugin/output' +require 'fluent/event' + +# Hook Agent#add_match so a test can emit during configure of a new