Class: ElasticAPM::Transport::Connection Private

Inherits:
Object
  • Object
show all
Includes:
Logging
Defined in:
lib/elastic_apm/transport/connection.rb,
lib/elastic_apm/transport/connection/http.rb,
lib/elastic_apm/transport/connection/proxy_pipe.rb

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Defined Under Namespace

Classes: Http, ProxyPipe

Constant Summary

Constants included from Logging

Logging::LEVELS, Logging::PREFIX

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Logging

#debug, #error, #fatal, #info, #warn

Constructor Details

#initialize(config) ⇒ Connection

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

A connection holds an instance ‘http` of an Http::Connection.

The HTTP::Connection itself is not thread safe.

The connection sends write requests and close requests to ‘http`, and has to ensure no write requests are sent after closing `http`.

The connection schedules a separate thread to close an ‘http` connection some time in the future. To avoid the thread interfering with ongoing write requests to `http`, write and close requests have to be synchronized.



38
39
40
41
42
43
44
45
46
47
# File 'lib/elastic_apm/transport/connection.rb', line 38

def initialize(config)
  @config = config
  @metadata = JSON.fast_generate(
    Serializers::MetadataSerializer.new(config).build(
      Metadata.new(config)
    )
  )
  @url = "#{config.server_url}/intake/v2/events"
  @mutex = Mutex.new
end

Instance Attribute Details

#httpObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



49
50
51
# File 'lib/elastic_apm/transport/connection.rb', line 49

def http
  @http
end

Instance Method Details

#flush(reason = :force) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



77
78
79
80
81
82
83
# File 'lib/elastic_apm/transport/connection.rb', line 77

def flush(reason = :force)
  # Could happen from the timertask so we need to sync
  @mutex.synchronize do
    return if http.nil?
    http.close(reason)
  end
end

#inspectObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



85
86
87
88
89
90
# File 'lib/elastic_apm/transport/connection.rb', line 85

def inspect
  format(
    '<%s url:%s closed:%s >',
    super.split.first, @url, http&.closed?
  )
end

#write(str) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/elastic_apm/transport/connection.rb', line 51

def write(str)
  return false if @config.disable_send

  begin
    bytes_written = 0

    # The request might get closed from timertask so let's make sure we
    # hold it open until we've written.
    @mutex.synchronize do
      connect if http.nil? || http.closed?
      bytes_written = http.write(str)
    end

    flush(:api_request_size) if bytes_written >= @config.api_request_size
  rescue IOError => e
    error('Connection error: %s', e.inspect)
    flush(:ioerror)
  rescue Errno::EPIPE => e
    error('Connection error: %s', e.inspect)
    flush(:broken_pipe)
  rescue Exception => e
    error('Connection error: %s', e.inspect)
    flush(:connection_error)
  end
end