Daniel Doubrovkine bio photo

Daniel Doubrovkine

aka dB., @awscloud, former CTO @artsy, +@vestris, NYC

Email Twitter LinkedIn Github Strava
Creative Commons License

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


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

group :production do
  gem "newrelic_rpm", "3.1.2"

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.

class Api < Grape::API
  if Rails.env.production?
    extend NewRelic::Agent::Instrumentation::Rack

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.

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