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
5 changes: 3 additions & 2 deletions lib/logstash/filters/array_of_maps_value_update.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@

module LogStash module Filters
class ArrayOfMapsValueUpdate
def initialize(iterate_on, field, destination, fallback, lookup)
def initialize(iterate_on, field, destination, fallback, lookup, dynamic)
@iterate_on = ensure_reference_format(iterate_on)
@field = ensure_reference_format(field)
@destination = ensure_reference_format(destination)
@fallback = fallback
@use_fallback = !fallback.nil? # fallback is not nil, the user set a value in the config
@lookup = lookup
@dynamic = dynamic
end

def test_for_inclusion(event, override)
Expand All @@ -27,7 +28,7 @@ def update(event)
matched = [true, nil]
@lookup.fetch_strategy.fetch(inner.to_s, matched)
if matched.first
event.set(nested_destination, matched.last)
event.set(nested_destination, @dynamic ? event.sprintf(matched.last) : matched.last)
matches[index] = true
elsif @use_fallback
event.set(nested_destination, event.sprintf(@fallback))
Expand Down
5 changes: 3 additions & 2 deletions lib/logstash/filters/array_of_values_update.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@ class CoerceOther
def call(source) Array(source); end
end

def initialize(iterate_on, destination, fallback, lookup)
def initialize(iterate_on, destination, fallback, lookup, dynamic)
@iterate_on = iterate_on
@destination = destination
@fallback = fallback
@use_fallback = !fallback.nil? # fallback is not nil, the user set a value in the config
@lookup = lookup
@dynamic = dynamic
@coercers_table = {}
@coercers_table.default = CoerceOther.new
@coercers_table[Array] = CoerceArray.new
Expand All @@ -37,7 +38,7 @@ def update(event)
matched = [true, nil]
@lookup.fetch_strategy.fetch(inner.to_s, matched)
if matched.first
target[index] = matched.last
target[index] = @dynamic ? event.sprintf(matched.last) : matched.last
end
end
event.set(@destination, target)
Expand Down
5 changes: 3 additions & 2 deletions lib/logstash/filters/single_value_update.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@ class CoerceOther
def call(source) source.to_s end
end

def initialize(field, destination, fallback, lookup)
def initialize(field, destination, fallback, lookup, dynamic)
@field = field
@destination = destination
@fallback = fallback
@use_fallback = !fallback.nil? # fallback is not nil, the user set a value in the config
@dynamic = dynamic
@lookup = lookup
@coercers_table = {}
@coercers_table.default = CoerceOther.new
Expand All @@ -38,7 +39,7 @@ def update(event)
matched = [true, nil]
@lookup.fetch_strategy.fetch(source, matched)
if matched.first
event.set(@destination, matched.last)
event.set(@destination, @dynamic ? event.sprintf(matched.last) : matched.last)
elsif @use_fallback
event.set(@destination, event.sprintf(@fallback))
matched[0] = true
Expand Down
9 changes: 6 additions & 3 deletions lib/logstash/filters/translate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,9 @@ class Translate < LogStash::Filters::Base
# the field to write the looked up value (or the `fallback` value) to with `destination`
config :iterate_on, :validate => :string

# If you'd like dictionary values to be evaluated dynamically set `dynamic => true`.
config :dynamic, :validate => :boolean, :default => false

attr_reader :lookup # for testing reloading

def register
Expand All @@ -168,11 +171,11 @@ def register
@lookup = Dictionary::Memory.new(@dictionary, @exact, @regex)
end
if @iterate_on.nil?
@updater = SingleValueUpdate.new(@field, @destination, @fallback, @lookup)
@updater = SingleValueUpdate.new(@field, @destination, @fallback, @lookup, @dynamic)
elsif @iterate_on == @field
@updater = ArrayOfValuesUpdate.new(@iterate_on, @destination, @fallback, @lookup)
@updater = ArrayOfValuesUpdate.new(@iterate_on, @destination, @fallback, @lookup, @dynamic)
else
@updater = ArrayOfMapsValueUpdate.new(@iterate_on, @field, @destination, @fallback, @lookup)
@updater = ArrayOfMapsValueUpdate.new(@iterate_on, @field, @destination, @fallback, @lookup, @dynamic)
end

@logger.debug? && @logger.debug("#{self.class.name}: Dictionary - ", :dictionary => @lookup.dictionary)
Expand Down
50 changes: 50 additions & 0 deletions spec/filters/translate_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -499,4 +499,54 @@ def self.build_fixture_path(filename)
end
end
end

describe "dnynamic translation" do

let(:config) do
{
"field" => "status",
"destination" => "translation",
"dictionary" => [ "200", "O%{[other]}K",
"300", "Redirect",
"400", "Client Error",
"500", "Server Error" ],
"exact" => true,
"regex" => false,
"dynamic" => true
}
end

let(:event) { LogStash::Event.new("status" => 200, "other" => "test123") }

it "coerces field to a string then returns the exact translation" do
subject.register
subject.filter(event)
expect(event.get("translation")).to eq("Otest123K")
end
end

describe "non-dynamic translation" do

let(:config) do
{
"field" => "status",
"destination" => "translation",
"dictionary" => [ "200", "OK",
"300", "Redirect",
"400", "Client Error",
"500", "Server Error",
"testdyn", "some%{[other]}value" ],
"exact" => true,
"regex" => false
}
end

let(:event) { LogStash::Event.new("status" => "testdyn", "other" => "test123") }

it "won't use dynamic evaluation when disabled" do
subject.register
subject.filter(event)
expect(event.get("translation")).to eq("some%{[other]}value")
end
end
end