opbeat 0.4 → 0.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -8,6 +8,36 @@ require 'opbeat/error'
8
8
 
9
9
  module Opbeat
10
10
 
11
+ class ClientState
12
+ def initialize(configuration)
13
+ @configuration = configuration
14
+ @retry_number = 0
15
+ @last_check = Time.now
16
+ end
17
+
18
+ def should_try?
19
+ return true if @status == :online
20
+
21
+ interval = ([@retry_number, 6].min() ** 2) * @configuration[:backoff_multiplier]
22
+ puts interval
23
+ return true if Time.now - @last_check > interval
24
+
25
+ false
26
+ end
27
+
28
+ def set_fail
29
+ @status = :error
30
+ @retry_number += 1
31
+ @last_check = Time.now
32
+ end
33
+
34
+ def set_success
35
+ @status = :online
36
+ @retry_number = 0
37
+ @last_check = nil
38
+ end
39
+ end
40
+
11
41
  class Client
12
42
 
13
43
  PROTOCOL_VERSION = '1.0'
@@ -15,9 +45,11 @@ module Opbeat
15
45
  AUTH_HEADER_KEY = 'Authorization'
16
46
 
17
47
  attr_accessor :configuration
48
+ attr_accessor :state
18
49
 
19
50
  def initialize(configuration)
20
51
  @configuration = configuration
52
+ @state = ClientState.new configuration
21
53
  end
22
54
 
23
55
  def conn
@@ -29,9 +61,13 @@ module Opbeat
29
61
 
30
62
  Opbeat.logger.debug "Opbeat client connecting to #{self.configuration[:server]}"
31
63
  @url = self.configuration[:server] + "/api/v1/organizations/" + self.configuration[:organization_id] + "/apps/" + self.configuration[:app_id] + "/errors/"
32
- @conn ||= Faraday.new(:url => @url) do |builder|
64
+ @conn ||= Faraday.new(:url => @url, :ssl => {:verify => self.configuration.ssl_verification}) do |builder|
33
65
  builder.adapter Faraday.default_adapter
34
66
  end
67
+
68
+ @conn.options[:timeout] = self.configuration[:timeout]
69
+ @conn.options[:open_timeout] = self.configuration[:open_timeout]
70
+ @conn
35
71
  end
36
72
 
37
73
  def generate_auth_header(data)
@@ -40,19 +76,30 @@ module Opbeat
40
76
 
41
77
  def send(event)
42
78
  return unless configuration.send_in_current_environment?
79
+ return unless state.should_try?
43
80
 
44
- # Set the project ID correctly
81
+ # Set the organization ID correctly
45
82
  event.organization = self.configuration[:organization_id]
46
83
  event.app = self.configuration[:app_id]
47
84
  Opbeat.logger.debug "Sending event #{event.id} to Opbeat"
48
85
 
49
- response = self.conn.post @url do |req|
50
- req.headers['Content-Type'] = 'application/json'
51
- req.body = MultiJson.encode(event.to_hash)
52
- req.headers[AUTH_HEADER_KEY] = self.generate_auth_header(req.body)
53
- req.headers["User-Agent"] = USER_AGENT
86
+ begin
87
+ response = self.conn.post @url do |req|
88
+ req.headers['Content-Type'] = 'application/json'
89
+ req.body = MultiJson.encode(event.to_hash)
90
+ req.headers[AUTH_HEADER_KEY] = self.generate_auth_header(req.body)
91
+ req.headers["User-Agent"] = USER_AGENT
92
+ end
93
+
94
+ unless response.status == 202
95
+ raise Error.new("Error from Opbeat server (#{response.status}): #{response.body}")
96
+ end
97
+ rescue
98
+ @state.set_fail
99
+ raise
54
100
  end
55
- raise Error.new("Error from Opbeat server (#{response.status}): #{response.body}") unless response.status == 202
101
+
102
+ @state.set_success
56
103
  response
57
104
  end
58
105
 
@@ -22,15 +22,24 @@ module Opbeat
22
22
  # Whitelist of environments that will send notifications to Opbeat
23
23
  attr_accessor :environments
24
24
 
25
- # Include module versions in reports?
26
- attr_accessor :send_modules
27
-
28
25
  # Which exceptions should never be sent
29
26
  attr_accessor :excluded_exceptions
30
27
 
31
28
  # Processors to run on data before sending upstream
32
29
  attr_accessor :processors
33
30
 
31
+ # Timeout when waiting for the server to return data in seconds
32
+ attr_accessor :timeout
33
+
34
+ # Timout when opening connection to the server
35
+ attr_accessor :open_timeout
36
+
37
+ # Backoff multipler
38
+ attr_accessor :backoff_multiplier
39
+
40
+ # Should the SSL certificate of the server be verified?
41
+ attr_accessor :ssl_verification
42
+
34
43
  attr_reader :current_environment
35
44
 
36
45
  def initialize
@@ -41,9 +50,11 @@ module Opbeat
41
50
  @context_lines = 3
42
51
  self.environments = %w[ production ]
43
52
  self.current_environment = ENV['RAILS_ENV'] || ENV['RACK_ENV'] || 'development'
44
- self.send_modules = true
45
53
  self.excluded_exceptions = []
46
54
  self.processors = [Opbeat::Processor::SanitizeData]
55
+ self.timeout = 1
56
+ self.backoff_multiplier = 2
57
+ self.ssl_verification = true
47
58
  end
48
59
 
49
60
  # Allows config options to be read like a hash
@@ -40,12 +40,6 @@ module Opbeat
40
40
  hostname = Socket.gethostbyname(hostname).first rescue hostname
41
41
  @hostname = options[:hostname] || hostname
42
42
 
43
- # Older versions of Rubygems don't support iterating over all specs
44
- if @configuration.send_modules && Gem::Specification.respond_to?(:map)
45
- options[:modules] ||= Hash[Gem::Specification.map {|spec| [spec.name, spec.version.to_s]}]
46
- end
47
- @modules = options[:modules]
48
-
49
43
  block.call(self) if block
50
44
 
51
45
  # Some type coercion
@@ -1,3 +1,3 @@
1
1
  module Opbeat
2
- VERSION = "0.4"
2
+ VERSION = "0.5"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: opbeat
3
3
  version: !ruby/object:Gem::Version
4
- version: '0.4'
4
+ version: '0.5'
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2013-02-23 00:00:00.000000000 Z
13
+ date: 2013-02-26 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: faraday