Skip to content

Commit

Permalink
Don't initialize Event objects when they won't be sent (#1687)
Browse files Browse the repository at this point in the history
* Don't initialize Event objects when they won't be sent

* Update changelog
  • Loading branch information
st0012 authored Jan 14, 2022
1 parent 7697af6 commit 4e840e6
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

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

## 4.9.0

Expand Down
5 changes: 4 additions & 1 deletion sentry-ruby/lib/sentry/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,9 @@ def capture_event(event, scope, hint = {})
# @param hint [Hash] the hint data that'll be passed to `before_send` callback and the scope's event processors.
# @return [Event, nil]
def event_from_exception(exception, hint = {})
return unless @configuration.sending_allowed? && @configuration.exception_class_allowed?(exception)

integration_meta = Sentry.integrations[hint[:integration]]
return unless @configuration.exception_class_allowed?(exception)

Event.new(configuration: configuration, integration_meta: integration_meta).tap do |event|
event.add_exception_interface(exception)
Expand All @@ -90,6 +91,8 @@ def event_from_exception(exception, hint = {})
# @param hint [Hash] the hint data that'll be passed to `before_send` callback and the scope's event processors.
# @return [Event]
def event_from_message(message, hint = {}, backtrace: nil)
return unless @configuration.sending_allowed?

integration_meta = Sentry.integrations[hint[:integration]]
event = Event.new(configuration: configuration, integration_meta: integration_meta, message: message)
event.add_threads_interface(backtrace: backtrace || caller)
Expand Down
3 changes: 3 additions & 0 deletions sentry-ruby/lib/sentry/hub.rb
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,9 @@ def capture_message(message, **options, &block)
options[:hint][:message] = message
backtrace = options.delete(:backtrace)
event = current_client.event_from_message(message, options[:hint], backtrace: backtrace)

return unless event

capture_event(event, **options, &block)
end

Expand Down
4 changes: 4 additions & 0 deletions sentry-ruby/spec/sentry_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,13 @@
context "with sending_allowed? condition" do
before do
expect(Sentry.configuration).to receive(:sending_allowed?).and_return(false)
capture_subject
end

it "doesn't send the event nor assign last_event_id" do
# don't even initialize Event objects
expect(Sentry::Event).not_to receive(:new)

described_class.send(capture_helper, capture_subject)

expect(transport.events).to be_empty
Expand Down

0 comments on commit 4e840e6

Please sign in to comment.