Class: LogStash::Filters::OpenSearch

Inherits:
Base
  • Object
show all
Defined in:
lib/logstash/filters/opensearch.rb

Constant Summary collapse

DEFAULT_HOST =
::LogStash::Util::SafeURI.new("//localhost:9200")

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#clients_poolObject (readonly)

Returns the value of attribute clients_pool.



77
78
79
# File 'lib/logstash/filters/opensearch.rb', line 77

def clients_pool
  @clients_pool
end

Instance Method Details

#filter(event) ⇒ Object

def register



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/logstash/filters/opensearch.rb', line 100

def filter(event)
  matched = false
  begin
    params = {:index => event.sprintf(@index) }

    if @query_dsl
      query = LogStash::Json.load(event.sprintf(@query_dsl))
      params[:body] = query
    else
      query = event.sprintf(@query)
      params[:q] = query
      params[:size] = result_size
      params[:sort] =  @sort if @enable_sort
    end

    @logger.debug("Querying opensearch for lookup", :params => params)

    results = get_client.search(params)
    raise "OpenSearch query error: #{results["_shards"]["failures"]}" if results["_shards"].include? "failures"

    event.set("[@metadata][total_hits]", extract_total_from_hits(results['hits']))

    resultsHits = results["hits"]["hits"]
    if !resultsHits.nil? && !resultsHits.empty?
      matched = true
      @fields.each do |old_key, new_key|
        old_key_path = extract_path(old_key)
        set = resultsHits.map do |doc|
          extract_value(doc["_source"], old_key_path)
        end
        event.set(new_key, set.count > 1 ? set : set.first)
      end
      @docinfo_fields.each do |old_key, new_key|
        old_key_path = extract_path(old_key)
        set = resultsHits.map do |doc|
          extract_value(doc, old_key_path)
        end
        event.set(new_key, set.count > 1 ? set : set.first)
      end
    end

    resultsAggs = results["aggregations"]
    if !resultsAggs.nil? && !resultsAggs.empty?
      matched = true
      @aggregation_fields.each do |agg_name, ls_field|
        event.set(ls_field, resultsAggs[agg_name])
      end
    end

  rescue => e
    if @logger.trace?
      @logger.warn("Failed to query opensearch for previous event", :index => @index, :query => query, :event => event.to_hash, :error => e.message, :backtrace => e.backtrace)
    elsif @logger.debug?
      @logger.warn("Failed to query opensearch for previous event", :index => @index, :error => e.message, :backtrace => e.backtrace)
    else
      @logger.warn("Failed to query opensearch for previous event", :index => @index, :error => e.message)
    end
    @tag_on_failure.each{|tag| event.tag(tag)}
  else
    filter_matched(event) if matched
  end
end

#registerObject



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/logstash/filters/opensearch.rb', line 79

def register
  @clients_pool = java.util.concurrent.ConcurrentHashMap.new

  #Load query if it exists
  if @query_template
    if File.zero?(@query_template)
      raise "template is empty"
    end
    file = File.open(@query_template, 'r')
    @query_dsl = file.read
  end

  validate_authentication
  fill_user_password_from_cloud_auth
  fill_hosts_from_cloud_id

  @hosts = Array(@hosts).map { |host| host.to_s } # for ES client URI#to_s

  test_connection!
end