Skip to content

Commit

Permalink
Refactor: unified ECS target + validate field reference (#86)
Browse files Browse the repository at this point in the history
  • Loading branch information
kares authored Jun 8, 2021
1 parent 4efc215 commit 30b2c23
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 16 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## 3.1.1
- Refactor: unified ECS target + validate field reference [#86](https://github.com/logstash-plugins/logstash-filter-csv/pull/86)

## 3.1.0
- Add ECS support [#85](https://github.com/logstash-plugins/logstash-filter-csv/pull/85)

Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3.1.0
3.1.1
24 changes: 10 additions & 14 deletions lib/logstash/filters/csv.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
# encoding: utf-8
require "logstash/filters/base"
require "logstash/namespace"
require "logstash/plugin_mixins/ecs_compatibility_support"
require 'logstash/plugin_mixins/ecs_compatibility_support'
require 'logstash/plugin_mixins/ecs_compatibility_support/target_check'
require 'logstash/plugin_mixins/validator_support/field_reference_validation_adapter'

require "csv"

Expand All @@ -10,6 +12,9 @@
# This filter can also parse data with any separator, not just commas.
class LogStash::Filters::CSV < LogStash::Filters::Base
include LogStash::PluginMixins::ECSCompatibilitySupport(:disabled, :v1, :v8 => :v1)
include LogStash::PluginMixins::ECSCompatibilitySupport::TargetCheck

extend LogStash::PluginMixins::ValidatorSupport::FieldReferenceValidationAdapter

config_name "csv"

Expand Down Expand Up @@ -38,7 +43,7 @@ class LogStash::Filters::CSV < LogStash::Filters::Base

# Define target field for placing the data.
# Defaults to writing to the root of the event.
config :target, :validate => :string
config :target, :validate => :field_reference

# Define whether column names should autogenerated or not.
# Defaults to true. If set to false, columns not having a header specified will not be parsed.
Expand Down Expand Up @@ -105,15 +110,6 @@ class LogStash::Filters::CSV < LogStash::Filters::Base
CONVERTERS.default = lambda {|v| v}
CONVERTERS.freeze

def initialize(params)
super
if ecs_compatibility != :disabled && @target.nil?
logger.info('ECS compatibility is enabled but no ``target`` option was specified, it is recommended'\
' to set the option to avoid potential schema conflicts (if your data is ECS compliant or'\
' non-conflicting feel free to ignore this message)')
end
end

def register
# validate conversion types to be the valid ones.
bad_types = @convert.values.select do |type|
Expand All @@ -125,7 +121,7 @@ def register
# @convert_symbols contains the symbolized types to avoid symbol conversion in the transform method
@convert_symbols = @convert.inject({}){|result, (k, v)| result[k] = v.to_sym; result}

# make sure @target is in the format [field name] if defined, i.e. surrounded by brakets
# make sure @target is in the format [field name] if defined, i.e. surrounded by brackets
@target = "[#{@target}]" if @target && !@target.start_with?("[")

# if the zero byte character is entered in the config, set the value
Expand All @@ -137,7 +133,7 @@ def register
end

def filter(event)
@logger.debug? && @logger.debug("Running csv filter", :event => event)
@logger.debug? && @logger.debug("Running csv filter", :event => event.to_hash)

if (source = event.get(@source))
begin
Expand Down Expand Up @@ -178,7 +174,7 @@ def filter(event)
end
end

@logger.debug? && @logger.debug("Event after csv filter", :event => event)
@logger.debug? && @logger.debug("Event after csv filter", :event => event.to_hash)
end

private
Expand Down
4 changes: 3 additions & 1 deletion logstash-filter-csv.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ Gem::Specification.new do |s|

# Gem dependencies
s.add_runtime_dependency "logstash-core-plugin-api", ">= 1.60", "<= 2.99"
s.add_runtime_dependency 'logstash-mixin-ecs_compatibility_support', '~> 1.3'
s.add_runtime_dependency 'logstash-mixin-validator_support', '~> 1.0'

s.add_development_dependency 'logstash-devutils'
s.add_runtime_dependency 'logstash-mixin-ecs_compatibility_support', '~>1.2'
end

0 comments on commit 30b2c23

Please sign in to comment.