sentry-ruby icon indicating copy to clipboard operation
sentry-ruby copied to clipboard

Capture exceptions that happen outside of Rails middleware

Open stanhu opened this issue 3 years ago • 3 comments

Describe the idea

Suppose you have a Rails middleware that does this:

def call(env)
  return [200, { 'Content-Type' => 'text/plain' }, 'hi']
end

Since 'hi' does not respond to each as specified in the Rack spec (https://github.com/rack/rack/blob/master/SPEC.rdoc#label-The+Body), this fails quietly in the Web server with this message:

#<NoMethodError: undefined method `each' for "hi":String>

Other users have reported this in https://github.com/puma/puma/issues/1782.

Why do you think it's beneficial to most of the users

While this problem is clearly a bug in the middleware, we've come across it enough times that sometimes it takes a long to troubleshoot. The backtrace is not always available, and the log messages are hidden away in an unstructured stderr output. It's surprising that this exception isn't reported via Sentry.

Possible implementation

Capturing this exception might depend on integration with the Web server in question. For example, if you use Thin, the exception is encountered here: https://github.com/macournoyer/thin/blob/0712d603a31d97b9fa8a0260da400da2e4217d60/lib/thin/connection.rb#L114-L123

For Puma, there is this low-level error handler: https://github.com/puma/puma#error-handling. Perhaps it makes sense to hook Sentry up to the Puma ErrorLogger? https://github.com/puma/puma/blob/v5.3.2/lib/puma/error_logger.rb

Note that the exception is:

  1. Hit on this line: https://github.com/puma/puma/blob/bda19f8225a36f83bb2671fc4baef870df9f76b4/lib/puma/request.rb#L149.
  2. Rescued here: https://github.com/puma/puma/blob/f4766ce46976ec623f163a0428515d157f53e420/lib/puma/server.rb#L476
  3. Reported here: https://github.com/puma/puma/blob/f4766ce46976ec623f163a0428515d157f53e420/lib/puma/server.rb#L517
  4. Logged here: https://github.com/puma/puma/blob/f4766ce46976ec623f163a0428515d157f53e420/lib/puma/events.rb#L123

The backtrace shows:

lib/ruby/gems/2.7.0/gems/rack-2.2.3/lib/rack/body_proxy.rb:41:in `method_missing'
lib/ruby/gems/2.7.0/gems/rack-2.2.3/lib/rack/body_proxy.rb:41:in `method_missing'
lib/ruby/gems/2.7.0/gems/rack-2.2.3/lib/rack/body_proxy.rb:41:in `method_missing'
lib/ruby/gems/2.7.0/gems/rack-2.2.3/lib/rack/body_proxy.rb:41:in `method_missing'
lib/ruby/gems/2.7.0/gems/rack-2.2.3/lib/rack/body_proxy.rb:41:in `method_missing'
lib/ruby/gems/2.7.0/gems/puma-5.3.2/lib/puma/request.rb:149:in `handle_request'
lib/ruby/gems/2.7.0/gems/puma-5.3.2/lib/puma/server.rb:438:in `process_client'
lib/ruby/gems/2.7.0/gems/puma-5.3.2/lib/puma/thread_pool.rb:145:in `block in spawn_thread'

Similar issues:

  1. https://github.com/getsentry/sentry-ruby/issues/917.
  2. https://forum.sentry.io/t/puma-exception-handling/979.

stanhu avatar Jul 22 '21 22:07 stanhu

Ok, I see this is already done: https://github.com/getsentry/sentry-ruby/issues/582

stanhu avatar Jul 22 '21 23:07 stanhu

@stanhu this was implemented in the old sentry-raven SDK but not the new sentry-ruby. I’ll think about adding it. Thanks for the proposal 🙂

st0012 avatar Jul 23 '21 01:07 st0012

This issue has gone three weeks without activity. In another week, I will close it.

But! If you comment or otherwise update it, I will reset the clock, and if you label it Status: Backlog or Status: In Progress, I will leave it alone ... forever!


"A weed is but an unloved flower." ― Ella Wheeler Wilcox 🥀

github-actions[bot] avatar Oct 28 '21 21:10 github-actions[bot]

In the meantime, is it possible to use the old sentry-raven SDK alongside the new sentry-ruby SDK? I transitioned from BugSnag to Sentry, and I'm trying to figure out why some issues are appearing in BugSnag and not Sentry.

From my POV, it seems like having support for capturing errors out of Rails Middleware would solve some issues.

Additionally, would it be possible to set up Sentry ruby rack support within rail's config.ru?

cyrorama avatar Jan 24 '23 16:01 cyrorama