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:

            "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.

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: