class LogStash::Filters::Translate

Attributes

lookup[R]
updater[R]

Public Instance Methods

filter(event) click to toggle source
# File lib/logstash/filters/translate.rb, line 260
def filter(event)
  return unless @updater.test_for_inclusion(event, @override)
  begin
    filter_matched(event) if @updater.update(event) || @source == @target
  rescue => e
    @logger.error("Something went wrong when attempting to translate from dictionary", :exception => e, :source => @source, :event => event.to_hash)
  end
end
register() click to toggle source
# File lib/logstash/filters/translate.rb, line 179
def register
  if @dictionary_path && !@dictionary.empty?
    raise LogStash::ConfigurationError, I18n.t(
      "logstash.agent.configuration.invalid_plugin_register",
      :plugin => "filter",
      :type => "translate",
      :error => "The configuration options 'dictionary' and 'dictionary_path' are mutually exclusive"
    )
  end

  # check and set yaml code point limit
  # set lookup dictionary
  if @dictionary_path
    if yaml_file?(@dictionary_path)
      @yaml_dictionary_code_point_limit ||= 134_217_728

      if @yaml_dictionary_code_point_limit <= 0
        raise LogStash::ConfigurationError, "Please set a positive number in `yaml_dictionary_code_point_limit => #{@yaml_dictionary_code_point_limit}`."
      else
        @lookup = Dictionary::File.create(@dictionary_path, @refresh_interval, @refresh_behaviour, @exact, @regex, yaml_code_point_limit: @yaml_dictionary_code_point_limit)
      end
    elsif @yaml_dictionary_code_point_limit != nil
      raise LogStash::ConfigurationError, "Please remove `yaml_dictionary_code_point_limit` for dictionary file in JSON or CSV format"
    else
      @lookup = Dictionary::File.create(@dictionary_path, @refresh_interval, @refresh_behaviour, @exact, @regex)
    end
  else
    @lookup = Dictionary::Memory.new(@dictionary, @exact, @regex)
  end

  if @field
    if @source
      raise LogStash::ConfigurationError, "Please remove `field => #{@field.inspect}` and only set the `source => ...` option instead"
    else
      deprecation_logger.deprecated("`field` option is deprecated; use `source` instead.")
      logger.debug("intercepting `field` to populate `source`: `#{@field}`")
      @source = @field
    end
  end
  unless @source
    raise LogStash::ConfigurationError, "No source field specified, please provide the `source => ...` option"
  end

  if @destination
    if @target
      raise LogStash::ConfigurationError, "Please remove `destination => #{@destination.inspect}` and only set the `target => ...` option instead"
    else
      deprecation_logger.deprecated("`destination` option is deprecated; use `target` instead.")
      logger.debug("intercepting `destination` to populate `target`: `#{@destination}`")
      @target = @destination
    end
  end
  @target ||= ecs_select[disabled: 'translation', v1: @source]

  if @source == @target
    @override = true if @override.nil?
    if @override.eql?(false)
      raise LogStash::ConfigurationError, "Configuring `override => false` with in-place translation has no effect, please remove the option"
    end
  end

  if @iterate_on.nil?
    @updater = SingleValueUpdate.new(@source, @target, @fallback, @lookup)
  elsif @iterate_on == @source
    @updater = ArrayOfValuesUpdate.new(@iterate_on, @target, @fallback, @lookup)
  else
    @updater = ArrayOfMapsValueUpdate.new(@iterate_on, @source, @target, @fallback, @lookup)
  end

  @logger.debug? && @logger.debug("#{self.class.name}: Dictionary - ", :dictionary => @lookup.dictionary)
  if @exact
    @logger.debug? && @logger.debug("#{self.class.name}: Dictionary translation method - Exact")
  else
    @logger.debug? && @logger.debug("#{self.class.name}: Dictionary translation method - Fuzzy")
  end

  if @lookup.respond_to?(:reload_dictionary) && @refresh_interval > 0 # a scheduler interval of zero makes no sense
    scheduler.interval("#{@refresh_interval}s", overlap: false) { @lookup.reload_dictionary }
  end
end
yaml_file?(path) click to toggle source
# File lib/logstash/filters/translate.rb, line 269
def yaml_file?(path)
  /\.y[a]?ml$/.match(path)
end