TraceD – A Ruby Method Tracer For StatsD

Graphite & StatsD

Using the combination of StatsD and Graphite is a really good solution if you’re looking for some code analysis tools that will survive very high loads.

In short, Graphite is a really cool realtime visualization tool that will take your stats and make graphs out of them, allowing you to combine a few stats into one graph or even perform transformation on the data itself.
StatsD is a NodeJS deamon that accepts UDP packages and flushes them to graphite on a predefined interval. Using UDP for this task is an important advantage: UDP is a “send and forget” protocol which means that even if your StatsD daemon is working really slow or is currently down your performance on production wouldn’t be affected at all.
Look at how cool the end result is:

Graphite Dashboard
Click To Enlarge

This is actually not Graphite’s default visualization, but one of the many visualization plugins available for it – Graphene. Now, it doesn’t show up on the image, but this is real-time data in 10 seconds resolution, which auto-updates every 10 seconds with some really slick graph transformations. A real treat for the eye.


By now you should be really thrilled about the possibility of having these cool tools integrated into your system. If you end up doing that, you should use the new RubyGem, TraceD.
TraceD makes it easy to report method execution times or number of times a method was called, with minimal clutter to your code base and with single line of code.


First step as always: gem install traced
TraceD needs a StatsD client to do the reporting. You can use an existing client like statsd-ruby or your own custom client as long as it adheres to the following interface:

Usage Examples

For the sake of simplicity, I will use statsd-ruby here.
Let’s define our first method tracer:

Now, everytime some_method is executed, the execution time is sent to your StatsD daemon under the default generated stat name method_tracer.Dummy.some_method
As you see, it is that clean and easy to monitor your code.

Addtional Options

If you’d like to create a different stat name than the default one, you could mention it in your tracing declaration:

By Default, TraceD will only report execution time. If you’d like to also report how many times the method has been executed you can also add this to your tracing declaration:

Of course you can combine both custom stat name and count options in the same declaration:

It is also possible to trace class methods:

*NOTE: you must declare the statsd_trace after the method declaration.

I strongly encourage you to give all these great tools a shot. It is really easy to set up, almost doesn’t clutter your code and allows you to freely measure everything in your system.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s