Skip to content

Commit 2fe24cd

Browse files
authored
Merge pull request #518 from nateberkopec/no-capture-when-no-dsn
Do not capture if env doesn't match or DSN not set. Close #513.
2 parents 8ccd00c + 6578655 commit 2fe24cd

File tree

8 files changed

+34
-36
lines changed

8 files changed

+34
-36
lines changed

lib/raven/base.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ def instance
3434

3535
def_delegators :instance, :client=, :configuration=, :context, :logger, :configuration,
3636
:client, :report_status, :configure, :send_event, :capture, :capture_type,
37-
:last_event_id, :should_capture?, :annotate_exception, :user_context,
37+
:last_event_id, :annotate_exception, :user_context,
3838
:tags_context, :extra_context, :rack_context, :breadcrumbs
3939

4040
def_delegator :instance, :report_status, :report_ready

lib/raven/cli.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ def self.test(dsn = nil)
1414
Raven.configuration.dsn = dsn if dsn
1515

1616
# wipe out env settings to ensure we send the event
17-
unless Raven.configuration.send_in_current_environment?
17+
unless Raven.configuration.capture_in_current_environment?
1818
env_name = Raven.configuration.environments.pop || 'production'
1919
puts "Setting environment to #{env_name}"
2020
Raven.configuration.current_environment = env_name

lib/raven/client.rb

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def initialize(configuration)
2222
end
2323

2424
def send_event(event)
25-
return false unless configuration_allows_sending
25+
return false unless configuration.sending_allowed?(event)
2626

2727
# Convert to hash
2828
event = event.to_hash
@@ -62,15 +62,6 @@ def transport
6262

6363
private
6464

65-
def configuration_allows_sending
66-
if configuration.send_in_current_environment?
67-
true
68-
else
69-
configuration.log_excluded_environment_message
70-
false
71-
end
72-
end
73-
7465
def encode(event)
7566
hash = @processors.reduce(event.to_hash) { |memo, p| p.process(memo) }
7667
encoded = JSON.generate(hash)

lib/raven/configuration.rb

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -215,12 +215,21 @@ def current_environment=(environment)
215215
@current_environment = environment.to_s
216216
end
217217

218-
def send_in_current_environment?
218+
def capture_allowed?(message_or_exc)
219+
capture_in_current_environment? &&
220+
capture_allowed_by_callback?(message_or_exc)
221+
end
222+
223+
# If we cannot capture, we cannot send.
224+
alias sending_allowed? capture_allowed?
225+
226+
def capture_in_current_environment?
219227
!!server && (environments.empty? || environments.include?(current_environment))
220228
end
221229

222-
def log_excluded_environment_message
223-
Raven.logger.debug "Event not sent due to excluded environment: #{current_environment}"
230+
def capture_allowed_by_callback?(message_or_exc)
231+
return true unless should_capture
232+
should_capture.call(*[message_or_exc])
224233
end
225234

226235
def verify!

lib/raven/instance.rb

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,11 @@ def client
6060

6161
# Tell the log that the client is good to go
6262
def report_status
63-
return if client.configuration.silence_ready
64-
if client.configuration.send_in_current_environment?
63+
return if configuration.silence_ready
64+
if configuration.capture_in_current_environment?
6565
logger.info "Raven #{VERSION} ready to catch errors"
6666
else
67-
logger.info "Raven #{VERSION} configured not to send errors."
67+
logger.info "Raven #{VERSION} configured not to capture errors."
6868
end
6969
end
7070

@@ -113,7 +113,11 @@ def capture(options = {})
113113
end
114114

115115
def capture_type(obj, options = {})
116-
return false unless should_capture?(obj)
116+
unless configuration.capture_allowed?(obj)
117+
Raven.logger.debug("#{obj} excluded from capture due to environment or should_capture callback")
118+
return false
119+
end
120+
117121
message_or_exc = obj.is_a?(String) ? "message" : "exception"
118122
if (evt = Event.send("from_" + message_or_exc, obj, options))
119123
yield evt if block_given?
@@ -131,14 +135,6 @@ def last_event_id
131135
Thread.current["sentry_#{object_id}_last_event_id".to_sym]
132136
end
133137

134-
def should_capture?(message_or_exc)
135-
if configuration.should_capture
136-
configuration.should_capture.call(*[message_or_exc])
137-
else
138-
true
139-
end
140-
end
141-
142138
# Provides extra context to the exception prior to it being handled by
143139
# Raven. An exception can have multiple annotations, which are merged
144140
# together.

spec/raven/configuration_spec.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@
6666

6767
it 'should not send events' do
6868
expect(subject[:server]).to eq(nil)
69-
expect(subject.send_in_current_environment?).to eq(false)
69+
expect(subject.capture_in_current_environment?).to eq(false)
7070
end
7171
end
7272

@@ -131,12 +131,12 @@
131131

132132
it 'should send events if test is whitelisted' do
133133
subject.environments = %w[test]
134-
expect(subject.send_in_current_environment?).to eq(true)
134+
expect(subject.capture_in_current_environment?).to eq(true)
135135
end
136136

137137
it 'should not send events if test is not whitelisted' do
138138
subject.environments = %w[not_test]
139-
expect(subject.send_in_current_environment?).to eq(false)
139+
expect(subject.capture_in_current_environment?).to eq(false)
140140
end
141141
end
142142

spec/raven/instance_spec.rb

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
allow(subject).to receive(:send_event)
1313
allow(Raven::Event).to receive(:from_message) { event }
1414
allow(Raven::Event).to receive(:from_exception) { event }
15+
16+
subject.configuration.dsn = "dummy://woopwoop"
1517
end
1618

1719
describe '#context' do
@@ -157,13 +159,13 @@ def ivars(object)
157159
end
158160

159161
let(:not_ready_message) do
160-
"Raven #{Raven::VERSION} configured not to send errors."
162+
"Raven #{Raven::VERSION} configured not to capture errors."
161163
end
162164

163165
it 'logs a ready message when configured' do
164166
subject.configuration.silence_ready = false
165167
expect(subject.configuration).to(
166-
receive(:send_in_current_environment?).and_return(true)
168+
receive(:capture_in_current_environment?).and_return(true)
167169
)
168170
expect(subject.logger).to receive(:info).with(ready_message)
169171
subject.report_status
@@ -172,7 +174,7 @@ def ivars(object)
172174
it 'logs not ready message if the config does not send in current environment' do
173175
subject.configuration.silence_ready = false
174176
expect(subject.configuration).to(
175-
receive(:send_in_current_environment?).and_return(false)
177+
receive(:capture_in_current_environment?).and_return(false)
176178
)
177179
expect(subject.logger).to receive(:info).with(not_ready_message)
178180
subject.report_status

spec/raven/raven_spec.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -176,13 +176,13 @@ def ivars(object)
176176
end
177177

178178
let(:not_ready_message) do
179-
"Raven #{Raven::VERSION} configured not to send errors."
179+
"Raven #{Raven::VERSION} configured not to capture errors."
180180
end
181181

182182
it 'logs a ready message when configured' do
183183
Raven.configuration.silence_ready = false
184184
expect(Raven.configuration).to(
185-
receive(:send_in_current_environment?).and_return(true)
185+
receive(:capture_in_current_environment?).and_return(true)
186186
)
187187
expect(Raven.logger).to receive(:info).with(ready_message)
188188
Raven.report_status
@@ -191,7 +191,7 @@ def ivars(object)
191191
it 'logs not ready message if the config does not send in current environment' do
192192
Raven.configuration.silence_ready = false
193193
expect(Raven.configuration).to(
194-
receive(:send_in_current_environment?).and_return(false)
194+
receive(:capture_in_current_environment?).and_return(false)
195195
)
196196
expect(Raven.logger).to receive(:info).with(not_ready_message)
197197
Raven.report_status

0 commit comments

Comments
 (0)