Skip to content

Commit 4e840e6

Browse files
authored
Don't initialize Event objects when they won't be sent (#1687)
* Don't initialize Event objects when they won't be sent * Update changelog
1 parent 7697af6 commit 4e840e6

File tree

4 files changed

+13
-1
lines changed

4 files changed

+13
-1
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
- Add workaround for ConnectionStub's missing interface [#1686](https://github.com/getsentry/sentry-ruby/pull/1686)
66
- Fixes [#1685](https://github.com/getsentry/sentry-ruby/issues/1685)
7+
- Don't initialize Event objects when they won't be sent [#1687](https://github.com/getsentry/sentry-ruby/pull/1687)
8+
- Fixes [#1683](https://github.com/getsentry/sentry-ruby/issues/1683)
79

810
## 4.9.0
911

sentry-ruby/lib/sentry/client.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,9 @@ def capture_event(event, scope, hint = {})
7676
# @param hint [Hash] the hint data that'll be passed to `before_send` callback and the scope's event processors.
7777
# @return [Event, nil]
7878
def event_from_exception(exception, hint = {})
79+
return unless @configuration.sending_allowed? && @configuration.exception_class_allowed?(exception)
80+
7981
integration_meta = Sentry.integrations[hint[:integration]]
80-
return unless @configuration.exception_class_allowed?(exception)
8182

8283
Event.new(configuration: configuration, integration_meta: integration_meta).tap do |event|
8384
event.add_exception_interface(exception)
@@ -90,6 +91,8 @@ def event_from_exception(exception, hint = {})
9091
# @param hint [Hash] the hint data that'll be passed to `before_send` callback and the scope's event processors.
9192
# @return [Event]
9293
def event_from_message(message, hint = {}, backtrace: nil)
94+
return unless @configuration.sending_allowed?
95+
9396
integration_meta = Sentry.integrations[hint[:integration]]
9497
event = Event.new(configuration: configuration, integration_meta: integration_meta, message: message)
9598
event.add_threads_interface(backtrace: backtrace || caller)

sentry-ruby/lib/sentry/hub.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,9 @@ def capture_message(message, **options, &block)
114114
options[:hint][:message] = message
115115
backtrace = options.delete(:backtrace)
116116
event = current_client.event_from_message(message, options[:hint], backtrace: backtrace)
117+
118+
return unless event
119+
117120
capture_event(event, **options, &block)
118121
end
119122

sentry-ruby/spec/sentry_spec.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,9 +102,13 @@
102102
context "with sending_allowed? condition" do
103103
before do
104104
expect(Sentry.configuration).to receive(:sending_allowed?).and_return(false)
105+
capture_subject
105106
end
106107

107108
it "doesn't send the event nor assign last_event_id" do
109+
# don't even initialize Event objects
110+
expect(Sentry::Event).not_to receive(:new)
111+
108112
described_class.send(capture_helper, capture_subject)
109113

110114
expect(transport.events).to be_empty

0 commit comments

Comments
 (0)