When it comes to reporting custom data not reported by default by New Relic solutions, two of the most popular solutions are custom attributes and custom events. This doc will go into more detail about why you'd use custom attributes.
For some New Relic solutions, one way to report custom data to New Relic is to use custom attributes. By adding custom attributes to your data, you can do more in-depth and customized analysis of your business. For example, for New Relic browser monitoring, you might create a custom attribute to track the user name associated with a slow or failing request.
Important
Custom attributes count toward GB Ingested and are billable data.
Requirements
Custom attributes are available for these New Relic solutions:
APM
Browser monitoring
Mobile monitoring
Infrastructure monitoring
Synthetic monitors
We'll go into more detail on these options below.
Recommendations for creating and using custom attributes
At New Relic, attributes are key-value pairs that provide metadata about the events they're attached to.
A common pattern when creating custom attributes is to capture user information, such as name, ID, email, and more. This allows you to create an association between your operational data and your business data. For example, if you have the user information, you tie together your service desk and CRM data with the operational data in New Relic.
Other types of business context might include:
Customer token
Customer market segment
Customer value classification
Workflow control values not obvious in the URIStem
User/product/account privilege context
Operational context might include:
Which feature flags were used
What datastore was accessed
What cache was accessed
What errors were detected and ignored (fault partitioning)
Once you add a custom attribute, you can query it in New Relic and create custom charts from that data. For example, if you used the Java agent API to add a userId attribute to your Transaction and TransactionError events, you could then create a NRQL query using that attribute, like:
SELECTcount(*)FROM TransactionError
WHERE userId ='1401961100' FACET dateOf(timestamp),`error.message`
Custom attribute collection is enabled by default in Java. You can collect custom attributes using XML and the Java agent APIs. These two methods can be used in conjunction with each other.
Note that collecting custom attributes requires that the New Relic Java API jar be in the application's classpath.
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:
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
Example 1: Adding custom attributes to transactions
To collect custom attributes using the agent's API, call the relevant methods:
For each method you want to record an attribute for, call NewRelic.addCustomParameter(...).
For example, to record a variable named userId, include this code in the parent method:
NewRelic.addCustomParameter("userId", userId);
Example 2: Adding custom attributes to spans in distributed traces
To collect custom attributes using the agent's API, call the relevant methods:
For each span (currently executing method) that you want to record an attribute for, call NewRelic.getAgent().getTracedMethod().addCustomAttribute(...).
The Java agent also includes a built-in mechanism to enable user attributes and collect user information from HttpServletRequest.getUserPrincipal() as custom attributes.
Custom attribute collection is enabled by default in .NET. To collect custom attributes, call the relevant API methods:
For each method for which you want to record an attribute, call AddCustomAttribute.
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:
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
});
Custom attribute collection is enabled by default in PHP. To collect custom attributes, call the relevant API method 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)
Custom attribute collection is enabled by default in Python. To collect custom attributes, call add_custom_attribute 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:
Custom attribute collection is enabled by default in Ruby. To collect custom attributes, call the add_custom_attributes method. For example, to record a variable named @user_id, include this code in the parent method:
Infrastructure monitoring: Record custom attributes
Our Infrastructure monitoring lets you create custom attributes that are used to annotate the data from the infrastructure agent. You can use this metadata to filter your entities, group your results, and annotate your data.
Mobile monitoring: Record custom attributes
Mobile agents include API calls to record custom attributes: