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

settings: add support for observing settings after post-process hooks #16339

Merged
merged 1 commit into from
Jul 24, 2024
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
23 changes: 20 additions & 3 deletions logstash-core/lib/logstash/settings.rb
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,13 @@ def post_process
callback.call(self)
end
end

# because we cannot rely on deprecation logger being wired up when the setters
# are initially used, we re-emit setter-related deprecations after all post-processing
# hooks have been activated (and therefore after logging has been configured)
@settings.each_value do |setting|
setting.observe_post_process if setting.respond_to?(:observe_post_process)
end
end

def on_post_process(&block)
Expand Down Expand Up @@ -839,9 +846,7 @@ def initialize(canonical_proxy, alias_name)
end

def set(value)
deprecation_logger.deprecated(I18n.t("logstash.settings.deprecation.set",
:deprecated_alias => name,
:canonical_name => canonical_proxy.name))
do_log_setter_deprecation
super
end

Expand All @@ -856,6 +861,18 @@ def validate_value
# bypass deprecation warning
wrapped.validate_value if set?
end

def observe_post_process
do_log_setter_deprecation if set?
end

private

def do_log_setter_deprecation
deprecation_logger.deprecated(I18n.t("logstash.settings.deprecation.set",
:deprecated_alias => name,
:canonical_name => canonical_proxy.name))
end
end

##
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,13 @@
end

include_examples '#validate_value success'

context "#observe_post_process" do
it 'does not emit a deprecation warning' do
expect(LogStash::Settings.deprecation_logger).to_not receive(:deprecated).with(a_string_including(deprecated_setting_name))
settings.get_setting(deprecated_setting_name).observe_post_process
end
end
end

context "when only the deprecated alias is set" do
Expand All @@ -71,6 +78,13 @@

include_examples '#validate_value success'

context "#observe_post_process" do
it 're-emits the deprecation warning' do
expect(LogStash::Settings.deprecation_logger).to receive(:deprecated).with(a_string_including(deprecated_setting_name))
settings.get_setting(deprecated_setting_name).observe_post_process
end
end

it 'validates deprecated alias' do
expect { settings.get_setting(canonical_setting_name).deprecated_alias.validate_value }.to_not raise_error
end
Expand Down Expand Up @@ -107,6 +121,13 @@
end

include_examples '#validate_value success'

context "#observe_post_process" do
it 'does not emit a deprecation warning' do
expect(LogStash::Settings.deprecation_logger).to_not receive(:deprecated).with(a_string_including(deprecated_setting_name))
settings.get_setting(deprecated_setting_name).observe_post_process
end
end
end

context "when both the canonical setting and deprecated alias are set" do
Expand Down
12 changes: 12 additions & 0 deletions logstash-core/spec/logstash/settings_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,18 @@
it "should preserve original settings" do
expect(settings.get("foo")).to eq("bar")
end

context 'when a registered setting responds to `observe_post_process`' do
let(:observe_post_process_setting) do
LogStash::Setting::Boolean.new("this.that", true).tap { |s| allow(s).to receive(:observe_post_process) }
end
subject(:settings) do
described_class.new.tap { |s| s.register(observe_post_process_setting) }
end
it 'it sends `observe_post_process`' do
expect(observe_post_process_setting).to have_received(:observe_post_process)
end
end
end

context "transient settings" do
Expand Down