- refactor json_encode filter to use source+target settings

- add specs to cover json_encode
This commit is contained in:
Jordan Sissel 2013-08-31 22:42:41 -07:00
parent a732b06c20
commit 516a0b6410
2 changed files with 66 additions and 37 deletions

View file

@ -2,36 +2,34 @@ require "logstash/filters/base"
require "logstash/namespace"
# JSON encode filter. Takes a field and serializes it into JSON
class LogStash::Filters::JsonEncode < LogStash::Filters::Base
#
# If no target is specified, the source field is overwritten with the JSON
# text.
#
# For example, if you have a field named 'foo', and you want to store the
# JSON encoded string in 'bar', do this:
#
# filter {
# json_encode {
# source => "foo"
# target => "bar"
# }
# }
class LogStash::Filters::JSONEncode < LogStash::Filters::Base
config_name "json_encode"
milestone 2
# Config for json_encode is:
#
# * source => dest
#
# For example, if you have a field named 'foo', and you want to store the
# JSON encoded string in 'bar', do this:
#
# filter {
# json_encode {
# foo => bar
# }
# }
#
# Note: if the "dest" field already exists, it will be overridden.
config /[A-Za-z0-9_@-]+/, :validate => :string
# The field to convert to JSON.
config :source, :validate => :string, :required => true
# The field to write the JSON into. If not specified, the source
# field will be overwritten.
config :target, :validate => :string
public
def register
@json = {}
@config.each do |field, dest|
next if RESERVED.member?(field)
@json[field] = dest
end
@target = @source if @target.nil?
end # def register
public
@ -40,20 +38,14 @@ class LogStash::Filters::JsonEncode < LogStash::Filters::Base
@logger.debug("Running JSON encoder", :event => event)
@json.each do |key, dest|
next unless event[key]
begin
event[dest] = JSON.pretty_generate(event[key])
filter_matched(event)
rescue => e
event.tag "_jsongeneratefailure"
@logger.warn("Trouble encoding JSON", :key => key, :raw => event[key],
:exception => e)
next
end
begin
event[@target] = JSON.pretty_generate(event[@source])
filter_matched(event)
rescue => e
event.tag "_jsongeneratefailure"
@logger.warn("Trouble encoding JSON", :key => key, :raw => event[key].inspect, :exception => e)
end
@logger.debug("Event after JSON encoder", :event => event)
@logger.debug? && @logger.debug("Event after JSON encoder", :event => event)
end # def filter
end # class LogStash::Filters::JsonEncode
end # class LogStash::Filters::JSONEncode

View file

@ -0,0 +1,37 @@
require "test_utils"
require "logstash/filters/json_encode"
describe LogStash::Filters::JSONEncode do
extend LogStash::RSpec
describe "encode a field as json" do
config <<-CONFIG
filter {
json_encode {
source => "hello"
target => "fancy"
}
}
CONFIG
hash = { "hello" => { "whoa" => [ 1,2,3 ] } }
sample(hash) do
insist { JSON.parse(subject["fancy"]).to_json } == hash["hello"].to_json
end
end
describe "encode a field as json and overwrite the original" do
config <<-CONFIG
filter {
json_encode {
source => "hello"
}
}
CONFIG
hash = { "hello" => { "whoa" => [ 1,2,3 ] } }
sample(hash) do
insist { JSON.parse(subject["hello"]).to_json } == hash["hello"].to_json
end
end
end