- Allow type coercion of named captures from grok.

Syntax: %{FOO:bar:type}
  Type can be 'int' or 'float' - otherwise is assumed string.
  The result is that the value captured is coerced to the requested type
  in the logstash event for later processing.

  Note: this required a change in grok upstream.
  minimum grok version required now: grok 1.20110223.1

  http://code.google.com/p/logstash/issues/detail?id=45
This commit is contained in:
Jordan Sissel 2011-02-24 00:02:46 -08:00
parent af2e249d7a
commit c124238ad7

View file

@ -52,9 +52,21 @@ class LogStash::Filters::Grok < LogStash::Filters::Base
if match if match
match.each_capture do |key, value| match.each_capture do |key, value|
match_type = nil
if key.include?(":") if key.include?(":")
key = key.split(":")[1] name, key, match_type = key.split(":")
end end
# http://code.google.com/p/logstash/issues/detail?id=45
# Permit typing of captures by giving an additional colon and a type,
# like: %{FOO:name:int} for int coercion.
case match_type
when "int"
value = value.to_i
when "float"
value = value.to_f
end
if event.message == value if event.message == value
# Skip patterns that match the entire line # Skip patterns that match the entire line
@logger.debug("Skipping capture '#{key}' since it matches the whole line.") @logger.debug("Skipping capture '#{key}' since it matches the whole line.")
@ -67,7 +79,9 @@ class LogStash::Filters::Grok < LogStash::Filters::Base
event.fields[key] = [] event.fields[key] = []
end end
if value && !value.empty? # If value is not nil, or responds to empty and is not empty, add the
# value to the event.
if !value.nil? && (!value.empty? rescue true)
event.fields[key] << value event.fields[key] << value
end end
end end