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

Split filter: ability to split arrays within a JSON structure #787

Closed
wants to merge 4 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
36 changes: 22 additions & 14 deletions lib/logstash/filters/split.rb
@@ -1,6 +1,6 @@
# encoding: utf-8
require "logstash/filters/base"
require "logstash/namespace"
require "logstash/event"

# The split filter is for splitting multiline messages into separate events.
#
Expand All @@ -22,6 +22,10 @@ class LogStash::Filters::Split < LogStash::Filters::Base
# The field which value is split by the terminator
config :field, :validate => :string, :default => "message"

# If true, the array data will be passed forward as a single hash element with @field as the key.
# If false, treat the entire array element as a new event for further processing.
config :reuse_element, :validate => :boolean, :default => true

public
def register
# Nothing to do
Expand All @@ -31,27 +35,31 @@ def register
def filter(event)
return unless filter?(event)

events = []
splits = []

original_value = event[@field]

# If for some reason the field is an array of values, take the first only.
original_value = original_value.first if original_value.is_a?(Array)

# Using -1 for 'limit' on String#split makes ruby not drop trailing empty
# splits.
splits = original_value.split(@terminator, -1)
if original_value.is_a?(Array)
splits = original_value
else
# Using -1 for 'limit' on String#split makes ruby not drop trailing empty
# splits.
splits = original_value.split(@terminator, -1)
end

# Skip filtering if splitting this event resulted in only one thing found.
return if splits.length == 1
#or splits[1].empty?
# Skip filtering if splitting this event resulted in only one thing found
return if splits.length <= 1

splits.each do |value|
next if value.empty?

event_split = event.clone
@logger.debug("Split event", :value => value, :field => @field)
event_split[@field] = value
if @reuse_element
event_split = event.clone
@logger.debug("Split event", :value => value, :field => @field)
event_split[@field] = value
else
event_split = LogStash::Event.new(value)
end
filter_matched(event_split)

# Push this new event onto the stack at the LogStash::FilterWorker
Expand Down