Merge pull request #312 from electrical/json_regexp_deprecate

Deprecating regexp fields for json filter
This commit is contained in:
Jordan Sissel 2013-02-04 16:15:04 -08:00
commit a103dc489e
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 {
@ -24,7 +24,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 {
@ -47,7 +47,7 @@ describe LogStash::Filters::Json do
end
end
describe "tag invalid json" do
describe "tag invalid json ( deprecated check )" do
config <<-CONFIG
filter {
json {
@ -61,4 +61,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