

# Retrieve a flame graph

The `SelectMergeStacktraces` operation merges the stack trace samples that match a profile type and a label selector over a time range. It returns the result as flame graph data. For more information about profiles and flame graphs, see [Profiles overview](CloudWatch-Profiles-overview.md).

You can view a flame graph in the CloudWatch console or retrieve it with the query API. This topic describes the console first, and then the API.

## Prerequisites
<a name="CloudWatch-Profiles-query-example-prereqs"></a>

Before you begin, make sure that you have the following:
+ An identity with the `cloudwatch:GetProfileRecords` permission. For more information, see [IAM permissions required](CloudWatch-Profiles-query.md#CloudWatch-Profiles-query-iam). You need this permission to view profiles in the CloudWatch console and to call the query API.
+ The profile type ID and the service name to query. For instructions, see [Discover profile types, services, and labels](CloudWatch-Profiles-query-discover.md).

## View a flame graph in the CloudWatch console
<a name="CloudWatch-Profiles-query-example-console"></a>

In the CloudWatch console, you select a service, a profile type, and a time range, and then run the query to view the flame graph.

**To view a flame graph in the CloudWatch console**

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, select 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 and the flame graph on the **Flamegraph** tab. You can use the following options to explore the results:
+ To list the functions with 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**.

To narrow the flame graph to matching profile data, in the **Label** row, select a **Label name** and a **Label value**, and then choose **Run** again. For more information, see [Query profile data in the CloudWatch console](CloudWatch-Profiles-query.md#CloudWatch-Profiles-query-console).

## Flame graph examples
<a name="CloudWatch-Profiles-query-example-requests"></a>

To retrieve a flame graph with the API, send a `SelectMergeStacktraces` request. The following examples show common requests and the response.

A `SelectMergeStacktraces` request requires `profileTypeID`. The time range can't be longer than 24 hours. Timestamps are in milliseconds since the Unix epoch. For all request fields, see [SelectMergeStacktraces](CloudWatch-Profiles-query-api-reference.md#CloudWatch-Profiles-query-api-selectmergestacktraces) in the query API reference.

**Example: retrieve a CPU flame graph for a service**  <a name="CloudWatch-Profiles-query-example-service"></a>
This request retrieves CPU profile data for `example-java-service` over a one-hour window.  

```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 '{
    "profileTypeID": "process_cpu:cpu:nanoseconds:cpu:nanoseconds",
    "labelSelector": "{service_name=\"example-java-service\"}",
    "start": 1793793600000,
    "end":   1793797200000
  }' \
  https://monitoring.us-east-1.amazonaws.com/querier.v1.QuerierService/SelectMergeStacktraces
```

**Example: retrieve a smaller flame graph for one host**  <a name="CloudWatch-Profiles-query-example-host"></a>
This request body narrows the query to one host and limits the size of the result with `maxNodes`. The limit is approximate. CloudWatch rolls up the nodes that it trims into nodes named `other` or `other ({{parent}})`. If you omit `maxNodes`, the response contains the full tree.  

```json
{
  "profileTypeID": "process_cpu:cpu:nanoseconds:cpu:nanoseconds",
  "labelSelector": "{service_name=\"example-java-service\",\"@resource.host.name\"=\"host-a.example.internal\"}",
  "maxNodes": 8,
  "start": 1793793600000,
  "end":   1793797200000
}
```

**Example: read the flame graph response**  <a name="CloudWatch-Profiles-query-example-response"></a>
The response contains a `flamegraph` object. The following response is trimmed to its first two levels, so the node with the largest self value (`maxSelf`) isn't shown.  

```json
{
  "flamegraph": {
    "names": ["total", "{{frame-a}}", "other", "{{frame-b}}"],
    "levels": [
      {"values": ["0", "70445399426", "0", "0"]},
      {"values": ["0", "68187123095", "0", "1", "0", "2258276331", "2258276331", "2"]}
    ],
    "total": "70445399426",
    "maxSelf": "68167123095"
  }
}
```
The response has the following fields:  
+ `names` – The function names in the flame graph. Index `0` is `total`.
+ `levels` – One entry for each depth of the stack. Each `values` list holds four numbers for each node: the x-offset, the total value, the self value, and the index of the node's name in `names`.
+ `total` and `maxSelf` – The total value of the flame graph and the largest self value of any node.
Values use the unit of the profile type, for example, nanoseconds of CPU time. The API returns numbers as strings. To get the result as a binary tree instead, set `"format": "PROFILE_FORMAT_TREE"`. The response then contains a `tree` field with base64-encoded data.