From e6e29ab11a6784036fb807a2af86fb6d8b134e56 Mon Sep 17 00:00:00 2001 From: Jon de Andres Date: Tue, 20 Dec 2016 16:59:30 +0100 Subject: [PATCH 1/2] Add Rollbar::Middleware::Rack Convert `Rollbar::Middleware::Rack` to be a class instead of a module, so it can be added to the Rack middleware chain with `use Rollbar::Middleware::Rack`. Also, move the code from `Rollbar::Middleware::Sinatra` to `Rollbar::Middleware::Rack` since Sinatra behavior is a subset of Rack behavior. --- lib/rollbar/middleware/rack.rb | 49 +++++++++++++++++++++ lib/rollbar/middleware/rack/builder.rb | 2 +- lib/rollbar/middleware/rack/test_session.rb | 2 +- lib/rollbar/middleware/sinatra.rb | 42 +----------------- lib/rollbar/plugins/rack.rb | 2 + spec/rollbar/plugins/rack_spec.rb | 4 +- 6 files changed, 57 insertions(+), 44 deletions(-) create mode 100644 lib/rollbar/middleware/rack.rb diff --git a/lib/rollbar/middleware/rack.rb b/lib/rollbar/middleware/rack.rb new file mode 100644 index 00000000..69391104 --- /dev/null +++ b/lib/rollbar/middleware/rack.rb @@ -0,0 +1,49 @@ +require 'rollbar' +require 'rollbar/exception_reporter' +require 'rollbar/request_data_extractor' + +module Rollbar + module Middleware + class Rack + include ::Rollbar::ExceptionReporter + include RequestDataExtractor + + def initialize(app) + @app = app + end + + def call(env) + Rollbar.reset_notifier! + + Rollbar.scoped(fetch_scope(env)) do + begin + response = @app.call(env) + report_exception_to_rollbar(env, framework_error(env)) if framework_error(env) + response + rescue Exception => e + report_exception_to_rollbar(env, e) + raise + end + end + end + + def fetch_scope(env) + { + :request => proc { extract_request_data_from_rack(env) }, + :person => person_data_proc(env) + } + rescue Exception => e + report_exception_to_rollbar(env, e) + raise + end + + def person_data_proc(env) + proc { extract_person_data_from_controller(env) } + end + + def framework_error(env) + nil + end + end + end +end diff --git a/lib/rollbar/middleware/rack/builder.rb b/lib/rollbar/middleware/rack/builder.rb index eaf9425a..7426f38c 100644 --- a/lib/rollbar/middleware/rack/builder.rb +++ b/lib/rollbar/middleware/rack/builder.rb @@ -3,7 +3,7 @@ module Rollbar module Middleware - module Rack + class Rack module Builder include ExceptionReporter include RequestDataExtractor diff --git a/lib/rollbar/middleware/rack/test_session.rb b/lib/rollbar/middleware/rack/test_session.rb index c6cbc112..ceb7c200 100644 --- a/lib/rollbar/middleware/rack/test_session.rb +++ b/lib/rollbar/middleware/rack/test_session.rb @@ -1,6 +1,6 @@ module Rollbar module Middleware - module Rack + class Rack module TestSession include ExceptionReporter diff --git a/lib/rollbar/middleware/sinatra.rb b/lib/rollbar/middleware/sinatra.rb index 47d4ba01..003db825 100644 --- a/lib/rollbar/middleware/sinatra.rb +++ b/lib/rollbar/middleware/sinatra.rb @@ -1,46 +1,8 @@ -require 'rollbar' -require 'rollbar/exception_reporter' -require 'rollbar/request_data_extractor' +require 'rollbar/middleware/rack' module Rollbar module Middleware - class Sinatra - include ::Rollbar::ExceptionReporter - include RequestDataExtractor - - def initialize(app) - @app = app - end - - def call(env) - Rollbar.reset_notifier! - - Rollbar.scoped(fetch_scope(env)) do - begin - response = @app.call(env) - report_exception_to_rollbar(env, framework_error(env)) if framework_error(env) - response - rescue Exception => e - report_exception_to_rollbar(env, e) - raise - end - end - end - - def fetch_scope(env) - { - :request => proc { extract_request_data_from_rack(env) }, - :person => person_data_proc(env) - } - rescue Exception => e - report_exception_to_rollbar(env, e) - raise - end - - def person_data_proc(env) - proc { extract_person_data_from_controller(env) } - end - + class Sinatra < Rollbar::Middleware::Rack def framework_error(env) env['sinatra.error'] end diff --git a/lib/rollbar/plugins/rack.rb b/lib/rollbar/plugins/rack.rb index 3e35328e..b4855805 100644 --- a/lib/rollbar/plugins/rack.rb +++ b/lib/rollbar/plugins/rack.rb @@ -4,11 +4,13 @@ execute do if defined?(Rack::Builder) + require 'rollbar/middleware/rack' require 'rollbar/middleware/rack/builder' Rack::Builder.send(:include, Rollbar::Middleware::Rack::Builder) end if defined?(Rack::Test::Session) + require 'rollbar/middleware/rack' require 'rollbar/middleware/rack/test_session' Rack::Test::Session.send(:include, Rollbar::Middleware::Rack::TestSession) end diff --git a/spec/rollbar/plugins/rack_spec.rb b/spec/rollbar/plugins/rack_spec.rb index f62c54bf..b0a6d2c3 100644 --- a/spec/rollbar/plugins/rack_spec.rb +++ b/spec/rollbar/plugins/rack_spec.rb @@ -16,11 +16,11 @@ class RackMockError < Exception; end let(:app) do action_proc = action - Rack::Builder.new { run action_proc } + ::Rack::Builder.new { run action_proc } end let(:request) do - Rack::MockRequest.new(app) + ::Rack::MockRequest.new(app) end let(:exception) { kind_of(RackMockError) } From 75c55d3fe569c13a205a94957114a85126d2f5b6 Mon Sep 17 00:00:00 2001 From: Jon de Andres Date: Tue, 20 Dec 2016 18:45:17 +0100 Subject: [PATCH 2/2] Add docs for Rollbar::Middleware::Rack [skip ci] --- README.md | 43 ++++++++++++++++++++++++++++++++----------- 1 file changed, 32 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index d13c3b0f..4f0dac7c 100644 --- a/README.md +++ b/README.md @@ -62,6 +62,31 @@ $ heroku config:add ROLLBAR_ACCESS_TOKEN=POST_SERVER_ITEM_ACCESS_TOKEN That's all you need to use Rollbar with Rails. +### Sinatra + +Initialize Rollbar with your access token somewhere during startup: + +```ruby +Rollbar.configure do |config| + config.access_token = 'POST_SERVER_ITEM_ACCESS_TOKEN' + # other configuration settings + # ... +end +``` + +Then mount the middleware in your app, like: + +```ruby +require 'rollbar/middleware/sinatra' + +class MyApp < Sinatra::Base + use Rollbar::Middleware::Sinatra + # other middleware/etc + # ... +end +``` + + ### Rack Initialize Rollbar with your access token somewhere during startup: @@ -77,28 +102,22 @@ end Be sure to replace ```POST_SERVER_ITEM_ACCESS_TOKEN``` with your project's ```post_server_item``` access token, which you can find in the Rollbar.com interface. -This monkey patches `Rack::Builder` to work with Rollbar automatically. - -For more control, disable the monkey patch: +The gem monkey patches `Rack::Builder` so Rollbar reports will be sent automatically without any other action. If you prefer to disable the monkey patch apply this change to your config: ```ruby Rollbar.configure do |config| - config.disable_monkey_patch = true + config.disable_rack_monkey_patch = true # other configuration settings # ... end ``` -Then mount the middleware in your app, like: +If you disabled the `Rack::Builder` monkey patch or it doesn't work for the Rack framework you are using, then add our Rack middleware to your app: ```ruby -require 'rollbar/middleware/sinatra' +require 'rollbar/middleware/rack -class MyApp < Sinatra::Base - use Rollbar::Middleware::Sinatra - # other middleware/etc - # ... -end +use Rollbar::Middleware::Rack ``` ### Plain Ruby @@ -371,6 +390,8 @@ end # You can add the middleware to your application, for example: +require 'rollbar/middleware/sinatra' + class App < Sinatra::Base use Rollbar::Middleware::Sinatra use RollbarPersonData