Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't do anything in Sentry unless enabled_in_current_env? #1682

Closed
wants to merge 1 commit into from

Conversation

henrahmagix
Copy link
Contributor

Otherwise Sentry can sometimes cause errors when trying to capture exceptions in tests, when test is not an enabled environment.

This has occurred for me when my system reaches its ulimit during many, many subsequent app requests in Capybara tests, which manifests as RuntimeError: failed to get urandom. Because Sentry captures the app exception, and then starts to build reporting around it, it also runs code that causes RuntimeError: failed to get urandom.

I then end up with a test failure report that only shows the trace of the Sentry exception, not the original app exception. Therefore I think this can be avoided by not doing anything in Sentry unless it's enabled for the current environment, because everything it does would end up being thrown away, if my thinking is correct?

For example:

Failure/Error: ret = Random.urandom(n)

RuntimeError:
  failed to get urandom
# /Users/me/path/to/gems/sentry-ruby-core-4.7.3/lib/sentry/event.rb:34:in `initialize'
# /Users/me/path/to/gems/sentry-ruby-core-4.7.3/lib/sentry/client.rb:54:in `new'
# /Users/me/path/to/gems/sentry-ruby-core-4.7.3/lib/sentry/client.rb:54:in `event_from_exception'
# /Users/me/path/to/gems/sentry-ruby-core-4.7.3/lib/sentry/hub.rb:99:in `capture_exception'
# /Users/me/path/to/gems/sentry-ruby-core-4.7.3/lib/sentry-ruby.rb:181:in `capture_exception'
# /Users/me/path/to/gems/sentry-ruby-core-4.7.3/lib/sentry/integrable.rb:15:in `capture_exception'
# /Users/me/path/to/gems/sentry-rails-4.7.3/lib/sentry/rails/capture_exceptions.rb:30:in `capture_exception'
# /Users/me/path/to/gems/sentry-ruby-core-4.7.3/lib/sentry/rack/capture_exceptions.rb:28:in `rescue in block in call'
# /Users/me/path/to/gems/sentry-ruby-core-4.7.3/lib/sentry/rack/capture_exceptions.rb:22:in `block in call'
# /Users/me/path/to/gems/sentry-ruby-core-4.7.3/lib/sentry/hub.rb:56:in `with_scope'
# /Users/me/path/to/gems/sentry-ruby-core-4.7.3/lib/sentry-ruby.rb:176:in `with_scope'
# /Users/me/path/to/gems/sentry-ruby-core-4.7.3/lib/sentry/rack/capture_exceptions.rb:14:in `call'
# /Users/me/path/to/gems/rack-2.2.3/lib/rack/sendfile.rb:110:in `call'
# /Users/me/path/to/gems/rack-cors-1.1.1/lib/rack/cors.rb:100:in `call'
# /Users/me/path/to/gems/railties-6.0.4.1/lib/rails/engine.rb:527:in `call'
# /Users/me/path/to/gems/rack-2.2.3/lib/rack/urlmap.rb:74:in `block in call'
# /Users/me/path/to/gems/rack-2.2.3/lib/rack/urlmap.rb:58:in `each'
# /Users/me/path/to/gems/rack-2.2.3/lib/rack/urlmap.rb:58:in `call'
# /Users/me/path/to/gems/capybara-3.32.2/lib/capybara/server/middleware.rb:58:in `call'
# /Users/me/path/to/gems/puma-5.5.1/lib/puma/configuration.rb:249:in `call'
# /Users/me/path/to/gems/puma-5.5.1/lib/puma/request.rb:77:in `block in handle_request'
# /Users/me/path/to/gems/puma-5.5.1/lib/puma/thread_pool.rb:340:in `with_force_shutdown'
# /Users/me/path/to/gems/puma-5.5.1/lib/puma/request.rb:76:in `handle_request'
# /Users/me/path/to/gems/puma-5.5.1/lib/puma/server.rb:447:in `process_client'
# /Users/me/path/to/gems/puma-5.5.1/lib/puma/thread_pool.rb:147:in `block in spawn_thread'
# ------------------
# --- Caused by: ---
# RuntimeError:
#   failed to get urandom
#   /Users/me/path/to/gems/rack-2.2.3/lib/rack/method_override.rb:24:in `call'

Thanks for your Pull Request 🎉

Please keep these instructions in mind so we can review it more efficiently:

  • Add the references of all the related issues/PRs in the description
  • Whether it's a new feature or a bug fix, make sure they're covered by new test cases
  • If this PR contains any refactoring work, please give it its own commit(s)
  • Finally, please add an entry to the corresponding changelog

Other Notes

  • We squash all commits before merging
  • We generally review new PRs within a week
  • If you have any question, you can ask for feedback in our discord community first

Description

Describe your changes:

Otherwise Sentry can sometimes cause errors when trying to capture exceptions in tests, when `test` is not an enabled environment.

This has occurred for me when my system reaches its `ulimit` during many, many subsequent app requests in Capybara tests, which manifests as `RuntimeError: failed to get urandom`. Because Sentry captures the app exception, and then starts to build reporting around it, it also runs code that causes `RuntimeError: failed to get urandom`.

I then end up with a test failure report that only shows the trace of the Sentry exception, not the original app exception. Therefore I think this can be avoided by not doing anything in Sentry unless it's enabled for the current environment, because everything it does would end up being thrown away, if my thinking is correct?

For example:
```
Failure/Error: ret = Random.urandom(n)

RuntimeError:
  failed to get urandom
# /Users/me/path/to/gems/sentry-ruby-core-4.7.3/lib/sentry/event.rb:34:in `initialize'
# /Users/me/path/to/gems/sentry-ruby-core-4.7.3/lib/sentry/client.rb:54:in `new'
# /Users/me/path/to/gems/sentry-ruby-core-4.7.3/lib/sentry/client.rb:54:in `event_from_exception'
# /Users/me/path/to/gems/sentry-ruby-core-4.7.3/lib/sentry/hub.rb:99:in `capture_exception'
# /Users/me/path/to/gems/sentry-ruby-core-4.7.3/lib/sentry-ruby.rb:181:in `capture_exception'
# /Users/me/path/to/gems/sentry-ruby-core-4.7.3/lib/sentry/integrable.rb:15:in `capture_exception'
# /Users/me/path/to/gems/sentry-rails-4.7.3/lib/sentry/rails/capture_exceptions.rb:30:in `capture_exception'
# /Users/me/path/to/gems/sentry-ruby-core-4.7.3/lib/sentry/rack/capture_exceptions.rb:28:in `rescue in block in call'
# /Users/me/path/to/gems/sentry-ruby-core-4.7.3/lib/sentry/rack/capture_exceptions.rb:22:in `block in call'
# /Users/me/path/to/gems/sentry-ruby-core-4.7.3/lib/sentry/hub.rb:56:in `with_scope'
# /Users/me/path/to/gems/sentry-ruby-core-4.7.3/lib/sentry-ruby.rb:176:in `with_scope'
# /Users/me/path/to/gems/sentry-ruby-core-4.7.3/lib/sentry/rack/capture_exceptions.rb:14:in `call'
# /Users/me/path/to/gems/rack-2.2.3/lib/rack/sendfile.rb:110:in `call'
# /Users/me/path/to/gems/rack-cors-1.1.1/lib/rack/cors.rb:100:in `call'
# /Users/me/path/to/gems/railties-6.0.4.1/lib/rails/engine.rb:527:in `call'
# /Users/me/path/to/gems/rack-2.2.3/lib/rack/urlmap.rb:74:in `block in call'
# /Users/me/path/to/gems/rack-2.2.3/lib/rack/urlmap.rb:58:in `each'
# /Users/me/path/to/gems/rack-2.2.3/lib/rack/urlmap.rb:58:in `call'
# /Users/me/path/to/gems/capybara-3.32.2/lib/capybara/server/middleware.rb:58:in `call'
# /Users/me/path/to/gems/puma-5.5.1/lib/puma/configuration.rb:249:in `call'
# /Users/me/path/to/gems/puma-5.5.1/lib/puma/request.rb:77:in `block in handle_request'
# /Users/me/path/to/gems/puma-5.5.1/lib/puma/thread_pool.rb:340:in `with_force_shutdown'
# /Users/me/path/to/gems/puma-5.5.1/lib/puma/request.rb:76:in `handle_request'
# /Users/me/path/to/gems/puma-5.5.1/lib/puma/server.rb:447:in `process_client'
# /Users/me/path/to/gems/puma-5.5.1/lib/puma/thread_pool.rb:147:in `block in spawn_thread'
# ------------------
# --- Caused by: ---
# RuntimeError:
#   failed to get urandom
#   /Users/me/path/to/gems/rack-2.2.3/lib/rack/method_override.rb:24:in `call'
```
@codecov-commenter
Copy link

codecov-commenter commented Jan 13, 2022

Codecov Report

Merging #1682 (aa308ea) into master (6d312f8) will increase coverage by 0.03%.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #1682      +/-   ##
==========================================
+ Coverage   98.46%   98.50%   +0.03%     
==========================================
  Files         136      136              
  Lines        7667     7667              
==========================================
+ Hits         7549     7552       +3     
+ Misses        118      115       -3     
Impacted Files Coverage Δ
sentry-ruby/lib/sentry/rack/capture_exceptions.rb 95.00% <100.00%> (ø)
sentry-ruby/lib/sentry/breadcrumb.rb 100.00% <0.00%> (+3.70%) ⬆️
sentry-ruby/lib/sentry/background_worker.rb 100.00% <0.00%> (+5.40%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 6d312f8...aa308ea. Read the comment docs.

@henrahmagix
Copy link
Contributor Author

This should fix #1683

@cvoege
Copy link

cvoege commented Jan 13, 2022

This would fix an issue I'm having as well! Same issue as AppSignal just fixed: appsignal/appsignal-ruby#684

@st0012
Copy link
Collaborator

st0012 commented Jan 15, 2022

As I explained in #1683, I think this fix isn't the best solution for the problem and the other fix has been released in v4.9.1. So I'm closing this.

@st0012 st0012 closed this Jan 15, 2022
@henrahmagix henrahmagix deleted the patch-1 branch January 15, 2022 10:21
@henrahmagix
Copy link
Contributor Author

No worries @st0012, what you’ve done in #1687 is better for sure 💪🙌

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants