There are two parts to enabling instrumentation for a Ruby appplication:

  1. Install and require the appoptics_apm gem (see the sections below for details on how to install and enable the agent for a particular framework.)
  2. Set the APPOPTICS_SERVICE_KEY system environment variable (see Configuration).


The appoptics_apm gem includes a c-extension that is available for Linux x86_64 systems and needs to compile on install. Therefore you’ll need to have the following on your system in order to install the gem:

  • Ruby header files
  • The gnu compiler
  • The make commandline command

On Debian/Ubuntu:

$ sudo apt install ruby-dev g++ make

On RHEL/CentOS/Amazon Linux:

$ sudo yum install ruby-devel gcc-c++ make

On Alpine:

$ sudo apk add ruby-dev g++ make



  • The appoptics_apm gem is hosted on RubyGems, making installation as simple as gem install appoptics_apm or adding gem 'appoptics_apm' to the end of your Gemfile if the application manages gems via bundler.
  • Ideally all application gems are required by Bundler.require, which garantees loading in the order they appear in the Gemfile. Otherwise make sure to require 'appoptics_apm' only after all gems that need instrumentation are loaded.



  • Rails will load the appoptics_apm gem and do instrumentation automatically if gem 'appoptics_apm' is added at the end of the Gemfile.
  • Optionally the command bundle exec rails generate appoptics_apm:install, will create a config file config/initializers/appoptics_apm.rb containing the complete list of configurations options including documentation. Most options are initially commented out. See also configure.



  • You can instrument your Sinatra application by listing gem 'appoptics_apm' after sinatra in your Gemfile and use Bundler.require during initialization or calling require 'appoptics_apm' after sinatra is loaded. With this, the appoptics_apm gem automatically detects sinatra on boot and instruments key components.
  • You may want to replace the AppOpticsAPM.logger with whichever logger you are using AppOpticsAPM.logger = MySinatra.logger



  • You can instrument your Padrino application by listing gem 'appoptics_apm' after padrino in your Gemfile and use Bundler.require during initialization or calling require 'appoptics_apm' after padrino is loaded. With this, the appoptics_apm gem automatically detects padrino on boot and instruments key components.

  • Fine tuning the AppOpticsAPM configuration is best done in the Padrino.before_load block in your config/boot.rb file. e.g:

    Padrino.before_load do
      # The appoptics_apm Ruby client has the ability to sanitize query literals
      # from SQL statements.  By default this is enabled.  Disable to
      # collect and report query literals to AppOptics.
      AppOpticsAPM::Config[:sanitize_sql] = false



  • You can instrument your Grape application by listing gem 'appoptics_apm' after grape in your Gemfile and use Bundler.require during initialization or calling require 'appoptics_apm' after grape is loaded.

Rails Metal Controllers

  • You can instrument your ruby on rails metal controllers by including the gem as described above and then adding the following code to the bottom of your metal controller declaration. Add a profile_method line for every method you want to instrument.

    class MetalController > ActionController::Metal
      def index
        self.response_body = 'Hello Metal!'
    opts = {}
    opts[:backtrace] = true
    AppOpticsAPM::API.profile_method(MetalController, :index, opts)

    see more detailed documentation in sdk.


  • If traces and metric data do not appear in the AppOptics dashboard, please review the Troubleshooting page for tips on how to resolve the issue.

Uninstalling the Agent

  • Remove gem appoptics_apm from the Gemfile (or gemspec file) as well as any require 'appoptics_apm' in the code. This has the effect that the code won’t get instrumented the next time the application is started.
  • To also remove the gem from your system either run bundle clean for gems managed through bundler or otherwise gem uninstall appoptics_apm.