Setting up NewRelic on Sinatra

This week I spent some time setting a NewRelic application on a Sinatra app. It actually turned out to be so simple that I almost hesitate to do a post on it, but hey, here it is any way.

Install NewRelic

gem install newrelic_rpm

With Bundler

# file: <app_root>/Gemfile
source :rubygems
gem 'newrelic_rpm'

Configure NewRelic

This is something you'll have to a bit of research. For the most part, the default NewRelic configuration (as of 2012-06-16) is setup with Rails defaults. That said, for the most part it will work with your Sinatra app. Here's and example of what I would typically start with.

common: &default_settings
  license_key: '<your_newrelic_key>'
  app_name: My Application
  monitor_mode: true
  developer_mode: false
  log_level: info
  ssl: false
  apdex_t: 0.5
  capture_params: false
  transaction_tracer:
    enabled: true
    transaction_threshold: apdex_f
    record_sql: obfuscated
    stack_trace_threshold: 0.500
  error_collector:
    enabled: true
    capture_source: true    
    ignore_errors: "NoMethodError"

development:
  <<: *default_settings
  monitor_mode: false
  developer_mode: true
  log_level: debug

test:
  <<: *default_settings
  monitor_mode: true
  app_name: My Application (Test)
  log_level: debug

production:
  <<: *default_settings
  monitor_mode: true

staging:
  <<: *default_settings
  monitor_mode: true
  app_name: My Application (Staging)

Some important things to note;

  • This configuration is keyed off of your 'RACK_ENV' setting, so be sure set it when starting your app server
  • 'log_level: debug' dumps data in to '\<app_root>/log/newrelic_agent.log'

Adding NewRelic to your Application

This is the part that I expected to be way more complicated then it actually was. The above is basically identical to Rails, aside from some slight tweaks to the configuration, so this is really the main divergence.

# file: <app_root>/config.ru
$:.unshift File.expand_path(File.dirname(__FILE__))

require "app"
require 'newrelic_rpm'
NewRelic::Agent.after_fork(:force_reconnect => true)

run App.new

Gotchas

  • It's important to note the order you load newrelic_rpm in your config.ru -- you must require your app before NewRelic for things to setup correctly.
  • The application I was setting this up for used rackup as it's development app server, the above did not work when using rack. Once I started the application using Unicorn (which is what it's using in test, stage and production) things worked as expected.

That's pretty much it, start your app and enjoy!