Class: Elastic::Transport::Client
- Inherits:
-
Object
- Object
- Elastic::Transport::Client
- Includes:
- MetaHeader
- Defined in:
- lib/elastic/transport/client.rb
Overview
Handles communication with an Elastic cluster.
See README for usage and code examples.
Constant Summary collapse
- DEFAULT_TRANSPORT_CLASS =
Transport::HTTP::Faraday
- DEFAULT_LOGGER =
lambda do require 'logger' logger = Logger.new(STDERR) logger.progname = 'elastic' logger.formatter = proc { |severity, datetime, progname, msg| "#{datetime}: #{msg}\n" } logger end
- DEFAULT_TRACER =
lambda do require 'logger' logger = Logger.new(STDERR) logger.progname = 'elastic.tracer' logger.formatter = proc { |severity, datetime, progname, msg| "#{msg}\n" } logger end
- DEFAULT_HOST =
The default host and port to use if not otherwise specified.
'localhost:9200'.freeze
- DEFAULT_PORT =
The default port to use if not otherwise specified.
9200
Instance Attribute Summary collapse
-
#transport ⇒ Object
Returns the transport object.
Instance Method Summary collapse
-
#initialize(arguments = {}) {|faraday| ... } ⇒ Client
constructor
Create a client connected to an Elastic cluster.
-
#perform_request(method, path, params = {}, body = nil, headers = nil, opts = {}) ⇒ Object
Performs a request through delegation to #transport.
Methods included from MetaHeader
#called_from?, #client_meta_version, #elasticsearch?, #enterprise_search?, #meta_header_adapter, #meta_header_engine, #meta_header_service_version, #serverless?, #set_meta_header
Constructor Details
#initialize(arguments = {}) {|faraday| ... } ⇒ Client
Create a client connected to an Elastic cluster.
Specify the URL via arguments or set the ‘ELASTICSEARCH_URL` environment variable.
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 162 163 164 165 166 167 168 169 170 171 |
# File 'lib/elastic/transport/client.rb', line 124 def initialize(arguments = {}, &block) @arguments = arguments.transform_keys(&:to_sym) @arguments[:logger] ||= @arguments[:log] ? DEFAULT_LOGGER.call() : nil @arguments[:tracer] ||= @arguments[:trace] ? DEFAULT_TRACER.call() : nil @arguments[:reload_connections] ||= false @arguments[:retry_on_failure] ||= false @arguments[:delay_on_retry] ||= 0 @arguments[:reload_on_failure] ||= false @arguments[:randomize_hosts] ||= false @arguments[:transport_options] ||= {} @arguments[:http] ||= {} @arguments[:enable_meta_header] = arguments.fetch(:enable_meta_header, true) @hosts ||= __extract_hosts(@arguments[:hosts] || @arguments[:host] || @arguments[:url] || @arguments[:urls] || ENV['ELASTICSEARCH_URL'] || DEFAULT_HOST) @send_get_body_as = @arguments[:send_get_body_as] || 'GET' @ca_fingerprint = @arguments.delete(:ca_fingerprint) if @arguments[:request_timeout] @arguments[:transport_options][:request] = { timeout: @arguments[:request_timeout] } end if @arguments[:transport] @transport = @arguments[:transport] else @transport_class = @arguments[:transport_class] || DEFAULT_TRANSPORT_CLASS @transport = if @transport_class == Transport::HTTP::Faraday @arguments[:adapter] ||= __auto_detect_adapter # from include MetaHeader @transport_class.new(hosts: @hosts, options: @arguments) do |faraday| faraday.adapter(@arguments[:adapter]) block&.call faraday end else # from include MetaHeader @transport_class.new(hosts: @hosts, options: @arguments) end end if defined?(::OpenTelemetry) && ENV[OpenTelemetry::ENV_VARIABLE_ENABLED] != 'false' @otel = OpenTelemetry.new(@arguments) end end |
Instance Attribute Details
#transport ⇒ Object
Returns the transport object.
61 62 63 |
# File 'lib/elastic/transport/client.rb', line 61 def transport @transport end |
Instance Method Details
#perform_request(method, path, params = {}, body = nil, headers = nil, opts = {}) ⇒ Object
Performs a request through delegation to #transport.
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 |
# File 'lib/elastic/transport/client.rb', line 175 def perform_request(method, path, params = {}, body = nil, headers = nil, opts = {}) method = @send_get_body_as if method == 'GET' && body validate_ca_fingerprints if @ca_fingerprint if @otel # If no endpoint is specified in the opts, use the HTTP method name span_name = opts[:endpoint] || method @otel.tracer.in_span(span_name) do |span| span['http.request.method'] = method span['db.system'] = 'elasticsearch' opts[:defined_params]&.each do |k, v| if v.respond_to?(:join) span["db.elasticsearch.path_parts.#{k}"] = v.join(',') else span["db.elasticsearch.path_parts.#{k}"] = v end end if body_as_json = @otel.process_body(body, opts[:endpoint]) span['db.statement'] = body_as_json end span['db.operation'] = opts[:endpoint] if opts[:endpoint] transport.perform_request(method, path, params || {}, body, headers) end else transport.perform_request(method, path, params || {}, body, headers) end end |