Skip to content

Commit

Permalink
imported patches to add tagger filter and test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
crankycoder committed Oct 18, 2011
1 parent 1216c67 commit 886b47f
Show file tree
Hide file tree
Showing 4 changed files with 173 additions and 0 deletions.
61 changes: 61 additions & 0 deletions lib/logstash/filters/tagger.rb
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
53 changes: 53 additions & 0 deletions test/logstash/filters/'
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
57 changes: 57 additions & 0 deletions test/logstash/filters/test_tagger.rb
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
2 changes: 2 additions & 0 deletions test/logstash_test_runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ def use(path)
use "logstash/filters/test_grep"
use "logstash/filters/test_multiline"
use "logstash/filters/test_grok"
use "logstash/filters/test_tagger"


use "logstash/outputs/test_elasticsearch"

Expand Down

0 comments on commit 886b47f

Please sign in to comment.