ruby-on-railsnewrelic

Proper way to record custom New Relic attribute across entire Rails 4 app


I'm using New Relic to monitor my Rails 4.2 app, and it works well.

However, I want to be able to know which user encountered an error when New Relic reports one to me.

I've read this, which I believe explains how to add a custom attribute on a per-controller basis.

However, in my case, I want to record current_user.id as a custom attribute across the entire app.

My first thought was to put the following into applications_controller.rb:

class ApplicationController < ActionController::Base

  ::NewRelic::Agent.add_custom_parameters(
    :user_name => current_user.full_name rescue "Not a logged-in user.",
    :user_id => current_user.id rescue "Not a logged-in user."
  )

... but that caused a server error.

Any suggestions?

Update/Solution

There were two issues with what I was doing above. First, I was using rescue improperly. Second, I needed to create a method for adding these custom attributes and call that method in ApplicationController before everything by using before_filter. Here is a sample of what ended up working for me:

class ApplicationController < ActionController::Base

  # attempt to gather user and organization attributes before all controller actions for New Relic error logging
  before_filter :record_new_relic_custom_attributes

  def record_new_relic_custom_attributes
    # record some custom attributes for New Relic
    new_relic_user_id = current_user.id rescue "Not a logged-in user."
    new_relic_user_name = current_user.full_name rescue "Not a logged-in user."
    new_relic_user_email = current_user.email rescue "Not a logged-in user."
    new_relic_organization_id = current_organization.id rescue "Not a logged-in user."
    new_relic_organization_name = current_organization.name rescue "Not a logged-in user."
    new_relic_organization_email = current_organization.email rescue "Not a logged-in user."
    ::NewRelic::Agent.add_custom_parameters(
      :user_id => new_relic_user_id,
      :user_name => new_relic_user_name,
      :user_email => new_relic_user_email,
      :organization_id => new_relic_organization_id,
      :organization_name => new_relic_organization_name,
      :organization_email => new_relic_organization_email
    )
  end

Update 2 As per one of the commenters below, using rescue isn't ideal in this case, instead I should have used try:

new_relic_user_id = current_user.try(:id) || "Not a logged-in user."

Solution

  • You probably want to include that code in a filter so that it runs before your controller actions, for example:

    class ApplicationController < ActionController::Base
      before_filter :set_new_relic_user
                                                        
      def set_new_relic_user
        ::NewRelic::Agent.add_custom_parameters(
          user_name: current_user&.full_name || "Not a logged-in user.",
          user_id: current_user&.id || "Not a logged-in user."
        )                                                
      end
    end