Integration Guide - Ruby (Rack, Rails) SDK

Rack Middleware that logs incoming API calls to Moesif for advanced error analysis for apps built on Ruby on Rails / Rack.

Source Code on GitHub

Ruby Gem

How to install

gem install moesif_rack

and if you have a Gemfile in your project, please add this line to

gem 'moesif_rack', '~> 1.1.0'

How to use

Create the options

moesif_options = {
  'application_id' => 'Your application Id'

You can find your Application Id from Moesif Dashboard -> Top Right Menu -> App Setup

Add to middleware

within config/application.rb

  class Application < Rails::Application
    # snip

    config.middleware.use "MoesifRack::MoesifMiddleware", moesif_options

    # snip

Order of Middleware Matters

Since Moesif Rack is basically a logging middleware, the ordering of middleware matters for accuracy and completeness. Many middlewares are installed by default by Rails.

To see the list of middlewares that your system already have, type this into the bash.

  bin/rails middleware

The best place for “MoesifRack::MoesifMidleware” is on top as possible (so it captures the data closest to the wire). Typically, right above the default logger of Rails apps, “Rails::Rack::Logger” is a good spot. Or if you want to be as close as wire as possible, put it before “ActionDispatch::Static”

You should use the following line of code to insert the middleware into the right spot.

config.middleware.insert_before "Rails::Rack::Logger", "MoesifRack::MoesifMiddleware", moesif_options

Please note, if you are using “Rack::Deflater” please make sure that “MoesifRack::MoesifMiddlware” is below it, so it can capture uncompressed data.

Configuration options

The options is a hash with these possible key value pairs.


Required. String. This is the Moesif application_id under settings from your Moesif account.


Optional. String. Tag requests with the version of your API.


Optional. identify_user is a Proc that takes env, headers, and body as arguments and returns a user_id string. This helps us attribute requests to unique users. Even though Moesif can automatically retrieve the user_id without this, this is highly recommended to ensure accurate attribution.

moesif_options['identify_user'] = { |env, headers, body|




Optional. A Proc that takes env, headers, body and returns a string.

moesif_options['identify_session'] = { |env, headers, body|




Optional. A Proc that takes event_model as an argument and returns event_model. With mask_data, you can make modifications to headers or body of the event before it is sent to Moesif.

moesif_options['mask_data'] = { |event_model|



For details for the spec of event model, please see the moesifapi-ruby git


Optional. A Proc that takes env, headers, body and returns a boolean.

moesif_options['skip'] = { |env, headers, body|



For details for the spec of event model, please see the Moesif Ruby API Documentation


Optional. Boolean. Default false. If true, it will print out debug messages. In debug mode, the processing is not done in backend thread.

Example Code

Moesif Rack Example is an example of Moesif Rack applied to an Rail application. Please check it out for reference.

Other integrations

To view more more documentation on integration options, please visit the Integration Options Documentation.

Leave a Comment