Module: Faulty::Patch::Elasticsearch

Includes:
Base
Included in:
Elasticsearch::Transport::Client, OpenSearch::Transport::Client
Defined in:
lib/faulty/patch/elasticsearch.rb

Overview

Patch Elasticsearch to run requests in a circuit

This module is not required by default

Pass a :faulty key into your Elasticsearch client options to enable circuit protection. See circuit_from_hash for the available options.

By default, all circuit errors raised by this patch inherit from ::Elasticsearch::Transport::Transport::Error. One side effect of the way this patch wraps errors is that host_unreachable_exceptions raised by the inner transport adapters are converted into Elasticsearch::Transport::Transport::Error instead of the transport error type such as Faraday::ConnectionFailed.

Examples:

require 'faulty/patch/elasticsearch'

es = Elasticsearch::Client.new(url: 'http://localhost:9200', faulty: {})
es.search(q: 'test') # raises Faulty::CircuitError if connection fails

# If the faulty key is not given, no circuit is used
es = Elasticsearch::Client.new(url: 'http://localhost:9200', faulty: {})
es.search(q: 'test') # not protected by a circuit

# With Searchkick
Searchkick.client_options[:faulty] = {}

See Also:

Defined Under Namespace

Modules: Error, Errors, ServerError, SnifferTimeoutError

Constant Summary collapse

PATCHED_MODULE =
if Gem.loaded_specs['opensearch-ruby']
  require 'opensearch'
  ::OpenSearch
else
  require 'elasticsearch'
  ::Elasticsearch
end

Instance Method Summary collapse

Methods included from Base

#faulty_run

Instance Method Details

#initialize(arguments = {}, &block) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/faulty/patch/elasticsearch.rb', line 72

def initialize(arguments = {}, &block)
  super

  errors = [PATCHED_MODULE::Transport::Transport::Error]
  errors.concat(@transport.host_unreachable_exceptions)

  @faulty_circuit = Patch.circuit_from_hash(
    'elasticsearch',
    arguments[:faulty],
    errors: errors,
    exclude: PATCHED_MODULE::Transport::Transport::Errors::NotFound,
    patched_error_mapper: ERROR_MAPPER
  )
end

#perform_request(*args) ⇒ Object

Protect all elasticsearch requests



88
89
90
# File 'lib/faulty/patch/elasticsearch.rb', line 88

def perform_request(*args)
  faulty_run { super }
end