New Relic: Performance Instrumentation with Grape API

Back | performance, grape, rails, ruby | 9/21/2011 |

We’re using Grape in our Rails app on Heroku, which comes with New Relic instrumentation out-of-the-box. Lets get the Grape API route details to show in New Relic as well.

Gemfile

Add the newrelic_rpm gem to Gemfile. It will give us a start with Grape Middleware.

  1. group :production do
  2.   gem "newrelic_rpm", "3.1.2"
  3. end

Rack Up

Grape is a Rack-based system. If we wanted to display a bulk metric for Rack middleware, we could simply extend our API model as follows.

  1. class Api < Grape::API
  2.   ...
  3.   if Rails.env.production?
  4.     extend NewRelic::Agent::Instrumentation::Rack
  5.   end
  6. end

I use New Relic in production only, hence the if block. But this is not good enough for us – this would show a single API entry for all calls.

API Instrumentation

Examining the source code for NewRelic’s Rack module, we can make a few small changes and show the API path itself. Copy-paste the code from my config/initializers/new_relic_agent_instrumentation_api.rb, importing it with extend NewRelic::Agent::Instrumentation::API in the API class. All it does is figure out a better name for New Relic to record – in this case the API path without forward slashes and an appended lowercase method after the #.

New Relic Screenshots

A breakdown per API. We got api/v1/search and api/v1/me.

image

Drilling through the top entry we can see the performance breakdown (including the MongoDB instrumentation that comes from the rpm_contrib gem).

image