Skip to content

Commit

Permalink
Deprecating regexp fields for json filter
Browse files Browse the repository at this point in the history
- Replace regexp fields with source and target
- Added test for the new fields
  • Loading branch information
Richard Pijnenburg committed Jan 12, 2013
1 parent c1aefbd commit 2a5e7a9
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 5 deletions.
39 changes: 37 additions & 2 deletions lib/logstash/filters/json.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
60 changes: 57 additions & 3 deletions spec/filters/json.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
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 {
Expand All @@ -21,7 +21,7 @@
end
end

describe "parse @message into a target field" do
describe "parse @message into a target field ( deprecated check )" do
config <<-CONFIG
filter {
json {
Expand All @@ -38,7 +38,7 @@
end
end

describe "tag invalid json" do
describe "tag invalid json ( deprecated check )" do
config <<-CONFIG
filter {
json {
Expand All @@ -52,4 +52,58 @@
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

0 comments on commit 2a5e7a9

Please sign in to comment.