Create custom metrics in Go

Custom metrics allow you to report any metric that passes through your code. For example, with New Relic APM'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:

Create a custom metric

  1. Instantiate your application by running the following:

        cfg := newrelic.NewConfig("Your App Name", mustGetEnv("NEW_RELIC_LICENSE_KEY"))
        cfg.Logger = newrelic.NewDebugLogger(os.Stdout)
    
        var err error
        app, err = newrelic.NewApplication(cfg)
  2. After instantiating your app, create a custom metric with the following code:

        app.RecordCustomMetric(
            "CustomMetricName",         //name of your metric
            132,                         //time in ms
        );
    • RecordCustomMetric's first parameter is a string that names your custom metric.
    • The RecordCustomMetric method will automatically prepend the string Custom/ to your metric name. This means the above code will generate a metric named Custom/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.

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.

For more help

Additional documentation resources include:

Recommendations for learning more: