Class: LogStash::Outputs::ElasticSearch::HttpClient

Inherits:
Object
  • Object
show all
Defined in:
lib/logstash/outputs/elasticsearch/http_client.rb,
lib/logstash/outputs/elasticsearch/http_client/pool.rb,
lib/logstash/outputs/elasticsearch/http_client/manticore_adapter.rb

Defined Under Namespace

Classes: ManticoreAdapter, Pool

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ HttpClient

This is here in case we use DEFAULT_OPTIONS in the future DEFAULT_OPTIONS =

:setting => value



15
16
17
18
19
20
21
22
23
# File 'lib/logstash/outputs/elasticsearch/http_client.rb', line 15

def initialize(options={})
  @logger = options[:logger]
  # Again, in case we use DEFAULT_OPTIONS in the future, uncomment this.
  # @options = DEFAULT_OPTIONS.merge(options)
  @options = options
  @pool = build_pool(@options)
  # mutex to prevent requests and sniffing to access the
  # connection pool at the same time
end

Instance Attribute Details

#action_countObject (readonly)

Returns the value of attribute action_count.



9
10
11
# File 'lib/logstash/outputs/elasticsearch/http_client.rb', line 9

def action_count
  @action_count
end

#clientObject (readonly)

Returns the value of attribute client.



9
10
11
# File 'lib/logstash/outputs/elasticsearch/http_client.rb', line 9

def client
  @client
end

#loggerObject (readonly)

Returns the value of attribute logger.



9
10
11
# File 'lib/logstash/outputs/elasticsearch/http_client.rb', line 9

def logger
  @logger
end

#optionsObject (readonly)

Returns the value of attribute options.



9
10
11
# File 'lib/logstash/outputs/elasticsearch/http_client.rb', line 9

def options
  @options
end

#poolObject (readonly)

Returns the value of attribute pool.



9
10
11
# File 'lib/logstash/outputs/elasticsearch/http_client.rb', line 9

def pool
  @pool
end

#recv_countObject (readonly)

Returns the value of attribute recv_count.



9
10
11
# File 'lib/logstash/outputs/elasticsearch/http_client.rb', line 9

def recv_count
  @recv_count
end

Instance Method Details

#bulk(actions) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/logstash/outputs/elasticsearch/http_client.rb', line 38

def bulk(actions)
  @action_count ||= 0
  @action_count += actions.size
  
  return if actions.empty?
  bulk_body = actions.collect do |action, args, source|
    args, source = update_action_builder(args, source) if action == 'update'

    if source && action != 'delete'
      next [ { action => args }, source ]
    else
      next { action => args }
    end
  end.
  flatten.
  reduce("") do |acc,line|
    acc << LogStash::Json.dump(line)
    acc << "\n"
  end

  # Discard the URL
  url, response = @pool.post("_bulk", nil, bulk_body)
  LogStash::Json.load(response.body)
end

#closeObject



63
64
65
# File 'lib/logstash/outputs/elasticsearch/http_client.rb', line 63

def close
  @pool.close
end

#get_versionObject



33
34
35
36
# File 'lib/logstash/outputs/elasticsearch/http_client.rb', line 33

def get_version
  url, response = @pool.get("")
  LogStash::Json.load(response.body)["version"]
end

#template_install(name, template, force = false) ⇒ Object



25
26
27
28
29
30
31
# File 'lib/logstash/outputs/elasticsearch/http_client.rb', line 25

def template_install(name, template, force=false)
  if template_exists?(name) && !force
    @logger.debug("Found existing Elasticsearch template. Skipping template management", :name => name)
    return
  end
  template_put(name, template)
end