Collect custom attributes

New Relic allows you to collect default and custom attributes and send them to several destinations. For example, you can create a custom attribute to track the user name associated with a slow or failing request. New Relic default attributes are documented in the New Relic data dictionary.

Enable attributes for APM

Before creating custom attributes, review New Relic's list of reserved terms used by NRQL. Otherwise unexpected results may occur.

To enable and use custom attributes for New Relic APM, follow the procedure for your agent:

C SDK

To add custom attributes to applications monitored by the C SDK, call one of the attribute functions; for example, newrelic_add_attribute_double(). The key name for your custom attribute depends on what you specify when you call the function.

Go

Custom attribute collection is enabled by default in the Go agent. However, you can disable custom attribute collection.

Java

Custom attribute collection is enabled by default in Java. You can collect custom attributes using XML and the New Relic Java agent APIs. These two methods can be used in conjunction with each other.

Method How to do it
Specify attributes in XML

XML allows you to specify custom attributes without changing any of your source code. You can have multiple XML files for custom attributes that are grouped by some logical facet.

To set custom attributes for your Java app via XML:

  1. Review the New Relic Java agent's documentation about XML file format, methods and classes, and examples.
  2. From your Extensions directory within the New Relic Java agent, create a single XML file.
  3. Define the methods you want New Relic to monitor by editing your XML file directly.
  4. Define an XML instrumentation file using the New Relic UI. This may require additional config in the common: block of your newrelic.yml. See Report Custom Attributes under Instrumentation Options for more detail.
Call the agent's API

To collect custom attributes using the agent's API, call the relevant methods:

  1. For each method you want to record an attribute, call addCustomParameter.
  2. Optional: Include or exclude certain attributes with attributes.include and attributes.exclude.

For example, to record a variable named userId, include this code in the parent method:

NewRelic.addCustomParameter("userId", userId);
Collect user attributes

The Java agent also includes a built-in mechanism to enable user attributes and collect user information from HttpServletRequest.getUserPrincipal() as custom attributes.

.NET

Custom attribute collection is enabled by default in .NET. To collect custom attributes, call the relevant API methods:

  1. For each method for which you want to record an attribute, call AddCustomAttribute.

  2. Optional: Include or exclude attributes with the include and exclude configuration options.

For example, to record attributes for a coupon code (string) and an item ID code (number), you could include this code in the parent method:

IAgent agent = NewRelic.Api.Agent.NewRelic.GetAgent(); 
ITransaction transaction = agent.CurrentTransaction;

transaction
    .AddCustomAttribute("Discount Code", "Summer Super Sale")
    .AddCustomAttribute("Item Code", 31456);

Node.js

Custom attribute collection is enabled by default in Node.js. To collect custom attributes, call the relevant API method:

For example, to record attributes for a coupon code and an item ID code, you could include this in the parent method:

newrelic.addCustomAttributes({
  "Discount Code": "Summer Super Sale",
  "Item Code": 31456
});
PHP

Custom attribute collection is enabled by default in PHP. To collect custom attributes, call the relevant API methods (newrelic_add_custom_parameter) for each method that you want to record an attribute.

For example, to record a variable named $userId, include this code in the parent method:

newrelic_add_custom_parameter ('userID', $userId)
Python

Custom attribute collection is enabled by default in Python. To collect custom attributes, call add_custom_parameter for each method that you want to record an attribute.

For example, to record a variable named user_id, include this code in the parent method:

newrelic.agent.add_custom_parameter('user_id', user_id)
Ruby

Custom attribute collection is enabled by default in Ruby. To collect custom attributes, call the relevant API methods:

  • For Ruby agent version 3.12.0 or higher, use the add_custom_attributes method. For example, to record a variable named @user_id, include this code in the parent method:

    ::NewRelic::Agent.add_custom_attributes({ user_id: @user.id })
  • For Ruby agent version 3.11.2 or lower, use the add_custom_parameters method. For example, to record a variable named @user_id, include this code in the parent method:

    ::NewRelic::Agent.add_custom_parameters({ user_id: @user.id })

For more information, check out New Relic University’s tutorial Adding Custom Attributes with the Ruby Agent API. Or, go directly to the full online course Custom Data with APM.

Record custom attributes from APM

Anywhere in the code that handles a web transaction in your application, you can call an API to define custom attributes. For more information, refer to the API documentation for your agent:

Record custom attributes from Browser

The New Relic Browser agent provides an API to specify extra details associated with a page view or browser interaction, either by forwarding attributes from APM to Browser or by specifying custom attributes through JavaScript. Values forwarded from the APM agent are encoded and injected into Browser attributes by the the New Relic Browser JavaScript agent.

Record custom attributes from Infrastructure

New Relic Infrastructure allows you to create custom attributes that are used to annotate the data from the Infrastructure agent. You can use this metadata to build filter sets, group your results, and annotate your data.

Record custom attributes from Mobile

The New Relic Mobile agents include API calls to record custom attributes:

For more help

Recommendations for learning more: