squash_rails 1.0.0 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -31,9 +31,16 @@ https://github.com/SquareSquash/ruby
31
31
  Usage
32
32
  -----
33
33
 
34
- See the `squash_ruby` gem for installation and configuration instructions. Note
35
- that it is no longer necessary to set the `:environment` configuration option;
36
- the Rails client library automatically sets that to the Rails environment.
34
+ Add this gem and the Squash Ruby gem to your Gemfile:
35
+
36
+ ```` ruby
37
+ gem 'squash_ruby', :require => 'squash/ruby'
38
+ gem 'squash_rails', :require => 'squash/rails'
39
+ ````
40
+
41
+ See the `squash_ruby` gem for configuration instructions. Note that it is no
42
+ longer necessary to set the `:environment` configuration option; the Rails
43
+ client library automatically sets that to the Rails environment.
37
44
 
38
45
  You can use the usual `Squash::Ruby.notify` method in your Rails projects, but
39
46
  you will miss out on some Rails-specific information in your exception logs. You
@@ -14,11 +14,15 @@
14
14
 
15
15
  # Capistrano tasks for Rails apps using Squash.
16
16
 
17
- desc "Notifies Squash of a new deploy."
18
- task 'squash:notify', :roles => :web, :only => {:primary => true}, :except => {:no_release => true} do
19
- rails_env = fetch(:rails_env, 'production')
20
- run "bundle exec rake squash:notify REVISION=#{real_revision} DEPLOY_ENV=#{rails_env}"
21
- end
17
+ Capistrano::Configuration.instance.load do
18
+ namespace :squash do
19
+ desc "Notifies Squash of a new deploy."
20
+ task :notify, :roles => :web, :only => {:primary => true}, :except => {:no_release => true} do
21
+ rails_env = fetch(:rails_env, 'production')
22
+ run "cd #{current_path} && env RAILS_ENV=#{rails_env} #{fetch(:bundle_cmd, "bundle")} exec rake squash:notify REVISION=#{real_revision} DEPLOY_ENV=#{rails_env}"
23
+ end
24
+ end
22
25
 
23
- after 'deploy:restart', 'squash:notify'
24
- after 'deploy:start', 'squash:notify'
26
+ after 'deploy:restart', 'squash:notify'
27
+ after 'deploy:start', 'squash:notify'
28
+ end
@@ -0,0 +1,94 @@
1
+ module Squash
2
+ module Rails
3
+
4
+ # Rack middleware that catches exceptions thrown outside the scope of
5
+ # Rails's request processing. This middleware is automatically added to your
6
+ # stack when you include the `squash_rails` gem.
7
+
8
+ class Rack
9
+
10
+ # Instantiates the middleware.
11
+ #
12
+ # @param [Array] app The middleware stack.
13
+
14
+ def initialize(app)
15
+ @app = app
16
+ end
17
+
18
+ # Rescues any exceptions thrown downstream, notifies Squash, then
19
+ # re-raises them.
20
+ #
21
+ # @param [Hash] env The Rack environment.
22
+ # @return [Hash] The Rack result to pass up the stack.
23
+
24
+ def call(env)
25
+ @env = env
26
+
27
+ begin
28
+ result = @app.call(env)
29
+ rescue ::Exception => ex
30
+ @env['squash.notified'] = ::Squash::Ruby.notify(ex, squash_rack_data)
31
+ raise ex
32
+ end
33
+
34
+ result
35
+ end
36
+
37
+ # @abstract
38
+ #
39
+ # Override this method to implement filtering of sensitive data in the
40
+ # headers, cookies, and rack hashes (see {#squash_rack_data}). The method
41
+ # signature is the same as `Squash::Ruby#filter_for_squash`, but `kind`
42
+ # can also be `:env` for the Rack environment hash.
43
+
44
+ def filter_for_squash(data, kind)
45
+ data
46
+ end
47
+
48
+ # @return [Hash<Symbol, Object>] The additional information this
49
+ # middleware gives to `Squash::Ruby.notify`.
50
+
51
+ def squash_rack_data
52
+ {
53
+ :environment => environment_name,
54
+ :root => root_path,
55
+
56
+ :headers => filter_for_squash(request_headers, :headers),
57
+ :request_method => @env['REQUEST_METHOD'].to_s.upcase,
58
+ :schema => @env['rack.url_scheme'],
59
+ :host => @env['SERVER_NAME'],
60
+ :port => @env['SERVER_PORT'],
61
+ :path => @env['PATH_INFO'],
62
+ :query => @env['QUERY_STRING'],
63
+
64
+ :params => filter_for_squash(@env['action_dispatch.request.parameters'], :params),
65
+ :session => filter_for_squash(@env['rack.session'], :session),
66
+ :flash => filter_for_squash(@env[ActionDispatch::Flash::KEY], :flash),
67
+ :cookies => filter_for_squash(@env['rack.request.cookie_hash'], :cookies),
68
+
69
+ :"rack.env" => filter_for_squash(@env, :rack)
70
+ }
71
+ end
72
+
73
+ private
74
+
75
+ def environment_name
76
+ if defined?(::Rails)
77
+ ::Rails.env.to_s
78
+ else
79
+ ENV['RAILS_ENV'] || ENV['RACK_ENV']
80
+ end
81
+ end
82
+
83
+ # Extract any rack key/value pairs where the key begins with HTTP_*
84
+ def request_headers
85
+ @env.select { |key, _| key[0, 5] == 'HTTP_' }
86
+ end
87
+
88
+ def root_path
89
+ defined?(::Rails) ? ::Rails.root.to_s : ENV['RAILS_ROOT']
90
+ end
91
+ end
92
+ end
93
+ end
94
+
data/lib/squash/rails.rb CHANGED
@@ -25,6 +25,19 @@ end
25
25
  module Squash::Ruby
26
26
  CONFIGURATION_DEFAULTS[:deploy_path] = "/api/1.0/deploy"
27
27
 
28
+ # @private
29
+ def self.failsafe_log(tag, message)
30
+ logger = Rails.respond_to?(:logger) ? Rails.logger : RAILS_DEFAULT_LOGGER
31
+ if (logger.respond_to?(:tagged))
32
+ logger.tagged(tag) { logger.error message }
33
+ else
34
+ logger.error "[#{tag}]\t#{message}"
35
+ end
36
+ rescue Object => err
37
+ $stderr.puts "Couldn't write to failsafe log (#{err.to_s}); writing to stderr instead."
38
+ $stderr.puts "#{Time.now.to_s}\t[#{tag}]\t#{message}"
39
+ end
40
+
28
41
  private
29
42
 
30
43
  def self.client_name() 'rails' end
@@ -17,11 +17,24 @@
17
17
  # versions, see `rails/init.rb`.
18
18
 
19
19
  class Squash::Ruby::Railtie < Rails::Railtie
20
- initializer "squash_client.configure_rails_initialization" do
20
+ initializer "squash_client.configure_rails_initialization" do |app|
21
21
  Squash::Ruby.configure :environment => Rails.env.to_s,
22
22
  :project_root => Rails.root.to_s,
23
23
  :repository_root => Rails.root.to_s,
24
24
  :failsafe_log => Rails.root.join('log', 'squash.failsafe.log').to_s
25
+
26
+ # Load the Rack middleware into the stack at the top.
27
+ if app.respond_to?(:config) && app.config.respond_to?(:middleware)
28
+ require 'squash/rails/rack'
29
+
30
+ if defined?(ActionDispatch::DebugExceptions)
31
+ app.config.middleware.insert_after ActionDispatch::DebugExceptions, Squash::Rails::Rack
32
+ elsif defined?(ActionDispatch::ShowExceptions)
33
+ app.config.middleware.insert_after ActionDispatch::ShowExceptions, Squash::Rails::Rack
34
+ else
35
+ app.config.middleware.insert 0, Squash::Rails::Rack
36
+ end
37
+ end
25
38
  end
26
39
 
27
40
  rake_tasks { load 'squash/rails/tasks.rake' }
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: squash_rails
3
3
  version: !ruby/object:Gem::Version
4
- hash: 23
4
+ hash: 19
5
5
  prerelease:
6
6
  segments:
7
7
  - 1
8
+ - 1
8
9
  - 0
9
- - 0
10
- version: 1.0.0
10
+ version: 1.1.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Tim Morgan
@@ -15,10 +15,9 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2012-12-07 00:00:00 Z
18
+ date: 2013-02-05 00:00:00 Z
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
21
- prerelease: false
22
21
  requirement: &id001 !ruby/object:Gem::Requirement
23
22
  none: false
24
23
  requirements:
@@ -28,11 +27,11 @@ dependencies:
28
27
  segments:
29
28
  - 0
30
29
  version: "0"
31
- type: :runtime
32
- name: squash_ruby
33
30
  version_requirements: *id001
34
- - !ruby/object:Gem::Dependency
31
+ name: squash_ruby
35
32
  prerelease: false
33
+ type: :runtime
34
+ - !ruby/object:Gem::Dependency
36
35
  requirement: &id002 !ruby/object:Gem::Requirement
37
36
  none: false
38
37
  requirements:
@@ -42,11 +41,11 @@ dependencies:
42
41
  segments:
43
42
  - 0
44
43
  version: "0"
45
- type: :development
46
- name: rails
47
44
  version_requirements: *id002
48
- - !ruby/object:Gem::Dependency
45
+ name: rails
49
46
  prerelease: false
47
+ type: :development
48
+ - !ruby/object:Gem::Dependency
50
49
  requirement: &id003 !ruby/object:Gem::Requirement
51
50
  none: false
52
51
  requirements:
@@ -56,11 +55,11 @@ dependencies:
56
55
  segments:
57
56
  - 0
58
57
  version: "0"
59
- type: :development
60
- name: rspec
61
58
  version_requirements: *id003
62
- - !ruby/object:Gem::Dependency
59
+ name: rspec
63
60
  prerelease: false
61
+ type: :development
62
+ - !ruby/object:Gem::Dependency
64
63
  requirement: &id004 !ruby/object:Gem::Requirement
65
64
  none: false
66
65
  requirements:
@@ -70,11 +69,11 @@ dependencies:
70
69
  segments:
71
70
  - 0
72
71
  version: "0"
73
- type: :development
74
- name: yard
75
72
  version_requirements: *id004
76
- - !ruby/object:Gem::Dependency
73
+ name: yard
77
74
  prerelease: false
75
+ type: :development
76
+ - !ruby/object:Gem::Dependency
78
77
  requirement: &id005 !ruby/object:Gem::Requirement
79
78
  none: false
80
79
  requirements:
@@ -84,11 +83,11 @@ dependencies:
84
83
  segments:
85
84
  - 0
86
85
  version: "0"
87
- type: :development
88
- name: redcarpet
89
86
  version_requirements: *id005
90
- - !ruby/object:Gem::Dependency
87
+ name: redcarpet
91
88
  prerelease: false
89
+ type: :development
90
+ - !ruby/object:Gem::Dependency
92
91
  requirement: &id006 !ruby/object:Gem::Requirement
93
92
  none: false
94
93
  requirements:
@@ -98,9 +97,10 @@ dependencies:
98
97
  segments:
99
98
  - 0
100
99
  version: "0"
101
- type: :development
102
- name: jeweler
103
100
  version_requirements: *id006
101
+ name: jeweler
102
+ prerelease: false
103
+ type: :development
104
104
  description: This client library records Ruby on Rails exceptions to Squash.
105
105
  email: tim@squareup.com
106
106
  executables: []
@@ -116,6 +116,7 @@ files:
116
116
  - lib/squash/rails.rb
117
117
  - lib/squash/rails/capistrano.rb
118
118
  - lib/squash/rails/configure.rb
119
+ - lib/squash/rails/rack.rb
119
120
  - lib/squash/rails/tasks.rake
120
121
  - lib/squash/ruby/controller_methods.rb
121
122
  - lib/squash/ruby/railtie.rb