Introduction to creating metric data from non-metric data
You can generate metric-type data from other types of data in New Relic, including events, logs, and spans. Metrics are aggregates of your data and are optimal for analyzing and monitoring trends over long time periods.
Using metrics allows for more efficient data storage. This in turn allows you to query your data and build charts more easily. The difference between metrics and other types of data in New Relic is based on time. For more information, see Understand data types.
Events, logs, spans: These types of data represent a single record at a specific moment in time. For example, you may have an event for every request to the system. This data is ideal for in-depth troubleshooting and analysis.
Metrics: These provide an aggregated view of your events, logs, or spans. Metrics are better for showing trends over longer time ranges. For example, you can aggregate the total number of requests per service to one metric and then examine this information month over month.
Why use metrics?
Comments
Flexibility
Metrics are dimensional. You can choose what metadata (like host name or app name) is attached to them.
Common metric measurements, like average, sum, minimum, and maximum, are already calculated.
Data aggregation and retention
The data has already been pre-aggregated into longer-period time buckets.
When you create metrics, this does not delete your events or other types of data. However, metrics are better for longer-range querying and charting.
Here's a video showing how to generate metric data from event data (7:47 minutes):
To get started converting your data to metrics, create a rule.
Available operations
To show, create, and delete rules for generating metrics from events, logs, or spans, use NerdGraph, our GraphQL-format API. Before performing any operation, we recommend reading Intro to NerdGraph and exploring your data with the GraphiQL API tool.
These operations fall under two basic request types:
Mutations, which are operations that make changes to existing rules or settings (for example, creating a new metrics rule).
Queries, for fetching existing data (for example, fetching existing metrics rules).
All operations are role-based in NerdGraph as the currently logged-in New Relic user.
Mutations
Mutation operations for events to metrics, logs to metrics, or spans to metrics include:
Here you define the data returned by a success or failure. Available parameters for these blocks:
id (or ruleId for submitted)
name
description
nrql
enabled
accountId
Example response for the request:
{
"data":{
"eventsToMetricsDeleteRule":{
"failures":[],
"successes":[
{
"id":"12",
"name":"Test Rule",
"nrql":"SELECT summary(duration) AS 'server.responseTime' FROM Transaction WHERE appName = 'Data Points Staging' FACET name, appName, host"
}
]
}
}
}
Important
This operation modifies production settings, so we recommend thoroughly reviewing your changes before you run the operation.
To enable or disable an existing rule for events to metrics, logs to metrics, or spans to metrics, use the same eventsToMetricsUpdateRule operation. The only difference is whether enabled is set to true or false.
Example request to enable an existing metrics rule:
Scope the data only for events-to-metrics, logs-to-metrics, or spans-to-metrics rules.
allRules
Returns all rules for that account.
rules
In the rules block, you can define what data you want returned. Available fields include:
id
name
description
nrql
accountId
enabled
Example response:
{
"data":{
"actor":{
"account":{
"eventsToMetrics":{
"allRules":{
"rules":[
{
"description":"Metric for total time",
"enabled":true,
"id":"1",
"name":"Total Time Tx",
"nrql":"SELECT summary(totalTime) AS 'server.totalTime' FROM Transaction WHERE appName = 'Data Points Staging' FACET name, appName, host"
},
{
"description":"Metric for duration",
"enabled":true,
"id":"2",
"name":"Duration Rule",
"nrql":"SELECT summary(duration) AS 'server.responseTime' FROM Transaction WHERE appName = 'Data Points Staging' FACET name, appName, host"
}
]
}
}
}
}
}
}
If you know the exact ID for a rule, then you can query for a specific rule. For example, you may have just created a rule and now you want to list its contents so you can review it.
Example listing rule 36 for New Relic account 123456:
query{
actor{
account(id:123456){
eventsToMetrics{
rulesById(ruleIds:"36"){
rules{
id
name
enabled
nrql
description
accountId
}
}
}
}
}
}
For more details about the elements in this query, see List all rules.
Example response:
{
"data":{
"actor":{
"account":{
"eventsToMetrics":{
"rulesById":{
"rules":[
{
"accountId":123456,
"description":"Metric for total time",
"enabled":true,
"id":"36",
"name":"Total Time Tx",
"nrql":"SELECT summary(totalTime) AS 'server.totalTime' FROM Transaction WHERE appName = 'Data Points Staging' FACET name, appName, host"
}
]
}
}
}
}
}
}
Use the NerdGraph API explorer tool
You can use NerdGraph API explorer, our GraphiQL tool, to explore the data structure. You can also use it to build and run the operations to convert events, logs, and spans to metrics. To use this tool:
Create the metrics operation's request with the required parameters.
Go to one.newrelic.com > All capabilities > Apps > NerdGraph API explorer and paste your query into the box.
To execute the operation, click the red Execute query button. Or, to get the curl format, select Tools > Copy as CURL.