Class: LogStash::Filters::Jdbc::Lookup

Inherits:
Object
  • Object
show all
Includes:
PluginMixins::Jdbc::ValueHandler, Util::Loggable
Defined in:
lib/logstash/filters/jdbc/lookup.rb

Defined Under Namespace

Classes: Getfier, Sprintfier

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from PluginMixins::Jdbc::ValueHandler

#decorate_value, #extract_values_from

Constructor Details

#initialize(options, globals, default_id) ⇒ Lookup

Returns a new instance of Lookup.



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/logstash/filters/jdbc/lookup.rb', line 56

def initialize(options, globals, default_id)
  @id = options["id"] || default_id
  @target = options["target"]
  @id_used_as_target = @target.nil?
  if @id_used_as_target
    # target shouldn't be nil if ecs_compatibility is not :disabled
    if globals[:ecs_compatibility] != :disabled
      logger.info('ECS compatibility is enabled but no ``target`` option was specified, it is recommended'\
                        ' to set the option to avoid potential schema conflicts (if your data is ECS compliant or'\
                        ' non-conflicting feel free to ignore this message)')
    end
    @target = @id
  end
  @options = options
  @globals = globals
  @valid = false
  @option_errors = []
  @default_result = nil
  @prepared_statement = nil
  @symbol_parameters = nil
  parse_options
  @load_method_ref = method(:load_data_from_local)
end

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



54
55
56
# File 'lib/logstash/filters/jdbc/lookup.rb', line 54

def id
  @id
end

#parametersObject (readonly)

Returns the value of attribute parameters.



54
55
56
# File 'lib/logstash/filters/jdbc/lookup.rb', line 54

def parameters
  @parameters
end

#queryObject (readonly)

Returns the value of attribute query.



54
55
56
# File 'lib/logstash/filters/jdbc/lookup.rb', line 54

def query
  @query
end

#targetObject (readonly)

Returns the value of attribute target.



54
55
56
# File 'lib/logstash/filters/jdbc/lookup.rb', line 54

def target
  @target
end

Class Method Details

.find_validation_errors(array_of_options) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/logstash/filters/jdbc/lookup.rb', line 39

def self.find_validation_errors(array_of_options)
  if !array_of_options.is_a?(Array)
    return "The options must be an Array"
    end
  errors = []
  array_of_options.each_with_index do |options, i|
    instance = new(options, {}, "lookup-#{i.next}")
    unless instance.valid?
      errors << instance.formatted_errors
    end
  end
  return nil if errors.empty?
  errors.join("; ")
end

Instance Method Details

#enhance(local, event) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/logstash/filters/jdbc/lookup.rb', line 92

def enhance(local, event)
  result = retrieve_local_data(local, event, &@load_method_ref) # return a LookupResult
  if result.failed? || result.parameters_invalid?
    tag_failure(event)
  end

  if result.valid?
    if @use_default && result.empty?
      tag_default(event)
      process_event(event, @default_result)
    else
      process_event(event, result)
    end
    true
  else
    false
  end
end

#formatted_errorsObject



88
89
90
# File 'lib/logstash/filters/jdbc/lookup.rb', line 88

def formatted_errors
  @option_errors.join(", ")
end

#id_used_as_target?Boolean

Returns:

  • (Boolean)


80
81
82
# File 'lib/logstash/filters/jdbc/lookup.rb', line 80

def id_used_as_target?
  @id_used_as_target
end

#prepare(local) ⇒ Object



115
116
117
118
119
120
121
# File 'lib/logstash/filters/jdbc/lookup.rb', line 115

def prepare(local)
  hash = {}
  @prepared_parameters.each_with_index { |v, i| hash[:"$p#{i}"] = v }
  @prepared_param_placeholder_map = hash
  @prepared_statement = local.prepare(query, hash.keys)
  @load_method_ref = method(:load_data_from_prepared)
end

#use_prepared_statement?Boolean

Returns:

  • (Boolean)


111
112
113
# File 'lib/logstash/filters/jdbc/lookup.rb', line 111

def use_prepared_statement?
  @prepared_parameters && !@prepared_parameters.empty?
end

#valid?Boolean

Returns:

  • (Boolean)


84
85
86
# File 'lib/logstash/filters/jdbc/lookup.rb', line 84

def valid?
  @valid
end