Class: OkComputer::ElasticsearchCheck

Inherits:
HttpCheck 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

Constants inherited from HttpCheck

HttpCheck::ConnectionFailed

Constants inherited from Check

Check::CheckNotDefined

Instance Attribute Summary collapse

Attributes inherited from HttpCheck

#basic_auth_password, #basic_auth_username, #request_timeout, #url

Attributes inherited from Check

#failure_occurred, #message, #registrant_name, #time

Instance Method Summary collapse

Methods inherited from HttpCheck

#basic_auth_options, #parse_url, #perform_request

Methods inherited from Check

#<=>, #clear, #mark_failure, #mark_message, #run, #success?, #to_json, #to_text, #with_benchmarking

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.



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

def initialize(host, request_timeout = 5)
  @host = URI(host)
  super("#{host}/_cluster/health", request_timeout)
end

Instance Attribute Details

#hostObject (readonly)

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

Instance Method Details

#checkObject

Public: Return the status of the elasticsearch cluster



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

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 => e
  mark_failure
  mark_message "Error: '#{e}'"
end

#cluster_healthObject

Returns a hash from elasticsearch’s cluster health API



36
37
38
39
# File 'lib/ok_computer/built_in_checks/elasticsearch_check.rb', line 36

def cluster_health
  response = perform_request
  JSON.parse(response, symbolize_names: true)
end