---
title: Create metrics from other data types
source: https://docs.newrelic.com/docs/data-apis/convert-to-metrics/create-metrics-other-data-types
---

Use New Relic's [metrics API service](https://docs.newrelic.com/docs/introduction-events-metrics-api) to define rules for creating [metrics](https://docs.newrelic.com/docs/telemetry-data-platform/ingest-manage-data/understand-data/metric-data-type) from your other types of data, such as events, logs, or spans. Recommendation: Before you begin, review our [requirements and tips for creating rules](https://docs.newrelic.com/docs/telemetry-data-platform/ingest-manage-data/convert-data-metrics/creating-metric-rules-requirements-tips).

## Create a metrics rule [#overview-process]

To create a rule for creating metrics from events, logs, or spans:

1.  [Construct the metrics rule using NRQL](#create-nrql).
2.  [Construct a NerdGraph (GraphQL format) API request](#create-request) that contains your NRQL rule.
3.  [Create the metric by making the API request](#graphql-api-request).

Once a metric is created, you can [query and chart it using NRQL](#find-your-data).

## Step 1. Create NRQL query rule [#create-nrql]

The most important part of [creating a metrics rule](#overview-process) is constructing the NRQL query that defines the metric for your data from events, logs, or spans. You can create up to [10 metrics with a single NRQL query](https://docs.newrelic.com/docs/telemetry-data-platform/ingest-manage-data/convert-data-metrics/creating-metric-rules-requirements-tips#multiple-metrics) by following this procedure:

1.  [Using New Relic's NRQL interface](https://docs.newrelic.com/docs/query-data/nrql-new-relic-query-language/getting-started/introduction-nrql#where), construct a query for the metric you want to create. For example:

    ```sql
    FROM ProcessSample SELECT average(ioTotalReadBytes)
    WHERE nr.entityType = 'HOST'
    ```

    > #### ⚠️ IMPORTANT
    >
    > [Events to metrics](https://docs.newrelic.com/docs/data-apis/convert-to-metrics/analyze-monitor-data-trends-metrics/) rules don't support the `WITH ... AS` clause in [NRQL queries](https://docs.newrelic.com/docs/query-your-data/nrql-new-relic-query-language/get-started/introduction-nrql-new-relics-query-language/).

2.  Edit the query to use one of the three available metric types:

    -   `summary`: Use if the query's function is `min`, `max`, `sum`, `count`, or `average`.
    -   `uniqueCount`: Use if the query's function is `uniqueCount`.
    -   `distribution`: Use if the query's function is [`percentile`](https://docs.newrelic.com/docs/query-data/nrql-new-relic-query-language/getting-started/nrql-syntax-clauses-functions#func-percentile) or [`histogram`](https://docs.newrelic.com/docs/query-data/nrql-new-relic-query-language/getting-started/nrql-syntax-clauses-functions#func-histogram).

        This example query uses `average`, so use `summary`:

        ```sql
        FROM ProcessSample SELECT summary(ioTotalReadBytes)
        WHERE nr.entityType = 'HOST'
        ```

        This example query uses `count` on a non-numeric field:

        ```sql
        FROM ProcessSample SELECT count(hostname)
        WHERE hostname LIKE '%prod%'
        ```

        For `summary` on a non-numeric field use `summary(1)`:

        ```sql
        FROM ProcessSample SELECT summary(1)
        WHERE hostname LIKE '%prod%'
        ```

        > #### 💡 TIP
        >
        > For more detailed information on using these metric types in rules, see [Creating metric rules: requirements and tips](https://docs.newrelic.com/docs/telemetry-data-platform/ingest-manage-data/convert-data-metrics/creating-metric-rules-requirements-tips).

3.  Decide on the attributes you want to attach to the metric, following the [limits on the cardinality of unique metric-name/attribute-value combinations](#attributes-limit).

    Recommendation: Run a separate query to ensure the maximum cardinality isn't over 50,000 for a 30 second window. For example, the following query will find the maximum cardinality encountered in a 30 second period over the last 3 hours for the `ProcessSample` event when including the `awsRegion`, `awsAvailabilityZone`, and `commandName` attributes:

    ```sql
    FROM (FROM ProcessSample
    SELECT rate(uniqueCount(awsRegion, awsAvailabilityZone, commandName), 30 seconds) AS 'cardinalityRate'
    WHERE nr.entityType = 'HOST' TIMESERIES 30 seconds) SELECT max(cardinalityRate) AS 'maxCardinalityRate' SINCE 3 hours AGO
    ```

4.  To be able to aggregate and filter your metrics, add the attributes you want to attach to the metric using the `FACET` clause. For example:

    ```sql
    FROM ProcessSample
    SELECT summary(ioTotalReadBytes) WHERE nr.entityType = 'HOST'
    FACET awsRegion, awsAvailabilityZone, commandName
    ```

5.  Set the [name of the metric](#naming) using the `AS` function. For example:

    ```sql
    FROM ProcessSample SELECT summary(ioTotalReadBytes) AS 'io.totalread.bytes'
    WHERE nr.entityType = 'HOST' FACET awsRegion, awsAvailabilityZone, commandName
    ```

Once your NRQL rule is complete, use it to [create the API request](#create-request).

## Step 2. Create API request [#create-request]

After you build the NRQL rule to convert data from events, logs, or spans to metrics, continue with building the API request. You can [use our NerdGraph API tool](https://docs.newrelic.com/docs/introduction-events-metrics-api#use-graphql-api-tool) to explore the data structure and to construct and make your request.

To check that the rule was created correctly, you can [run a query to return that rule using its ID](https://docs.newrelic.com/docs/introduction-events-metrics-api#list-rule-by-rule-id). For tips on querying the metrics you've created, see [Query and chart your metrics](#find-your-data).

**Example NerdGraph API request**

The following example NerdGraph API request uses the same NRQL rule from step 1. The `IO Total Read Bytes Rule` creates a metric named `io.totalread.bytes`. (The rule name can have spaces, which differs from the [metric naming rules](#naming).)

````graphql
mutation {
  eventsToMetricsCreateRule(
    rules: {
      name: "io.totalread.bytes for computeSample entities",
      description: "Created by Zach on March 27, 2019. Used by team Network.",
      nrql: "FROM ProcessSample SELECT summary(ioTotalReadBytes) AS 'io.totalread.bytes' WHERE nr.entityType = 'ComputeSample' FACET awsRegion, awsAvailabilityZone, commandName",
      accountId: 123456
    }
  ) {
    successes {
      id
      name
      nrql
      enabled
    }
    failures {
      submitted {
        name
        nrql
        accountId
      }
      errors {
        reason
        description
      }
    }
  }
}
```

In this request:

<table>
  <thead>
    <tr>
      <th style={{ width: "200px" }}>
        Request elements
      </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>
        `eventsToMetricsCreateRule`
      </td>

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

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

      <td>
        Takes four parameters:

        * `name`: The [name of the rule](#naming).
        * `description`: Optional. The description of the rule. We recommend you include information about who created the metric data and who will be using the data.
        * `accountId`: The [New Relic account ID](/docs/accounts/install-new-relic/account-setup/account-id) where the events, logs, or spans live and the metrics will be created.
        * `nrql`: The NRQL query that creates the rule. For more on this, see [Create NRQL query](#create-nrql).
      </td>
    </tr>

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

      <td>
        Here you define the data returned by a successful or failed response. Available parameters for these blocks include:

        * `id` (`ruleId` for `submitted`)
        * `name`
        * `description`
        * `nrql`
        * `enabled` (enabled/disabled status)
        * `accountId`
      </td>
    </tr>

    <tr>
      <td>
        `ruleId` and `accountId`
      </td>

      <td>
        If a failure occurs, then the submitted `ruleId` and `accountId` will be returned along with the error reason and error description.
      </td>
    </tr>
  </tbody>
</table>

````

**Example NerdGraph API response**

Here's an example of a returned response:

````json
{
  "data": {
    "eventsToMetricsCreateRule": {
      "failures": [],
      "successes": [
        {
          "enabled": true,
          "id": "46",
          "name": "io.totalread.bytes for computeSample entities",
          "nrql": "FROM ProcessSample SELECT summary(ioTotalReadBytes) AS 'io.totalread.bytes' WHERE nr.entityType = 'ComputeSample' FACET awsRegion, awsAvailabilityZone, commandName"
        }
      ]
    }
  }
}
```

````

## Step 3. Create a metrics rule with API request [#graphql-api-request]

When your API request is ready, you can [use the NerdGraph API](https://docs.newrelic.com/docs/introduction-events-metrics-api#use-graphql-api-tool) to make the request, which will create the metrics.

## Query and chart your metrics [#find-your-data]

After you create a metrics rule to convert data for your events, logs, or spans, you can view the new metric data in the New Relic UI. To view your data:

1.  Go to [New Relic's NRQL query interface](https://docs.newrelic.com/docs/query-data/nrql-new-relic-query-language/getting-started/introduction-nrql#where).
2.  Run the following query to see the name of all your metrics:

    ```sql
    SELECT uniques(metricName) FROM Metric
    ```
3.  Pick the metric of interest, then run the following query to see the available attributes:

    ```sql
    SELECT * FROM Metric WHERE metricName = 'yourMetric'
    ```
4.  If you don't see expected data, follow the [troubleshooting](#troubleshooting) procedures.

The available [NRQL aggregator functions](https://docs.newrelic.com/docs/insights/nrql-new-relic-query-language/nrql-reference/nrql-syntax-components-functions#functions) depend on the [metric type](#summary-and-uniquecount) you created. Here are some examples.

**Summary metric example**

If you created a summary metric type, you can use the `count`, `sum`, `max`, `min`, and `average` aggregator functions, as shown in the following query:

````sql
SELECT count(appStartResponseTime), 
  sum(appStartResponseTime), 
  max(appStartResponseTime), 
  min(appStartResponseTime), 
  average(appStartResponseTime) 
FROM Metric
```

````

**Count metric example**

If you created a `uniqueCount` metric type, you can only use the `uniqueCount` function, as shown in the following query:

````sql
SELECT uniqueCount(playbackErrorStreamUniqueCount) * 100 / uniqueCount(streamUniqueCount) AS '% of Streams Impacted' FROM Metric
```

````

**Distribution metric example**

If you created a `distribution` metric type, use the `percentile` or `histogram` functions, as shown in the following queries:

````sql
SELECT percentile(service.responseTime, 95) FROM Metric
```

OR

```sql
SELECT histogram(service.responseTime, 10, 20) FROM Metric
```

````

## Troubleshooting

If your NerdGraph call is not constructed correctly, you may receive a message like this:

```
Cannot parse the unexpected character "\u201C”
```

Verify the quotes in the NerdGraph call are not smart quotes (curly quotes). Our NerdGraph API only accepts straight quotes.
