Your p95 hides 95% of requests; ES|QL histogram charts don't
Latency has a shape, and a percentile flattens it to a single number. Three lines of ES|QL show the whole distribution as a bar chart you can split by endpoint or as a heatmap that shifts after a deploy.
Get hands-on with Elasticsearch: Dive into our sample notebooks in the Elasticsearch Labs repo, start a free cloud trial, or try Elastic on your local machine now.
A 95th percentile (p95) of 2.5 seconds tells you that 5% of your requests are slower than that and nothing about the shape of the other 95%. Starting with Elasticsearch 9.6, Elasticsearch Query Language (ES|QL) can break a histogram metric down by the values it actually recorded. BUCKET now works on histogram fields, and a new COUNT(histogram, bucket) signature counts how many values land in each bucket. So three lines of ES|QL draw the whole distribution as a bar chart and split it by any attribute or spread it over time as a heatmap. This builds on the exponential histogram support that we shipped in Elasticsearch 9.4, and it's already live in Elastic Cloud Serverless (Serverless).
From percentiles to the full histogram distribution
Elasticsearch has supported histogram metrics for a while now. In a previous blog post, we showed how Elasticsearch natively stores OpenTelemetry (OTel) exponential histograms and how ES|QL lets you compute averages, medians, or any percentile from them at query time. All of these aggregations have one thing in common, though: they condense the whole distribution into a single number. That's exactly what you want for a dashboard tile or an alert threshold. But sometimes you want to look at the distribution itself. Do your response times cluster around a single value, or are there multiple peaks? Is there a long tail, and which endpoints does it come from?
With Elasticsearch 9.6, ES|QL can also break a histogram down by the values it actually recorded.
Throughout this blog post, we'll use an HTTP server response time metric as an example: http.server.request.duration, a histogram metric defined by the OTel semantic conventions and recorded by most OTel HTTP server instrumentations.
Our demo service exposes four endpoints: /api/products, /api/orders, /api/search, and /api/reports. While our example uses OTel data, nothing in the following queries is OTel-specific; they work the same way for any exponential_histogram or TDigest field and for legacy histogram fields after a cast.
Here’s what each field type supports:
Field type | BUCKET + COUNT support | Empty buckets |
|---|---|---|
| Native | Skipped |
TDigest | Native | Can return count 0 between smallest and largest centroid |
Legacy | After a | Depends on cast target |
What a p95 query in ES|QL already tells you
Let's start with what was already possible. To get an overview of how our endpoints perform, we can plot the 95th percentile of the response time per route over time:
TS metrics-generic.otel-default
| STATS PERCENTILE(http.server.request.duration, 95) BY TBUCKET(100), http.routeThis already tells us a lot: /api/reports is by far our slowest endpoint, with a p95 of around 2.5 seconds, followed by /api/search, /api/orders, and /api/products, which stays at around 240 milliseconds. All of them are pretty stable over time.
But what does a p95 of 2.5 seconds actually mean for the remaining 95% of the requests? Are they all just slightly faster? Or are most of them really fast, with a bunch of slow outliers? Is there one typical response time or several? The percentile alone can't answer these questions. We can, of course, add more percentiles to the chart, but at some point, we’re just approximating the distribution with a handful of lines.
How to plot a latency distribution as a bar chart in ES|QL
Instead of approximating, we can now look at the actual distribution of the response times. The following query splits the response time range into buckets of 100 milliseconds and counts how many requests fall into each of them:
TS metrics-generic.otel-default
| STATS COUNT(http.server.request.duration, buck)
BY buck = BUCKET(http.server.request.duration, 0.1)
| EVAL buck = RANGE_MIN(buck)There are a few new things going on in this query, so let's step through them:
BUCKETnow works on histograms. For regular numeric fields, theBUCKEToperator assigns each value to exactly one bucket. For a histogram, it returns all buckets of the given size (in our case, 0.1 seconds) that contain at least one value recorded by that histogram. So a single histogram data point usually contributes to many buckets.COUNThas a new signature.COUNT(histogram, bucket)gives an estimate of how many values the histogram recorded within the given bucket. In our case, that's the number of requests with a response time in that range.The counts are estimates. Most of the time, the buckets we ask for don't align with the buckets of the internal histogram representation, so ES|QL has to estimate how many values fall on either side of a bucket boundary. However, the function is designed so that the rounding errors cancel each other out across buckets. The sum of all bucket counts is always equal to the total number of requests that the histogram metric recorded. No request is lost, and none is counted twice.
The bucket is a range. The result of
BUCKETon a histogram is a double range, consisting of the lower and upper bound of the response time range. Currently, Kibana's support for double ranges on chart axes is still limited, so we useEVALwithRANGE_MINto extract the lower bound for visualization.
To turn the result into a bar chart:
Select the bar chart visualization.
Put the bucket (
buck) on the horizontal axis.Put the result of the
COUNTaggregation on the vertical axis:
Each bar now represents one response time bucket and shows how many requests had a response time in that range. For example, about 131,000 requests took between 1.0 and 1.1 seconds. And now the shape of the distribution becomes visible. There’s a sharp spike of almost 1.9 million requests between 0.1 and 0.2 seconds, followed by a broad plateau between roughly 0.3 and 0.9 seconds and a tail that fades out toward two seconds. That's something none of our percentile lines could have shown us.
Which endpoints drive your latency spikes and long tail?
It doesn't end here. Since the response time bucket is just another grouping key, we can combine it with any other dimension to gain more insights. Let's find out where that spike and the plateau come from by adding the http.route attribute to the BY clause:
TS metrics-generic.otel-default
| STATS COUNT(http.server.request.duration, buck)
BY buck = BUCKET(http.server.request.duration, 0.1), http.route
| EVAL buck = RANGE_MIN(buck)In the bar chart configuration, we keep the axes as before and break down the bars by http.route:
For each response time bucket, we now see the number of requests split by the route of the endpoint. The mystery is solved. The spike between 0.1 and 0.2 seconds comes almost entirely from /api/products, while the plateau is mostly made up of /api/search requests, with /api/orders taking over toward one second. Hovering over the 1.0 to 1.1 seconds bucket, for example, shows that it contains roughly 68,000 requests to /api/orders, 46,000 to /api/reports, and 22,000 to /api/search.
How to track a latency distribution over time with a Kibana heatmap
Instead of splitting the distribution by route, you can split it by time. This lets you see how the distribution evolves; for example, after a deployment or during a load spike. To keep the chart readable, we focus on a single route here, /api/reports:
TS metrics-generic.otel-default
| WHERE http.route == "/api/reports"
| STATS COUNT(http.server.request.duration, buck)
BY TBUCKET(70), buck = BUCKET(http.server.request.duration, 0.1)
| EVAL buck = RANGE_MIN(buck)Compared to the previous query, we replaced http.route in the BY clause with a time bucket using TBUCKET(70), which splits the selected time range into at most 70 buckets, and added a WHERE clause to filter on /api/reports. The filter is just there to keep the example focused. You can drop it to look at all routes combined or pick any other route.
To visualize this, we select the heatmap visualization type and configure it as follows:
The time bucket (
TBUCKET) goes on the horizontal axis.The response time bucket (
buck) goes on the vertical axis.The result of the
COUNTaggregation defines the cell value.
Each cell represents the combination of a time bucket and a response time bucket. Its color is based on the number of requests; that is, how many requests with a response time in that range occurred in the given time range. For example, the highlighted cell tells us that 301 requests to /api/reports took between 1.2 and 1.3 seconds in the time bucket starting at 16:25.
In our demo, the distribution is fairly boring (in a good way). Most requests consistently take between 0.9 and 1.2 seconds. If a deployment introduced a regression, we'd see the dark band shift upward. And if only a fraction of the requests became slower (for example, because of a cache that stopped working), we'd see a second band appear, which is something that a single percentile line could easily hide.
Histogram distributions in ES|QL: What to use them for
Percentiles remain the right tool for dashboards and alerts, but they only give you a few points of a distribution. With BUCKET support for histograms and the new COUNT(histogram, bucket) signature, ES|QL now lets you look at the whole distribution of your histogram metrics, including as a bar chart or broken down by any attribute, as well as over time as a heatmap. And since the bucket is just another grouping key, you can combine it with everything else that ES|QL offers.
The feature is available in Elasticsearch 9.6 and is already live in Serverless. For more details, refer to the ES|QL histogram fields documentation. Give it a try with your own histogram metrics, and see what your percentiles have been hiding from you.
Frequently Asked Questions
How do I create a bar chart of histogram values in ES|QL?
Starting with Elasticsearch 9.6, you can combine `BUCKET` with the two-argument `COUNT` on a histogram field, for example `STATS COUNT(field, b) BY b = BUCKET(field, 0.1)`. `BUCKET` returns `double_range` buckets of the given width, and `COUNT` estimates how many recorded values fall into each of them. Use `RANGE_MIN` to extract the lower bound of each bucket for the chart axis.
Which histogram field types support BUCKET and COUNT(histogram, bucket)?
Both `exponential_histogram` and TDigest fields support value-distribution buckets in ES|QL. Legacy histogram fields work after casting them with `::tdigest` or `::exponential_histogram`. For TDigest fields, some buckets between the smallest and the largest centroid can have a count of zero, while exponential histograms skip empty buckets.
Are the bucket counts from COUNT(histogram, bucket) exact?
No, they’re estimates. Histograms store an approximate distribution, and the buckets you request rarely align with the histogram's internal buckets. The estimation is designed so that the per-bucket counts add up to the total number of values that the histogram recorded, so no value is lost or counted twice across buckets.
How do I visualize a histogram metric as a heatmap over time in Kibana?
In ES|QL, group by both a time bucket (`TBUCKET`) and a value bucket (`BUCKET` on the histogram field), and count the values per combination with `COUNT(histogram, bucket)`. In Kibana, select the heatmap visualization, and put the time bucket on the horizontal axis and the value bucket on the vertical axis. Use the `COUNT` result as the cell value.
Which Elasticsearch version supports histogram value distributions in ES|QL?
`BUCKET` and `COUNT(histogram, bucket)` on histogram fields are generally available (GA) starting with Elasticsearch 9.6 and in Elastic Cloud Serverless. Percentiles and other aggregations on exponential histograms have been available since Elasticsearch 9.4.
How helpful was this content?




