Skip to content

Commit 9080466

Browse files
alefebvre-lsph
authored andcommitted
Added tag_on_failure filter attribute
Fixes #21
1 parent 3adff15 commit 9080466

File tree

2 files changed

+6
-2
lines changed

2 files changed

+6
-2
lines changed

lib/logstash/filters/json.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,9 @@ class LogStash::Filters::Json < LogStash::Filters::Base
4646
# NOTE: if the `target` field already exists, it will be overwritten!
4747
config :target, :validate => :string
4848

49-
JSONPARSEFAILURE_TAG = "_jsonparsefailure"
49+
# Append values to the `tags` field when there has been no
50+
# successful match
51+
config :tag_on_failure, :validate => :array, :default => ["_jsonparsefailure"]
5052

5153
def register
5254
# Nothing to do here
@@ -61,7 +63,7 @@ def filter(event)
6163
begin
6264
parsed = LogStash::Json.load(source)
6365
rescue => e
64-
event.tag(JSONPARSEFAILURE_TAG)
66+
@tag_on_failure.each{|tag| event.tag(tag)}
6567
@logger.warn("Error parsing json", :source => @source, :raw => source, :exception => e)
6668
return
6769
end

spec/filters/json_spec.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,14 @@
4848
# Parse message as JSON, store the results in the 'data' field'
4949
source => "message"
5050
target => "data"
51+
tag_on_failure => ["_jsonparsefailure","_custom_failure_tag"]
5152
}
5253
}
5354
CONFIG
5455

5556
sample "invalid json" do
5657
insist { subject["tags"] }.include?("_jsonparsefailure")
58+
insist { subject["tags"] }.include?("_custom_failure_tag")
5759
end
5860
end
5961

0 commit comments

Comments
 (0)