Skip to content

Commit 7e83499

Browse files
committed
Merge pull request #221 from getsentry/add-configurable-callback-issue-167
Add configurable callback for deciding whether or not to send an event
2 parents b268176 + 6cc385d commit 7e83499

File tree

4 files changed

+37
-4
lines changed

4 files changed

+37
-4
lines changed

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,16 @@ end
209209

210210
You can find the list of exceptions that are excluded by default in [Raven::Configuration::IGNORE_DEFAULT](https://github.com/getsentry/raven-ruby/blob/master/lib/raven/configuration.rb). Remember you'll be overriding those defaults by setting this configuration.
211211

212+
You can also use a configuration option to determine if an individual event should
213+
be sent to Sentry. Events are passed to the Proc or lambda you provide - returning
214+
`false` will stop the event from sending to Sentry:
215+
216+
```ruby
217+
Raven.configure do |config|
218+
config.should_send = Proc.new { |e| true unless e.contains_sensitive_info? }
219+
end
220+
```
221+
212222
### Tags
213223

214224
You can configure default tags to be sent with every event. These can be

lib/raven/base.rb

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ def capture(options = {}, &block)
101101
end
102102

103103
def capture_exception(exception, options = {})
104-
send_or_skip do
104+
send_or_skip(exception) do
105105
if evt = Event.from_exception(exception, options)
106106
yield evt if block_given?
107107
if configuration.async?
@@ -114,7 +114,7 @@ def capture_exception(exception, options = {})
114114
end
115115

116116
def capture_message(message, options = {})
117-
send_or_skip do
117+
send_or_skip(message) do
118118
if evt = Event.from_message(message, options)
119119
yield evt if block_given?
120120
if configuration.async?
@@ -126,8 +126,14 @@ def capture_message(message, options = {})
126126
end
127127
end
128128

129-
def send_or_skip
130-
if configuration.send_in_current_environment?
129+
def send_or_skip(exc)
130+
should_send = if configuration.should_send
131+
configuration.should_send.call(*[exc])
132+
else
133+
true
134+
end
135+
136+
if configuration.send_in_current_environment? && should_send
131137
yield if block_given?
132138
else
133139
configuration.log_excluded_environment_message

lib/raven/configuration.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,9 @@ class Configuration
8080
# ActionDispatch::ShowExceptions or ActionDispatch::DebugExceptions
8181
attr_accessor :catch_debugged_exceptions
8282

83+
# Provide a configurable callback to block or send events
84+
attr_accessor :should_send
85+
8386
IGNORE_DEFAULT = ['ActiveRecord::RecordNotFound',
8487
'ActionController::RoutingError',
8588
'ActionController::InvalidAuthenticityToken',

spec/raven/raven_spec.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,20 @@
7070
end
7171
end
7272

73+
describe '.capture_exception with a should_send callback' do
74+
let(:exception) { build_exception }
75+
76+
it 'sends the result of Event.capture_exception according to the result of should_send' do
77+
expect(Raven).not_to receive(:send).with(event)
78+
79+
prior_should_send = Raven.configuration.should_send
80+
Raven.configuration.should_send = Proc.new { |e| false }
81+
expect(Raven.configuration.should_send).to receive(:call).with(exception)
82+
Raven.capture_exception(exception, options)
83+
Raven.configuration.should_send = prior_should_send
84+
end
85+
end
86+
7387
describe '.annotate_exception' do
7488
let(:exception) { build_exception }
7589

0 commit comments

Comments
 (0)