class LogStash::Filters::Bytes

This filter will parse a given string as a computer storage value (e.g. “123 MB” or “5.3GB”) and add a new numeric field with size in bytes

Constants

DIGIT_GROUP_SEPARATORS
PREFIX_POWERS

Public Instance Methods

filter(event) click to toggle source
# File lib/logstash/filters/bytes.rb, line 63
def filter(event)

  source = event.get(@source)

  if !source or !source.is_a? String
    @tag_on_failure.each{|tag| event.tag(tag)}
    return
  end
  source.strip!

  # Parse the source into the number part (e.g. 123),
  # the unit prefix part (e.g. M), and the unit suffix part (e.g. B)
  match = source.match(/^([0-9#{DIGIT_GROUP_SEPARATORS}#{@decimal_separator}]*)\s*([kKmMgGtTpPeE]?)([bB]?)$/)
  if !match
    @tag_on_failure.each{|tag| event.tag(tag)}
    return
  end

  number, prefix, suffix = match.captures

  # Flag error if more than one decimal separator is found
  num_decimals = number.count(@decimal_separator)
  if num_decimals > 1
    @tag_on_failure.each{|tag| event.tag(tag)}
    return
  end

  number = normalize_number(number.strip)
  if number == ''
    @tag_on_failure.each{|tag| event.tag(tag)}
    return
  end

  if suffix == ''
    suffix = 'B'
  end

  # Convert the number to bytes
  result = number.to_f
  if prefix != ''
    if @conversion_method == 'binary'
      result *= (1024 ** PREFIX_POWERS[prefix.downcase])
    else
      result *= (1000 ** PREFIX_POWERS[prefix.downcase])
    end
  end
  result = result.round

  event.set(@target, result)

  # filter_matched should go in the last line of our successful code
  filter_matched(event)
end
register() click to toggle source
# File lib/logstash/filters/bytes.rb, line 58
def register
  # Add instance variables
end

Private Instance Methods

normalize_number(number) click to toggle source
# File lib/logstash/filters/bytes.rb, line 51
def normalize_number(number)
  number
    .tr("^0-9#{@decimal_separator}", '')
    .tr(@decimal_separator, '.')
end