honeybadger 1.16.7 → 2.0.0.beta.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (200) hide show
  1. checksums.yaml +4 -4
  2. data/LICENSE +19 -0
  3. data/README.md +37 -16
  4. data/bin/honeybadger +5 -0
  5. data/lib/honeybadger.rb +167 -191
  6. data/lib/honeybadger/agent.rb +136 -0
  7. data/lib/honeybadger/backend.rb +26 -0
  8. data/lib/honeybadger/backend/base.rb +66 -0
  9. data/lib/honeybadger/backend/debug.rb +12 -0
  10. data/lib/honeybadger/backend/null.rb +16 -0
  11. data/lib/honeybadger/backend/server.rb +51 -0
  12. data/lib/honeybadger/backend/test.rb +24 -0
  13. data/lib/honeybadger/backtrace.rb +29 -24
  14. data/lib/honeybadger/cli.rb +367 -0
  15. data/lib/honeybadger/config.rb +333 -0
  16. data/lib/honeybadger/config/callbacks.rb +70 -0
  17. data/lib/honeybadger/config/defaults.rb +175 -0
  18. data/lib/honeybadger/config/env.rb +40 -0
  19. data/lib/honeybadger/config/yaml.rb +43 -0
  20. data/lib/honeybadger/const.rb +28 -0
  21. data/lib/honeybadger/init/rails.rb +84 -0
  22. data/lib/honeybadger/init/sinatra.rb +27 -0
  23. data/lib/honeybadger/logging.rb +133 -0
  24. data/lib/honeybadger/notice.rb +243 -280
  25. data/lib/honeybadger/plugin.rb +110 -0
  26. data/lib/honeybadger/plugins/delayed_job.rb +22 -0
  27. data/lib/honeybadger/{integrations → plugins}/delayed_job/plugin.rb +6 -7
  28. data/lib/honeybadger/{integrations → plugins}/local_variables.rb +7 -8
  29. data/lib/honeybadger/{integrations → plugins}/net_http.rb +10 -8
  30. data/lib/honeybadger/plugins/passenger.rb +24 -0
  31. data/lib/honeybadger/plugins/rails.rb +61 -0
  32. data/lib/honeybadger/plugins/sidekiq.rb +35 -0
  33. data/lib/honeybadger/{integrations → plugins}/thor.rb +9 -8
  34. data/lib/honeybadger/{integrations → plugins}/unicorn.rb +10 -9
  35. data/lib/honeybadger/rack/error_notifier.rb +44 -27
  36. data/lib/honeybadger/rack/metrics_reporter.rb +41 -0
  37. data/lib/honeybadger/rack/request_hash.rb +50 -0
  38. data/lib/honeybadger/rack/user_feedback.rb +15 -10
  39. data/lib/honeybadger/rack/user_informer.rb +14 -3
  40. data/lib/honeybadger/trace.rb +185 -0
  41. data/lib/honeybadger/util/http.rb +79 -0
  42. data/lib/honeybadger/util/request_sanitizer.rb +35 -0
  43. data/lib/honeybadger/util/sanitizer.rb +71 -0
  44. data/lib/honeybadger/util/stats.rb +31 -0
  45. data/lib/honeybadger/version.rb +4 -0
  46. data/lib/honeybadger/worker.rb +224 -0
  47. data/lib/honeybadger/worker/batch.rb +50 -0
  48. data/lib/honeybadger/worker/metered_queue.rb +80 -0
  49. data/lib/honeybadger/worker/metrics_collection.rb +61 -0
  50. data/lib/honeybadger/worker/metrics_collector.rb +96 -0
  51. data/{lib/honeybadger/capistrano.rb → vendor/capistrano-honeybadger/lib/capistrano/honeybadger.rb} +1 -3
  52. data/vendor/capistrano-honeybadger/lib/capistrano/tasks/deploy.cap +76 -0
  53. data/vendor/capistrano-honeybadger/lib/honeybadger/capistrano.rb +2 -0
  54. data/{lib → vendor/capistrano-honeybadger/lib}/honeybadger/capistrano/legacy.rb +16 -15
  55. data/vendor/thor/lib/thor.rb +484 -0
  56. data/vendor/thor/lib/thor/actions.rb +319 -0
  57. data/vendor/thor/lib/thor/actions/create_file.rb +103 -0
  58. data/vendor/thor/lib/thor/actions/create_link.rb +59 -0
  59. data/vendor/thor/lib/thor/actions/directory.rb +118 -0
  60. data/vendor/thor/lib/thor/actions/empty_directory.rb +135 -0
  61. data/vendor/thor/lib/thor/actions/file_manipulation.rb +316 -0
  62. data/vendor/thor/lib/thor/actions/inject_into_file.rb +107 -0
  63. data/vendor/thor/lib/thor/base.rb +656 -0
  64. data/vendor/thor/lib/thor/command.rb +133 -0
  65. data/vendor/thor/lib/thor/core_ext/hash_with_indifferent_access.rb +77 -0
  66. data/vendor/thor/lib/thor/core_ext/io_binary_read.rb +10 -0
  67. data/vendor/thor/lib/thor/core_ext/ordered_hash.rb +98 -0
  68. data/vendor/thor/lib/thor/error.rb +32 -0
  69. data/vendor/thor/lib/thor/group.rb +281 -0
  70. data/vendor/thor/lib/thor/invocation.rb +178 -0
  71. data/vendor/thor/lib/thor/line_editor.rb +17 -0
  72. data/vendor/thor/lib/thor/line_editor/basic.rb +35 -0
  73. data/vendor/thor/lib/thor/line_editor/readline.rb +88 -0
  74. data/vendor/thor/lib/thor/parser.rb +4 -0
  75. data/vendor/thor/lib/thor/parser/argument.rb +73 -0
  76. data/vendor/thor/lib/thor/parser/arguments.rb +175 -0
  77. data/vendor/thor/lib/thor/parser/option.rb +125 -0
  78. data/vendor/thor/lib/thor/parser/options.rb +218 -0
  79. data/vendor/thor/lib/thor/rake_compat.rb +71 -0
  80. data/vendor/thor/lib/thor/runner.rb +322 -0
  81. data/vendor/thor/lib/thor/shell.rb +81 -0
  82. data/vendor/thor/lib/thor/shell/basic.rb +421 -0
  83. data/vendor/thor/lib/thor/shell/color.rb +149 -0
  84. data/vendor/thor/lib/thor/shell/html.rb +126 -0
  85. data/vendor/thor/lib/thor/util.rb +267 -0
  86. data/vendor/thor/lib/thor/version.rb +3 -0
  87. metadata +97 -305
  88. data/Appraisals +0 -95
  89. data/CHANGELOG.md +0 -422
  90. data/Gemfile +0 -8
  91. data/Gemfile.lock +0 -136
  92. data/Guardfile +0 -5
  93. data/MIT-LICENSE +0 -32
  94. data/Rakefile +0 -159
  95. data/features/metal.feature +0 -20
  96. data/features/rack.feature +0 -55
  97. data/features/rails.feature +0 -343
  98. data/features/rails3.x.feature +0 -26
  99. data/features/rake.feature +0 -25
  100. data/features/sinatra.feature +0 -27
  101. data/features/standalone.feature +0 -73
  102. data/features/step_definitions/metal_steps.rb +0 -24
  103. data/features/step_definitions/rack_steps.rb +0 -18
  104. data/features/step_definitions/rails_steps.rb +0 -270
  105. data/features/step_definitions/rake_steps.rb +0 -17
  106. data/features/step_definitions/standalone_steps.rb +0 -12
  107. data/features/step_definitions/thor_steps.rb +0 -4
  108. data/features/support/env.rb +0 -22
  109. data/features/support/honeybadger_failure_shim.rb.template +0 -5
  110. data/features/support/honeybadger_shim.rb.template +0 -6
  111. data/features/support/rails.rb +0 -202
  112. data/features/support/rake/Rakefile +0 -68
  113. data/features/support/test.thor +0 -22
  114. data/features/thor.feature +0 -5
  115. data/gemfiles/binding_of_caller.gemfile +0 -13
  116. data/gemfiles/delayed_job.gemfile +0 -13
  117. data/gemfiles/rack.gemfile +0 -13
  118. data/gemfiles/rails.gemfile +0 -16
  119. data/gemfiles/rails2.3.gemfile +0 -15
  120. data/gemfiles/rails3.0.gemfile +0 -16
  121. data/gemfiles/rails3.1.gemfile +0 -16
  122. data/gemfiles/rails3.2.gemfile +0 -16
  123. data/gemfiles/rails4.0.gemfile +0 -16
  124. data/gemfiles/rails4.1.gemfile +0 -16
  125. data/gemfiles/rake.gemfile +0 -13
  126. data/gemfiles/sinatra.gemfile +0 -13
  127. data/gemfiles/standalone.gemfile +0 -12
  128. data/gemfiles/thor.gemfile +0 -13
  129. data/generators/honeybadger/honeybadger_generator.rb +0 -95
  130. data/generators/honeybadger/lib/insert_commands.rb +0 -34
  131. data/generators/honeybadger/lib/rake_commands.rb +0 -24
  132. data/generators/honeybadger/templates/capistrano_hook.rb +0 -6
  133. data/generators/honeybadger/templates/honeybadger_tasks.rake +0 -25
  134. data/generators/honeybadger/templates/initializer.rb +0 -6
  135. data/honeybadger.gemspec +0 -174
  136. data/lib/honeybadger/array.rb +0 -53
  137. data/lib/honeybadger/capistrano/tasks.rake +0 -73
  138. data/lib/honeybadger/configuration.rb +0 -397
  139. data/lib/honeybadger/dependency.rb +0 -65
  140. data/lib/honeybadger/integrations.rb +0 -9
  141. data/lib/honeybadger/integrations/delayed_job.rb +0 -20
  142. data/lib/honeybadger/integrations/passenger.rb +0 -18
  143. data/lib/honeybadger/integrations/sidekiq.rb +0 -37
  144. data/lib/honeybadger/monitor.rb +0 -17
  145. data/lib/honeybadger/monitor/railtie.rb +0 -53
  146. data/lib/honeybadger/monitor/sender.rb +0 -44
  147. data/lib/honeybadger/monitor/trace.rb +0 -187
  148. data/lib/honeybadger/monitor/worker.rb +0 -169
  149. data/lib/honeybadger/payload.rb +0 -101
  150. data/lib/honeybadger/rack.rb +0 -12
  151. data/lib/honeybadger/rails.rb +0 -45
  152. data/lib/honeybadger/rails/action_controller_catcher.rb +0 -30
  153. data/lib/honeybadger/rails/controller_methods.rb +0 -78
  154. data/lib/honeybadger/rails/middleware/exceptions_catcher.rb +0 -29
  155. data/lib/honeybadger/rails3_tasks.rb +0 -94
  156. data/lib/honeybadger/railtie.rb +0 -52
  157. data/lib/honeybadger/rake_handler.rb +0 -66
  158. data/lib/honeybadger/sender.rb +0 -185
  159. data/lib/honeybadger/shared_tasks.rb +0 -56
  160. data/lib/honeybadger/stats.rb +0 -29
  161. data/lib/honeybadger/tasks.rb +0 -95
  162. data/lib/honeybadger/user_feedback.rb +0 -8
  163. data/lib/honeybadger/user_informer.rb +0 -8
  164. data/lib/honeybadger_tasks.rb +0 -69
  165. data/lib/rails/generators/honeybadger/honeybadger_generator.rb +0 -99
  166. data/rails/init.rb +0 -1
  167. data/resources/README.md +0 -34
  168. data/script/integration_test.rb +0 -38
  169. data/spec/allocation_stats.rb +0 -32
  170. data/spec/honeybadger/backtrace_spec.rb +0 -242
  171. data/spec/honeybadger/capistrano_spec.rb +0 -36
  172. data/spec/honeybadger/configuration_spec.rb +0 -328
  173. data/spec/honeybadger/dependency_spec.rb +0 -134
  174. data/spec/honeybadger/integrations/delayed_job_spec.rb +0 -82
  175. data/spec/honeybadger/integrations/local_variables_spec.rb +0 -60
  176. data/spec/honeybadger/integrations/net_http_spec.rb +0 -29
  177. data/spec/honeybadger/integrations/passenger_spec.rb +0 -29
  178. data/spec/honeybadger/integrations/sidekiq_spec.rb +0 -60
  179. data/spec/honeybadger/integrations/thor_spec.rb +0 -32
  180. data/spec/honeybadger/integrations/unicorn_spec.rb +0 -40
  181. data/spec/honeybadger/logger_spec.rb +0 -79
  182. data/spec/honeybadger/monitor/trace_spec.rb +0 -65
  183. data/spec/honeybadger/monitor/worker_spec.rb +0 -274
  184. data/spec/honeybadger/notice_spec.rb +0 -669
  185. data/spec/honeybadger/notifier_spec.rb +0 -328
  186. data/spec/honeybadger/payload_spec.rb +0 -162
  187. data/spec/honeybadger/rack_spec.rb +0 -85
  188. data/spec/honeybadger/rails/action_controller_spec.rb +0 -328
  189. data/spec/honeybadger/rails_spec.rb +0 -37
  190. data/spec/honeybadger/sender_spec.rb +0 -317
  191. data/spec/honeybadger/stats_spec.rb +0 -57
  192. data/spec/honeybadger/user_feedback_spec.rb +0 -80
  193. data/spec/honeybadger/user_informer_spec.rb +0 -30
  194. data/spec/honeybadger_tasks_spec.rb +0 -171
  195. data/spec/spec_helper.rb +0 -24
  196. data/spec/support/array_including.rb +0 -31
  197. data/spec/support/backtraced_exception.rb +0 -9
  198. data/spec/support/collected_sender.rb +0 -12
  199. data/spec/support/defines_constants.rb +0 -18
  200. data/spec/support/helpers.rb +0 -101
@@ -0,0 +1,35 @@
1
+ module Honeybadger
2
+ module Util
3
+ class RequestSanitizer
4
+
5
+ def initialize(sanitizer)
6
+ @sanitizer = sanitizer
7
+ end
8
+
9
+ def sanitize(request_hash)
10
+ request_hash.merge({
11
+ url: sanitize_url(request_hash[:url]),
12
+ component: request_hash[:component],
13
+ action: request_hash[:action],
14
+ params: sanitize_hash(request_hash[:params]),
15
+ session: sanitize_hash(request_hash[:session]),
16
+ cgi_data: sanitize_hash(request_hash[:cgi_data])
17
+ })
18
+ end
19
+
20
+ private
21
+
22
+ def sanitize_url(url)
23
+ if url
24
+ @sanitizer.filter_url(url)
25
+ end
26
+ end
27
+
28
+ def sanitize_hash(hash)
29
+ if hash
30
+ @sanitizer.filter(@sanitizer.sanitize(hash))
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,71 @@
1
+ module Honeybadger
2
+ module Util
3
+ class Sanitizer
4
+ OBJECT_WHITELIST = [Hash, Array, String, Integer, Float, TrueClass, FalseClass, NilClass]
5
+
6
+ def initialize(opts = {})
7
+ @max_depth = opts[:max_depth] || 20
8
+ @filters = Array(opts[:filters])
9
+ end
10
+
11
+ # Removes non-serializable data and truncates to max depth.
12
+ def sanitize(data, depth = 0, stack = [])
13
+ return '[possible infinite recursion halted]' if stack.any?{|item| item == data.object_id }
14
+
15
+ if data.respond_to?(:to_hash)
16
+ return '[max depth reached]' if depth >= max_depth
17
+ data.to_hash.reduce({}) do |result, (key, value)|
18
+ result.merge(key => sanitize(value, depth+1, stack + [data.object_id]))
19
+ end
20
+ elsif data.respond_to?(:to_ary)
21
+ return '[max depth reached]' if depth >= max_depth
22
+ data.to_ary.collect do |value|
23
+ sanitize(value, depth+1, stack + [data.object_id])
24
+ end
25
+ elsif OBJECT_WHITELIST.any? {|c| data.kind_of?(c) }
26
+ data
27
+ else
28
+ data.to_s
29
+ end
30
+ end
31
+
32
+ def filter(hash)
33
+ {}.tap do |filtered_hash|
34
+ hash.each_pair do |key, value|
35
+ if value.respond_to?(:to_hash)
36
+ filtered_hash[key] = filter(hash[key])
37
+ else
38
+ filtered_hash[key] = filter_key?(key) ? '[FILTERED]' : hash[key]
39
+ end
40
+ end
41
+ end
42
+ end
43
+
44
+ def filter_url(url)
45
+ filtered_url = url.to_s.dup
46
+ filtered_url.scan(/(?:^|&|\?)([^=?&]+)=([^&]+)/).each do |m|
47
+ next unless filter_key?(m[0])
48
+ filtered_url.gsub!(/#{m[1]}/, '[FILTERED]')
49
+ end
50
+
51
+ filtered_url
52
+ end
53
+
54
+ private
55
+
56
+ attr_reader :max_depth, :filters
57
+
58
+ def filter_key?(key)
59
+ return false unless filters
60
+
61
+ filters.any? do |filter|
62
+ if filter.is_a?(Regexp)
63
+ key.to_s =~ filter
64
+ else
65
+ key.to_s.eql?(filter.to_s)
66
+ end
67
+ end
68
+ end
69
+ end
70
+ end
71
+ end
@@ -0,0 +1,31 @@
1
+ module Honeybadger
2
+ module Util
3
+ class Stats
4
+ HAS_MEM = File.exists?("/proc/meminfo")
5
+ HAS_LOAD = File.exists?("/proc/loadavg")
6
+
7
+ class << self
8
+ def all
9
+ { :mem => memory, :load => load }
10
+ end
11
+
12
+ # From https://github.com/bloopletech/webstats/blob/master/server/data_providers/mem_info.rb
13
+ def memory
14
+ out = {}
15
+ if HAS_MEM
16
+ out[:total], out[:free], out[:buffers], out[:cached] = IO.readlines("/proc/meminfo")[0..4].map { |l| l =~ /^.*?\: +(.*?) kB$/; ($1.to_i / 1024.0).to_f }
17
+ out[:free_total] = out[:free] + out[:buffers] + out[:cached]
18
+ end
19
+ out
20
+ end
21
+
22
+ # From https://github.com/bloopletech/webstats/blob/master/server/data_providers/cpu_info.rb
23
+ def load
24
+ out = {}
25
+ out[:one], out[:five], out[:fifteen] = IO.read("/proc/loadavg").split(' ', 4).map(&:to_f) if HAS_LOAD
26
+ out
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,4 @@
1
+ module Honeybadger
2
+ # Public: The current String Honeybadger version.
3
+ VERSION = '2.0.0.beta.1'.freeze
4
+ end
@@ -0,0 +1,224 @@
1
+ require 'forwardable'
2
+ require 'net/http'
3
+
4
+ require 'honeybadger/logging'
5
+
6
+ module Honeybadger
7
+ class Worker
8
+ extend Forwardable
9
+
10
+ include Honeybadger::Logging::Helper
11
+
12
+ autoload :Batch, 'honeybadger/worker/batch'
13
+ autoload :MetricsCollector, 'honeybadger/worker/metrics_collector'
14
+ autoload :MeteredQueue, 'honeybadger/worker/metered_queue'
15
+
16
+ # Sub-class thread so we have a named thread (useful for debugging in Thread.list).
17
+ class Thread < ::Thread; end
18
+
19
+ attr_reader :backend, :queue, :features, :metrics, :traces, :pid, :mutex, :thread
20
+
21
+ def initialize(config)
22
+ @backend = config.backend
23
+ @config = config
24
+ @mutex = Mutex.new
25
+ prepare
26
+ end
27
+
28
+ def start
29
+ debug { 'starting worker' }
30
+
31
+ @pid = Process.pid
32
+ @thread = Thread.new { run }
33
+
34
+ true
35
+ end
36
+
37
+ def stop(force = false)
38
+ debug { 'stopping worker' }
39
+ if thread
40
+ if force
41
+ debug { 'killing worker' }
42
+ Thread.kill(thread)
43
+ else
44
+ thread[:should_exit] = true
45
+ unless thread.eql?(Thread.current)
46
+ mutex.unlock if mutex.locked?
47
+ thread.join
48
+ end
49
+ end
50
+ end
51
+ @thread = nil
52
+ @pid = nil
53
+ end
54
+
55
+ def fork
56
+ debug { 'forking worker' }
57
+
58
+ stop
59
+
60
+ mutex.synchronize { prepare }
61
+
62
+ start
63
+ end
64
+
65
+ def notice(notice)
66
+ debug { sprintf('worker adding notice feature=notices id=%s', notice.id) }
67
+ push(:notices, notice)
68
+ end
69
+
70
+ def trace(trace)
71
+ if trace.duration > config[:'traces.threshold']
72
+ debug { sprintf('worker adding trace duration=%s feature=traces id=%s', trace.duration.round(2), trace.id) }
73
+ mutex.synchronize do
74
+ traces.push(trace)
75
+ end
76
+ else
77
+ debug { sprintf('worker discarding trace duration=%s feature=traces id=%s', trace.duration.round(2), trace.id) }
78
+ end
79
+ end
80
+
81
+ def_delegators :@metrics, :timing, :increment
82
+
83
+ private
84
+
85
+ attr_reader :config
86
+
87
+ def init_queue
88
+ @queue = {
89
+ notices: MeteredQueue.new,
90
+ metrics: MeteredQueue.new,
91
+ traces: MeteredQueue.new
92
+ }.freeze
93
+
94
+ @features = {
95
+ notices: true,
96
+ metrics: true,
97
+ traces: true
98
+ }.freeze
99
+ end
100
+
101
+ def init_traces
102
+ @traces = Batch.new(config, :traces, 20, config[:debug] ? 10 : 60)
103
+ end
104
+
105
+ def init_metrics
106
+ @metrics = MetricsCollector.new(config, config[:debug] ? 10 : 60)
107
+ end
108
+
109
+ def flush_metrics
110
+ debug { 'worker flushing metrics feature=metrics' } # TODO: Include count.
111
+ metrics.chunk(100, &method(:push).to_proc.curry[:metrics])
112
+ mutex.synchronize { init_metrics }
113
+ end
114
+
115
+ def flush_traces
116
+ debug { sprintf('worker flushing traces feature=traces count=%d', traces.size) }
117
+ push(:traces, traces) unless traces.empty?
118
+ mutex.synchronize { init_traces }
119
+ end
120
+
121
+ def flush_queue
122
+ mutex.synchronize do
123
+ queue.each_pair do |feature, queue|
124
+ while payload = queue.pop!
125
+ handle_response(feature, notify_backend(feature, payload))
126
+ end
127
+ end
128
+ end
129
+ end
130
+
131
+ def prepare
132
+ init_queue
133
+ init_metrics
134
+ init_traces
135
+ end
136
+
137
+ def push(feature, object)
138
+ unless features[feature]
139
+ debug { sprintf('worker dropping feature=%s reason=collector', feature) }
140
+ return false
141
+ end
142
+
143
+ unless config.features[feature]
144
+ debug { sprintf('worker dropping feature=%s reason=ping', feature) }
145
+ return false
146
+ end
147
+
148
+ mutex.synchronize do
149
+ queue[feature].push(object)
150
+ end
151
+
152
+ true
153
+ end
154
+
155
+ def run
156
+ begin
157
+ debug { 'worker started' }
158
+ work until finish
159
+ rescue Exception => e
160
+ error(sprintf('error in worker thread (shutting down) class=%s message=%s location=%s', e.class, e.message.dump, e.backtrace.first.dump))
161
+ raise e
162
+ ensure
163
+ debug { 'stopping worker' }
164
+ end
165
+ end
166
+
167
+ def work
168
+ flush_metrics if metrics.flush?
169
+ flush_traces if traces.flush?
170
+
171
+ queue.each_pair do |feature, queue|
172
+ if payload = queue.pop
173
+ handle_response(feature, notify_backend(feature, payload))
174
+ end
175
+ end
176
+
177
+ sleep(0.1)
178
+ rescue StandardError => e
179
+ error(sprintf('error in worker thread class=%s message=%s location=%s', e.class, e.message.dump, e.backtrace.first.dump))
180
+ sleep(1)
181
+ end
182
+
183
+ def finish
184
+ if Thread.current[:should_exit]
185
+ debug { 'flushing worker data' }
186
+
187
+ flush_metrics
188
+ flush_traces
189
+ flush_queue
190
+
191
+ true
192
+ end
193
+ end
194
+
195
+ def notify_backend(feature, payload)
196
+ debug { sprintf('worker notifying backend feature=%s id=%s', feature, payload.id) }
197
+ backend.notify(feature, payload)
198
+ end
199
+
200
+ def handle_response(feature, response)
201
+ debug { sprintf('worker response feature=%s code=%s message=%s', feature, response.code, response.message.to_s.dump) }
202
+
203
+ case response.code
204
+ when 429, 503
205
+ debug { sprintf('worker applying throttle=1.25 feature=%s code=%s', feature, response.code) }
206
+ queue[feature].throttle(1.25)
207
+ when 402
208
+ warn { sprintf('worker disabling feature=%s code=%s', feature, response.code) }
209
+ mutex.synchronize { features[feature] = false }
210
+ when 403
211
+ error { sprintf('worker shutting down (unauthorized) feature=%s code=%s', feature, response.code) }
212
+ Honeybadger::Agent.stop(true)
213
+ when 201
214
+ if throttle = queue[feature].unthrottle
215
+ debug { sprintf('worker removing throttle=%s feature=%s code=%s', throttle, feature, response.code) }
216
+ end
217
+ when :error
218
+ # Error logged by backend.
219
+ else
220
+ warn { sprintf('worker unknown response feature=%s code=%s', feature, response.code) }
221
+ end
222
+ end
223
+ end
224
+ end
@@ -0,0 +1,50 @@
1
+ require 'securerandom'
2
+
3
+ module Honeybadger
4
+ class Worker
5
+ class Batch
6
+ def initialize(config, name = :data, max = 100, interval = 60, now = now)
7
+ @id = SecureRandom.uuid
8
+ @config = config
9
+ @name = name
10
+ @max = max
11
+ @interval = interval
12
+ @future = now + interval
13
+ @values = Array.new
14
+ @mutex = Mutex.new
15
+ end
16
+
17
+ attr_reader :id
18
+
19
+ def push(val)
20
+ mutex.synchronize { values.push(val) }
21
+ end
22
+
23
+ def empty?
24
+ mutex.synchronize { values.empty? }
25
+ end
26
+
27
+ def size
28
+ mutex.synchronize { values.size }
29
+ end
30
+
31
+ def flush?
32
+ size >= max || now >= future
33
+ end
34
+
35
+ def as_json(*args)
36
+ mutex.synchronize do
37
+ { name => values.compact.map(&:to_h), :environment => config[:env], :hostname => config[:hostname] }
38
+ end
39
+ end
40
+
41
+ private
42
+
43
+ attr_reader :config, :name, :max, :values, :future, :mutex
44
+
45
+ def now
46
+ Time.now.to_i
47
+ end
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,80 @@
1
+ module Honeybadger
2
+ class Worker
3
+ # Internal: A thread-safe first-in-first-out queue. Values are pushed onto
4
+ # the queue and released at a defined interval.
5
+ class MeteredQueue
6
+ def initialize(interval = 1, max = 1000, now = now)
7
+ @interval = interval
8
+ @max = max
9
+ @values = Array.new
10
+ @throttles = Array.new
11
+ @future = calculate_future(now, interval)
12
+ @mutex = Mutex.new
13
+ end
14
+
15
+ def size
16
+ mutex.synchronize { values.size }
17
+ end
18
+
19
+ def push(value)
20
+ unless values.size == max
21
+ mutex.synchronize { values.push(value) }
22
+ end
23
+ end
24
+
25
+ def pop
26
+ if now.to_i >= future
27
+ mutex.synchronize do
28
+ @future = calculate_future
29
+ values.shift
30
+ end
31
+ end
32
+ end
33
+
34
+ def pop!
35
+ mutex.synchronize { values.shift }
36
+ end
37
+
38
+ # Applies a new throttle to this queue and adjusts the future.
39
+ #
40
+ # Returns nothing
41
+ def throttle(throttle)
42
+ mutex.synchronize do
43
+ old_interval = throttled_interval
44
+ throttles << throttle
45
+ @future += (throttled_interval - old_interval)
46
+ end
47
+ end
48
+
49
+ # Removes the last throttle from this queue and adjusts the future.
50
+ #
51
+ # Returns Float throttle
52
+ def unthrottle
53
+ mutex.synchronize do
54
+ old_interval = throttled_interval
55
+ throttles.pop.tap do |throttle|
56
+ if throttle
57
+ @future -= (old_interval - throttled_interval)
58
+ end
59
+ end
60
+ end
61
+ end
62
+
63
+ private
64
+
65
+ attr_reader :interval, :max, :values, :throttles, :future, :mutex
66
+
67
+ def now
68
+ Time.now
69
+ end
70
+
71
+ def calculate_future(now = now, interval = interval)
72
+ now.to_i + throttled_interval(interval)
73
+ end
74
+
75
+ def throttled_interval(interval = interval)
76
+ throttles.reduce(interval) {|a,e| a * e }
77
+ end
78
+ end
79
+ end
80
+ end