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.
Merge pull request elastic#335 from paulczar/master
new filter 'translate'
- Loading branch information
Showing
1 changed file
with
71 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,71 @@ | ||
require "logstash/filters/base" | ||
require "logstash/namespace" | ||
|
||
# Originally written to translate HTTP response codes | ||
# but turned into a general translation tool which uses | ||
# .yaml files as a dictionary. | ||
# response codes in default dictionary were scraped from | ||
# 'gem install cheat; cheat status_codes' | ||
|
||
class LogStash::Filters::Translate < LogStash::Filters::Base | ||
config_name "translate" | ||
plugin_status "experimental" | ||
|
||
|
||
# The field containing a response code If this field is an | ||
# array, only the first value will be used. | ||
config :field, :validate => :string, :required => true | ||
|
||
# name with full path of external dictionary file. | ||
# format of the table should be a YAML file. | ||
# make sure you encase any integer based keys in quotes. | ||
# For simple string search and replacements for just a few values | ||
# use the gsub function of the mutate filter. | ||
config :dictionary_path, :validate => :string, :required => true | ||
|
||
# The destination you wish to populate with the response code. | ||
# default is http_response_code. set to the same value as source | ||
# if you want to do a substitution. | ||
config :destination, :validate => :string, :default => "translation" | ||
|
||
# set to false if you want to match multiple terms. | ||
# a large dictionary could get expensive if set to false. | ||
config :exact, :validate => :boolean, :default => true | ||
|
||
|
||
|
||
public | ||
def register | ||
if File.exists?(@dictionary_path) | ||
begin | ||
@dictionary = YAML.load_file(@dictionary_path) | ||
rescue Exception => e | ||
raise "Bad Syntax in dictionary file" | ||
end | ||
end # if File.exists? | ||
@logger.info("Dictionary - ", :dictionary => @dictionary) | ||
if @exact | ||
@logger.info("Dictionary translation method - Exact") | ||
else | ||
@logger.info("Dictionary translation method - Fuzzy") | ||
end # if @exact | ||
end # def register | ||
|
||
public | ||
def filter(event) | ||
return unless filter?(event) | ||
begin | ||
source = event[@field] | ||
source = source.first if source.is_a? Array # if array, just use first value | ||
source = source.to_s # make sure its a string. Is this really needed? | ||
if @exact | ||
translation = @dictionary[source] if @dictionary.include?(source) | ||
else | ||
translation = source.gsub(Regexp.union(@dictionary.keys), @dictionary) | ||
end # if @exact | ||
rescue Exception => e | ||
@logger.error("Something went wrong when attempting to translate from dictionary", :exception => e, :field => @field, :event => event) | ||
end | ||
event[@destination] = translation | ||
end # def filter | ||
end # class LogStash::Filters::Translate |