class Airbrake::Sender

Sends out the notice to Airbrake

Constants

HEADERS
HTTP_ERRORS
JSON_API_URI
NOTICES_URI

Attributes

api_key[R]
host[R]
http_open_timeout[R]
http_read_timeout[R]
port[R]
project_id[R]
protocol[R]
proxy_host[R]
proxy_pass[R]
proxy_port[R]
proxy_user[R]
secure[R]
secure?[R]
use_system_ssl_cert_chain[R]
use_system_ssl_cert_chain?[R]

Public Class Methods

new(options = {}) click to toggle source
# File lib/airbrake/sender.rb, line 26
def initialize(options = {})
  [ :proxy_host,
    :proxy_port,
    :proxy_user,
    :proxy_pass,
    :protocol,
    :host,
    :port,
    :secure,
    :use_system_ssl_cert_chain,
    :http_open_timeout,
    :http_read_timeout,
    :project_id,
    :api_key
  ].each do |option|
    instance_variable_set("@#{option}", options[option])
  end
end

Public Instance Methods

send_to_airbrake(notice) click to toggle source

Sends the notice data off to Airbrake for processing.

@param [Notice or String] notice The notice to be sent off

# File lib/airbrake/sender.rb, line 49
def send_to_airbrake(notice)
  data = prepare_notice(notice)
  http = setup_http_connection

  response = begin
               http.post(request_uri,
                         data,
                         headers)
             rescue *HTTP_ERRORS => e
               log :level => :error,
                   :message => "Unable to contact the Airbrake server. HTTP Error=#{e}"
               nil
             end

  case response
  when Net::HTTPSuccess then
    log :level => :info,
        :message => "Success: #{response.class}",
        :response => response
  else
    log :level => :error,
        :message => "Failure: #{response.class}",
        :response => response,
        :notice => notice
  end

  if response && response.respond_to?(:body)
    error_id = response.body.match(%r{<id[^>]*>(.*?)</id>})
    error_id[1] if error_id
  end
rescue => e
  log :level => :error,
    :message => "[Airbrake::Sender#send_to_airbrake] Cannot send notification. Error: #{e.class}" +
    " - #{e.message}\nBacktrace:\n#{e.backtrace.join("\n\t")}"

  nil
end

Private Instance Methods

api_url() click to toggle source
# File lib/airbrake/sender.rb, line 119
def api_url
  if json_api_enabled?
    "#{JSON_API_URI}/#{project_id}/notices?key=#{api_key}"
  else
    NOTICES_URI
  end
end
headers() click to toggle source
# File lib/airbrake/sender.rb, line 127
def headers
  if json_api_enabled?
    HEADERS[:json]
  else
    HEADERS[:xml]
  end
end
json_api_enabled?() click to toggle source
# File lib/airbrake/sender.rb, line 175
def json_api_enabled?
  !!(host =~ /collect.airbrake.io/) &&
    project_id =~ /\S/
end
log(opts = {}) click to toggle source
# File lib/airbrake/sender.rb, line 139
def log(opts = {})
  (opts[:logger] || logger).send(opts[:level], LOG_PREFIX + opts[:message])
  Airbrake.report_environment_info
  Airbrake.report_response_body(opts[:response].body) if opts[:response] && opts[:response].respond_to?(:body)
  Airbrake.report_notice(opts[:notice]) if opts[:notice]
end
logger() click to toggle source
# File lib/airbrake/sender.rb, line 146
def logger
  Airbrake.logger
end
prepare_notice(notice) click to toggle source
# File lib/airbrake/sender.rb, line 106
def prepare_notice(notice)
  if json_api_enabled?
    begin
      JSON.parse(notice)
      notice
    rescue
      notice.to_json
    end
  else
    notice.respond_to?(:to_xml) ? notice.to_xml : notice
  end
end
request_uri() click to toggle source

Use request_uri to keep the query since it contains the API_KEY.

0

ruby-doc.org/stdlib-2.2.2/libdoc/uri/rdoc/URI/HTTP.html#method-i-request_uri

# File lib/airbrake/sender.rb, line 183
def request_uri
  url.respond_to?(:request_uri) ? url.request_uri : url
end
setup_http_connection() click to toggle source
# File lib/airbrake/sender.rb, line 150
def setup_http_connection
  http =
    Net::HTTP::Proxy(proxy_host, proxy_port, proxy_user, proxy_pass).
    new(url.host, url.port)

  http.read_timeout = http_read_timeout
  http.open_timeout = http_open_timeout

  if secure?
    http.use_ssl     = true

    http.ca_file      = Airbrake.configuration.ca_bundle_path
    http.verify_mode  = OpenSSL::SSL::VERIFY_PEER
  else
    http.use_ssl     = false
  end

  http
rescue => e
  log :level => :error,
      :message => "[Airbrake::Sender#setup_http_connection] Failure initializing the HTTP connection.\n" +
                  "Error: #{e.class} - #{e.message}\nBacktrace:\n#{e.backtrace.join("\n\t")}"
  raise e
end
url() click to toggle source
# File lib/airbrake/sender.rb, line 135
def url
  URI.parse("#{protocol}://#{host}:#{port}").merge(api_url)
end