---
name: azion-query-usage-data-from-functions
description: >-
  Query the Consumption dataset from the GraphiQL Playground for the compute time and the invocations a function consumed.
---

# Query usage data from Functions

The **workloadConsumptionMetrics** dataset lets you get real-time aggregated data related to consumption and usage of Azion products, including Functions.

The information can be accessed through the GraphQL API, allowing you to transfer this data to a third-party platform, and enabling you to further analyze and review. Additionally, the data is available for up to *24 months*.

You read the compute time and the invocations of Functions from the [GraphiQL Playground](/en/documentation/devtools/graphql/graphql-playground/).

---

## Query the compute time and invocations

To read both metrics for a period:

1. Access the GraphiQL Playground by going to the following link: `https://api.azion.com/v4/consumption/graphql`.
   - You must be logged in to your Azion account. Otherwise, the endpoint returns an error message.
2. Send a query following this format:

```graphql
query {
  workloadConsumptionMetrics(
    filter: {
      tsRange: { 
        begin: "2025-02-01T00:00:00",
        end: "2025-03-01T00:00:00",
      }
      productId: 1531930033
      metricNameIn: ["invocations", "compute_time"]
    }
    aggregate: {
      sum: accounted
    }
    limit: 10000
    groupBy: [clientId, workloadId, productId, metricName, region]
  ) {
    clientId
    workloadId
    productId
    metricName
    region
    total: sum
  }
}
```

Where:

| Field            | Description                                                                                                                                                                                                        |
| ---------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| `filter`         | Defines the criteria used to filter the data returned by the query.                                                                                                                                                |
| `tsRange`        | A subfield of `filter`. Specifies a time range for filtering data. It includes `begin` and `end` fields to define the start and end timestamps. Format: `"YYYY-MM-DDTHH:mm:ss"`; example: `"2024-04-11T00:00:00"`. |
| `sum: accounted` | As a subfield of `aggregate`, calculates the total accounted usage for events matching the query's filters and groups.                                                                                             |
| `limit`          | Specifies the maximum number of results to return. System maximum: `10000`.                                                                                                                                        |
| `groupBy`        | Specifies the fields by which the query results should be grouped. Example: `[clientId, metricName]`.                                                                                                              |
| `productId`      | Unique identifier of the product being used. In this case, `1531930033` for Functions.                                                                                                                             |
| `metricName`     | Name of the calculated metric for analytics. Example: `invocations` or `compute_time`.                                                                                                                             |

> **Note**
>
> This example retrieves data for the total number of invocations or compute time used by Functions. The results are grouped by `clientId` and `workloadId`. To learn more about the available fields, check the [Consumption GraphQL API Fields](/en/documentation/devtools/graphql/gql-consumption-fields/) documentation.

3. The response carries a JSON object similar to this:

```json
{
	"data": {
		"workloadConsumptionMetrics": [
			{
				"clientId": "12345a",
				"workloadId": 1234567890,
				"productId": 1531930033,
				"metricName": "invocations",
				"region": "United States",
				"Total": 816931
			},
			{
				"clientId": "67890a",
				"workloadId": 1987654321,
				"productId": 1531930033,
				"metricName": "invocations",
				"region": "Europe",
				"Total": 33134
			},
			{
				"clientId": "111111b",
				"workloadId": 1231231230,
				"productId": 1531930033,
				"metricName": "compute_time",
				"region": "All Other Regions",
				"Total": 436363.41
			}
		]
	}
}
```

Where:

| Field        | Description                                                                                                                                                                            |
| ------------ | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `clientId`   | Client unique identifier on Azion. Example: `8437r.`                                                                                                                                   |
| `workloadId` | Identifier for the workload associated with the usage. Example: `4829301746`.                                                                                                          |
| `productId`  | Unique identifier of the product being used. In this case, `1531930033` for Functions.                                                                                                 |
| `metricName` | Name of the measured metric for analytics. Example: `invocations` or `compute_time`.                                                                                                   |
| `region`     | Geographical region where the usage was recorded. Example: `Europe`.                                                                                                                   |
| `total`      | The result of the sum. For `compute_time`, the total execution time of Functions in seconds, such as `436363.41`. For `invocations`, the total number of invocations, such as `33134`. |

> **Tip**
>
> To know more about the available fields, check the [Consumption GraphQL API Fields](/en/documentation/devtools/graphql/gql-consumption-fields/) documentation.
