-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feat: introduce a target check helper (#6)
Co-authored-by: Ry Biesemeyer <yaauie@users.noreply.github.com>
- Loading branch information
Showing
4 changed files
with
129 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
lib/logstash/plugin_mixins/ecs_compatibility_support/target_check.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
# encoding: utf-8 | ||
|
||
require_relative '../ecs_compatibility_support' | ||
|
||
module LogStash | ||
module PluginMixins | ||
module ECSCompatibilitySupport | ||
# A target option check that can be included into any `LogStash::Plugin`. | ||
# | ||
# @see ECSCompatibilitySupport() | ||
module TargetCheck | ||
## | ||
# @api internal (use: `LogStash::Plugin::include`) | ||
# @param base [Class]: a class that inherits `LogStash::Plugin`, typically one | ||
# descending from one of the four plugin base classes | ||
# (e.g., `LogStash::Inputs::Base`) | ||
# @return [void] | ||
def self.included(base) | ||
fail(ArgumentError, "`#{base}` must inherit LogStash::Plugin") unless base < LogStash::Plugin | ||
fail(ArgumentError, "`#{base}` must include #{ECSCompatibilitySupport}") unless base < ECSCompatibilitySupport | ||
base.prepend(RegisterDecorator) | ||
end | ||
|
||
TARGET_NOT_SET_MESSAGE = ("ECS compatibility is enabled but `target` option was not specified. " + | ||
"This may cause fields to be set at the top-level of the event where they are likely to clash with the Elastic Common Schema. " + | ||
"It is recommended to set the `target` option to avoid potential schema conflicts (if your data is ECS compliant " + | ||
"or non-conflicting, feel free to ignore this message)").freeze | ||
|
||
private | ||
|
||
## | ||
# Check whether `target` is specified. | ||
# | ||
# @return [nil] if target is unspecified and ECS compatibility is disabled | ||
# @return [true, false] | ||
def target_set? | ||
return true unless target.nil? | ||
return nil if ecs_compatibility == :disabled | ||
false # target isn't set | ||
end | ||
|
||
module RegisterDecorator | ||
|
||
## | ||
# Logs an info message when ecs_compatibility is enabled but plugin has no `target` configuration specified. | ||
# @override | ||
def register | ||
super.tap do | ||
logger.info(TARGET_NOT_SET_MESSAGE) if target_set? == false | ||
end | ||
end | ||
|
||
end | ||
private_constant :RegisterDecorator | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
spec/logstash/plugin_mixins/ecs_compatibility_support/target_check_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
# encoding: utf-8 | ||
|
||
require "logstash-core" | ||
|
||
require 'logstash/inputs/base' | ||
require 'logstash/filters/base' | ||
require 'logstash/codecs/base' | ||
require 'logstash/outputs/base' | ||
|
||
require "logstash/plugin_mixins/ecs_compatibility_support/target_check" | ||
|
||
describe LogStash::PluginMixins::ECSCompatibilitySupport::TargetCheck do | ||
|
||
describe "check_target_set_in_ecs_mode" do | ||
|
||
context 'with a plugin' do | ||
|
||
subject(:plugin_class) do | ||
Class.new(LogStash::Filters::Base) do | ||
include LogStash::PluginMixins::ECSCompatibilitySupport | ||
include LogStash::PluginMixins::ECSCompatibilitySupport::TargetCheck | ||
|
||
config :target, :validate => :string | ||
|
||
def register; 42 end | ||
|
||
end | ||
end | ||
|
||
it 'skips check when ECS disabled' do | ||
plugin = plugin_class.new('ecs_compatibility' => 'disabled') | ||
allow( plugin.logger ).to receive(:info) | ||
expect( plugin.register ).to eql 42 | ||
expect( plugin.logger ).to_not have_received(:info).with(a_string_including "`target` option") | ||
end | ||
|
||
it 'warns when target is not set in ECS mode' do | ||
plugin = plugin_class.new('ecs_compatibility' => 'v1') | ||
allow( plugin.logger ).to receive(:info) | ||
expect( plugin.register ).to eql 42 | ||
expect( plugin.logger ).to have_received(:info).with(a_string_including "ECS compatibility is enabled but `target` option was not specified.") | ||
end | ||
|
||
it 'does not warn when target is set' do | ||
plugin = plugin_class.new('ecs_compatibility' => 'v1', 'target' => 'foo') | ||
allow( plugin.logger ).to receive(:info) | ||
expect( plugin.register ).to eql 42 | ||
expect( plugin.logger ).to_not have_received(:info).with(a_string_including "`target` option") | ||
end | ||
|
||
end | ||
|
||
it 'fails check when no target config' do | ||
plugin_class = Class.new(LogStash::Filters::Base) do | ||
include LogStash::PluginMixins::ECSCompatibilitySupport | ||
include LogStash::PluginMixins::ECSCompatibilitySupport::TargetCheck | ||
|
||
def register; end | ||
|
||
end | ||
plugin = plugin_class.new('ecs_compatibility' => 'v1') | ||
expect { plugin.register }.to raise_error NameError, /\btarget\b/ | ||
end | ||
|
||
end | ||
|
||
end |