Rails で発生した Error を NewRelicに通知する

Rails アプリでは newrelic_rpm を Gemfile に書いておけば、よしなにエラーを NewRelic のダッシュボードに通知してくれます。

が、

rescue_from Exception, with: :render_500

def render_500(e = nil)
  logger.error e
  render file: 'public/500', status: :internal_server_error, layout: false   
end

のようなコードで Exception を握りつぶしてるとダッシュボードで通知されません。

Sending New Relic handled errors によれば、

Use the Ruby Agent API NewRelic::Agent.notice_error within your error handler, which tells the agent to send the error data to New Relic. This API call takes the exception and an optional options hash. Use this format:

とのことなので、

def render_500(e = nil)
  logger.error e
  NewRelic::Agent.notice_error(e)

  render file: 'public/500', status: :internal_server_error, layout: false
end

と書けば ok です。