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

add ECS support #85

Merged
merged 6 commits into from
Jun 3, 2021
Merged
Show file tree
Hide file tree
Changes from 3 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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## 3.1.0
- Add ECS support [#85](https://github.com/logstash-plugins/logstash-filter-csv/pull/85)

## 3.0.11
- [DOC] Fixed formatting to improve readability [#84](https://github.com/logstash-plugins/logstash-filter-csv/pull/84)

Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3.0.11
3.1.0
22 changes: 22 additions & 0 deletions docs/index.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ The CSV filter takes an event field containing CSV data, parses it,
and stores it as individual fields with optionally-specified field names.
This filter can parse data with any separator, not just commas.

[id="plugins-{type}s-{plugin}-ecs_metadata"]
==== Event Metadata and the Elastic Common Schema (ECS)
When ECS compatibility is disabled, the value is stored in the root level.
When ECS is enabled, the value is stored in the `target` field.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I haven't added the table to compare the columns of ECS disabled and ECS v1 because the field name is depending on the value of target which has no default value

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understand the intention but from a plugin user perspective the docs seems confusing.
Maybe we should just include the same message as the one that gets logged to explain?

The plugin behaves the same regardless of ECS compatibility, except issuing a warning when `target` isn't set.
It is recommended to set the `target` option to avoid potential schema conflicts.

(just a draft - probably need rewording if we decide to use it)

[id="plugins-{type}s-{plugin}-options"]
==== Csv Filter Configuration Options

Expand All @@ -36,6 +41,7 @@ This plugin supports the following configuration options plus the <<plugins-{typ
| <<plugins-{type}s-{plugin}-autogenerate_column_names>> |<<boolean,boolean>>|No
| <<plugins-{type}s-{plugin}-columns>> |<<array,array>>|No
| <<plugins-{type}s-{plugin}-convert>> |<<hash,hash>>|No
| <<plugins-{type}s-{plugin}-ecs_compatibility>> | <<string,string>>|No
| <<plugins-{type}s-{plugin}-quote_char>> |<<string,string>>|No
| <<plugins-{type}s-{plugin}-separator>> |<<string,string>>|No
| <<plugins-{type}s-{plugin}-skip_empty_columns>> |<<boolean,boolean>>|No
Expand Down Expand Up @@ -104,6 +110,18 @@ Example:
}
}

[id="plugins-{type}s-{plugin}-ecs_compatibility"]
===== `ecs_compatibility`

* Value type is <<string,string>>
* Supported values are:
** `disabled`: does not use ECS-compatible field names
** `v1`: uses the value in `target` as field name

Controls this plugin's compatibility with the
{ecs-ref}[Elastic Common Schema (ECS)].
See <<plugins-{type}s-{plugin}-ecs_metadata>> for detailed information.

[id="plugins-{type}s-{plugin}-quote_char"]
===== `quote_char`

Expand Down Expand Up @@ -179,6 +197,10 @@ data structure.
Define target field for placing the data.
Defaults to writing to the root of the event.

Without a `target`, events are created from each row column at the root level.
When the `target` is set to a field reference, the column of each row is placed in the target field instead.

This option can be useful to avoid populating unknown fields when a downstream schema such as ECS is enforced.


[id="plugins-{type}s-{plugin}-common-options"]
Expand Down
12 changes: 12 additions & 0 deletions lib/logstash/filters/csv.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
# encoding: utf-8
require "logstash/filters/base"
require "logstash/namespace"
require "logstash/plugin_mixins/ecs_compatibility_support"

require "csv"

# The CSV filter takes an event field containing CSV data, parses it,
# and stores it as individual fields (can optionally specify the names).
# 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)

config_name "csv"

# The CSV data in the value of the `source` field will be expanded into a
Expand Down Expand Up @@ -102,6 +105,15 @@ 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 Down
2 changes: 1 addition & 1 deletion logstash-filter-csv.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ Gem::Specification.new do |s|

# Gem dependencies
s.add_runtime_dependency "logstash-core-plugin-api", ">= 1.60", "<= 2.99"

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

Loading