Skip to content

Commit 7296094

Browse files
andselkares
andauthored
Inform the user if target option is not defined in local_lookups definitions when ECS is enabled (#71)
Configuration of `logstash-filter-jdbc_static` filter definition requires also the definition of `local_lookups` sections. Those sections has an optional `target` field to specify the Event's field name where to put the loaded columns, if the plugin is used in with ECS enabled and any blocks doesn't define explicitly a target, then a informative line is printed out to the user. Co-authored-by: Karol Bucek <[email protected]>
1 parent 8e9aee1 commit 7296094

File tree

4 files changed

+45
-0
lines changed

4 files changed

+45
-0
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
- Added `target` option to JDBC input, allowing the row columns to target a specific field instead of being expanded
33
at the root of the event. This allows the input to play nicer with the Elastic Common Schema when
44
the input does not follow the schema. [#69](https://github.com/logstash-plugins/logstash-integration-jdbc/issues/69)
5+
6+
- Added `target` to JDBC filter static `local_lookups` to verify it's properly valued when ECS is enabled.
7+
[#71](https://github.com/logstash-plugins/logstash-integration-jdbc/issues/71)
58

69
## 5.0.7
710
- Feat: try hard to log Java cause (chain) [#62](https://github.com/logstash-plugins/logstash-integration-jdbc/pull/62)

lib/logstash/filters/jdbc/lookup.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,12 @@ def initialize(options, globals, default_id)
5656
@target = options["target"]
5757
@id_used_as_target = @target.nil?
5858
if @id_used_as_target
59+
# target shouldn't be nil if ecs_compatibility is not :disabled
60+
if globals[:ecs_compatibility] != :disabled
61+
logger.info('ECS compatibility is enabled but no ``target`` option was specified, it is recommended'\
62+
' to set the option to avoid potential schema conflicts (if your data is ECS compliant or'\
63+
' non-conflicting feel free to ignore this message)')
64+
end
5965
@target = @id
6066
end
6167
@options = options

lib/logstash/filters/jdbc_static.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
require "logstash-integration-jdbc_jars"
33
require "logstash/filters/base"
44
require "logstash/namespace"
5+
require "logstash/plugin_mixins/ecs_compatibility_support"
56
require_relative "jdbc/loader"
67
require_relative "jdbc/loader_schedule"
78
require_relative "jdbc/repeating_load_runner"
@@ -14,6 +15,9 @@
1415

1516
#
1617
module LogStash module Filters class JdbcStatic < LogStash::Filters::Base
18+
# adds ecs_compatibility config which could be :disabled or :v1
19+
include LogStash::PluginMixins::ECSCompatibilitySupport(:disabled, :v1, :v8 => :v1)
20+
1721
config_name "jdbc_static"
1822

1923
# Define the loaders, an Array of Hashes, to fetch remote data and create local tables.
@@ -214,6 +218,7 @@ def global_lookup_options(options = Hash.new)
214218
options["lookup_jdbc_driver_class"] = @lookup_jdbc_driver_class
215219
options["lookup_jdbc_driver_library"] = @lookup_jdbc_driver_library
216220
options["lookup_jdbc_connection_string"] = @lookup_jdbc_connection_string
221+
options["ecs_compatibility"] = ecs_compatibility
217222
options
218223
end
219224

spec/filters/jdbc/lookup_spec.rb

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,37 @@ module LogStash module Filters module Jdbc
248248
expect(subject.valid?).to be_falsey
249249
end
250250
end
251+
252+
describe "validation of target option" do
253+
let(:lookup_hash) do
254+
{
255+
"query" => "select * from servers WHERE ip LIKE ? AND os LIKE ?",
256+
"prepared_parameters" => ["%%{[ip]}"],
257+
}
258+
end
259+
260+
it "should log a warn when ECS is enabled and target not defined" do
261+
262+
class LoggableLookup < Lookup
263+
264+
@@TEST_LOGGER = nil
265+
266+
def self.logger=(log)
267+
@@TEST_LOGGER = log
268+
end
269+
270+
def self.logger
271+
@@TEST_LOGGER
272+
end
273+
end
274+
275+
spy_logger = double("logger")
276+
expect(spy_logger).to receive(:info).once.with(/ECS compatibility is enabled but no .*?target.*? was specified/)
277+
LoggableLookup.logger = spy_logger
278+
279+
LoggableLookup.new(lookup_hash, {:ecs_compatibility => 'v1'}, "lookup-1")
280+
end
281+
end
251282
end
252283
end end end
253284

0 commit comments

Comments
 (0)