Class: Raven::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/raven/client.rb

Constant Summary collapse

PROTOCOL_VERSION =
'2.0'
USER_AGENT =
"raven-ruby/#{Raven::VERSION}"
AUTH_HEADER_KEY =
'X-Sentry-Auth'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(configuration) ⇒ Client

Returns a new instance of Client.



19
20
21
# File 'lib/raven/client.rb', line 19

def initialize(configuration)
  @configuration = configuration
end

Instance Attribute Details

#configurationObject

Returns the value of attribute configuration.



17
18
19
# File 'lib/raven/client.rb', line 17

def configuration
  @configuration
end

Instance Method Details

#connObject

Raises:



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/raven/client.rb', line 23

def conn
  # Error checking
  raise Error.new('No server specified') unless self.configuration[:server]
  raise Error.new('No public key specified') unless self.configuration[:public_key]
  raise Error.new('No secret key specified') unless self.configuration[:secret_key]
  raise Error.new('No project ID specified') unless self.configuration[:project_id]

  Raven.logger.debug "Raven client connecting to #{self.configuration[:server]}"

  @conn ||=  Faraday.new(
      :url => self.configuration[:server],
      :ssl => {:verify => self.configuration.ssl_verification}
    ) do |builder|
      builder.adapter  Faraday.default_adapter
      builder.options[:timeout] = self.configuration.timeout if self.configuration.timeout
      builder.options[:open_timeout] = self.configuration.open_timeout if self.configuration.open_timeout
    end
end

#generate_auth_header(data) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
# File 'lib/raven/client.rb', line 47

def generate_auth_header(data)
  now = Time.now.to_i.to_s
  fields = {
    'sentry_version' => PROTOCOL_VERSION,
    'sentry_client' => USER_AGENT,
    'sentry_timestamp' => now,
    'sentry_key' => self.configuration[:public_key],
    'sentry_signature' => generate_signature(now, data)
  }
  'Sentry ' + fields.map{|key, value| "#{key}=#{value}"}.join(', ')
end

#generate_signature(timestamp, data) ⇒ Object



43
44
45
# File 'lib/raven/client.rb', line 43

def generate_signature(timestamp, data)
  OpenSSL::HMAC.hexdigest(OpenSSL::Digest::Digest.new('sha1'), self.configuration[:secret_key], "#{timestamp} #{data}")
end

#send(event) ⇒ Object

Raises:



59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/raven/client.rb', line 59

def send(event)
  return unless configuration.send_in_current_environment?

  # Set the project ID correctly
  event.project = self.configuration[:project_id]
  Raven.logger.debug "Sending event #{event.id} to Sentry"
  response = self.conn.post '/api/store/' do |req|
    req.headers['Content-Type'] = 'application/json'
    req.body = MultiJson.encode(event.to_hash)
    req.headers[AUTH_HEADER_KEY] = self.generate_auth_header(req.body)
  end
  raise Error.new("Error from Sentry server (#{response.status}): #{response.body}") unless response.status == 200
  response
end