重要
We recommend to use the Metric API to send your custom metrics to the New Relic platform.
Custom metrics allow you to report any metric that passes through your code. For example, with New Relic's Go monitoring, you can create custom metrics to time a component of your app that may not captured by default. Once you capture a custom metric, you can:
- Unify your monitoring inside New Relic through the Metrics and events explorer.
- Use our REST API to programmatically retrieve and use custom metric data outside of the UI.
- Create custom metric alert conditions to notify you or your team when your custom metric exceeds specific values.
Create a custom metric
Instantiate your application by running the following:
app, err := newrelic.NewApplication(newrelic.ConfigAppName("Your Application Name"),newrelic.ConfigLicense("NEW_RELIC_LICENSE_KEY"),newrelic.ConfigDebugLogger(os.Stdout),)After instantiating your app, create a custom metric with the following code:
app.RecordCustomMetric("CustomMetricName", //name of your metric132, //time in ms);RecordCustomMetric
's first parameter is a string that names your custom metric.- The
RecordCustomMetric
method will automatically prepend the stringCustom/
to your metric name. This means the above code will generate a metric namedCustom/CustomMetricName
. RecordCustomMetric
's second parameter is the time, in milliseconds, that you want to record for your custom transaction. This means the above code will produce a metric of .132 seconds in New Relic's systems.- To use a custom metric as a counter, it can be incremented by making the call as described above, and the
count
value for that metric will be incremented by 1. The value you use for the second parameter (time in ms) is irrelevant if you are only using the metric as a counter, and thus you can use a static number, such as 0 or 1.- To increment a counter by a number greater than 1, call
app.RecordCustomMetric
multiple times - each call will only increment the metric by 1 no matter what the value of the second parameter is.
- To increment a counter by a number greater than 1, call
Name a custom metric
Carefully consider how you name your custom metrics. If your program creates too many uniquely-named metrics, you may end up with a metric grouping issue (MGI).
MGIs occur when the granularity of metric names is too fine, resulting in hundreds or thousands of different metric names. One common cause of MGIs is relying on the full URL name for metric naming in web transactions. A few major code paths may generate many different full URL paths to unique documents, articles, page, etc.
If the unique element of the URL path is included in the metric name, each of these common paths will have its own unique metric name. If metric grouping issues occur, follow the troubleshooting procedures.