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.2.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
  end

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.

application_id

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

api_version

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

identify_user

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'] = Proc.new { |env, headers, body|

  #snip

  'the_user_id'
}

get_metadata

Optional. get_metadata is a Proc that takes env, headers, and body as arguments and returns a Hash that is representation of a JSON object. This allows you to attach any metadata to this event.


moesif_options['get_metadata'] = Proc.new { |env, headers, body|

  #snip
  value = {
      'foo'  => 'abc',
      'bar'  => '123'
  }

  value
}

identify_session

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


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

  #snip

  'the_session_token'
}

mask_data

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'] = Proc.new { |event_model|

  #snip

  event_model
}

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

skip

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


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

  #snip

  false
}

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

debug

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