Class: LogStash::Filters::Jdbc::LookupProcessor

Inherits:
Object
  • Object
show all
Defined in:
lib/logstash/filters/jdbc/lookup_processor.rb

Constant Summary collapse

CONNECTION_ERROR_MSG =
"Connection error when initialising lookup (local) db"
DISCONNECTION_ERROR_MSG =
"Connection error when disconnecting from lookup (local) db"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(lookups_array, globals) ⇒ LookupProcessor

Returns a new instance of LookupProcessor.



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/logstash/filters/jdbc/lookup_processor.rb', line 30

def initialize(lookups_array, globals)
  @lookups = lookups_array.map.with_index do |options, i|
    Lookup.new(options, globals, "lookup-#{i.next}")
  end
  @lookups_errors = validate_lookups
  if @lookups_errors.empty? && !globals.empty?
    @local = ReadWriteDatabase.create(*globals.values_at(
      "lookup_jdbc_connection_string",
      "lookup_jdbc_driver_class",
      "lookup_jdbc_driver_library").compact)
    @local.connect(CONNECTION_ERROR_MSG)

    create_prepared_statements_for_lookups
  end
end

Instance Attribute Details

#localObject (readonly)

Returns the value of attribute local.



7
8
9
# File 'lib/logstash/filters/jdbc/lookup_processor.rb', line 7

def local
  @local
end

#lookupsObject (readonly)

Returns the value of attribute lookups.



7
8
9
# File 'lib/logstash/filters/jdbc/lookup_processor.rb', line 7

def lookups
  @lookups
end

Class Method Details

.find_validation_errors(array_of_options) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/logstash/filters/jdbc/lookup_processor.rb', line 12

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

Instance Method Details

#closeObject



50
51
52
53
# File 'lib/logstash/filters/jdbc/lookup_processor.rb', line 50

def close
  @local.disconnect(DISCONNECTION_ERROR_MSG)
  @local = nil
end

#enhance(event) ⇒ Object



46
47
48
# File 'lib/logstash/filters/jdbc/lookup_processor.rb', line 46

def enhance(event)
  @lookups.map { |lookup| lookup.enhance(@local, event) }
end

#formatted_errorsObject



55
56
57
# File 'lib/logstash/filters/jdbc/lookup_processor.rb', line 55

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

#valid?Boolean

Returns:

  • (Boolean)


59
60
61
# File 'lib/logstash/filters/jdbc/lookup_processor.rb', line 59

def valid?
  @lookups_errors.empty?
end