29

In our Rails app we rescue most of the exceptions on ApplicationController to give correct API response, but still want to track errors happening using ErrorCollector. Is there a way to manually send error to NewRelic?

Igor R.
  • 518
  • 5
  • 9

3 Answers3

41

Based on what I see in the New Relic agent code you can do

NewRelic::Agent.notice_error(exception, options)

I have tested this and have this running in my stack

Here's an example in a controller:

class ApplicationController < ActionController::Base
  rescue_from ActiveRecord::RecordInvalid, with: :rescue_invalid_record

  private

  def rescue_invalid_record(exception)
    NewRelic::Agent.notice_error(exception)
  end
end
fivetwentysix
  • 7,379
  • 9
  • 40
  • 58
nort
  • 1,625
  • 13
  • 12
  • 3
    Here's the documentation for this: http://rdoc.info/github/newrelic/rpm/NewRelic/Agent:notice_error It's basically an alias to the method Igor included in his solution, except it returns nil instead of the exception. So while it behaves a little differently, it's essentially the same thing and much less verbose. – Sean Moubry Sep 20 '13 at 18:19
  • To add more context I precede that with: NewRelic::Agent.add_custom_attributes({ custom_params: params.to_unsafe_h.merge(current_user: current_user.id), uri: request.original_url }) – Alberto T. Jan 10 '19 at 16:05
5

Not sure if it's recommended way to use, but this works perfectly:

NewRelic::Agent.agent.error_collector.notice_error( exception )
Igor R.
  • 518
  • 5
  • 9
  • 20
    I work at New Relic. While this method may work today, it is *not* part of our public API, and thus shouldn't be used - it may change at any point in the future. The correct method to use is `NewRelic::Agent.notice_error`, as noted in nort's response. Anything that's not documented in our public [API docs](http://rubydoc.info/github/newrelic/rpm) falls into the same category. – grumbler May 16 '14 at 04:21
1

Full documentation of the API call to notice errors and increment the error metric is the New Relic Ruby Agent API documentation here http://rdoc.info/github/newrelic/rpm/NewRelic/Agent/Transaction.notice_error

Nočnica
  • 787
  • 3
  • 12