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 convenience scope! method to Rollbar public interface. #212

Merged
merged 4 commits into from
Feb 4, 2015
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
20 changes: 19 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ notifier = notifier.scope({
notifier.info('Jobs processed')
```

If you don't want to work with a new `Notifier` instance `#scoped` will do it for you:
If you don't want to work with a new `Notifier` instance `.scoped` will do it for you:

```ruby
while job
Expand All @@ -208,6 +208,24 @@ while job
end
```

To modify the current scope (rather than creating a new one), use `Rollbar.scope!`. You can use this to add additional context data from inside a web request, background job, etc.

```ruby
class NotificationJob
include Sidekiq::Worker

def perform(user_id)
Rollbar.scope!(:person => { :id => :user_id })

# If this next line causes an exception, the reported exception (which will
# be reported by Rollbar's standard Sidekiq instrumentation) will also
# include the above person information.
Notification.send_to_user(user_id)
end
end
```


## Person tracking

Rollbar will send information about the current user (called a "person" in Rollbar parlance) along with each error report, when available. This works by calling the ```current_user``` controller method. The return value should be an object with an ```id``` method and, optionally, ```username``` and ```email``` methods.
Expand Down
9 changes: 9 additions & 0 deletions lib/rollbar.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@ def scope(options = {})
self.class.new(self, options)
end

def scope!(options = {})
Rollbar::Util.deep_merge(@configuration.payload_options, options)
self
end

# Turns off reporting for the given block.
#
# @example
Expand Down Expand Up @@ -753,6 +758,10 @@ def scoped(options = {})
self.notifier = old_notifier
end

def scope!(options = {})
notifier.scope!(options)
end

# Backwards compatibility methods

def report_exception(exception, request_data = nil, person_data = nil, level = 'error')
Expand Down
15 changes: 15 additions & 0 deletions spec/rollbar_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1581,6 +1581,21 @@ class DummyClass
end
end

describe '.scope!' do
let(:new_scope) do
{ :person => { :id => 1 } }
end

before { reconfigure_notifier }

it 'adds the new scope to the payload options' do
configuration = Rollbar.notifier.configuration
Rollbar.scope!(new_scope)

expect(configuration.payload_options).to be_eql(new_scope)
end
end

describe '.reset_notifier' do
it 'resets the notifier' do
notifier1_id = Rollbar.notifier.object_id
Expand Down