airbrake 3.0.rc1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (79) hide show
  1. data/.gitignore +18 -0
  2. data/.yardopts +3 -0
  3. data/CHANGELOG +441 -0
  4. data/Gemfile +3 -0
  5. data/INSTALL +25 -0
  6. data/MIT-LICENSE +22 -0
  7. data/README.md +431 -0
  8. data/README_FOR_HEROKU_ADDON.md +93 -0
  9. data/Rakefile +188 -0
  10. data/SUPPORTED_RAILS_VERSIONS +14 -0
  11. data/TESTING.md +26 -0
  12. data/airbrake.gemspec +33 -0
  13. data/features/metal.feature +23 -0
  14. data/features/rack.feature +27 -0
  15. data/features/rails.feature +254 -0
  16. data/features/rails_with_js_notifier.feature +78 -0
  17. data/features/rake.feature +23 -0
  18. data/features/sinatra.feature +33 -0
  19. data/features/step_definitions/airbrake_shim.rb.template +15 -0
  20. data/features/step_definitions/file_steps.rb +10 -0
  21. data/features/step_definitions/metal_steps.rb +23 -0
  22. data/features/step_definitions/rack_steps.rb +20 -0
  23. data/features/step_definitions/rails_application_steps.rb +401 -0
  24. data/features/step_definitions/rake_steps.rb +17 -0
  25. data/features/support/airbrake_shim.rb.template +15 -0
  26. data/features/support/env.rb +18 -0
  27. data/features/support/matchers.rb +35 -0
  28. data/features/support/rails.rb +181 -0
  29. data/features/support/rake/Rakefile +57 -0
  30. data/features/support/terminal.rb +103 -0
  31. data/features/user_informer.feature +63 -0
  32. data/generators/airbrake/airbrake_generator.rb +90 -0
  33. data/generators/airbrake/lib/insert_commands.rb +34 -0
  34. data/generators/airbrake/lib/rake_commands.rb +24 -0
  35. data/generators/airbrake/templates/airbrake_tasks.rake +25 -0
  36. data/generators/airbrake/templates/capistrano_hook.rb +6 -0
  37. data/generators/airbrake/templates/initializer.rb +6 -0
  38. data/install.rb +1 -0
  39. data/lib/airbrake.rb +150 -0
  40. data/lib/airbrake/backtrace.rb +100 -0
  41. data/lib/airbrake/capistrano.rb +21 -0
  42. data/lib/airbrake/configuration.rb +247 -0
  43. data/lib/airbrake/notice.rb +348 -0
  44. data/lib/airbrake/rack.rb +42 -0
  45. data/lib/airbrake/rails.rb +41 -0
  46. data/lib/airbrake/rails/action_controller_catcher.rb +30 -0
  47. data/lib/airbrake/rails/controller_methods.rb +68 -0
  48. data/lib/airbrake/rails/error_lookup.rb +33 -0
  49. data/lib/airbrake/rails/javascript_notifier.rb +42 -0
  50. data/lib/airbrake/rails3_tasks.rb +82 -0
  51. data/lib/airbrake/railtie.rb +33 -0
  52. data/lib/airbrake/rake_handler.rb +65 -0
  53. data/lib/airbrake/sender.rb +83 -0
  54. data/lib/airbrake/shared_tasks.rb +30 -0
  55. data/lib/airbrake/tasks.rb +83 -0
  56. data/lib/airbrake/user_informer.rb +25 -0
  57. data/lib/airbrake/version.rb +3 -0
  58. data/lib/airbrake_tasks.rb +50 -0
  59. data/lib/rails/generators/airbrake/airbrake_generator.rb +96 -0
  60. data/lib/templates/javascript_notifier.erb +13 -0
  61. data/lib/templates/rescue.erb +91 -0
  62. data/rails/init.rb +1 -0
  63. data/script/integration_test.rb +38 -0
  64. data/test/airbrake_2_2.xsd +78 -0
  65. data/test/airbrake_tasks_test.rb +163 -0
  66. data/test/backtrace_test.rb +163 -0
  67. data/test/catcher_test.rb +333 -0
  68. data/test/configuration_test.rb +216 -0
  69. data/test/helper.rb +251 -0
  70. data/test/javascript_notifier_test.rb +52 -0
  71. data/test/logger_test.rb +85 -0
  72. data/test/notice_test.rb +459 -0
  73. data/test/notifier_test.rb +235 -0
  74. data/test/rack_test.rb +58 -0
  75. data/test/rails_initializer_test.rb +36 -0
  76. data/test/recursion_test.rb +10 -0
  77. data/test/sender_test.rb +193 -0
  78. data/test/user_informer_test.rb +29 -0
  79. metadata +365 -0
@@ -0,0 +1,83 @@
1
+ module Airbrake
2
+ # Sends out the notice to Airbrake
3
+ class Sender
4
+
5
+ NOTICES_URI = '/notifier_api/v2/notices/'.freeze
6
+ HTTP_ERRORS = [Timeout::Error,
7
+ Errno::EINVAL,
8
+ Errno::ECONNRESET,
9
+ EOFError,
10
+ Net::HTTPBadResponse,
11
+ Net::HTTPHeaderSyntaxError,
12
+ Net::ProtocolError,
13
+ Errno::ECONNREFUSED].freeze
14
+
15
+ def initialize(options = {})
16
+ [:proxy_host, :proxy_port, :proxy_user, :proxy_pass, :protocol,
17
+ :host, :port, :secure, :http_open_timeout, :http_read_timeout].each do |option|
18
+ instance_variable_set("@#{option}", options[option])
19
+ end
20
+ end
21
+
22
+ # Sends the notice data off to Airbrake for processing.
23
+ #
24
+ # @param [String] data The XML notice to be sent off
25
+ def send_to_airbrake(data)
26
+ logger.debug { "Sending request to #{url.to_s}:\n#{data}" } if logger
27
+
28
+ http =
29
+ Net::HTTP::Proxy(proxy_host, proxy_port, proxy_user, proxy_pass).
30
+ new(url.host, url.port)
31
+
32
+ http.read_timeout = http_read_timeout
33
+ http.open_timeout = http_open_timeout
34
+
35
+ if secure
36
+ http.use_ssl = true
37
+ http.ca_file = OpenSSL::X509::DEFAULT_CERT_FILE if File.exist?(OpenSSL::X509::DEFAULT_CERT_FILE)
38
+ http.verify_mode = OpenSSL::SSL::VERIFY_PEER
39
+ else
40
+ http.use_ssl = false
41
+ end
42
+
43
+ response = begin
44
+ http.post(url.path, data, HEADERS)
45
+ rescue *HTTP_ERRORS => e
46
+ log :error, "Timeout while contacting the Airbrake server."
47
+ nil
48
+ end
49
+
50
+ case response
51
+ when Net::HTTPSuccess then
52
+ log :info, "Success: #{response.class}", response
53
+ else
54
+ log :error, "Failure: #{response.class}", response
55
+ end
56
+
57
+ if response && response.respond_to?(:body)
58
+ error_id = response.body.match(%r{<error-id[^>]*>(.*?)</error-id>})
59
+ error_id[1] if error_id
60
+ end
61
+ end
62
+
63
+ private
64
+
65
+ attr_reader :proxy_host, :proxy_port, :proxy_user, :proxy_pass, :protocol,
66
+ :host, :port, :secure, :http_open_timeout, :http_read_timeout
67
+
68
+ def url
69
+ URI.parse("#{protocol}://#{host}:#{port}").merge(NOTICES_URI)
70
+ end
71
+
72
+ def log(level, message, response = nil)
73
+ logger.send level, LOG_PREFIX + message if logger
74
+ Airbrake.report_environment_info
75
+ Airbrake.report_response_body(response.body) if response && response.respond_to?(:body)
76
+ end
77
+
78
+ def logger
79
+ Airbrake.logger
80
+ end
81
+
82
+ end
83
+ end
@@ -0,0 +1,30 @@
1
+ namespace :airbrake do
2
+ desc "Notify Airbrake of a new deploy."
3
+ task :deploy => :environment do
4
+ require 'airbrake_tasks'
5
+ AirbrakeTasks.deploy(:rails_env => ENV['TO'],
6
+ :scm_revision => ENV['REVISION'],
7
+ :scm_repository => ENV['REPO'],
8
+ :local_username => ENV['USER'],
9
+ :api_key => ENV['API_KEY'],
10
+ :dry_run => ENV['DRY_RUN'])
11
+ end
12
+
13
+ task :log_stdout do
14
+ require 'logger'
15
+ RAILS_DEFAULT_LOGGER = Logger.new(STDOUT)
16
+ end
17
+
18
+ namespace :heroku do
19
+ desc "Install Heroku deploy notifications addon"
20
+ task :add_deploy_notification => [:environment] do
21
+ heroku_api_key = `heroku console 'puts ENV[%{AIRBRAKE_API_KEY}]' | head -n 1`.strip
22
+ heroku_rails_env = `heroku console 'puts RAILS_ENV' | head -n 1`.strip
23
+
24
+ command = %Q(heroku addons:add deployhooks:http url="http://airbrakeapp.com/deploys.txt?deploy[rails_env]=#{heroku_rails_env}&api_key=#{heroku_api_key}")
25
+
26
+ puts "\nRunning:\n#{command}\n"
27
+ puts `#{command}`
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,83 @@
1
+ require 'airbrake'
2
+ require File.join(File.dirname(__FILE__), 'shared_tasks')
3
+
4
+ namespace :airbrake do
5
+ desc "Verify your gem installation by sending a test exception to the airbrake service"
6
+ task :test => ['airbrake:log_stdout', :environment] do
7
+ RAILS_DEFAULT_LOGGER.level = Logger::DEBUG
8
+
9
+ require 'action_controller/test_process'
10
+
11
+ Dir["app/controllers/application*.rb"].each { |file| require(File.expand_path(file)) }
12
+
13
+ class AirbrakeTestingException < RuntimeError; end
14
+
15
+ unless Airbrake.configuration.api_key
16
+ puts "Airbrake needs an API key configured! Check the README to see how to add it."
17
+ exit
18
+ end
19
+
20
+ Airbrake.configuration.development_environments = []
21
+
22
+ catcher = Airbrake::Rails::ActionControllerCatcher
23
+ in_controller = ApplicationController.included_modules.include?(catcher)
24
+ in_base = ActionController::Base.included_modules.include?(catcher)
25
+ if !in_controller || !in_base
26
+ puts "Rails initialization did not occur"
27
+ exit
28
+ end
29
+
30
+ puts "Configuration:"
31
+ Airbrake.configuration.to_hash.each do |key, value|
32
+ puts sprintf("%25s: %s", key.to_s, value.inspect.slice(0, 55))
33
+ end
34
+
35
+ unless defined?(ApplicationController)
36
+ puts "No ApplicationController found"
37
+ exit
38
+ end
39
+
40
+ puts 'Setting up the Controller.'
41
+ class ApplicationController
42
+ # This is to bypass any filters that may prevent access to the action.
43
+ prepend_before_filter :test_airbrake
44
+ def test_airbrake
45
+ puts "Raising '#{exception_class.name}' to simulate application failure."
46
+ raise exception_class.new, 'Testing airbrake via "rake airbrake:test". If you can see this, it works.'
47
+ end
48
+
49
+ def rescue_action(exception)
50
+ rescue_action_in_public exception
51
+ end
52
+
53
+ # Ensure we actually have an action to go to.
54
+ def verify; end
55
+
56
+ def consider_all_requests_local
57
+ false
58
+ end
59
+
60
+ def local_request?
61
+ false
62
+ end
63
+
64
+ def exception_class
65
+ exception_name = ENV['EXCEPTION'] || "AirbrakeTestingException"
66
+ exception_name.split("::").inject(Object){|klass, name| klass.const_get(name)}
67
+ rescue
68
+ Object.const_set(exception_name.gsub(/:+/, "_"), Class.new(Exception))
69
+ end
70
+
71
+ def logger
72
+ nil
73
+ end
74
+ end
75
+ class AirbrakeVerificationController < ApplicationController; end
76
+
77
+ puts 'Processing request.'
78
+ request = ActionController::TestRequest.new("REQUEST_URI" => "/airbrake_verification_controller")
79
+ response = ActionController::TestResponse.new
80
+ AirbrakeVerificationController.new.process(request, response)
81
+ end
82
+ end
83
+
@@ -0,0 +1,25 @@
1
+ module Airbrake
2
+ class UserInformer
3
+ def initialize(app)
4
+ @app = app
5
+ end
6
+
7
+ def replacement(with)
8
+ @replacement ||= Airbrake.configuration.user_information.gsub(/\{\{\s*error_id\s*\}\}/, with.to_s)
9
+ end
10
+
11
+ def call(env)
12
+ status, headers, body = @app.call(env)
13
+ if env['airbrake.error_id'] && Airbrake.configuration.user_information
14
+ new_body = []
15
+ body.each do |chunk|
16
+ new_body << chunk.gsub("<!-- AIRBRAKE ERROR -->", replacement(env['airbrake.error_id']))
17
+ end
18
+ headers['Content-Length'] = new_body.sum(&:length).to_s
19
+ body = new_body
20
+ end
21
+ [status, headers, body]
22
+ end
23
+ end
24
+ end
25
+
@@ -0,0 +1,3 @@
1
+ module Airbrake
2
+ VERSION = "3.0.rc1".freeze
3
+ end
@@ -0,0 +1,50 @@
1
+ require 'net/http'
2
+ require 'uri'
3
+ require 'active_support'
4
+
5
+ # Capistrano tasks for notifying Airbrake of deploys
6
+ module AirbrakeTasks
7
+
8
+ # Alerts Airbrake of a deploy.
9
+ #
10
+ # @param [Hash] opts Data about the deploy that is set to Airbrake
11
+ #
12
+ # @option opts [String] :rails_env Environment of the deploy (production, staging)
13
+ # @option opts [String] :scm_revision The given revision/sha that is being deployed
14
+ # @option opts [String] :scm_repository Address of your repository to help with code lookups
15
+ # @option opts [String] :local_username Who is deploying
16
+ def self.deploy(opts = {})
17
+ if Airbrake.configuration.api_key.blank?
18
+ puts "I don't seem to be configured with an API key. Please check your configuration."
19
+ return false
20
+ end
21
+
22
+ if opts[:rails_env].blank?
23
+ puts "I don't know to which Rails environment you are deploying (use the TO=production option)."
24
+ return false
25
+ end
26
+
27
+ dry_run = opts.delete(:dry_run)
28
+ params = {'api_key' => opts.delete(:api_key) ||
29
+ Airbrake.configuration.api_key}
30
+ opts.each {|k,v| params["deploy[#{k}]"] = v }
31
+
32
+ url = URI.parse("http://#{Airbrake.configuration.host || 'airbrakeapp.com'}/deploys.txt")
33
+
34
+ proxy = Net::HTTP.Proxy(Airbrake.configuration.proxy_host,
35
+ Airbrake.configuration.proxy_port,
36
+ Airbrake.configuration.proxy_user,
37
+ Airbrake.configuration.proxy_pass)
38
+
39
+ if dry_run
40
+ puts url, params.inspect
41
+ return true
42
+ else
43
+ response = proxy.post_form(url, params)
44
+
45
+ puts response.body
46
+ return Net::HTTPSuccess === response
47
+ end
48
+ end
49
+ end
50
+
@@ -0,0 +1,96 @@
1
+ require 'rails/generators'
2
+
3
+ class AirbrakeGenerator < Rails::Generators::Base
4
+
5
+ class_option :api_key, :aliases => "-k", :type => :string, :desc => "Your Airbrake API key"
6
+ class_option :heroku, :type => :boolean, :desc => "Use the Heroku addon to provide your Airbrake API key"
7
+ class_option :app, :aliases => "-a", :type => :string, :desc => "Your Heroku app name (only required if deploying to >1 Heroku app)"
8
+
9
+ def self.source_root
10
+ @_airbrake_source_root ||= File.expand_path("../../../../../generators/airbrake/templates", __FILE__)
11
+ end
12
+
13
+ def install
14
+ ensure_api_key_was_configured
15
+ ensure_plugin_is_not_present
16
+ append_capistrano_hook
17
+ generate_initializer unless api_key_configured?
18
+ determine_api_key if heroku?
19
+ test_airbrake
20
+ end
21
+
22
+ private
23
+
24
+ def ensure_api_key_was_configured
25
+ if !options[:api_key] && !options[:heroku] && !api_key_configured?
26
+ puts "Must pass --api-key or --heroku or create config/initializers/airbrake.rb"
27
+ exit
28
+ end
29
+ end
30
+
31
+ def ensure_plugin_is_not_present
32
+ if plugin_is_present?
33
+ puts "You must first remove the airbrake plugin. Please run: script/plugin remove airbrake"
34
+ exit
35
+ end
36
+ end
37
+
38
+ def append_capistrano_hook
39
+ if File.exists?('config/deploy.rb') && File.exists?('Capfile')
40
+ append_file('config/deploy.rb', <<-HOOK)
41
+
42
+ require './config/boot'
43
+ require 'airbrake/capistrano'
44
+ HOOK
45
+ end
46
+ end
47
+
48
+ def api_key_expression
49
+ s = if options[:api_key]
50
+ "'#{options[:api_key]}'"
51
+ elsif options[:heroku]
52
+ "ENV['AIRBRAKE_API_KEY']"
53
+ end
54
+ end
55
+
56
+ def generate_initializer
57
+ template 'initializer.rb', 'config/initializers/airbrake.rb'
58
+ end
59
+
60
+ def determine_api_key
61
+ puts "Attempting to determine your API Key from Heroku..."
62
+ ENV['AIRBRAKE_API_KEY'] = heroku_api_key
63
+ if ENV['AIRBRAKE_API_KEY'].blank?
64
+ puts "... Failed."
65
+ puts "WARNING: We were unable to detect the Airbrake API Key from your Heroku environment."
66
+ puts "Your Heroku application environment may not be configured correctly."
67
+ exit 1
68
+ else
69
+ puts "... Done."
70
+ puts "Heroku's Airbrake API Key is '#{ENV['AIRBRAKE_API_KEY']}'"
71
+ end
72
+ end
73
+
74
+ def heroku_api_key
75
+ app = options[:app] ? " --app #{options[:app]}" : ''
76
+ `heroku console#{app} 'puts ENV[%{AIRBRAKE_API_KEY}]'`.split("\n").first
77
+ end
78
+
79
+ def heroku?
80
+ options[:heroku] ||
81
+ system("grep AIRBRAKE_API_KEY config/initializers/airbrake.rb") ||
82
+ system("grep AIRBRAKE_API_KEY config/environment.rb")
83
+ end
84
+
85
+ def api_key_configured?
86
+ File.exists?('config/initializers/airbrake.rb')
87
+ end
88
+
89
+ def test_airbrake
90
+ puts run("rake airbrake:test --trace")
91
+ end
92
+
93
+ def plugin_is_present?
94
+ File.exists?('vendor/plugins/airbrake')
95
+ end
96
+ end
@@ -0,0 +1,13 @@
1
+ <%= javascript_tag %Q{
2
+ var notifierJsScheme = (("https:" == document.location.protocol) ? "https://" : "http://");
3
+ document.write(unescape("%3Cscript src='" + notifierJsScheme + "#{host}/javascripts/notifier.js' type='text/javascript'%3E%3C/script%3E"));
4
+ }
5
+ %>
6
+
7
+ <%= javascript_tag %Q{
8
+ Airbrake.setKey('#{api_key}');
9
+ Airbrake.setHost('#{host}');
10
+ Airbrake.setEnvironment('#{environment}');
11
+ Airbrake.setErrorDefaults({ url: "#{escape_javascript url}", component: "#{controller_name}", action: "#{action_name}" });
12
+ }
13
+ %>
@@ -0,0 +1,91 @@
1
+ <script type="text/javascript">
2
+ var Airbrake = {
3
+ host : <%= host.to_json %>,
4
+ api_key : <%= api_key.to_json %>,
5
+ notice : <%= notice.to_json %>,
6
+ message : 'This error exists in production!',
7
+
8
+ initialize: function() {
9
+ if (this.initialized) {
10
+ return;
11
+ } else {
12
+ this.initialized = true;
13
+ }
14
+
15
+ var data = [];
16
+
17
+ for (var key in this.notice) {
18
+ data[data.length] = 'notice[' + key + ']=' + this.notice[key];
19
+ }
20
+
21
+ data[data.length] = 'notice[api_key]=' + this.api_key;
22
+ data[data.length] = 'callback=Airbrake.onSuccess';
23
+ data[data.length] = '_=' + (new Date()).getTime();
24
+
25
+ var head = document.getElementsByTagName('head')[0];
26
+ var done = false;
27
+
28
+ var
29
+ script = document.createElement('script');
30
+ script.src = 'http://' + this.host + '/notices_api/v1/notices/exist?' +
31
+ data.join('&');
32
+ script.type = 'text/javascript';
33
+ script.onload = script.onreadystatechange = function(){
34
+ if (!done && (!this.readyState ||
35
+ this.readyState == 'loaded' || this.readyState == 'complete')) {
36
+
37
+ done = true;
38
+
39
+ // Handle memory leak in IE. (via jQuery)
40
+ script.onload = script.onreadystatechange = null;
41
+ head.removeChild(script);
42
+ }
43
+ };
44
+
45
+ head.appendChild(script);
46
+ },
47
+
48
+ onSuccess: function(error) {
49
+ var body = document.getElementsByTagName('body')[0];
50
+ var text = document.createTextNode(this.message);
51
+ var element = document.createElement('a');
52
+
53
+ element.id = 'airbrake';
54
+ element.href = 'http://' + error.subdomain + '.' + this.host +
55
+ '/projects/' + error.project_id + '/errors/' + error.id;
56
+ element.appendChild(text);
57
+
58
+ body.insertBefore(element, body.firstChild);
59
+
60
+ var h1 = document.getElementsByTagName('h1')[0];
61
+ var pre = document.getElementsByTagName('pre')[0];
62
+ var wrapper = document.createElement('div');
63
+
64
+ wrapper.id = 'wrapper';
65
+ wrapper.appendChild(h1);
66
+ wrapper.appendChild(pre);
67
+
68
+ body.insertBefore(wrapper, body.children[1]);
69
+ }
70
+ };
71
+
72
+ window.onload = function() {
73
+ Airbrake.initialize.apply(Hoptoad);
74
+ };
75
+ </script>
76
+
77
+ <style type="text/css">
78
+ #airbrake {
79
+ background: #FFF url(http://airbrakeapp.com/images/fell-off-the-toad.gif) no-repeat top right;
80
+ color: #F00;
81
+ padding: 45px 101px 45px 12px;
82
+ font-size: 14px;
83
+ font-weight: bold;
84
+ display: block;
85
+ float: right;
86
+ }
87
+
88
+ #wrapper {
89
+ padding-right: 360px;
90
+ }
91
+ </style>