atmos-merb_hoptoad_notifier 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2008 Corey Donohoe<atmos@atmos.org>, Joakim Kolsjö<trejje@gmail.com>
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README ADDED
@@ -0,0 +1,59 @@
1
+ merb_hoptoad_notifier
2
+ ---------------------------------------------
3
+ This is merb plugin for exception notification with hoptoad. It should work with
4
+ any merb app that's based on merb 1.0 and higher
5
+
6
+ This plugin: http://github.com/atmos/merb_hoptoad_notifier/tree/master
7
+ The original: http://github.com/thoughtbot/hoptoad_notifier/tree/master
8
+
9
+ Usage:
10
+
11
+ 1) Get your api key for your app from hoptoadapp.com
12
+
13
+ 2) Add the api key to config/hoptoad.yml with a similar syntax as the following
14
+ ---
15
+ :development: &defaults
16
+ :api_key: ZOMGLOLROFLMAO
17
+
18
+ :rake:
19
+ <<: *defaults
20
+
21
+ :test:
22
+ <<: *defaults
23
+
24
+ :production:
25
+ :api_key: UBERSECRETSHIT
26
+
27
+
28
+ 3) Require hoptoad in init.rb
29
+ require 'merb_hoptoad_notifier'
30
+
31
+ 4) Add the following method to your Exceptions controller. Depending on your merb version you'll need to use the exceptions,standard_error, or internal_server error as the action name. Kinda weak, but the API changed a lot in 0.9.x
32
+
33
+ class Exceptions < Merb::Controller
34
+ if %w( staging production ).include?(Merb.env)
35
+ def standard_error
36
+ HoptoadNotifier.notify_hoptoad(request, session)
37
+ render
38
+ end
39
+ end
40
+ end
41
+
42
+ 5) Restart the server, trigger an error(in staging or prod) and check that it arrived at hoptoad :)
43
+
44
+
45
+ Filtersing your post environment
46
+ --------------------------------
47
+ If you have environmental variables set in your ruby process that should not be sent to hoptoad,
48
+ there's a mechanism for filtering those attributes now. Throw something like this in
49
+ config/init.rb
50
+
51
+ Merb::BootLoader.after_app_loads do
52
+ HoptoadNotifier.environment_filters = %w(^AWS ^EC2 SECRET PRIVATE KEY)
53
+ end
54
+
55
+ Each of these words will be compiled into a regex so you should be able to use anchors if needed.
56
+
57
+ Thanks to the following GitHubbers
58
+ ----------------------------------
59
+ joakimk, fairchild and cv.
data/Rakefile ADDED
@@ -0,0 +1,67 @@
1
+ require 'rubygems'
2
+ require 'rake/gempackagetask'
3
+ require 'rubygems/specification'
4
+ require 'date'
5
+ require 'merb-core/version'
6
+ require 'merb-core/tasks/merb_rake_helper'
7
+ require 'spec/rake/spectask'
8
+
9
+ NAME = "merb_hoptoad_notifier"
10
+ GEM_VERSION = "1.0.5"
11
+ AUTHOR = "Corey Donohoe"
12
+ EMAIL = "atmos@atmos.org"
13
+ HOMEPAGE = "http://github.com/atmos"
14
+ SUMMARY = "Merb plugin that provides hoptoad exception notification"
15
+
16
+ spec = Gem::Specification.new do |s|
17
+ s.rubyforge_project = 'merb'
18
+ s.name = NAME
19
+ s.version = GEM_VERSION
20
+ s.platform = Gem::Platform::RUBY
21
+ s.has_rdoc = true
22
+ s.extra_rdoc_files = ["README", "LICENSE", 'TODO']
23
+ s.summary = SUMMARY
24
+ s.description = s.summary
25
+ s.author = AUTHOR
26
+ s.email = EMAIL
27
+ s.homepage = HOMEPAGE
28
+ # s.add_dependency('merb', '>= 0.9.7')
29
+ # s.add_dependency('extlib', '>= 0.9.6')
30
+ s.require_path = 'lib'
31
+ s.files = %w(LICENSE README Rakefile TODO) + Dir.glob("{lib,spec}/**/*")
32
+ end
33
+
34
+ Rake::GemPackageTask.new(spec) do |pkg|
35
+ pkg.gem_spec = spec
36
+ end
37
+
38
+ desc "install the plugin locally"
39
+ task :install => [:package] do
40
+ sh %{#{sudo} gem install #{install_home} pkg/#{NAME}-#{GEM_VERSION} --no-update-sources}
41
+ end
42
+
43
+ desc "create a gemspec file"
44
+ task :make_spec do
45
+ File.open("#{NAME}.gemspec", "w") do |file|
46
+ file.puts spec.to_ruby
47
+ end
48
+ end
49
+
50
+ namespace :jruby do
51
+ desc "Run :package and install the resulting .gem with jruby"
52
+ task :install => :package do
53
+ sh %{#{sudo} jruby -S gem install #{install_home} pkg/#{NAME}-#{GEM_VERSION}.gem --no-rdoc --no-ri}
54
+ end
55
+ end
56
+
57
+ Spec::Rake::SpecTask.new(:default) do |t|
58
+ t.spec_opts << %w(-fs --color) << %w(-O spec/spec.opts)
59
+ t.spec_opts << '--loadby' << 'random'
60
+ t.spec_files = Dir["spec/*_spec.rb"]
61
+ t.rcov = ENV.has_key?('NO_RCOV') ? ENV['NO_RCOV'] != 'true' : true
62
+ t.rcov_opts << '--exclude' << '.gem/'
63
+
64
+ t.rcov_opts << '--text-summary'
65
+ t.rcov_opts << '--sort' << 'coverage' << '--sort-reverse'
66
+ t.rcov_opts << '--only-uncovered'
67
+ end
data/TODO ADDED
@@ -0,0 +1,2 @@
1
+ TODO:
2
+ See if there's any more rich information we could provide to hoptoad
@@ -0,0 +1,8 @@
1
+ require File.expand_path(File.dirname(__FILE__)+'/merb_hoptoad_notifier/hoptoad_notifier')
2
+ # make sure we're running inside Merb
3
+ if defined?(Merb::Plugins)
4
+ Merb::BootLoader.after_app_loads do
5
+ HoptoadNotifier.configure
6
+ end
7
+ Merb::Plugins.add_rakefiles "merb_hoptoad_notifier/merbtasks"
8
+ end
@@ -0,0 +1,104 @@
1
+ require 'net/http'
2
+
3
+ module HoptoadNotifier
4
+ class << self
5
+ attr_accessor :api_key, :logger
6
+
7
+ def configure
8
+ key = YAML.load_file(Merb.root / 'config' / 'hoptoad.yml')
9
+ if key
10
+ env = key[Merb.env.to_sym]
11
+ env ? @api_key = env[:api_key] : raise(ArgumentError, "No hoptoad key for Merb environment #{Merb.env}")
12
+ end
13
+ end
14
+
15
+ def logger
16
+ @logger || Merb.logger
17
+ end
18
+
19
+ def environment_filters
20
+ @environment_filters ||= %w(SSH_AUTH_SOCK)
21
+ end
22
+
23
+ def notify_hoptoad(request, session)
24
+ return if request.nil?
25
+ params = request.params
26
+
27
+ request.exceptions.each do |exception|
28
+ data = {
29
+ :api_key => HoptoadNotifier.api_key,
30
+ :error_class => Extlib::Inflection.camelize(exception.class.name),
31
+ :error_message => "#{Extlib::Inflection.camelize(exception.class.name)}: #{exception.message}",
32
+ :backtrace => exception.backtrace,
33
+ :environment => ENV.to_hash
34
+ }
35
+
36
+ data[:request] = {
37
+ :params => params[:original_params]
38
+ }
39
+
40
+ data[:environment] = clean_hoptoad_environment(ENV.to_hash.merge(request.env))
41
+ data[:environment][:RAILS_ENV] = Merb.env
42
+
43
+ data[:session] = {
44
+ :key => session.instance_variable_get("@session_id"),
45
+ :data => session.instance_variable_get("@data")
46
+ }
47
+
48
+ send_to_hoptoad :notice => default_notice_options.merge(data)
49
+ end
50
+ true
51
+ end
52
+
53
+ def send_to_hoptoad(data) #:nodoc:
54
+ url = URI.parse("http://hoptoadapp.com:80/notices/")
55
+
56
+ Net::HTTP.start(url.host, url.port) do |http|
57
+ headers = {
58
+ 'Content-type' => 'application/x-yaml',
59
+ 'Accept' => 'text/xml, application/xml'
60
+ }
61
+ http.read_timeout = 5 # seconds
62
+ http.open_timeout = 2 # seconds
63
+ # http.use_ssl = HoptoadNotifier.secure
64
+ response = begin
65
+ http.post(url.path, stringify_keys(data).to_yaml, headers)
66
+ rescue TimeoutError => e
67
+ logger.error "Timeout while contacting the Hoptoad server."
68
+ nil
69
+ end
70
+ case response
71
+ when Net::HTTPSuccess then
72
+ logger.info "Hoptoad Success: #{response.class}"
73
+ else
74
+ logger.error "Hoptoad Failure: #{response.class}\n#{response.body if response.respond_to? :body}"
75
+ end
76
+ end
77
+ end
78
+
79
+ def default_notice_options #:nodoc:
80
+ {
81
+ :api_key => HoptoadNotifier.api_key,
82
+ :error_message => 'Notification',
83
+ :backtrace => nil,
84
+ :request => {},
85
+ :session => {},
86
+ :environment => {}
87
+ }
88
+ end
89
+
90
+ def stringify_keys(hash) #:nodoc:
91
+ hash.inject({}) do |h, pair|
92
+ h[pair.first.to_s] = pair.last.is_a?(Hash) ? stringify_keys(pair.last) : pair.last
93
+ h
94
+ end
95
+ end
96
+ def clean_hoptoad_environment(env) #:nodoc:
97
+ env.each do |k, v|
98
+ env[k] = "[FILTERED]" if HoptoadNotifier.environment_filters.any? do |filter|
99
+ k.to_s.match(/#{filter}/)
100
+ end
101
+ end
102
+ end
103
+ end
104
+ end
@@ -0,0 +1,2 @@
1
+ namespace :merb_hoptoad_notifier do
2
+ end
@@ -0,0 +1,9 @@
1
+ ---
2
+ :development: &defaults
3
+ :api_key: ZOMGLOLROFLMAO
4
+
5
+ :test:
6
+ <<: *defaults
7
+
8
+ :production:
9
+ :api_key: UBERSECRETSHIT
@@ -0,0 +1,113 @@
1
+ require File.dirname(__FILE__) + '/spec_helper'
2
+
3
+ describe "HoptoadNotifier" do
4
+ include Merb::Spec::Helpers
5
+
6
+ before(:each) do
7
+ stub(Merb).env { :production }
8
+ stub(Merb).root { Dir.tmpdir }
9
+
10
+ @http = Net::HTTP.new('hoptoadapp.com')
11
+ @headers = { "Content-type" => "application/x-yaml", "Accept" => "text/xml, application/xml" }
12
+ @config = {:development => {:api_key=>"ZOMGLOLROFLMAO"}, :production => {:api_key=>"UBERSECRETSHIT"}, :test => {:api_key=>"ZOMGLOLROFLMAO"}}
13
+ end
14
+
15
+ it "should define a constant" do
16
+ HoptoadNotifier.should_not be_nil
17
+ end
18
+
19
+ describe ".configure" do
20
+ before(:each) do
21
+ mock(YAML).load_file(File.join(Merb.root / 'config' / 'hoptoad.yml')) { @config }
22
+ HoptoadNotifier.configure
23
+ end
24
+ it "should know the api key after configuring" do
25
+ HoptoadNotifier.api_key.should == 'UBERSECRETSHIT'
26
+ end
27
+ end
28
+
29
+ describe ".stringify_key" do
30
+ it "should turn string keys into symbols" do
31
+ HoptoadNotifier.stringify_keys({'foo' => 'bar', :baz => 'foo', :bar => 'foo'}).should == { 'foo' => 'bar', 'baz' => 'foo', 'bar' => 'foo'}
32
+ end
33
+ end
34
+
35
+
36
+ describe "notification" do
37
+ before(:each) do
38
+ stub(Net::HTTP).new('hoptoadapp.com', 80, nil, nil, nil, nil) { @http }
39
+ mock(YAML).load_file(File.join(Merb.root / 'config' / 'hoptoad.yml')) { @config }
40
+ HoptoadNotifier.configure
41
+ end
42
+
43
+ describe ".default_notice_options" do
44
+ it "should return sane defaults" do
45
+ HoptoadNotifier.default_notice_options.should == {
46
+ :api_key => HoptoadNotifier.api_key,
47
+ :error_message => 'Notification',
48
+ :backtrace => nil,
49
+ :request => {},
50
+ :session => {},
51
+ :environment => {}
52
+ }
53
+ end
54
+ end
55
+
56
+ describe ".notify_hoptoad" do
57
+ describe "bad input" do
58
+ it "should handle nil input" do
59
+ HoptoadNotifier.notify_hoptoad(nil, nil).should be_nil
60
+ end
61
+ end
62
+
63
+ describe "good input" do
64
+ before(:each) do
65
+ mock(HoptoadNotifier).send_to_hoptoad(anything).times(2) { true }
66
+ @request = setup_merb_request
67
+ mock(@request).exceptions { [RuntimeError.new('ZOMG'), RuntimeError.new('ORLY')]}
68
+ mock(@request).params { {"q"=>"0017000000SmnJ0"} }
69
+ end
70
+ it "should return true" do
71
+ HoptoadNotifier.notify_hoptoad(@request, {}).should be_true
72
+ end
73
+ end
74
+ end
75
+
76
+ describe ".send_to_hoptoad" do
77
+ describe "any 2XX response" do
78
+ before(:each) do
79
+ response = Net::HTTPOK.new('1.1', 200, 'Wazzup?')
80
+ mock(@http).post("/notices/", "--- {}\n\n", @headers) { response }
81
+ end
82
+ it "should log success" do
83
+ mock(HoptoadNotifier.logger).info "Hoptoad Success: Net::HTTPOK"
84
+ HoptoadNotifier.send_to_hoptoad({})
85
+ end
86
+ end
87
+ describe "any non 2XX response" do
88
+ before(:each) do
89
+ response = Net::HTTPInternalServerError.new('1.1', 500, 'Upstream unavailable')
90
+ mock(response).body { 'Upstream unavailable' }
91
+
92
+ stub(@http.instance_variable_get('@socket')).closed? { true }
93
+ mock(@http).post("/notices/", "--- {}\n\n", @headers) { response }
94
+ end
95
+ it "should log failure" do
96
+ mock(HoptoadNotifier.logger).error "Hoptoad Failure: Net::HTTPInternalServerError\nUpstream unavailable"
97
+ HoptoadNotifier.send_to_hoptoad({})
98
+ end
99
+ end
100
+ describe "a timeout exception is thrown" do
101
+ before(:each) do
102
+ response = TimeoutError.new("It took too fucking long")
103
+ mock(@http).post("/notices/", "--- {}\n\n", @headers) { raise response }
104
+ end
105
+ it "should log failure" do
106
+ mock(HoptoadNotifier.logger).error("Hoptoad Failure: NilClass\n")
107
+ mock(HoptoadNotifier.logger).error("Timeout while contacting the Hoptoad server.")
108
+ HoptoadNotifier.send_to_hoptoad({})
109
+ end
110
+ end
111
+ end
112
+ end
113
+ end
data/spec/spec.opts ADDED
File without changes
@@ -0,0 +1,33 @@
1
+ $TESTING=true
2
+ $:.push File.join(File.dirname(__FILE__), '..', 'lib')
3
+ require 'rr'
4
+ require 'merb-core'
5
+ require 'merb_hoptoad_notifier'
6
+ require 'tmpdir'
7
+ require 'pp'
8
+
9
+ Spec::Runner.configure do |config|
10
+ config.mock_with :rr
11
+ end
12
+
13
+ module Merb
14
+ module Spec
15
+ module Helpers
16
+ def setup_merb_request
17
+ rack_env = {"SERVER_NAME"=>"localhost",
18
+ "rack.run_once"=>false, "rack.url_scheme"=>"http", "HTTP_USER_AGENT"=>"Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_5_5; en-us) AppleWebKit/526.1+ (KHTML, like Gecko) Version/3.1.2 Safari/525.20.1",
19
+ "HTTP_ACCEPT_ENCODING"=>"gzip, deflate", "PATH_INFO"=>"/search/", "HTTP_CACHE_CONTROL"=>"max-age=0",
20
+ "HTTP_ACCEPT_LANGUAGE"=>"en-us", "HTTP_HOST"=>"localhost:4000", "SERVER_PROTOCOL"=>"HTTP/1.1", "SCRIPT_NAME"=>"",
21
+ "REQUEST_PATH"=>"/search/", "SERVER_SOFTWARE"=>"Mongrel 1.1.5", "REMOTE_ADDR"=>"127.0.0.1", "rack.streaming"=>true,
22
+ "rack.version"=>[0, 1], "rack.multithread"=>true, "HTTP_VERSION"=>"HTTP/1.1", "rack.multiprocess"=>false,
23
+ "REQUEST_URI"=>"/search/?q=0017000000SmnJ0", "SERVER_PORT"=>"4000", "QUERY_STRING"=>"q=0017000000SmnJ0",
24
+ "GATEWAY_INTERFACE"=>"CGI/1.2", "HTTP_ACCEPT"=>"text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5",
25
+ "REQUEST_METHOD"=>"GET", "HTTP_CONNECTION"=>"keep-alive"}
26
+
27
+ request = Merb::Request.new(rack_env)
28
+ stub(request).env { rack_env }
29
+ request
30
+ end
31
+ end
32
+ end
33
+ end
metadata ADDED
@@ -0,0 +1,67 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: atmos-merb_hoptoad_notifier
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Corey Donohoe
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2008-12-16 00:00:00 -08:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: Merb plugin that provides hoptoad exception notification
17
+ email: atmos@atmos.org
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - README
24
+ - LICENSE
25
+ - TODO
26
+ files:
27
+ - LICENSE
28
+ - README
29
+ - Rakefile
30
+ - TODO
31
+ - lib/merb_hoptoad_notifier
32
+ - lib/merb_hoptoad_notifier/hoptoad_notifier.rb
33
+ - lib/merb_hoptoad_notifier/merbtasks.rb
34
+ - lib/merb_hoptoad_notifier.rb
35
+ - spec/fixtures
36
+ - spec/fixtures/hoptoad.yml
37
+ - spec/merb_hoptoad_notifier_spec.rb
38
+ - spec/spec.opts
39
+ - spec/spec_helper.rb
40
+ has_rdoc: true
41
+ homepage: http://github.com/atmos
42
+ post_install_message:
43
+ rdoc_options: []
44
+
45
+ require_paths:
46
+ - lib
47
+ required_ruby_version: !ruby/object:Gem::Requirement
48
+ requirements:
49
+ - - ">="
50
+ - !ruby/object:Gem::Version
51
+ version: "0"
52
+ version:
53
+ required_rubygems_version: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ version: "0"
58
+ version:
59
+ requirements: []
60
+
61
+ rubyforge_project: merb
62
+ rubygems_version: 1.2.0
63
+ signing_key:
64
+ specification_version: 2
65
+ summary: Merb plugin that provides hoptoad exception notification
66
+ test_files: []
67
+