Monitoring Rails with statsd and graphite

Assuming you’ve googled this topic at all, you are bound to have come across this article from 37signals, pssst-your-rails-application-has-a-secret-to-tell-you. You can basically build an initializer doing all the things in that post to get started sending data to statsd.

Statsd is a fairly small node.js application that listens for UDP requests on a configured port and collects them for a short period of time before then sending them over TCP to a graphite server. The upshot is if your statsd daemon goes down, your application continues to operate, thanks to UDP’s fire and forget nature.

If you want to save a little time, try checking out Nunes, a gem to automatically cause Rail’s builtin instrumentation to send data to statsd, see

Graphite is an interesting beast worthy of some time reading the documentation to become familiar with the whisper database structure and to learn how you might tweak the configuration. A busy graphite server can be as taxing as a database server, making use of SSD drives, etc. Graphite is horizontally scalable, but documentation on scaling graphite in this manner is particularly lacking. This post by Richard Crowley on federated graphite is all I’ve found at the time of this writing.

Now go and instrument all the things.

Posted in rails | Tagged , , , | Leave a comment

Ruby connection pooling made easy

If you ever want to have a connection pool, you need not reinvent the wheel because Mike Perham has done all the work for you already. Browse on over to connection_pool on github.

Posted in Uncategorized | Tagged , | Leave a comment

Write your own custom RSpec formatter

Earlier this year I spent some time writing a custom RSpec formatter. The purpose in my case was to be able to automatically update a remote service with the results of testing some code. You can find that project here, jaribio_formatter.

The code for a basic no-op formatter is below. If you want to output a message when a group is started or finished, or an example starts or ends, or simply record failures to summarize at the end, all the hooks are well defined by RSpec though not well documented.

class MyRSpecFormatter

    attr_reader :output, :results, :example_group

    def initialize(output)
      @output = output ||
      @results = {}

    def start(example_count)

    def example_group_started(example_group)

    def example_group_finished(example_group)

    def example_started(example)

    def example_passed(example)

    def example_pending(example)

    def example_failed(example)

    def message(message)

    def stop

    def start_dump()

    def dump_pending()

    def dump_failures()

    def dump_summary(duration, example_count, failure_count, pending_count)

    # no-op
    def seed(seed)

    def close()


You can also add your own custom configuration to RSpec. To do that, you need to call add_setting. An example from jaribio_formatter below. One more line of code calls this method when the formatter is required. The relevant source code to the jaribio_formatter is here if needed for reference.

    def self.configure()
      RSpec.configure do |c|
        c.add_setting :jaribio_url
        c.add_setting :jaribio_api_key
        c.add_setting :jaribio_plans, :default => []
        c.add_setting :jaribio_auto_create, :default => false
        c.add_setting :jaribio_timeout, :default => 5
Posted in Uncategorized | Tagged , , | 2 Comments

Ruby’s Singleton and Custom Rails Application Configuration

A relatively simple way to load some configuration once and safely, is through the use of the Singleton module provided with Ruby.

class FooConfig
  include Singleton  
  attr_reader :foo

  def initialize()
    @foo = true

Accessing the instance is easy, call FooConfig.instance. This method is also thread-safe so no two threads are going to create/access a different instance. The example is trite, but imagine instead loading data from a remote service. Now combine this with Mike Perham’s Configuration for Rails, the Right Way. = FooConfig.instance

There is really little reason to access the configuration via, except that this way you can change it for things like tests more easily.

Speaking of testing, if you find you need to reset a singleton for some reason, the following seems to do the trick, which comes from Just paste into a file like spec/support/singleton_reset.rb (if using rspec) and require it in spec/spec_helper.rb

require 'singleton'

class <<Singleton
  def included_with_reset(klass)
    class <<klass
      def reset_instance
        Singleton.send :__init__, self
  alias_method :included_without_reset, :included
  alias_method :included, :included_with_reset
Posted in rails | Tagged , , , | 1 Comment

Require a Gemfile from a Gemfile

It is probably best I don’t bother explaining how or why I ended up wanting to reference an existing Gemfile from another Gemfile, but suffice to say it is something I sort of needed to do. After a bit of searching I came across the following post,

The most relevant bit is the use of eval like below in the Gemfile you want to include another Gemfile in.

eval(, binding)
Posted in Uncategorized | Tagged , | Leave a comment

Ruby segfault with net/http and openssl

I was attempting to push a new gem this evening and ruby 1.9.3 segfaulted. The dump indicated openssl and net/http as the issue.

Pushing gem to
/Users/bjones/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/1.9.1/net/http.rb:799: [BUG] Segmentation fault

After a bit of googling I found a nice post describing how to resolve the issue.

The key part for me was adding –with-openssl-dir=/opt/local to the end of the command when running `rvm install ruby-1.9.3`.

Posted in Uncategorized | Tagged | Leave a comment

Updated safe_attributes now works with devise

After getting someone interested in using devise with safe_attributes to post a little information on the issue to github, I investigated and found something new. The model validators in ActiveRecord can be used on things other than attributes of the model. Devise uses them to check things about the password before it is eventually encrypted and stored in the database in that form only. So this required a small update to safe_attributes and should now be resolved.

Posted in Uncategorized | Tagged , , , | Leave a comment