Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Rollbar::Middleware::Rack #558

Merged
merged 2 commits into from
Dec 20, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 32 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -77,28 +102,22 @@ end
<!-- RemoveNextIfProject -->
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missing an end '

class MyApp < Sinatra::Base
use Rollbar::Middleware::Sinatra
# other middleware/etc
# ...
end
use Rollbar::Middleware::Rack
```
### Plain Ruby
Expand Down Expand Up @@ -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
Expand Down
49 changes: 49 additions & 0 deletions lib/rollbar/middleware/rack.rb
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion lib/rollbar/middleware/rack/builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

module Rollbar
module Middleware
module Rack
class Rack
module Builder
include ExceptionReporter
include RequestDataExtractor
Expand Down
2 changes: 1 addition & 1 deletion lib/rollbar/middleware/rack/test_session.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module Rollbar
module Middleware
module Rack
class Rack
module TestSession
include ExceptionReporter

Expand Down
42 changes: 2 additions & 40 deletions lib/rollbar/middleware/sinatra.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 2 additions & 0 deletions lib/rollbar/plugins/rack.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions spec/rollbar/plugins/rack_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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) }
Expand Down