BharteeTechRubyOnRails
3 min readJun 18, 2024

Ultimate Guide: Configuring a New Relic in Your Ruby on Rails Application for Improved Performance Monitoring

NewRelic

It’s important to closely monitor your Ruby on Rails application’s performance to ensure it runs smoothly and satisfies users. New Relic offers comprehensive insights into your application’s performance and can help you quickly pinpoint and resolve any issues. This guide will take you through the process of setting up New Relic in your Rails app, from creating an account to advanced configuration.

1. Sign Up for New Relic

Create an Account

Start by visiting [New Relic](https://newrelic.com/) and signing up for an account if you don’t already have one. Once signed in, create a new application. New Relic will provide a license key to set it up.

2. Install the New Relic Gem

To integrate the New Relic with your Rails app, add the New Relic gem. Open your `Gemfile` and add the following line:

gem 'newrelic_rpm'

Next, proceed with the gem installation by executing:

bundle install

3. Configure New Relic

Next, generate the configuration file that New Relic requires:

bundle exec newrelic install

This command shall generate a newrelic.yml file within your config directory.

4. Update the Configuration File

Open the newly created `config/newrelic.yml` file and add your New Relic license key:

common: &default_settings
license_key: 'YOUR_NEW_RELIC_LICENSE_KEY'
app_name: My Application

Replace `’YOUR_NEW_RELIC_LICENSE_KEY’` with the license key you obtained from New Relic.

5. Environment-specific Configuration

You can configure New Relic differently for each environment (development, test, production). Here’s an example configuration:

development:
<<: *default_settings
monitor_mode: false

test:
<<: *default_settings
monitor_mode: false

production:
<<: *default_settings
monitor_mode: true
app_name: My Application (Production)

In this setup, New Relic monitoring is disabled in development and test environments but enabled in production.

6. Handle and Report Errors

The new Relic automatically tracks unhandled exceptions in your app. You can also manually report errors, which can be useful for capturing exceptions that you handle within your application. Here’s how:

begin
# Code that might raise an exception
rescue => e
NewRelic::Agent.notice_error(e)
# Handle the exception
end

You can add custom parameters to provide more context about the error:

begin
# Code that might raise an exception
rescue => e
NewRelic::Agent.notice_error(e, custom_params: { user_id: current_user.id, action: 'process_order' })
# Handle the exception
end

7. Advanced Configuration (Optional)

New Relic offers more advanced settings that you can explore in the `newrelic.yml` file, such as:

- Transaction Tracing: For detailed information about specific actions.

- Error Collection: To track and monitor errors in your app.

- Custom Instrumentation: To monitor specific parts of your code.

Refer to the [New Relic documentation](https://docs.newrelic.com/docs/agents/ruby-agent) for more details on these advanced settings.

8. Monitor and Optimize

Once data starts populating in New Relic, use the dashboard to monitor your app’s performance. Look for slow actions, check throughput, and optimize your app based on this data. Regular monitoring and optimization will ensure your app runs smoothly and efficiently.

By following these steps, you’ll set up New Relic in your Ruby on Rails application, gaining valuable insights into its performance and error tracking. This setup will help you maintain a high-performing and reliable application.