Class: Fluent::Plugin::ElasticsearchGenidFilter

Inherits:
Filter
  • Object
show all
Defined in:
lib/fluent/plugin/filter_elasticsearch_genid.rb

Instance Method Summary collapse

Constructor Details

#initializeElasticsearchGenidFilter

Returns a new instance of ElasticsearchGenidFilter.



18
19
20
# File 'lib/fluent/plugin/filter_elasticsearch_genid.rb', line 18

def initialize
  super
end

Instance Method Details

#configure(conf) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/fluent/plugin/filter_elasticsearch_genid.rb', line 22

def configure(conf)
  super

  if !@use_entire_record
    if @record_keys.empty? && @use_record_as_seed
      raise Fluent::ConfigError, "When using record as hash seed, users must specify `record_keys`."
    end
  end

  if @use_record_as_seed
    class << self
      alias_method :filter, :filter_seed_as_record
    end
  else
    class << self
      alias_method :filter, :filter_simple
    end
  end
end

#encode_hash(type, seed) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/fluent/plugin/filter_elasticsearch_genid.rb', line 64

def encode_hash(type, seed)
  case type
  when :md5
    Digest::MD5.digest(seed)
  when :sha1
    Digest::SHA1.digest(seed)
  when :sha256
    Digest::SHA256.digest(seed)
  when :sha512
    Digest::SHA512.digest(seed)
  end
end

#filter(tag, time, record) ⇒ Object



42
43
44
# File 'lib/fluent/plugin/filter_elasticsearch_genid.rb', line 42

def filter(tag, time, record)
  # for safety.
end

#filter_seed_as_record(tag, time, record) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/fluent/plugin/filter_elasticsearch_genid.rb', line 51

def filter_seed_as_record(tag, time, record)
  seed = ""
  seed += tag + separator if @include_tag_in_seed
  seed += time.to_s + separator if @include_time_in_seed
  if @use_entire_record
    record.keys.sort.each {|k| seed += "|#{k}|#{record[k]}"}
  else
    seed += record_keys.map {|k| record[k]}.join(separator)
  end
  record[@hash_id_key] = Base64.strict_encode64(encode_hash(@hash_type, seed))
  record
end

#filter_simple(tag, time, record) ⇒ Object



46
47
48
49
# File 'lib/fluent/plugin/filter_elasticsearch_genid.rb', line 46

def filter_simple(tag, time, record)
  record[@hash_id_key] = Base64.strict_encode64(SecureRandom.uuid)
  record
end