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

Ruby custom metrics

Custom metrics let you record arbitrary performance data via an API call (for example, timing or computer resource data). Then use the query builder to create charts and track that metric. 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 identify specific data values tracked by New Relic. When using the New Relic Ruby agent's API to track custom metrics, it's important to consider your metric naming and how the values will aggregate.

A custom metric name consists of the prefix Custom/, the class or category name, and a label, each separated with a slash mark /: Custom/<class>/<method> or Custom/<category>/<name> (for example, Custom/MyClass/My_method).

Record custom metrics

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

Tip

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, such as 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 (for example, 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)
charge_customer(amount)
...
end
end

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

View custom metrics

To view these custom metrics, use the query builder to search metrics, create customizable charts, and add those charts to dashboards.

Copyright © 2024 New Relic Inc.

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