Sha256: 4de9c238422b0d6998e95247b26d5574b38bbcb473fe62c76197e4b205cdcfa0

Contents?: true

Size: 1.56 KB

Versions: 3

Compression:

Stored size: 1.56 KB

Contents

# encoding: utf-8
require "logstash/filters/base"
require "logstash/namespace"
require "securerandom"

# The uuid filter allows you to add a `UUID` field to messages.
# This is useful to be able to control the `_id` messages are indexed into Elasticsearch
# with, so that you can insert duplicate messages (i.e. the same message multiple times
# without creating duplicates) - for log pipeline reliability
#
class LogStash::Filters::Uuid < LogStash::Filters::Base
  config_name "uuid"

  # Add a UUID to a field.
  #
  # Example:
  # [source,ruby]
  #     filter {
  #       uuid {
  #         target => "@uuid"
  #       }
  #     }
  config :target, :validate => :string, :required => true

  # If the value in the field currently (if any) should be overridden
  # by the generated UUID. Defaults to `false` (i.e. if the field is
  # present, with ANY value, it won't be overridden)
  #
  # Example:
  # [source,ruby]
  #    filter {
  #       uuid {
  #         target    => "@uuid"
  #         overwrite => true
  #       }
  #    }
  config :overwrite, :validate => :boolean, :default => false

  public
  def register
  end # def register

  public
  def filter(event)
    
    # SecureRandom.uuid returns a non UTF8 string and since
    # only UTF8 strings can be passed to a LogStash::Event
    # we need to reencode it here
    if overwrite
      event[target] = SecureRandom.uuid.force_encoding(Encoding::UTF_8)
    else
      event[target] ||= SecureRandom.uuid.force_encoding(Encoding::UTF_8)
    end

    filter_matched(event)
  end # def filter

end # class LogStash::Filters::Uuid

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
logstash-filter-uuid-2.0.5 lib/logstash/filters/uuid.rb
logstash-filter-uuid-2.0.3 lib/logstash/filters/uuid.rb
logstash-filter-uuid-2.0.2 lib/logstash/filters/uuid.rb