Class: Opbeat::Client

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

Constant Summary collapse

PROTOCOL_VERSION =
'1.0'
USER_AGENT =
"opbeat/#{Opbeat::VERSION}"
AUTH_HEADER_KEY =
'Authorization'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(configuration) ⇒ Client

Returns a new instance of Client.



49
50
51
52
53
# File 'lib/opbeat/client.rb', line 49

def initialize(configuration)
  @configuration = configuration
  @state = ClientState.new configuration
  @processors = configuration.processors.map { |p| p.new(self) }
end

Instance Attribute Details

#configurationObject

Returns the value of attribute configuration.



46
47
48
# File 'lib/opbeat/client.rb', line 46

def configuration
  @configuration
end

#stateObject

Returns the value of attribute state.



47
48
49
# File 'lib/opbeat/client.rb', line 47

def state
  @state
end

Instance Method Details

#connObject

Raises:



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/opbeat/client.rb', line 55

def conn
  # Error checking
  raise Error.new('No server specified') unless self.configuration[:server]
  raise Error.new('No secret token specified') unless self.configuration[:secret_token]
  raise Error.new('No organization ID specified') unless self.configuration[:organization_id]
  raise Error.new('No app ID specified') unless self.configuration[:app_id]

  Opbeat.logger.debug "Opbeat client connecting to #{self.configuration[:server]}"
  @base_url = self.configuration[:server] + 
                        "/api/v1/organizations/" +
                        self.configuration[:organization_id] +
                        "/apps/" + self.configuration[:app_id]
  @conn ||=  Faraday.new(:url => @base_url, :ssl => {:verify => self.configuration.ssl_verification}) do |builder|
    builder.adapter  Faraday.default_adapter
  end

  @conn.options[:timeout] = self.configuration[:timeout]
  @conn.options[:open_timeout] = self.configuration[:open_timeout]
  @conn
end

#encode(event) ⇒ Object



80
81
82
83
84
85
86
87
88
# File 'lib/opbeat/client.rb', line 80

def encode(event)
  event_hash = event.to_hash
  
  @processors.each do |p|
    event_hash = p.process(event_hash)
  end
  
  return MultiJson.encode(event_hash)
end

#generate_auth_header(data) ⇒ Object



76
77
78
# File 'lib/opbeat/client.rb', line 76

def generate_auth_header(data)
  'Bearer ' + self.configuration[:secret_token]
end

#send(url_postfix, message) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/opbeat/client.rb', line 90

def send(url_postfix, message)
  begin
    response = self.conn.post @base_url + url_postfix do |req|
      req.headers['Content-Type'] = 'application/json'
      req.body = self.encode(message)
      req.headers[AUTH_HEADER_KEY] = self.generate_auth_header(req.body)
      req.headers["User-Agent"] = USER_AGENT
    end
    unless response.status == 202
      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) ⇒ Object



110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/opbeat/client.rb', line 110

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 #{event.id} to Opbeat"
  send("/errors/", event)
end

#send_release(release) ⇒ Object



124
125
126
127
# File 'lib/opbeat/client.rb', line 124

def send_release(release)
  Opbeat.logger.debug "Sending release to Opbeat"
  send("/releases/", release)
end