class Opbeat::Client

Constants

USER_AGENT

Attributes

configuration[RW]
state[RW]

Public Class Methods

new(conf) click to toggle source
# File lib/opbeat/client.rb, line 48
def initialize(conf)
  raise Error.new('No server specified') unless conf.server
  raise Error.new('No secret token specified') unless conf.secret_token
  raise Error.new('No organization ID specified') unless conf.organization_id
  raise Error.new('No app ID specified') unless conf.app_id

  @configuration = conf
  @state = ClientState.new conf
  @filter = Filter.new conf.filter_parameters
  @base_url = "#{conf.server}/api/v1/organizations/#{conf.organization_id}/apps/#{conf.app_id}"
  @auth_header = 'Bearer ' + conf.secret_token
end

Public Instance Methods

conn() click to toggle source
# File lib/opbeat/client.rb, line 61
def conn
  @conn ||= Faraday.new(@base_url) do |faraday|
    Opbeat.logger.debug "Initializing connection to #{self.configuration.server}"
    faraday.adapter Faraday.default_adapter
    faraday.ssl[:verify] = self.configuration.ssl_verification
    faraday.options[:timeout] = self.configuration.timeout if self.configuration.timeout
    faraday.options[:open_timeout] = self.configuration.open_timeout if self.configuration.open_timeout
  end
end
encode(event) click to toggle source
# File lib/opbeat/client.rb, line 71
def encode(event)
  event_hash = event.to_hash
  event_hash = @filter.process_event_hash(event_hash)
  return MultiJson.encode(event_hash)
end
send(url_postfix, message) click to toggle source
# File lib/opbeat/client.rb, line 77
def send(url_postfix, message)
  begin
    response = self.conn.post @base_url + url_postfix do |req|
      req.body = self.encode(message)
      req.headers['Authorization'] = @auth_header
      req.headers['Content-Type'] = 'application/json'
      req.headers['Content-Length'] = req.body.bytesize.to_s
      req.headers['User-Agent'] = USER_AGENT
    end
    unless response.status.between?(200, 299)
      raise Error.new("Error from Opbeat server (#{response.status}): #{response.body}")
    end
  rescue
    @state.set_fail
    raise
  end

  @state.set_success
  response
end
send_event(event) click to toggle source
# File lib/opbeat/client.rb, line 98
def send_event(event)
  return unless configuration.send_in_current_environment?
  unless state.should_try?
    Opbeat.logger.info "Temporarily skipping sending to Opbeat due to previous failure."
    return
  end

  # Set the organization ID correctly
  event.organization = self.configuration.organization_id
  event.app = self.configuration.app_id
  Opbeat.logger.debug "Sending event to Opbeat"
  response = send("/errors/", event)
  if response.status.between?(200, 299)
    Opbeat.logger.info "Event logged successfully at " + response.headers["location"].to_s
  end
  response
end
send_release(release) click to toggle source
# File lib/opbeat/client.rb, line 116
def send_release(release)
  Opbeat.logger.debug "Sending release to Opbeat"
  send("/releases/", release)
end