Class: OkComputer::ElasticsearchCheck

Inherits:
Check
  • Object
show all
Defined in:
lib/ok_computer/built_in_checks/elasticsearch_check.rb

Overview

This class performs a health check on an elasticsearch cluster using the cluster health API.

It reports the cluster's name, number of nodes, and status (green, yellow, or red). A cluster status of red is reported as a failure, since this means one or more primary shards are unavailable. Note that the app may still be able to perform some queries on the available indices/shards.

Constant Summary collapse

ConnectionFailed =
Class.new(StandardError)

Constants inherited from Check

Check::CheckNotDefined

Instance Attribute Summary collapse

Attributes inherited from Check

#failure_occurred, #message, #registrant_name

Instance Method Summary collapse

Methods inherited from Check

#clear, #mark_failure, #mark_message, #run, #success?, #to_json, #to_text

Constructor Details

#initialize(host, request_timeout = 5) ⇒ ElasticsearchCheck

Public: Initialize a new elasticsearch check.

host - The hostname of elasticsearch request_timeout - How long to wait to connect before timing out. Defaults to 5 seconds.



17
18
19
20
# File 'lib/ok_computer/built_in_checks/elasticsearch_check.rb', line 17

def initialize(host, request_timeout = 5)
  self.host = host
  self.request_timeout = request_timeout.to_i
end

Instance Attribute Details

#host ⇒ Object

Returns the value of attribute host.



10
11
12
# File 'lib/ok_computer/built_in_checks/elasticsearch_check.rb', line 10

def host
  @host
end

#request_timeout ⇒ Object

Returns the value of attribute request_timeout.



11
12
13
# File 'lib/ok_computer/built_in_checks/elasticsearch_check.rb', line 11

def request_timeout
  @request_timeout
end

Instance Method Details

#check ⇒ Object

Public: Return the status of the elasticsearch cluster



23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/ok_computer/built_in_checks/elasticsearch_check.rb', line 23

def check
  cluster_health = self.cluster_health

  if cluster_health[:status] == 'red'
    mark_failure
  end

  mark_message "Connected to elasticseach cluster '#{cluster_health[:cluster_name]}', #{cluster_health[:number_of_nodes]} nodes, status '#{cluster_health[:status]}'"
rescue ConnectionFailed => e
  mark_failure
  mark_message "Error: '#{e}'"
end

#cluster_health ⇒ Object

Returns a hash from elasticsearch's cluster health API



37
38
39
40
41
42
# File 'lib/ok_computer/built_in_checks/elasticsearch_check.rb', line 37

def cluster_health
  response = timeout(request_timeout) { health_url.read(read_timeout: request_timeout) }
  JSON.parse(response, symbolize_names: true)
rescue => e
  raise ConnectionFailed, e
end

#health_url ⇒ Object



44
45
46
# File 'lib/ok_computer/built_in_checks/elasticsearch_check.rb', line 44

def health_url
  @health_url ||= URI.join(host, '_cluster/health')
end