Integrating batsd with graphite and statsd

At Mailprotector we recently integrated batsd with our graphite statistics and metrics system. The result is a faster and native interface for querying our metrics data using ruby. This change has made it easy for us to create our own dashboards and reports in our Rails apps. Since batsd was built with compatibility for statsd, integrating was simple. Using the repeater option for statsd, we are able to continue to send the data to graphite but also repeat it to our batsd system. Here is an example of the repeater configuration.

backends: [ "./backends/graphite", "./backends/repeater" ],
repeater: [ { host: '1.2.3.4', port: 8125 } ]

Now we have the data in two places. We can also optimize the retention in batsd for our needs there and keep using graphite setup the way it was.

The Batsd Client Gem

We needed a client to query the data from batsd for our dashboards and reports we wanted to build in our internal Rails apps. There is an example ruby client provided in the batsd repository but nothing packaged up and ready to go. So using that example as a starting point, I created a batsd ruby client and bundled it up as a gem.

Installation

It’s a simple gem installation.

gem install batsd`

Or you can include it in your Gemfile.

gem "batsd"`

Getting Started

You can connect to Batsd by instantiating the Batsd class:

client = Batsd.new

This assumes Batsd was started with a default configuration, and it listening on `localhost`, port `8127`. If you need to connect to a remote server or a different port, pass in the appropriate options:

client = Batsd.new(:host => "10.0.0.1", :port => 8127)`

The options and defaults are:

:host => "127.0.0.1"
:port => 8127
:timeout => 2000 #milliseconds
:max_attempts => 2

Now you can grab the list of available keys:

keys = client.available

To pull the stats for a key:

start_timestamp = Time.now - (60*60) # 1 hour ago
end_timestamp = Time.now
stats = client.stats("metric_name", start_timestamp, end_timestamp)

Each stat is returned as a hash with a :timestamp and :value. To pull only the timestamps or values for a range:

start_timestamp = Time.now - (60*60) # 1 hour ago
end_timestamp = Time.now
values = client.values("metric_name", start_timestamp, end_timestamp)
timestamps = client.timestamps("metric_name", start_timestamp, end_timestamp)

Pull requests are always welcome. Just fork the project.