- make 'update' set a field value if it exists but do nothing if not

- make 'replace' do what it should, which is replace a field (or create
  it if it doesn't exist).
This commit is contained in:
Jordan Sissel 2013-05-06 07:49:41 -07:00
parent e42ecc420c
commit 40532b266e
2 changed files with 27 additions and 3 deletions

View file

@ -45,6 +45,18 @@ class LogStash::Filters::Mutate < LogStash::Filters::Base
# }
config :replace, :validate => :hash
# Update an existing field with a new value. If the field does not exist,
# then no action will be taken.
#
# Example:
#
# filter {
# mutate {
# update => [ "sample", "My new message" ]
# }
# }
config :update, :validate => :hash
# Convert a field's value to a different type, like turning a string to an
# integer. If the field value is an array, all members will be converted.
# If the field is a hash, no action will be taken.
@ -220,12 +232,18 @@ class LogStash::Filters::Mutate < LogStash::Filters::Base
end # def rename
private
def replace(event)
# TODO(sissel): use event.sprintf on the field names?
@replace.each do |field, newvalue|
def update(event)
@update.each do |field, newvalue|
next unless event.include?(field)
event[field] = event.sprintf(newvalue)
end
end # def update
private
def replace(event)
@replace.each do |field, newvalue|
event[field] = event.sprintf(newvalue)
end
end # def replace
def convert(event)

View file

@ -13,6 +13,8 @@ describe LogStash::Filters::Mutate do
convert => [ "intme", "integer", "floatme", "float" ]
rename => [ "rename1", "rename2" ]
replace => [ "replaceme", "hello world" ]
replace => [ "newfield", "newnew" ]
update => [ "nosuchfield", "weee" ]
remove => [ "removeme" ]
}
}
@ -37,6 +39,10 @@ describe LogStash::Filters::Mutate do
reject { subject }.include?("rename1")
insist { subject["rename2"] } == [ "hello world" ]
reject { subject }.include?("removeme")
insist { subject }.include?("newfield")
insist { subject["newfield"] } == "newnew"
reject { subject }.include?("nosuchfield")
end
end