Deprecating regexp fields for json filter

- Replace regexp fields with source and target
- Added test for the new fields
This commit is contained in:
Richard Pijnenburg 2013-01-12 14:07:20 +01:00
parent c1aefbd0b2
commit 2a5e7a9c3e
2 changed files with 94 additions and 5 deletions

View file

@ -26,15 +26,50 @@ class LogStash::Filters::Json < LogStash::Filters::Base
# already exists, it will be overridden.
config /[A-Za-z0-9_@-]+/, :validate => :string
# Config for json is:
#
# source => source_field
#
# For example, if you have json data in the @message field:
#
# filter {
# json {
# source => "@message"
# }
# }
#
# The above would parse the xml from the @message field
config :source, :validate => :string
# Define target for placing the data
#
# for example if you want the data to be put in the 'doc' field:
#
# filter {
# json {
# target => "doc"
# }
# }
#
# json in the value of the source field will be expanded into a
# datastructure in the "target" field.
# Note: if the "target" field already exists, it will be overridden
# Required
config :target, :validate => :string
public
def register
@json = {}
@config.each do |field, dest|
next if RESERVED.member?(field)
next if (RESERVED + ["source", "target"]).member?(field)
@json[field] = dest
end
if @source
@json[@source] = @target
end
end # def register
public

View file

@ -4,7 +4,7 @@ require "logstash/filters/json"
describe LogStash::Filters::Json do
extend LogStash::RSpec
describe "parse @message into @fields" do
describe "parse @message into @fields ( deprecated check )" do
config <<-CONFIG
filter {
json {
@ -21,7 +21,7 @@ describe LogStash::Filters::Json do
end
end
describe "parse @message into a target field" do
describe "parse @message into a target field ( deprecated check )" do
config <<-CONFIG
filter {
json {
@ -38,7 +38,7 @@ describe LogStash::Filters::Json do
end
end
describe "tag invalid json" do
describe "tag invalid json ( deprecated check )" do
config <<-CONFIG
filter {
json {
@ -52,4 +52,58 @@ describe LogStash::Filters::Json do
insist { subject.tags }.include?("_jsonparsefailure")
end
end
## New tests
describe "parse @message into @fields" do
config <<-CONFIG
filter {
json {
# Parse @message as JSON, store the results in the 'data' field'
source => "@message"
target => "@fields"
}
}
CONFIG
sample '{ "hello": "world", "list": [ 1, 2, 3 ], "hash": { "k": "v" } }' do
insist { subject["hello"] } == "world"
insist { subject["list" ] } == [1,2,3]
insist { subject["hash"] } == { "k" => "v" }
end
end
describe "parse @message into a target field" do
config <<-CONFIG
filter {
json {
# Parse @message as JSON, store the results in the 'data' field'
source => "@message"
target => "data"
}
}
CONFIG
sample '{ "hello": "world", "list": [ 1, 2, 3 ], "hash": { "k": "v" } }' do
insist { subject["data"]["hello"] } == "world"
insist { subject["data"]["list" ] } == [1,2,3]
insist { subject["data"]["hash"] } == { "k" => "v" }
end
end
describe "tag invalid json" do
config <<-CONFIG
filter {
json {
# Parse @message as JSON, store the results in the 'data' field'
source => "@message"
target => "data"
}
}
CONFIG
sample "invalid json" do
insist { subject.tags }.include?("_jsonparsefailure")
end
end
end