The other day a long-time customer wrote in with a problem. They use Honeybadger to monitor their Ruby apps for exceptions but were having trouble catching timeouts. If their app took too long to respond, their application server, Puma, would abort the request. The only insight their team had into this problem was through Puma's logs.
Most people consider timeouts to be a kind of error, so it'd be nice to have them reported by Honeybadger like any other errors. This article will show you how to set that up.
What are timeouts?
When a user requests a webpage, and your app doesn't respond in time, the infrastructure that serves your app assumes something went wrong and kills the request. This process is called a timeout.
Timeouts aren't always caused by errors. The user may have asked your application to perform a task that legitimately takes five minutes. However, web apps aren't usually engineered to handle such long requests, so they abort them after some amount of time: 5, 10, 20 seconds.
The tricky thing about timeouts is that they can originate outside of your application code.
In a Ruby web application, requests typically flow from a web server (like Nginx) to your application server (like unicorn) and from there to your rack application (which is where your code lives).
As you can see, Honeybadger lives inside your application. If your web server or application server cancel the request, there's no way for us to know about it.
How to report timeouts to Honeybadger
We know that Honeybadger can't report timeouts that happen outside the application, but what if they came from inside the application?
That's what the rack-timeout
gem is built to do.
To install rack-timeout in your Rails app, add this line to your Gemfile:
gem "rack-timeout"
You can configure the timeout values. Here, we set the service timeout to 60 seconds:
export RACK_TIMEOUT_SERVICE_TIMEOUT=60
Now, if our rack application takes more than 60 seconds to respond to a request, rack-timeout raises a Rack::Timeout::RequestTimeoutException
, which is reported to Honeybadger.
The rack-timeout gem has quite a few advanced options, as you'd expect from a project maintained by Heroku. Go check them all out!
Use timeouts carefully
Request timeouts are a very blunt instrument. Because they can interrupt your code at any point, they can result in state corruption and other nasty side-effects. You should only use them in the worst case scenario: when you're reasonably sure that the request has hung and not killing it might degrade service for your other users.
Any timeout that you can anticipate, such as a network or an IO timeout should be handled gracefully in your code.