class Raven::Client

Encodes events and sends them to the Sentry server.

Constants

CONTENT_TYPE
PROTOCOL_VERSION
USER_AGENT

Attributes

configuration[RW]

Public Class Methods

new(configuration) click to toggle source
# File lib/raven/client.rb, line 14
def initialize(configuration)
  @configuration = configuration
  @processors = configuration.processors.map { |v| v.new(self) }
  @state = ClientState.new
end

Public Instance Methods

send_event(event) click to toggle source
# File lib/raven/client.rb, line 20
def send_event(event)
  return false unless configuration.sending_allowed?(event)

  # Convert to hash
  event = event.to_hash

  unless @state.should_try?
    failed_send(nil, event)
    return
  end

  configuration.logger.info "Sending event #{event[:event_id]} to Sentry"

  content_type, encoded_data = encode(event)

  begin
    transport.send_event(generate_auth_header, encoded_data,
                         :content_type => content_type)
    successful_send
  rescue => e
    failed_send(e, event)
    return
  end

  event
end
transport() click to toggle source
# File lib/raven/client.rb, line 47
def transport
  @transport ||=
    case configuration.scheme
    when 'http', 'https'
      Transports::HTTP.new(configuration)
    when 'dummy'
      Transports::Dummy.new(configuration)
    else
      fail "Unknown transport scheme '#{configuration.scheme}'"
    end
end

Private Instance Methods

encode(event) click to toggle source
# File lib/raven/client.rb, line 61
def encode(event)
  hash = @processors.reduce(event.to_hash) { |a, e| e.process(a) }
  encoded = JSON.fast_generate(hash)

  case configuration.encoding
  when 'gzip'
    ['application/octet-stream', Base64.strict_encode64(Zlib::Deflate.deflate(encoded))]
  else
    ['application/json', encoded]
  end
end
failed_send(e, event) click to toggle source
# File lib/raven/client.rb, line 93
def failed_send(e, event)
  @state.failure
  if e # exception was raised
    configuration.logger.error "Unable to record event with remote Sentry server (#{e.class} - #{e.message}):\n#{e.backtrace[0..10].join("\n")}"
  else
    configuration.logger.error "Not sending event due to previous failure(s)."
  end
  configuration.logger.error("Failed to submit event: #{get_log_message(event)}")
  configuration.transport_failure_callback.call(event) if configuration.transport_failure_callback
end
generate_auth_header() click to toggle source
# File lib/raven/client.rb, line 77
def generate_auth_header
  now = Time.now.to_i.to_s
  fields = {
    'sentry_version' => PROTOCOL_VERSION,
    'sentry_client' => USER_AGENT,
    'sentry_timestamp' => now,
    'sentry_key' => configuration.public_key
  }
  fields['sentry_secret'] = configuration.secret_key unless configuration.secret_key.nil?
  'Sentry ' + fields.map { |key, value| "#{key}=#{value}" }.join(', ')
end
get_log_message(event) click to toggle source
# File lib/raven/client.rb, line 73
def get_log_message(event)
  (event && event[:message]) || '<no message value>'
end
successful_send() click to toggle source
# File lib/raven/client.rb, line 89
def successful_send
  @state.success
end