Skip to content
Open
Show file tree
Hide file tree
Changes from all 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.5.1
- feature: Added 'omitempty' feature to remove fields with default or null values

## 3.4.0
- Added ability to directly convert from integer and float to boolean [#127](https://github.com/logstash-plugins/logstash-filter-mutate/pull/127)

Expand Down
1 change: 1 addition & 0 deletions CONTRIBUTORS
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ Contributors:
* Tal Levy (talevy)
* piavlo
* Abdul Haseeb Hussain (AbdulHaseebHussain)
* Gregory Ferreux (gferreux)

Note: If you've sent us patches, bug reports, or otherwise contributed to
Logstash, and you aren't on the list above and want to be, please let us know
Expand Down
32 changes: 32 additions & 0 deletions lib/logstash/filters/mutate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,17 @@ class LogStash::Filters::Mutate < LogStash::Filters::Base
# }
config :copy, :validate => :hash

# Omit a variable if it as a default value.
#
# Example:
# [source,ruby]
# filter {
# mutate {
# omitempty => [ "fieldname" ]
# }
# }
config :omitempty, :validate => :array

# Tag to apply if the operation errors
config :tag_on_failure, :validate => :string, :default => '_mutate_error'

Expand Down Expand Up @@ -263,6 +274,7 @@ def filter(event)
join(event) if @join
merge(event) if @merge
copy(event) if @copy
omitempty(event) if @omitempty

filter_matched(event)
rescue => ex
Expand Down Expand Up @@ -474,6 +486,26 @@ def capitalize(event)
end
end

def omitempty(event)
@omitempty.each do |field|
original = event.get(field)
if original.nil?
event.remove(field)
next
end
result = case original
when String, Array, Hash
original.empty? ? nil : original
when Integer
original == 0 ? nil : original
else
@logger.debug? && @logger.debug("Can't omitempty something that isn't a string,array,hash,integer", :field => field, :value => original)
original
end
event.remove(field) if result.nil?
end
end

def split(event)
@split.each do |field, separator|
value = event.get(field)
Expand Down
2 changes: 1 addition & 1 deletion logstash-filter-mutate.gemspec
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Gem::Specification.new do |s|

s.name = 'logstash-filter-mutate'
s.version = '3.5.0'
s.version = '3.5.1'
s.licenses = ['Apache License (2.0)']
s.summary = "Performs mutations on fields"
s.description = "This gem is a Logstash plugin required to be installed on top of the Logstash core pipeline using $LS_HOME/bin/logstash-plugin install gemname. This gem is not a stand-alone program"
Expand Down
41 changes: 41 additions & 0 deletions spec/filters/mutate_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1088,4 +1088,45 @@ def pattern_path(path)
end
end

describe "omitempty" do

config <<-CONFIG
filter {
mutate {
omitempty => ["field"]
}
}
CONFIG

context 'when field is a string' do
sample({'field' => ''}) do
expect(subject).not_to include("field")
end
end

context 'when field is an integer' do
sample({'field' => 0}) do
expect(subject).not_to include("field")
end
end

context 'when field is an empty hash' do
sample({'field' => {}}) do
expect(subject).not_to include("field")
end
end

context 'when field is an empty array' do
sample({'field' => []}) do
expect(subject).not_to include("field")
end
end

context 'when field is null' do
sample({'field' => nil}) do
expect(subject).not_to include("field")
end
end
end

end