Skip to content

Commit

Permalink
Merge pull request #212 from rollbar/override-scope-method
Browse files Browse the repository at this point in the history
Add convenience scope! method to Rollbar public interface.
  • Loading branch information
brianr committed Feb 4, 2015
2 parents 86a9b69 + 794a1cc commit 0f8161f
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 1 deletion.
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

0 comments on commit 0f8161f

Please sign in to comment.