

# Query profile data in Amazon CloudWatch

You can analyze the profile data that you send to CloudWatch in the CloudWatch console. For instructions, see [Query profile data in the CloudWatch console](#CloudWatch-Profiles-query-console).

For programmatic access, Amazon CloudWatch Profiles has an HTTP query API that you can use to analyze the profile data that you send to CloudWatch. You can find which services and profile types have data, retrieve flame graphs and time series of profile values, and compare two time ranges or hosts. You can also retrieve heatmaps of the profile values of individual trace spans, with trace exemplars.

Each operation has its own path and takes a signed HTTP `POST` request with a JSON body. For the endpoint and signing requirements, see [Authentication](#CloudWatch-Profiles-query-auth).

The query API has the following operations:
+ `ProfileTypes` – List every profile type in the account for a time range. Inputs: a time range.
+ `LabelNames` – List the label names in the profile data. Inputs: optional label selectors and a time range.
+ `LabelValues` – List the values of one label, such as the service names. Inputs: a label name, optional label selectors, and a time range.
+ `Series` – List the series (unique label sets) that match a label selector. Inputs: label selectors, label names, and a time range.
+ `SelectSeries` – Retrieve a time series of profile values, optionally grouped by labels. Inputs: a profile type, a label selector, a time range, and a step.
+ `SelectMergeStacktraces` – Retrieve merged stack trace data as a flame graph. Inputs: a profile type, a label selector, and a time range.
+ `Diff` – Compare two flame graph selections, such as two time ranges or two hosts. Inputs: a left selection and a right selection.
+ `SelectHeatmap` – Retrieve a heatmap of the profile values of individual trace spans, optionally with trace exemplars. Inputs: a profile type, a label selector, a time range, and a step.

This topic first describes how to query profile data in the CloudWatch console. The API sections that follow the console section are for programmatic access. They describe how to call the query API, the permissions that you need, and the API limits. The following topics describe the request fields of each operation and show how to use the API for common tasks.

**Topics**
+ [Query profile data in the CloudWatch console](#CloudWatch-Profiles-query-console)
+ [Authentication](#CloudWatch-Profiles-query-auth)
+ [IAM permissions required](#CloudWatch-Profiles-query-iam)
+ [Query operations](#CloudWatch-Profiles-query-endpoints)
+ [Limits and behavior](#CloudWatch-Profiles-query-limits)
+ [CloudTrail events for profile operations](#CloudWatch-Profiles-query-cloudtrail)
+ [Amazon CloudWatch Profiles query API reference](CloudWatch-Profiles-query-api-reference.md)
+ [Discover profile types, services, and labels](CloudWatch-Profiles-query-discover.md)
+ [Retrieve a flame graph](CloudWatch-Profiles-query-example.md)
+ [Compare two profiles](CloudWatch-Profiles-query-compare.md)
+ [Retrieve a profile time series](CloudWatch-Profiles-query-timeseries.md)
+ [Find costly spans with a heatmap and trace exemplars](CloudWatch-Profiles-query-heatmap.md)

## Query profile data in the CloudWatch console
<a name="CloudWatch-Profiles-query-console"></a>

In the CloudWatch console, you can view the flame graph and time series of a service, filter them by labels, and compare two time ranges or label sets. You need credentials that have the `cloudwatch:GetProfileRecords` permission. For more information, see [IAM permissions required](#CloudWatch-Profiles-query-iam).

**To view a flame graph**

1. Open the CloudWatch console at [https://console.aws.amazon.com/cloudwatch/](https://console.aws.amazon.com/cloudwatch/).

1. In the navigation pane, choose **Profiles**.

1. In the navigation bar at the top, choose the Region where you send your profile data.

1. For **Service**, select the service name that your application reports in `service.name`, for example, `example-java-service`.

1. For **Profile type**, select the profile type that your profiler sends, for example, `process_cpu`.

1. At the top of the page, choose the time range that you want to analyze, for example, **1h**.

1. Choose **Run**.

The results show a time series chart of the profile value and the flame graph on the **Flamegraph** tab. To list functions by their self and total values, choose the **Top Functions** tab. To view the call hierarchy, choose the **Call tree** tab. To find a function, enter its name in **Search frames**. To show two views side by side, choose **Split**.

After you view the flame graph, you can optionally narrow it or compare it. The following procedures start from the flame graph.

**(Optional) To filter the flame graph by labels**

1. In the **Label** row, for **Label name**, select a label, such as `k8s.pod.name`.

1. For **Label value**, select the label value.

1. Choose **Run**.

To add another label filter, choose **Add matcher** (**\+**). To browse the label names and values that you can filter by, expand the groups in the **Filters** panel.

**(Optional) To compare two time ranges or label sets**

1. For **Compare with**, choose **Time** or **Labels**.

1. Set up the comparison selection:
   + For **Time**, choose a time offset, such as **1h ago**.
   + For **Labels**, in the second **Label** row, select a **Label name** and a **Label value**.

1. Choose **Run**.

The comparison flame graph marks each function as **Added**, **Grew**, **Removed**, **Shrank**, or **Unchanged**. For more information, see [Compare two profiles](CloudWatch-Profiles-query-compare.md).

Heatmaps and trace exemplars are available only through the query API. For more information, see [Find costly spans with a heatmap and trace exemplars](CloudWatch-Profiles-query-heatmap.md).

## Authentication
<a name="CloudWatch-Profiles-query-auth"></a>

You must sign all query API requests with AWS Signature Version 4 (SigV4). The signing service name is `monitoring`. If you use temporary credentials, also send the session token in the `x-amz-security-token` header. You can't use CloudWatch API keys to query profile data.

Use the following base URL:

```
https://monitoring.{{region}}.amazonaws.com
```

Replace {{region}} with the AWS Region where your profile data is stored, for example, `us-east-1`.

Each operation has its own path under the base URL, in the form `/querier.v1.QuerierService/{{Operation}}`. Send an HTTP `POST` request with a JSON body and a `Content-Type` of `application/json`.

**Example – sign a query request with curl**  <a name="CloudWatch-Profiles-query-auth-curl"></a>
This command uses the SigV4 support in curl to list the profile types that were available between the `start` and `end` times. The times are in milliseconds since the Unix epoch.  

```bash
curl --request POST \
  --aws-sigv4 "aws:amz:us-east-1:monitoring" \
  --user "$AWS_ACCESS_KEY_ID:$AWS_SECRET_ACCESS_KEY" \
  --header "x-amz-security-token: $AWS_SESSION_TOKEN" \
  --header "Content-Type: application/json" \
  --data '{"start": 1793793600000, "end": 1793797200000}' \
  https://monitoring.us-east-1.amazonaws.com/querier.v1.QuerierService/ProfileTypes
```
Use temporary credentials for these environment variables, such as those from an IAM role, rather than storing long-term access keys in scripts.

**Example – sign a query request with awscurl**  <a name="CloudWatch-Profiles-query-auth-awscurl"></a>
This command sends the same request with awscurl, which reads your AWS credentials from the standard credential sources. For installation instructions, see [awscurl](https://github.com/okigan/awscurl) on the GitHub website.  

```bash
awscurl --service monitoring --region us-east-1 \
  -X POST \
  -H "Content-Type: application/json" \
  -d '{"start": 1793793600000, "end": 1793797200000}' \
  https://monitoring.us-east-1.amazonaws.com/querier.v1.QuerierService/ProfileTypes
```

The examples in this section use fixed times. To query the last hour, you can compute the times in your shell:

```bash
END=$(( $(date +%s) * 1000 ))
START=$(( END - 3600000 ))
```

## IAM permissions required
<a name="CloudWatch-Profiles-query-iam"></a>

To query profile data, the identity needs the `cloudwatch:GetProfileRecords` permission. This permission covers every operation in the query API. The `CloudWatchReadOnlyAccess` managed policy includes it. You can also attach the following policy:

```json
{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "QueryProfiles",
            "Effect": "Allow",
            "Action": "cloudwatch:GetProfileRecords",
            "Resource": "arn:aws:cloudwatch:{{us-east-1}}:{{123456789012}}:dataset/default"
        }
    ]
}
```

You can't use API keys to query profile data.

If the `default` dataset uses a customer managed key, the identity also needs permission to use `kms:Decrypt` on the key. CloudWatch checks this permission on every query. The `CloudWatchReadOnlyAccess` managed policy doesn't include `kms:Decrypt`. For an example policy and the key policy requirement, see [Permissions for datasets that use a customer managed AWS KMS key](CloudWatch-Profiles-publish.md#CloudWatch-Profiles-publish-iam-kms).

## Query operations
<a name="CloudWatch-Profiles-query-endpoints"></a>

All operations use HTTP `POST`. Request and response bodies are JSON. The following table lists the path of each query operation. For the request fields of every operation, see [Amazon CloudWatch Profiles query API reference](CloudWatch-Profiles-query-api-reference.md).


**Amazon CloudWatch Profiles query operations**  

| **Path** | **Description** | 
| --- | --- | 
| `/querier.v1.QuerierService/ProfileTypes` | List all profile types in the account for the selected time range, such as `process_cpu:cpu:nanoseconds:cpu:nanoseconds`. For the request fields, see [ProfileTypes](CloudWatch-Profiles-query-api-reference.md#CloudWatch-Profiles-query-api-profiletypes). | 
| `/querier.v1.QuerierService/LabelNames` | List label names (for example, `service_name` or `@resource.host.name`) for a time range. For the request fields, see [LabelNames](CloudWatch-Profiles-query-api-reference.md#CloudWatch-Profiles-query-api-labelnames). | 
| `/querier.v1.QuerierService/LabelValues` | List the values of a specific label, such as the service names. For the request fields, see [LabelValues](CloudWatch-Profiles-query-api-reference.md#CloudWatch-Profiles-query-api-labelvalues). | 
| `/querier.v1.QuerierService/Series` | List the series (unique label sets) that match a label selector. For the request fields, see [Series](CloudWatch-Profiles-query-api-reference.md#CloudWatch-Profiles-query-api-series). | 
| `/querier.v1.QuerierService/SelectSeries` | Retrieve a time series of profile values for a profile type, optionally grouped by labels. For the request fields, see [SelectSeries](CloudWatch-Profiles-query-api-reference.md#CloudWatch-Profiles-query-api-selectseries). | 
| `/querier.v1.QuerierService/SelectMergeStacktraces` | Retrieve merged stack trace data as a flame graph. This is the main operation for retrieving profile results. For the request fields, see [SelectMergeStacktraces](CloudWatch-Profiles-query-api-reference.md#CloudWatch-Profiles-query-api-selectmergestacktraces). | 
| `/querier.v1.QuerierService/Diff` | Compare two profiles side by side to identify regressions or improvements between time ranges, deployments, or service versions. For the request fields, see [Diff](CloudWatch-Profiles-query-api-reference.md#CloudWatch-Profiles-query-api-diff). | 
| `/querier.v1.QuerierService/SelectHeatmap` | Retrieve a heatmap of the profile values of individual trace spans over time, optionally with trace exemplars. For the request fields, see [SelectHeatmap](CloudWatch-Profiles-query-api-reference.md#CloudWatch-Profiles-query-api-selectheatmap). | 

## Limits and behavior
<a name="CloudWatch-Profiles-query-limits"></a>

The query API has the following limits and behavior.

Data retention  
CloudWatch keeps profile data for 7 days. You can't change the retention period. If the start of a query is more than 7 days ago, the query uses data from the last 7 days only, without an error.

Time units  
`start` and `end` are in milliseconds since the Unix epoch. If you send values in seconds, the query returns an empty result. `step` is in seconds. If `end` is earlier than `start`, the query returns an empty result without an error.

Large numbers  
The API returns 64-bit integer values, such as timestamps, totals, and flame graph values, as JSON strings. For example, a timestamp is returned as `"1793796000000"`.

Maximum query window  
The time range of a `Series`, `SelectSeries`, `SelectMergeStacktraces`, `SelectHeatmap`, or `Diff` request can't be longer than 24 hours. For `Diff`, the limit applies to each side. A longer window returns HTTP 400 with a message such as `query window is longer than the max query length`. `ProfileTypes`, `LabelNames`, and `LabelValues` don't have this limit.

Minimum step for time series  
`SelectSeries` raises a smaller `step` to a minimum resolution without an error. For windows of 1 hour or less, the minimum is 300 seconds (5 minutes). For longer windows, the minimum is 900 seconds (15 minutes).

Enumeration values  
Use the full name of each enumeration value, such as `EXEMPLAR_TYPE_SPAN` or `HEATMAP_QUERY_TYPE_SPAN`. If you send an unknown or shortened value, such as `SPAN`, the API ignores it without an error and uses the default value.

Label selectors and label names  
Label selectors use the form `{{{key}}="{{value}}"}`, with the `=`, `!=`, `=~`, and `!~` operators. The braces are required. For the operator definitions, see [Common request conventions](CloudWatch-Profiles-query-api-reference.md#CloudWatch-Profiles-query-api-reference-conventions). The only short label alias is `service_name`. For other labels, use the full key, such as `@resource.host.name`, and quote keys that contain `@` or `.`. A label name that doesn't exist returns an empty result, not an error. For more information about labels, see [Profile type IDs and labels](CloudWatch-Profiles-overview.md#CloudWatch-Profiles-overview-labels).

Profile type on time series  
If a `SelectSeries` request doesn't include `profileTypeID`, the result adds together the values of every profile type, even types with different units. Always include `profileTypeID`, or group by `__profile_type__`.

Errors  
Errors return a JSON body with a `code` and a `message`, for example, `{"code": "invalid_argument", "message": "..."}`. An unsigned request returns HTTP 403 with the code `permission_denied`.

## CloudTrail events for profile operations
<a name="CloudWatch-Profiles-query-cloudtrail"></a>

CloudTrail records profile ingestion and every query operation as data events on the `AWS::CloudWatch::Dataset` resource type. CloudTrail doesn't log data events by default. For information about how to log them, see [CloudWatch data events in CloudTrail](logging_cw_api_calls.md#CloudWatch-data-plane-events).

The following table maps each operation to its CloudTrail event name and to the IAM action that authorizes it. The event names aren't IAM actions. To allow or deny these operations in a policy, use `cloudwatch:PutProfileRecords` or `cloudwatch:GetProfileRecords`.


**CloudTrail event names and IAM actions for Amazon CloudWatch Profiles**  

| **Operation** | **CloudTrail event name** | **IAM action** | 
| --- | --- | --- | 
| Ingest (`/v1development/profiles`) | `PutOtelProfiles` | `cloudwatch:PutProfileRecords` | 
| `ProfileTypes` | `GetProfileTypes` | `cloudwatch:GetProfileRecords` | 
| `LabelNames` | `GetProfileLabelNames` | `cloudwatch:GetProfileRecords` | 
| `LabelValues` | `GetProfileLabelValues` | `cloudwatch:GetProfileRecords` | 
| `Series` | `GetProfileSeries` | `cloudwatch:GetProfileRecords` | 
| `SelectSeries` | `SelectProfileSeries` | `cloudwatch:GetProfileRecords` | 
| `SelectMergeStacktraces` | `SelectProfileMergeStacktraces` | `cloudwatch:GetProfileRecords` | 
| `Diff` | `GetProfileDiff` | `cloudwatch:GetProfileRecords` | 
| `SelectHeatmap` | `SelectProfileHeatmap` | `cloudwatch:GetProfileRecords` | 