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
@@ -1,65 +0,0 @@
1
- module Honeybadger
2
- class Dependency
3
- class << self
4
- @@instances = []
5
-
6
- def instances
7
- @@instances
8
- end
9
-
10
- def register
11
- instances << new.tap { |d| d.instance_eval(&Proc.new) }
12
- end
13
-
14
- def inject!
15
- instances.each do |dependency|
16
- dependency.inject! if dependency.ok?
17
- end
18
- end
19
-
20
- def reset!
21
- instances.each(&:reset!)
22
- end
23
- end
24
-
25
- def initialize
26
- @injected = false
27
- @requirements = []
28
- @injections = []
29
- end
30
-
31
- def requirement
32
- @requirements << Proc.new
33
- end
34
-
35
- def injection
36
- @injections << Proc.new
37
- end
38
-
39
- def ok?
40
- !@injected && @requirements.all?(&:call)
41
- rescue => e
42
- Honeybadger.write_verbose_log("Exception caught while verifying dependency: #{e.class} -- #{e.message}", :error)
43
- false
44
- end
45
-
46
- def inject!
47
- @injections.each(&:call)
48
- rescue => e
49
- Honeybadger.write_verbose_log("Exception caught while injecting dependency: #{e.class} -- #{e.message}", :error)
50
- false
51
- ensure
52
- @injected = true
53
- end
54
-
55
- def reset!
56
- @injected = false
57
- end
58
-
59
- def injected?
60
- @injected
61
- end
62
-
63
- attr_reader :requirements, :injections
64
- end
65
- end
@@ -1,9 +0,0 @@
1
- module Honeybadger::Integrations; end
2
-
3
- require 'honeybadger/integrations/delayed_job'
4
- require 'honeybadger/integrations/sidekiq'
5
- require 'honeybadger/integrations/thor'
6
- require 'honeybadger/integrations/net_http'
7
- require 'honeybadger/integrations/passenger'
8
- require 'honeybadger/integrations/unicorn'
9
- require 'honeybadger/integrations/local_variables'
@@ -1,20 +0,0 @@
1
- module Honeybadger
2
- Dependency.register do
3
- requirement { defined?(::Delayed::Plugin) }
4
- requirement { defined?(::Delayed::Worker.plugins) }
5
- requirement do
6
- if delayed_job_honeybadger = defined?(::Delayed::Plugins::Honeybadger)
7
- Honeybadger.write_verbose_log("Support for Delayed Job has been moved " \
8
- "to the honeybadger gem. Please remove " \
9
- "delayed_job_honeybadger from your " \
10
- "Gemfile.", :warn)
11
- end
12
- !delayed_job_honeybadger
13
- end
14
-
15
- injection do
16
- require 'honeybadger/integrations/delayed_job/plugin'
17
- ::Delayed::Worker.plugins << Integrations::DelayedJob::Plugin
18
- end
19
- end
20
- end
@@ -1,18 +0,0 @@
1
- module Honeybadger
2
- Dependency.register do
3
- requirement { defined?(::PhusionPassenger) }
4
- requirement { defined?(::Honeybadger::Monitor) }
5
-
6
- injection do
7
- ::PhusionPassenger.on_event(:starting_worker_process) do |forked|
8
- Honeybadger.write_verbose_log('Starting passenger worker process')
9
- Honeybadger::Monitor.worker.fork if forked
10
- end
11
-
12
- ::PhusionPassenger.on_event(:stopping_worker_process) do
13
- Honeybadger.write_verbose_log('Stopping passenger worker process')
14
- Honeybadger::Monitor.worker.stop
15
- end
16
- end
17
- end
18
- end
@@ -1,37 +0,0 @@
1
- module Honeybadger
2
- module Integrations
3
- module Sidekiq
4
- class Middleware
5
- def call(worker, msg, queue)
6
- Honeybadger.context.clear!
7
- Honeybadger::Monitor::Trace.instrument("#{msg['class']}#perform", { :source => 'sidekiq', :jid => msg['jid'], :class => msg['class'] }) do
8
- yield
9
- end
10
- end
11
- end
12
- end
13
- end
14
-
15
- Dependency.register do
16
- requirement { defined?(::Sidekiq) }
17
- requirement { defined?(::Honeybadger::Monitor) }
18
-
19
- injection do
20
- ::Sidekiq.configure_server do |config|
21
- config.server_middleware do |chain|
22
- chain.add Integrations::Sidekiq::Middleware
23
- end
24
- end
25
- end
26
- end
27
-
28
- Dependency.register do
29
- requirement { defined?(::Sidekiq::VERSION) && ::Sidekiq::VERSION > '3' }
30
-
31
- injection do
32
- ::Sidekiq.configure_server do |config|
33
- config.error_handlers << Proc.new {|ex,context| Honeybadger.notify_or_ignore(ex, :parameters => context) }
34
- end
35
- end
36
- end
37
- end
@@ -1,17 +0,0 @@
1
- require "honeybadger/array"
2
- require "honeybadger/monitor/sender"
3
- require "honeybadger/monitor/worker"
4
- require "honeybadger/monitor/trace"
5
- require "honeybadger/monitor/railtie" if defined?(Rails::Railtie)
6
-
7
- module Honeybadger
8
- module Monitor
9
- class << self
10
-
11
- def worker
12
- Worker.instance
13
- end
14
-
15
- end
16
- end
17
- end
@@ -1,53 +0,0 @@
1
- module Honeybadger
2
- module Monitor
3
- class Railtie < ::Rails::Railtie
4
-
5
- config.after_initialize do
6
- if Honeybadger.configuration.traces?
7
- ActiveSupport::Notifications.subscribe('start_processing.action_controller') do |name, started, finished, id, data|
8
- Trace.create(id)
9
- end
10
-
11
- ActiveSupport::Notifications.subscribe('sql.active_record') do |*args|
12
- event = ActiveSupport::Notifications::Event.new(*args)
13
- Monitor.worker.trace.add_query(event) if Monitor.worker.trace and event.name != 'SCHEMA'
14
- end
15
-
16
- ActiveSupport::Notifications.subscribe(/^render_(template|action|collection)\.action_view/) do |*args|
17
- event = ActiveSupport::Notifications::Event.new(*args)
18
- Monitor.worker.trace.add(event) if Monitor.worker.trace
19
- end
20
-
21
- ActiveSupport::Notifications.subscribe('net_http.request') do |*args|
22
- event = ActiveSupport::Notifications::Event.new(*args)
23
- Monitor.worker.trace.add(event) if Monitor.worker.trace
24
- end
25
-
26
- ActiveSupport::Notifications.subscribe('process_action.action_controller') do |*args|
27
- event = ActiveSupport::Notifications::Event.new(*args)
28
- if event.payload[:controller] && event.payload[:action] && Monitor.worker.trace
29
- Monitor.worker.trace.complete(event)
30
- end
31
- end
32
- end
33
-
34
- if Honeybadger.configuration.metrics?
35
- ActiveSupport::Notifications.subscribe('process_action.action_controller') do |*args|
36
- event = ActiveSupport::Notifications::Event.new(*args)
37
- status = event.payload[:exception] ? 500 : event.payload[:status]
38
- Monitor.worker.timing("app.request.#{status}", event.duration)
39
-
40
- controller = event.payload[:controller]
41
- action = event.payload[:action]
42
- if controller && action
43
- Monitor.worker.timing("app.controller.#{controller}.#{action}.total", event.duration)
44
- Monitor.worker.timing("app.controller.#{controller}.#{action}.view", event.payload[:view_runtime]) if event.payload[:view_runtime]
45
- Monitor.worker.timing("app.controller.#{controller}.#{action}.db", event.payload[:db_runtime]) if event.payload[:db_runtime]
46
- end
47
- end
48
- end
49
- end
50
-
51
- end
52
- end
53
- end
@@ -1,44 +0,0 @@
1
- module Honeybadger
2
- module Monitor
3
- class Sender < Honeybadger::Sender
4
- def send_metrics(data)
5
- return unless Honeybadger.configuration.metrics?
6
- return unless Honeybadger.configuration.features['metrics']
7
-
8
- response = send_request('/v1/metrics', data.to_json)
9
-
10
- if Net::HTTPSuccess === response
11
- log(:info, "Metrics Success: #{response.class}", response) if Honeybadger.configuration.debug
12
- true
13
- else
14
- Honeybadger.configuration.features['metrics'] = false if Net::HTTPForbidden === response
15
- log(:error, "Metrics Failure: #{response.class}", response, data)
16
- false
17
- end
18
- rescue => e
19
- log(:error, "[Honeybadger::Monitor::Sender#send_metrics] Error: #{e.class} - #{e.message}\nBacktrace:\n#{e.backtrace.join("\n\t")}")
20
- true
21
- end
22
-
23
- def send_traces(data)
24
- return unless Honeybadger.configuration.traces?
25
- return unless Honeybadger.configuration.features['traces']
26
-
27
- response = send_request('/v1/traces', data.to_json)
28
-
29
- if Net::HTTPSuccess === response
30
- log(:info, "Traces Success: #{response.class}", response) if Honeybadger.configuration.debug
31
- true
32
- else
33
- Honeybadger.configuration.features['traces'] = false if Net::HTTPForbidden === response
34
- log(:error, "Traces Failure: #{response.class}", response, data)
35
- false
36
- end
37
- rescue => e
38
- log(:error, "[Honeybadger::Monitor::Sender#send_traces] Error: #{e.class} - #{e.message}\nBacktrace:\n#{e.backtrace.join("\n\t")}")
39
- true
40
- end
41
-
42
- end
43
- end
44
- end
@@ -1,187 +0,0 @@
1
- require 'securerandom'
2
-
3
- module Honeybadger
4
- module Monitor
5
- class Trace
6
- UUIDS_ENABLED = SecureRandom.respond_to?(:uuid).freeze
7
-
8
- attr_reader :id, :duration, :key
9
-
10
- def self.create(id)
11
- Thread.current[:hb_trace_id] = id
12
- Monitor.worker.pending_traces[id] = new(id)
13
- end
14
-
15
- def self.instrument(key, payload = {}, &block)
16
- create(generate_secure_id).instrument(key, payload, &block)
17
- end
18
-
19
- def self.generate_secure_id
20
- UUIDS_ENABLED ? SecureRandom.uuid : SecureRandom.hex
21
- end
22
-
23
- def initialize(id)
24
- @id = id
25
- @events = []
26
- @meta = {}
27
- @fast_queries = {}
28
- @duration = 0
29
- end
30
-
31
- def add(event)
32
- ce = clean_event(event)
33
- @events << ce.to_a if ce.render?
34
- end
35
-
36
- def add_query(event)
37
- if event.duration < 6
38
- ce = clean_event(event)
39
- return unless ce.render?
40
- query = ce.to_s
41
- if @fast_queries[query]
42
- @fast_queries[query][:duration] += ce.event.duration
43
- @fast_queries[query][:count] += 1
44
- else
45
- @fast_queries[query] = { :duration => ce.event.duration, :count => 1 }
46
- end
47
- else
48
- add(event)
49
- end
50
- end
51
-
52
- def complete(event)
53
- @meta = clean_event(event).to_h
54
- @duration = event.duration
55
- @key = "#{event.payload[:controller]}##{event.payload[:action]}"
56
- Monitor.worker.queue_trace
57
- end
58
-
59
- def instrument(key, payload)
60
- @key = key
61
- @meta = payload
62
- started = Time.now
63
- yield
64
- rescue Exception => e
65
- @meta[:exception] = [e.class.name, e.message]
66
- raise e
67
- ensure
68
- @meta.merge!(:duration => @duration = 1000.0 * (Time.now - started))
69
- Monitor.worker.queue_trace
70
- end
71
-
72
- def to_h
73
- @meta.merge({ :events => @events, :key => @key, :fast_queries => @fast_queries.map {|k,v| [ k, v[:duration], v[:count] ] } })
74
- end
75
-
76
- protected
77
-
78
- def clean_event(event)
79
- TraceCleaner.create(event)
80
- end
81
-
82
- end
83
-
84
- module TraceCleaner
85
-
86
- def self.create(event)
87
- Classes[event.name].new(event)
88
- end
89
-
90
- class Base
91
- attr_reader :event
92
-
93
- def initialize(event)
94
- @event = event
95
- end
96
-
97
- def render?
98
- true
99
- end
100
-
101
- def payload
102
- event.payload
103
- end
104
-
105
- def to_s
106
- payload[:path] || payload[:key] || payload.inspect
107
- end
108
-
109
- def to_h
110
- { :name => event.name, :desc => to_s, :duration => event.duration }
111
- end
112
-
113
- def to_a
114
- [ event.name, event.duration, to_s ]
115
- end
116
-
117
- end
118
-
119
- class NetHttpRequest < Base
120
- Replacement = "..."
121
- def to_s
122
- uri = payload[:uri]
123
- uri.user = Replacement if uri.user
124
- uri.password = Replacement if uri.password
125
- uri.query = Replacement if uri.query
126
- "#{payload[:method]} #{uri}"
127
- end
128
- end
129
-
130
- class ActiveRecord < Base
131
- Schema = "SCHEMA".freeze
132
- SchemaMigrations = /schema_migrations/.freeze
133
- EscapedQuotes = /(\\"|\\')/.freeze
134
- SQuotedData = /'(?:[^']|'')*'/.freeze
135
- DQuotedData = /"(?:[^"]|"")*"/.freeze
136
- NumericData = /\b\d+\b/.freeze
137
- Newline = /\n/.freeze
138
- Replacement = "?".freeze
139
- EmptyReplacement = "".freeze
140
- DoubleQuoters = /(postgres|sqlite|postgis)/.freeze
141
-
142
- def render?
143
- event.payload[:name] != Schema && !event.payload[:sql].match(SchemaMigrations)
144
- end
145
-
146
- def to_s
147
- sql = event.payload[:sql]
148
- sql = sql.gsub(EscapedQuotes, EmptyReplacement).gsub(SQuotedData, Replacement)
149
- sql = sql.gsub(DQuotedData, Replacement) unless ::ActiveRecord::Base.connection_pool.spec.config[:adapter] =~ DoubleQuoters
150
- sql.gsub(NumericData, Replacement).gsub(Newline, EmptyReplacement).squeeze(' ')
151
- sql.bytesize < 1024 ? sql : sql[0..1023] + '...'
152
- end
153
- end
154
-
155
- class ActionView < Base
156
- EmptyReplacement = "".freeze
157
-
158
- def to_s
159
- event.payload[:identifier].to_s.gsub(::Rails.root.to_s + '/', EmptyReplacement)
160
- end
161
- end
162
-
163
- class ActionController < Base
164
- def payload
165
- event.payload.reject {|k, v| k == :params }
166
- end
167
-
168
- def to_s
169
- payload.inspect
170
- end
171
-
172
- def to_h
173
- payload.merge({ :duration => event.duration })
174
- end
175
- end
176
-
177
- Classes = Hash.new(Base).merge({
178
- 'sql.active_record' => ActiveRecord,
179
- 'render_template.action_view' => ActionView,
180
- 'render_partial.action_view' => ActionView,
181
- 'render_collection.action_view' => ActionView,
182
- 'process_action.action_controller' => ActionController,
183
- 'net_http.request' => NetHttpRequest
184
- })
185
- end
186
- end
187
- end