Ruby custom metrics

Custom metrics allow you to record arbitrary metrics via an API call. For example, you could report the value of each shopping cart as part of the checkout transaction. You could then monitor the fluctuations in your shopping cart value via a custom dashboard. You can use custom metrics to unify your monitoring inside New Relic.

Caution: Collecting too many metrics can impact the performance of your application and your New Relic agent. To avoid data problems, keep the total number of unique custom metrics under 2000.

Naming metrics

Metric names are paths delimited by the "/" character. For custom instrumentation use this pattern:


Note: For custom metric names, use Custom/<class>/<method> or Custom/<category>/<name> (for example, Custom/MyClass/My_method).

For more information on naming metrics and how metrics are represented, see Ruby custom metric naming.

Recording custom metrics

The public API for recording metric data consists of two methods on NewRelic::Agent, record_metric and increment_metric.

Note: Both record_metric and increment_metric are thread safe.

record_metric(metric_name, value)

record_metric should be used to record an event-based metric, usually associated with a particular duration. metric_name must be a String following standard metric naming rules. value will usually be a Numeric, but may also be a Hash.

When value is a numeric value, it should represent the magnitude of a measurement associated with an event, e.g. the duration for a particular method call.

When value is a Hash, it must contain :count, :total, :min, :max, and :sum_of_squares keys, all with Numeric values. This form is useful if you wish to aggregate metrics on your own and report them periodically (e.g. from a background thread). The provided stats will be aggregated with any previously collected values for the same metric. The names of the hash keys have been chosen to match the names of the keys used by the platform API.

increment_metric(metric_name, amount=1)

increment_metric should be used to update a metric that acts as a simple counter. The count of the selected metric will be incremented by the specified amount.

Example custom metric

Here is an example that shows how you might use metrics to track currency flowing through a site:

class Cart

  def checkout()
    amount = compute_cart_total    # computes the amount to charge the customer

    ::NewRelic::Agent.record_metric('Custom/Cart/charge_amount', amount)


For more information about how data aggregates over time, see Stat aggregation policy.

Viewing custom metrics

Viewing custom metrics requires custom dashboards (available to all paid accounts except Standard). For more information, see Selecting custom dashboard metrics.

For more help

Additional documentation resources include:

Join the discussion about Ruby in the New Relic Community Forum! The Community Forum is a public platform to discuss and troubleshoot your New Relic toolset.

If you need additional help, get support at