Saturday, March 8, 2014

Keeping Track of Unicorn Workers

When you are running infrastructure and hosting rails, sinatra or other rack applications there are usually a handful of other technologies in play. A common stack for serving up your rack application is Nginx and Unicorn. While most people realize how to gather request metrics from their rails application and from Nginx there is usually another metric that really helps understand the performance of your rack application.

If you are using Unicorn, you most likely understand that there is a Unicorn master that manages a set of Unicorn workers. Each worker can handle 1 request at a time and if you do not have enough workers to handle all your requests, those requests will back up into a queue until a free worker can handle the requests.

In ensuring you have enough unicorn workers and to make sure you are not dropping requests or serving up slow requests you should keep track and gather metrics from your unicorn workers as well. While there are a few interesting things you can do with Raindrops, I have been using it in my unicorn.rb file and sending stats through Sensu to a graphite server. Additionally, I am notifying when I have the number of free workers drops below a reasonable threshold.

In order to monitor your unicorn workers, add raindrops to your gem file and you can simply spin a background thread in your unicorn.rb file to reach into the socket and send stats back out periodically. 

Here is what the part of my unicorn.rb file looks like for instrumenting active and queued workers.


Remember not to reach into the port too frequently as not to cause performance problems with your unicorns.