Is there way to push NewRelic error manually?
Asked Answered
E

3

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?

Elbrus answered 26/9, 2012 at 17:38 Comment(0)
A
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
Acreinch answered 15/5, 2013 at 4:11 Comment(2)
Here's the documentation for this: 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.Otiliaotina
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 })Mcfadden
E
5

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

NewRelic::Agent.agent.error_collector.notice_error( exception )
Elbrus answered 26/9, 2012 at 19:54 Comment(1)
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 falls into the same category.Brythonic
D
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

Digression answered 7/6, 2013 at 21:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.