forked from elastic/logstash
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
imported patches to add tagger filter and test cases
- Loading branch information
1 parent
1216c67
commit 886b47f
Showing
4 changed files
with
173 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
# This filter decodes JSON blobs and matches on | ||
# simple key/value pairs. | ||
# | ||
# Basic usage in your logstash configuration | ||
# | ||
# | ||
# | ||
# | ||
|
||
|
||
require "logstash/filters/base" | ||
require "logstash/namespace" | ||
|
||
class LogStash::Filters::Tagger < LogStash::Filters::Base | ||
# Setting the cfig_name here is required. This is how you | ||
# cfigure this filter from your logstash cfig. | ||
# | ||
# filter { | ||
# foo { ... } | ||
# } | ||
config_name "tagger" | ||
|
||
# Specify a pattern to parse with. This will match the JSON blob. | ||
# For patterns will match only with exact matches. These are not | ||
# regular expressions. | ||
config :pattern, :validate => :hash, :default => {} | ||
|
||
public | ||
def register | ||
# Don't think we need to do anything special here | ||
end # def register | ||
|
||
public | ||
def filter(event) | ||
|
||
if @pattern.length > 0 | ||
matched = true | ||
else | ||
matched = false | ||
end | ||
|
||
@pattern.each_pair{ |keypath, match_pattern| | ||
obj = event.fields | ||
keypath.split('/').each{ |segment| | ||
obj = obj[segment] | ||
} | ||
|
||
if obj.to_s != match_pattern | ||
matched = false | ||
break | ||
end | ||
} | ||
|
||
if matched | ||
# This will automatically apply all the tags that we | ||
# defined in the configuration file | ||
filter_matched(event) | ||
end | ||
end # def filter | ||
|
||
end # class LogStash::Filters::Tagger |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
require "rubygems" | ||
require File.join(File.dirname(__FILE__), "..", "minitest") | ||
|
||
require "logstash/loadlibs" | ||
require "logstash" | ||
require "logstash/filters" | ||
require "logstash/filters/tagger" | ||
require "logstash/event" | ||
require "json" | ||
require "ruby-debug" | ||
|
||
describe LogStash::Filters::Tagger do | ||
before do | ||
@typename = "metlog" | ||
end | ||
|
||
def config(cfg) | ||
# this method is *not* executed automatically | ||
cfg["type"] = @typename | ||
cfg['pattern'] = ['logger', "toy2", 42] | ||
cfg['add_tag'] = ['metlog_dest_bagheera'] | ||
|
||
@myconfig = cfg | ||
|
||
@filter = LogStash::Filters::Tagger.new(cfg) | ||
@filter.register | ||
end # def config | ||
|
||
test "simple_test" do | ||
# weird - config {} doesn't seem to work | ||
config ({}) | ||
|
||
event = LogStash::Event.new | ||
event.type = @typename | ||
|
||
jdata = { timestamp: '2011-10-13T09:43:44.386392', | ||
metadata: {'some_data' => 'foo' }, | ||
type: 'error', | ||
logger: 'toy2', | ||
severity: 0, | ||
message: 'some log text', | ||
} | ||
|
||
event["message"] = JSON(jdata).to_s | ||
|
||
@filter.filter(event) | ||
|
||
#assert not event.tags.include? "metlog_dest_bagheera" | ||
# This event should be tagged as a sentry event | ||
assert event.tags.include? "metlog_dest_bagheera" | ||
end # testing a single match | ||
|
||
end # TestFilterGrep |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
require "rubygems" | ||
require File.join(File.dirname(__FILE__), "..", "minitest") | ||
|
||
require "logstash/loadlibs" | ||
require "logstash" | ||
require "logstash/filters" | ||
require "logstash/filters/tagger" | ||
require "logstash/event" | ||
require "json" | ||
require "ruby-debug" | ||
|
||
describe LogStash::Filters::Tagger do | ||
before do | ||
@typename = "metlog" | ||
end | ||
|
||
def config(cfg) | ||
# this method is *not* executed automatically | ||
# | ||
# A bit confused here. When you set the type, the configuration | ||
# takes in a string, but the testcase expects a list? | ||
|
||
cfg["type"] = ["metlog"] | ||
cfg['pattern'] = ['logger', "toy2"] | ||
cfg['add_tag'] = ['metlog_dest_bagheera'] | ||
|
||
@filter = LogStash::Filters::Tagger.new(cfg) | ||
@filter.register | ||
end # def config | ||
|
||
test "logger based routing" do | ||
# weird - config {} doesn't seem to work | ||
config ({}) | ||
|
||
event = LogStash::Event.new | ||
event.type = @typename | ||
|
||
# TODO: refactor out the creation of the JSON blob | ||
jdata = { 'timestamp' => '2011-10-13T09:43:44.386392', | ||
'metadata' => {'some_data' => 'foo' }, | ||
'type' => 'error', | ||
'logger' => 'toy2', | ||
'severity' => 0, | ||
'message' => 'some log text', | ||
} | ||
|
||
jdata.each { |k, v| event[k] = v } | ||
|
||
@filter.filter(event) | ||
|
||
# Check that we've got just the tags that we're expecting | ||
assert event.tags.include? "metlog_dest_bagheera" | ||
assert !(event.tags.include?"metlog_dest_sentry") | ||
|
||
end # testing a single match | ||
|
||
end # TestFilterGrep |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters