Analyze and monitor data trends with metrics

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.

This document explains:

Why create metrics from other data types?

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.
  • Data retention is 13 months.
Query capabilities
  • You can query using the Metric data type.
  • 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.

To get started converting your data to metrics, create a rule.

Available operations

To perform operations for events, logs, or spans as metrics, 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:

Create a rule

See Create metrics.

Delete a rule

This operation modifies production settings, so we recommend thoroughly reviewing your changes before you run the operation.

To delete a rule, you need the rule ID and the New Relic account ID.

Example request:

mutation {
  eventsToMetricsDeleteRule(deletes: {ruleId: "12", accountId: 123456}) {
    successes {
      id
      name
      nrql
    }
    failures {
      errors {
        description
        reason
      }
      submitted {
        ruleId
        accountId
      }
    }
  }
}

In this request:

Element Description
mutation

One of the basic API operation types.

eventsToMetricsDeleteRule

The method being called to delete a rule.

deletes

This takes two parameters:

  • ruleId: The ID of the rule for events to metrics, logs to metrics, or spans to metrics.
  • accountId: The New Relic account ID.
successes and submitted blocks

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"
        }
      ]
    }
  }
}
Enable or disable a rule

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:

mutation {
  eventsToMetricsUpdateRule(updates: {ruleId: "12", accountId: 123456, enabled: true}) {
    successes {
      id
      name
      nrql
    }
    failures {
      errors {
        description
        reason
      }
      submitted {
        ruleId
        accountId
      }
    }
  }
}

In this request:

Element Description
mutation

One of the basic API operation types.

eventsToMetricsUpdateRule

The method being called to update an existing rule and either enable it or disable it.

updates

This takes three required parameters:

  • ruleId: The ID of the rule for events to metrics, logs to metrics, or spans to metrics.
  • accountId: The New Relic account ID.
  • enabled: To enable a disabled rule, set this to true. To disable a rule, set this to false.
successes and submitted blocks

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

Queries

Query operations include:

List all rules for a New Relic account

You can list all rules in a New Relic account or return a specific rule.

Example listing all rules for account 123456:

query {
  actor {
    account(id:123456) {
      eventsToMetrics{
        allRules{
          rules{
            id
            name
            enabled
            nrql
            description
          }
        }
      }
    }
  }
}

In this request:

Element Description
query

One of the basic API operation types. Used to query but not make changes.

actor

This specifies the current New Relic user.

account(id: 123456)

Specify the ID for the New Relic account where to retrieve data.

eventsToMetrics

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"
              }
            ]
          }
        }
      }
    }
  }
}
List rule by rule ID

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 GraphiQL API tool

You can use 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:

  1. Create the metrics operation's request with the required parameters.
  2. Go to api.newrelic.com/graphiql, and paste your query into the box.
  3. To execute the operation, press Play. Or, to get the cURL format, select Copy as cURL.)
  4. Validate the response in the response box.
  5. Optional: To verify that your rule-creation operation was performed successfully, run a list query for that rule ID.

For more help

If you need more help, check out these support and learning resources: