在整个 Rails 4 个应用程序中记录自定义 New Relic 属性的正确方法
Proper way to record custom New Relic attribute across entire Rails 4 app
我正在使用 New Relic 监控我的 Rails 4.2 应用程序,它运行良好。
但是,当 New Relic 向我报告一个错误时,我希望能够知道哪个用户遇到了错误。
我读过 this,我相信它解释了如何在 每个控制器 的基础上添加自定义属性。
但是,就我而言,我想将 current_user.id
记录为整个 整个 应用程序的自定义属性。
我的第一个想法是将以下内容放入 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."
)
...但这导致了服务器错误。
有什么建议吗?
Update/Solution
我在上面所做的事情有两个问题。首先,我使用 rescue
不当。其次,我需要创建一个方法来添加这些自定义属性 并且 在 ApplicationController
中使用 before_filter
在一切之前调用该方法。这是最终对我有用的示例:
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
更新 2
根据下面的一位评论者,在这种情况下使用 rescue
并不理想,相反我应该使用 try
:
new_relic_user_id = current_user.try(:id) || "Not a logged-in user."
您可能希望将该代码包含在过滤器中,以便它在您的控制器操作之前运行,例如:
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 rescue "Not a logged-in user.",
:user_id => current_user.id rescue "Not a logged-in user."
)
end
end
我正在使用 New Relic 监控我的 Rails 4.2 应用程序,它运行良好。
但是,当 New Relic 向我报告一个错误时,我希望能够知道哪个用户遇到了错误。
我读过 this,我相信它解释了如何在 每个控制器 的基础上添加自定义属性。
但是,就我而言,我想将 current_user.id
记录为整个 整个 应用程序的自定义属性。
我的第一个想法是将以下内容放入 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."
)
...但这导致了服务器错误。
有什么建议吗?
Update/Solution
我在上面所做的事情有两个问题。首先,我使用 rescue
不当。其次,我需要创建一个方法来添加这些自定义属性 并且 在 ApplicationController
中使用 before_filter
在一切之前调用该方法。这是最终对我有用的示例:
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
更新 2
根据下面的一位评论者,在这种情况下使用 rescue
并不理想,相反我应该使用 try
:
new_relic_user_id = current_user.try(:id) || "Not a logged-in user."
您可能希望将该代码包含在过滤器中,以便它在您的控制器操作之前运行,例如:
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 rescue "Not a logged-in user.",
:user_id => current_user.id rescue "Not a logged-in user."
)
end
end