Class: PlainApm::Transport

Inherits:
Object
  • Object
show all
Defined in:
lib/plain_apm/transport.rb

Constant Summary collapse

HTTP_READ_TIMEOUT_SECONDS =

TODO: tune these.

8
HTTP_WRITE_TIMEOUT_SECONDS =

default is 60

8
HTTP_OPEN_TIMEOUT_SECONDS =

default is 60

8
HTTP_TIMEOUTS =

default is 60

[
  Net::OpenTimeout,
  Net::ReadTimeout,
  Net::WriteTimeout
].compact.freeze
ERRNO_ERRORS =
[
  Errno::ECONNABORTED,
  Errno::ECONNREFUSED,
  Errno::ECONNRESET,
  Errno::EHOSTDOWN,
  Errno::EHOSTUNREACH,
  Errno::EINVAL,
  Errno::ENETUNREACH,
  Errno::EPIPE
].freeze

Instance Method Summary collapse

Constructor Details

#initialize(endpoint:, app_key:, http_client: Net::HTTP) ⇒ Transport

Returns a new instance of Transport.

Parameters:

  • endpoint (String)

    http URL to send the event to

  • app_key (String)

    api key / token identifying this app

  • http_client (Net::HTTP) (defaults to: Net::HTTP)

    for dependency injection in tests



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/plain_apm/transport.rb', line 38

def initialize(endpoint:, app_key:, http_client: Net::HTTP)
  @uri = URI.parse(endpoint)
  @app_key = app_key
  @http = http_client.new(uri.host, uri.port)

  # Forgotten /, e.g. https://example.com:3000
  uri.path = "/" if uri.path.nil? || uri.path.empty?

  # TODO: our own CA bundle?
  http.use_ssl = uri.scheme == "https"

  http.open_timeout = HTTP_OPEN_TIMEOUT_SECONDS
  http.read_timeout = HTTP_READ_TIMEOUT_SECONDS
  http.write_timeout = HTTP_WRITE_TIMEOUT_SECONDS

  at_exit { shutdown }
end

Instance Method Details

#deliver(data, meta = {}) ⇒ Array

Performs the actual HTTP request.

Parameters:

  • data (String)

    serialized payload to POST

Returns:

  • (Array)
    response, error, retriable


61
62
63
64
65
# File 'lib/plain_apm/transport.rb', line 61

def deliver(data, meta = {})
  http_response do
    http_request(http, uri.path, data, meta)
  end
end