---
title: Introduction to creating metric data from non-metric data
source: https://docs.newrelic.com/docs/data-apis/convert-to-metrics/analyze-monitor-data-trends-metrics
---

You can generate [metric-type data](https://docs.newrelic.com/docs/telemetry-data-platform/ingest-manage-data/understand-data/new-relic-data-types#dimensional-metrics) from other types of data in New Relic, including [events, logs, and spans](https://docs.newrelic.com/docs/telemetry-data-platform/ingest-manage-data/understand-data/new-relic-data-types#events-new-relic). Metrics are aggregates of your data and are optimal for analyzing and monitoring trends over long time periods.

This document explains:

-   [Reasons to use this feature](#feature-description)
-   [Available operations](#operations)
-   [How to use our NerdGraph API tool](#use-graphql-api-tool) to perform operations

## Why create metrics from other data types? [#feature-description]

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](https://docs.newrelic.com/docs/using-new-relic/data/understand-data/new-relic-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](https://docs.newrelic.com/docs/telemetry-data-platform/ingest-manage-data/understand-data/new-relic-data-types#dimensional-metrics). 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](https://docs.newrelic.com/docs/telemetry-data-platform/get-data/apis/query-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.                                           |

Here's a video showing how to generate metric data from event data (7:47 minutes):

[Video](https://www.youtube.com/embed/2nF_UnKX18c)

To get started converting your data to metrics, [create a rule.](https://docs.newrelic.com/docs/events-metrics-api-create-metrics)

## Available operations [#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](https://docs.newrelic.com/docs/apis/graphql-api/getting-started/introduction-new-relic-graphql-api) and exploring your data with the [GraphiQL API tool](https://api.newrelic.com/graphiql).

These operations fall under two basic [request types](https://docs.newrelic.com/docs/apis/graphql-api/getting-started/introduction-new-relic-graphql-api#terminology):

-   **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](https://docs.newrelic.com/docs/events-metrics-api-create-metrics).

**Delete a rule**

> #### ⚠️ IMPORTANT
>
> 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](https://docs.newrelic.com/docs/accounts/install-new-relic/account-setup/account-id).

**Example request:**

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

In this request:

<table>
  <thead>
    <tr>
      <th style={{ width: "200px" }}>
        Element
      </th>

      <th>
        Description
      </th>
    </tr>
  </thead>

  <tbody>
    <tr>
      <td>
        `mutation`
      </td>

      <td>
        One of the basic [API operation types](/docs/apis/graphql-api/getting-started/introduction-new-relic-graphql-api#tutorials).
      </td>
    </tr>

    <tr>
      <td>
        `eventsToMetricsDeleteRule`
      </td>

      <td>
        The method being called to delete a rule.
      </td>
    </tr>

    <tr>
      <td>
        `deletes`
      </td>

      <td>
        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](/docs/accounts/install-new-relic/account-setup/account-id).
      </td>
    </tr>

    <tr>
      <td>
        `successes` and `submitted` blocks
      </td>

      <td>
        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`
      </td>
    </tr>
  </tbody>
</table>

<DNT>
  **Example response for the request:**
</DNT>

```json
{
  "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**

> #### ⚠️ 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:**

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

In this request:

<table>
  <thead>
    <tr>
      <th style={{ width: "200px" }}>
        Element
      </th>

      <th>
        Description
      </th>
    </tr>
  </thead>

  <tbody>
    <tr>
      <td>
        `mutation`
      </td>

      <td>
        One of the basic [API operation types](/docs/apis/graphql-api/getting-started/introduction-new-relic-graphql-api#tutorials).
      </td>
    </tr>

    <tr>
      <td>
        `eventsToMetricsUpdateRule`
      </td>

      <td>
        The method being called to update an existing rule and either enable it or disable it.
      </td>
    </tr>

    <tr>
      <td>
        `updates`
      </td>

      <td>
        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](/docs/accounts/install-new-relic/account-setup/account-id).
        * `enabled`: To enable a disabled rule, set this to `true`. To disable a rule, set this to `false`.
      </td>
    </tr>

    <tr>
      <td>
        `successes` and `submitted` blocks
      </td>

      <td>
        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`
      </td>
    </tr>
  </tbody>
</table>

````

### 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](#list-rule-by-rule-id).

**Example listing all rules for account `123456`:**

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

In this request:

<table>
  <thead>
    <tr>
      <th style={{ width: "200px" }}>
        Element
      </th>

      <th>
        Description
      </th>
    </tr>
  </thead>

  <tbody>
    <tr>
      <td>
        `query`
      </td>

      <td>
        One of the basic [API operation types](/docs/apis/graphql-api/getting-started/introduction-new-relic-graphql-api#tutorials). Used to query but not make changes.
      </td>
    </tr>

    <tr>
      <td>
        `actor`
      </td>

      <td>
        This specifies the current New Relic user.
      </td>
    </tr>

    <tr>
      <td>
        `account(id: 123456)`
      </td>

      <td>
        Specify the [ID for the New Relic account](/docs/accounts/install-new-relic/account-setup/account-id) where to retrieve data.
      </td>
    </tr>

    <tr>
      <td>
        `eventsToMetrics`
      </td>

      <td>
        Scope the data only for events-to-metrics, logs-to-metrics, or spans-to-metrics rules.
      </td>
    </tr>

    <tr>
      <td>
        `allRules`
      </td>

      <td>
        Returns all rules for that account.
      </td>
    </tr>

    <tr>
      <td>
        `rules`
      </td>

      <td>
        In the `rules` block, you can define what data you want returned. Available fields include:

        * `id`
        * `name`
        * `description`
        * `nrql`
        * `accountId`
        * `enabled`
      </td>
    </tr>
  </tbody>
</table>

<DNT>
  **Example response:**
</DNT>

```json
{
  "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`:**

````graphql
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](#list-all-rules).

<DNT>
  **Example response:**
</DNT>

```json
{
  "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 [#use-graphql-api-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](#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 **[one.newrelic.com](https://one.newrelic.com) > All capabilities > Apps > NerdGraph API explorer** and paste your query into the box.
3.  To execute the operation, click the red **Execute query** button. Or, to get the curl format, select **Tools > 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](#list-rule-by-rule-id).
