class ElasticAPM::Transport::Connection::Http

@api private

Public Class Methods

new(config, headers: nil) click to toggle source
# File lib/elastic_apm/transport/connection/http.rb, line 29
def initialize(config, headers: nil)
  @config = config
  @headers = headers || Headers.new(config)
  @client = build_client
  @closed = Concurrent::AtomicBoolean.new(true)
end
open(config, url) click to toggle source
# File lib/elastic_apm/transport/connection/http.rb, line 42
def self.open(config, url)
  new(config).tap do |http|
    http.open(url)
  end
end

Public Instance Methods

close(reason) click to toggle source
# File lib/elastic_apm/transport/connection/http.rb, line 71
def close(reason)
  return if closed?

  debug '%s: Closing request with reason %s', thread_str, reason
  @closed.make_true

  @wr&.close

  if @request&.join(5)
    @rd&.close
    return
  end

  @rd&.close

  return if @request.nil?

  error(
    '%s: APM Server not responding in time, terminating request',
    thread_str
  )
  @request.kill
end
closed?() click to toggle source
# File lib/elastic_apm/transport/connection/http.rb, line 95
def closed?
  @rd.closed? && @closed.true?
end
get(url, headers: nil) click to toggle source
# File lib/elastic_apm/transport/connection/http.rb, line 52
def get(url, headers: nil)
  request(:get, url, headers: headers)
end
inspect() click to toggle source
Calls superclass method
# File lib/elastic_apm/transport/connection/http.rb, line 99
def inspect
  format(
    '%s closed: %s>',
    super.split.first,
    closed?
  )
end
open(url) click to toggle source
# File lib/elastic_apm/transport/connection/http.rb, line 36
def open(url)
  @closed.make_false
  @rd, @wr = ProxyPipe.pipe(compress: @config.http_compression?)
  @request = open_request_in_thread(url)
end
post(url, body: nil, headers: nil) click to toggle source
# File lib/elastic_apm/transport/connection/http.rb, line 48
def post(url, body: nil, headers: nil)
  request(:post, url, body: body, headers: headers)
end
request(method, url, body: nil, headers: nil) click to toggle source
# File lib/elastic_apm/transport/connection/http.rb, line 56
def request(method, url, body: nil, headers: nil)
  @client.send(
    method,
    url,
    body: body,
    headers: (headers ? @headers.merge(headers) : @headers).to_h,
    ssl_context: @config.ssl_context
  ).flush
end
write(str) click to toggle source
# File lib/elastic_apm/transport/connection/http.rb, line 66
def write(str)
  @wr.write(str)
  @wr.bytes_sent
end

Private Instance Methods

build_client() click to toggle source
# File lib/elastic_apm/transport/connection/http.rb, line 132
def build_client
  client = HTTP.headers(@headers)
  return client unless @config.proxy_address && @config.proxy_port

  client.via(
    @config.proxy_address,
    @config.proxy_port,
    @config.proxy_username,
    @config.proxy_password,
    @config.proxy_headers
  )
end
open_request_in_thread(url) click to toggle source
# File lib/elastic_apm/transport/connection/http.rb, line 113
def open_request_in_thread(url)
  debug '%s: Opening new request', thread_str
  Thread.new do
    begin
      resp = post(url, body: @rd, headers: @headers.chunked.to_h)

      if resp&.status == 202
        debug 'APM Server responded with status 202'
      elsif resp
        error "APM Server responded with an error:\n%p", resp.body.to_s
      end
    rescue Exception => e
      error(
        "Couldn't establish connection to APM Server:\n%p", e.inspect
      )
    end
  end
end
thread_str() click to toggle source
# File lib/elastic_apm/transport/connection/http.rb, line 109
def thread_str
  format('[THREAD:%s]', Thread.current.object_id)
end