Class: LogStash::Filters::Translate

Inherits:
Base
  • Object
show all
Extended by:
PluginMixins::ValidatorSupport::FieldReferenceValidationAdapter
Includes:
PluginMixins::DeprecationLoggerSupport, PluginMixins::Scheduler
Defined in:
lib/logstash/filters/translate.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#lookupObject (readonly)

for testing reloading



180
181
182
# File 'lib/logstash/filters/translate.rb', line 180

def lookup
  @lookup
end

#updaterObject (readonly)

for tests



181
182
183
# File 'lib/logstash/filters/translate.rb', line 181

def updater
  @updater
end

Instance Method Details

#filter(event) ⇒ Object

def register



264
265
266
267
268
269
270
271
# File 'lib/logstash/filters/translate.rb', line 264

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

#registerObject



183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
# File 'lib/logstash/filters/translate.rb', line 183

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, yaml_load_strategy: @yaml_load_strategy)
      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) ⇒ Boolean

def filter

Returns:

  • (Boolean)


273
274
275
# File 'lib/logstash/filters/translate.rb', line 273

def yaml_file?(path)
  /\.y[a]?ml$/.match(path)
end