From 2a5e7a9c3ebadcf2c41d1b12cda5518221467a6d Mon Sep 17 00:00:00 2001 From: Richard Pijnenburg Date: Sat, 12 Jan 2013 14:07:20 +0100 Subject: [PATCH] Deprecating regexp fields for json filter - Replace regexp fields with source and target - Added test for the new fields --- lib/logstash/filters/json.rb | 39 +++++++++++++++++++++-- spec/filters/json.rb | 60 ++++++++++++++++++++++++++++++++++-- 2 files changed, 94 insertions(+), 5 deletions(-) diff --git a/lib/logstash/filters/json.rb b/lib/logstash/filters/json.rb index 61bc3ebb3..01300c77e 100644 --- a/lib/logstash/filters/json.rb +++ b/lib/logstash/filters/json.rb @@ -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 diff --git a/spec/filters/json.rb b/spec/filters/json.rb index 491f53593..5bf29470b 100644 --- a/spec/filters/json.rb +++ b/spec/filters/json.rb @@ -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