• English日本語한국어
  • Log inStart now

Sending handled errors to New Relic

To send error data that you are handling in your own code to New Relic, use the Ruby agent API NewRelic::Agent.notice_error call within your error handler.

Notify the New Relic Ruby agent of an error

This API call takes the exception and an optional options hash. Use this format:

NewRelic::Agent.notice_error(exception, options = { })Object

This function records the given error and passes it through the normal error filtering process, including configuration-based ignoring of errors and the global #ignore_error_filter method if defined.

The exception is the exception to be recorded, or an error message. If needed, you can also include options = { }. The following parameters will receive special treatment, and any other parameters you supply will be treated as custom parameters.

options = { }

Comments

:expected

Only records the error trace. This does not affect the error rate or Apdex status. For information on expected errors in the UI, see View expected errors.

Replaces the :trace_only option, which was deprecated in version 4.3.x of the Ruby agent.

:custom_params

Custom parameters.

:uri

The request path, minus any request parameters or query string.

Usually not needed. Include this only if you are calling notice_error outside a transaction.

:metric

The metric name associated with the transaction.

Usually not needed. Include this only if you are calling notice_error outside a transaction.

:request_params (deprecated)

Older Ruby agent versions allowed passing a :request_params option, but those are now ignored. If you need to record the request parameters, call this method inside a transaction, or pass the information in :custom_params.

Error fingerprinting: dynamically apply an error group to each noticed error

Are your error occurrences grouped poorly? Set your own error fingerprint via a callback function.

A Proc based callback can be supplied to the agent to dynamically apply a desired error group to each noticed error. Use the Ruby agent API NewRelic::Agent.set_error_group_callback to provide the agent with a callback.

This API call takes a callback method (must be of type Proc) as its only argument. The argument is required. The API call only needs to be made once per New Relic Ruby agent launch, so the call can be placed in a Rails initializer or similar. If subsequent calls to the API are made, the callback method will update to the newest one provided. Here's an example of a callback being defined and passed to the NewRelic::Agent.set_error_group_callback API method:

proc = proc { |hash| "Access" if hash[:'http.statusCode'] == 401 }
NewRelic::Agent.set_error_group_callback(proc)

In the example shown, a callback proc is created that will accept a hash as its only argument and then return the string "Access" for the desired error group name if the hash contains an HTTP status code key with a value of 401.

The callback proc is expected to receive exactly one input argument, a hash. The hash contains the following:

Key

Value

:error

The Ruby error class instance. Offers #class, #message, and #backtrace

:customAttributes

Any custom attributes for the current transaction

:'request.uri'

The current requeset URI if available

:'http.statusCode'

The HTTP status code (200, 404, etc.) if available

:'http.method'

The HTTP method (GET, PUT, etc.) if available

:'error.expected'

Whether (true) or not (false) the error was expected

:'options'

The options hash passed to NewRelic::Agent.notice_error

The callback proc is expected to return a string representing the desired error group name if one can be determined. If the proc returns a nil or empty string (''), then the error will receive server-side grouping logic.

User tracking: associating a user ID with each transaction and error

You can now see the number of users impacted by an error group.

Transactions and errors can be associated with a user ID if one is known to the New Relic Ruby agent. Use the Ruby agent API NewRelic::Agent.set_user_id to provide the agent with a user ID.

This API call requires a single argument of a string representing a unique identifier for an end user. This string can be a UUID, a database id, or similar. The API call should be made at least once per transaction to inform the New Relic Ruby agent of what user ID to associate the transaction with. Then in turn, when the agent notices errors during the lifespan of the transaction, the errors will bear an enduser.id agent attribute that holds the user ID value.

Because the API is intended to be called every time a new user ID has entered scope, it will ideally be called via middleware that is aware of user session creation. Once the New Relic Ruby agent has been made aware of the user ID, it will supply the enduser.id agent attribute on the current transaction as well as on any errors noticed during the current transaction's lifespan.

Copyright © 2024 New Relic Inc.

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.