

# Find costly spans with a heatmap and trace exemplars

The `SelectHeatmap` operation groups the profile values of individual trace spans into time columns and value buckets. For example, it shows how many spans used a given amount of CPU time in each time step. With exemplars turned on, the response also includes example trace IDs and span IDs. You can use a trace ID to open the trace in your tracing tool, and the labels of the exemplar to narrow a flame graph to the same resources. For more information about how profiles relate to traces, see [Correlating profiles with other signals](CloudWatch-Profiles-overview.md#CloudWatch-Profiles-overview-correlation).

To retrieve heatmaps and trace exemplars, use the query API.

## Prerequisites
<a name="CloudWatch-Profiles-query-heatmap-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.
+ Profile data that is linked to trace spans. The profiler must record trace context (trace IDs and span IDs) with each sample. For example, use the ADOT Java agent with tracing turned on.

**Note**  
Heatmaps and exemplars exist only for services whose profiles are linked to trace spans. For a service that is profiled without spans, `SelectHeatmap` returns an empty object, `{}`. An empty `{}` response means that there's no span-linked profile data for the label selector in the time range.

## Heatmaps in the CloudWatch console
<a name="CloudWatch-Profiles-query-heatmap-console"></a>

Heatmaps and trace exemplars are available only through the query API. To view the flame graph for the same service and time range in the CloudWatch console, see [View a flame graph in the CloudWatch console](CloudWatch-Profiles-query-example.md#CloudWatch-Profiles-query-example-console).

## Heatmap examples
<a name="CloudWatch-Profiles-query-heatmap-requests"></a>

To retrieve a heatmap and its trace exemplars with the API, send a `SelectHeatmap` request. The following example shows a request and the response.

A `SelectHeatmap` request requires `step`, in seconds. The time range can't be longer than 24 hours. The only supported query type is `HEATMAP_QUERY_TYPE_SPAN`, which is also the default. A request that omits `queryType` returns the same result. `HEATMAP_QUERY_TYPE_INDIVIDUAL` isn't supported. You can request heatmaps for CPU and memory profile types, such as `memory:alloc_space:bytes:alloc_space:bytes`. For all request fields, see [SelectHeatmap](CloudWatch-Profiles-query-api-reference.md#CloudWatch-Profiles-query-api-selectheatmap) in the query API reference.

**Example: span heatmap with trace exemplars**  <a name="CloudWatch-Profiles-query-heatmap-exemplars"></a>
This request returns a CPU heatmap for `example-java-service` in 10-minute columns. Setting `exemplarType` to `EXEMPLAR_TYPE_SPAN` adds example spans to each column. Use the full enumeration names. CloudWatch ignores a shortened value, such as `SPAN`, and the response contains no exemplars.  

```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\"}",
    "step": 600,
    "queryType": "HEATMAP_QUERY_TYPE_SPAN",
    "exemplarType": "EXEMPLAR_TYPE_SPAN",
    "start": 1793793600000,
    "end":   1793797200000
  }' \
  https://monitoring.us-east-1.amazonaws.com/querier.v1.QuerierService/SelectHeatmap
```
Example response (trimmed):  

```json
{
  "series": [
    {
      "slots": [
        {
          "timestamp": "1793796437783",
          "yMin": [1.0E7, 2.15E7, 3.3E7, ...],
          "counts": [23, 3, 2, ...],
          "exemplars": [
            {
              "timestamp": "1793795893000",
              "traceId": "7c9e66797425440de944be07fc1f90ae",
              "spanId": "{{16-hex-character-span-id}}",
              "value": "20000000",
              "labels": [
                {"name": "@resource.service.name", "value": "example-java-service"},
                ...
              ]
            }
          ]
        }
      ]
    }
  ]
}
```
The response has the following fields:  
+ `slots` – One entry for each time column. The response includes only columns that contain data. Each slot has 20 value buckets.
+ `yMin` – The lower bound of each value bucket, in the unit of the profile type. In this example, the unit is nanoseconds of CPU time for each span.
+ `counts` – The number of spans in each bucket.
+ `exemplars` – Example spans, each with a `timestamp`, a `traceId` (32 hexadecimal characters), a `spanId`, the profile `value` of the span, and the resource `labels`.

## Investigate an exemplar
<a name="CloudWatch-Profiles-query-trace-filter"></a>

Each exemplar identifies one span that falls in a heatmap bucket. To investigate an exemplar, use its fields as follows:
+ Use the `traceId` to open the trace in your tracing tool, for example, the trace views in CloudWatch Application Signals or AWS X-Ray. The trace ID is a 32-character hexadecimal string (128 bits) in the W3C Trace Context format.
+ Use the `labels` of the exemplar in a `labelSelector` to narrow the flame graph to the same resources. Use the same time range as the heatmap.

**Example: flame graph for the resources of an exemplar**  <a name="CloudWatch-Profiles-query-trace-filter-example"></a>
This request body for `SelectMergeStacktraces` returns the flame graph for the service and host from the labels of an exemplar, over the same time range as the heatmap.  

```json
{
  "profileTypeID": "process_cpu:cpu:nanoseconds:cpu:nanoseconds",
  "labelSelector": "{service_name=\"example-java-service\",\"@resource.host.name\"=\"host-a.example.internal\"}",
  "start": 1793793600000,
  "end":   1793797200000
}
```
For more information about flame graph requests, see [SelectMergeStacktraces](CloudWatch-Profiles-query-api-reference.md#CloudWatch-Profiles-query-api-selectmergestacktraces).