Evaluate dynamic fields in event when used with gsub

Closes #1529
This commit is contained in:
Suyog Rao 2014-07-18 15:26:56 -07:00
parent 4fa053dff8
commit 6e3d377bba
2 changed files with 62 additions and 3 deletions

View file

@ -191,9 +191,10 @@ class LogStash::Filters::Mutate < LogStash::Filters::Base
@logger.error("Invalid gsub configuration. gsub has to define 3 elements per config entry", :field => field, :needle => needle, :replacement => replacement)
raise "Bad configuration, aborting."
end
@gsub_parsed << {
:field => field,
:needle => Regexp.new(needle),
:needle => (needle.index("%{").nil?? Regexp.new(needle): needle),
:replacement => replacement
}
end
@ -303,7 +304,7 @@ class LogStash::Filters::Mutate < LogStash::Filters::Base
"skipping", :field => field, :value => v)
v
else
v.gsub(needle, replacement)
gsub_dynamic_fields(event, v, needle, replacement)
end
end
else
@ -312,11 +313,21 @@ class LogStash::Filters::Mutate < LogStash::Filters::Base
"skipping", :field => field, :value => event[field])
next
end
event[field] = event[field].gsub(needle, replacement)
event[field] = gsub_dynamic_fields(event, event[field], needle, replacement)
end
end # @gsub_parsed.each
end # def gsub
private
def gsub_dynamic_fields(event, original, needle, replacement)
if needle.is_a? Regexp
original.gsub(needle, event.sprintf(replacement))
else
# we need to replace any dynamic fields
original.gsub(Regexp.new(event.sprintf(needle)), event.sprintf(replacement))
end
end
private
def uppercase(event)
@uppercase.each do |field|

View file

@ -178,5 +178,53 @@ describe LogStash::Filters::Mutate do
insist { subject["[foo][bar]"] }.is_a?(Fixnum)
end
end
#LOGSTASH-1529
describe "gsub on a String with dynamic fields (%{}) in pattern" do
config '
filter {
mutate {
gsub => [ "unicorns", "of type %{unicorn_type}", "green" ]
}
}'
sample("unicorns" => "Unicorns of type blue are common", "unicorn_type" => "blue") do
insist { subject["unicorns"] } == "Unicorns green are common"
end
end
#LOGSTASH-1529
describe "gsub on a String with dynamic fields (%{}) in pattern and replace" do
config '
filter {
mutate {
gsub => [ "unicorns2", "of type %{unicorn_color}", "%{unicorn_color} and green" ]
}
}'
sample("unicorns2" => "Unicorns of type blue are common", "unicorn_color" => "blue") do
insist { subject["unicorns2"] } == "Unicorns blue and green are common"
end
end
#LOGSTASH-1529
describe "gsub on a String array with dynamic fields in pattern" do
config '
filter {
mutate {
gsub => [ "unicorns_array", "of type %{color}", "blue and green" ]
}
}'
sample("unicorns_array" => [
"Unicorns of type blue are found in Alaska", "Unicorns of type blue are extinct" ],
"color" => "blue"
) do
insist { subject["unicorns_array"] } == [
"Unicorns blue and green are found in Alaska",
"Unicorns blue and green are extinct"
]
end
end
end