# How to calculate quantile from histogram bucket metrics

**URL:** <https://discuss.elastic.co/t/how-to-calculate-quantile-from-histogram-bucket-metrics/201333>\
**Category:** Kibana\
**Created:** [September 27, 2019, 3:58am UTC](https://discuss.elastic.co/t/how-to-calculate-quantile-from-histogram-bucket-metrics/201333 "2019-09-27T03:58:35Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![surenraju](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/surenraju/32/62606_2.png) [@surenraju](https://discuss.elastic.co/u/surenraju)\
**Post date:** [September 27, 2019, 3:58am UTC](https://discuss.elastic.co/t/how-to-calculate-quantile-from-histogram-bucket-metrics/201333/1 "2019-09-27T03:58:35Z")

</div>

Hello! I am using spring boot and micro meter for application metrics. My spring boot application is pushing metrics about the latency to elastic search.

I am using micrometers **Percentile histograms** - Micrometer accumulates values to an underlying histogram and ships a predetermined set of buckets to the monitoring system.

[https://micrometer.io/docs/concepts#\_histograms\_and\_percentiles](https://micrometer.io/docs/concepts#_histograms_and_percentiles)

From the histogram buckets, is there any way in elastic search or in kibana to calculate 50th, 95th, 99th percentile latency?

Prometheus supports this through function histogram\_quantile  
[https://prometheus.io/docs/prometheus/latest/querying/functions/#histogram\_quantile](https://prometheus.io/docs/prometheus/latest/querying/functions/#histogram_quantile)

Is there way to achieve similar result in elastic search or kibana?

Thank you.

---

<div class="post-metadata">

**Author:** ![Nathan\_Reese](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nathan_reese/32/84829_2.png) [@Nathan\_Reese](https://discuss.elastic.co/u/Nathan_Reese)\
**Post date:** [September 27, 2019, 2:27pm UTC](https://discuss.elastic.co/t/how-to-calculate-quantile-from-histogram-bucket-metrics/201333/2 "2019-09-27T14:27:41Z")

</div>

You can use `Visualize` application in Kibana to calculate and display percentiles for histogram buckets.

The below screen shot shows an example configuration for creating a visualization to display percentiles for histogram buckets.

 ![29%20AM](https://us1.discourse-cdn.com/elastic/original/3X/b/6/b6fa1a54705c1e1d7648ba47ce49d8c096168b2c.png)

Under the covers, Kibana is just using Elasticsearch's [\_search](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-search.html) endpoint with [histogram](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-histogram-aggregation.html) bucket aggregation and [percentiles](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-percentile-aggregation.html) metric aggregation

```auto
POST /kibana_sample_data_logs/_search
{
  "aggs": {
    "2": {
      "histogram": {
        "script": {
          "source": "doc['timestamp'].value.getHour()",
          "lang": "painless"
        },
        "interval": 1,
        "min_doc_count": 1
      },
      "aggs": {
        "1": {
          "percentiles": {
            "field": "bytes",
            "percents": [
              50,
              95,
              99
            ],
            "keyed": false
          }
        }
      }
    }
  },
  "size": 0,
  "_source": {
    "excludes": []
  },
  "stored_fields": [
    "*"
  ],
  "script_fields": {
    "hour_of_day": {
      "script": {
        "source": "doc['timestamp'].value.getHour()",
        "lang": "painless"
      }
    }
  },
  "docvalue_fields": [
    {
      "field": "@timestamp",
      "format": "date_time"
    },
    {
      "field": "timestamp",
      "format": "date_time"
    },
    {
      "field": "utc_time",
      "format": "date_time"
    }
  ],
  "query": {
    "bool": {
      "must": [],
      "filter": [
        {
          "match_all": {}
        },
        {
          "range": {
            "timestamp": {
              "format": "strict_date_optional_time",
              "gte": "2019-09-20T14:22:14.133Z",
              "lte": "2019-09-27T14:22:14.133Z"
            }
          }
        }
      ],
      "should": [],
      "must_not": []
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![surenraju](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/surenraju/32/62606_2.png) [@surenraju](https://discuss.elastic.co/u/surenraju)\
**Post date:** [September 28, 2019, 7:07am UTC](https://discuss.elastic.co/t/how-to-calculate-quantile-from-histogram-bucket-metrics/201333/3 "2019-09-28T07:07:58Z")

</div>

Thanks for the input. I cannot see bucket split row section in screenshot. Could you please provide that as well.

---

<div class="post-metadata">

**Author:** ![surenraju](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/surenraju/32/62606_2.png) [@surenraju](https://discuss.elastic.co/u/surenraju)\
**Post date:** [October 4, 2019, 7:28am UTC](https://discuss.elastic.co/t/how-to-calculate-quantile-from-histogram-bucket-metrics/201333/4 "2019-10-04T07:28:08Z")

</div>

@Nathan_Reese This works fine if the bytes are not aggregated already. But in my case, input is already bucketed.

 ![image](https://us1.discourse-cdn.com/elastic/original/3X/7/d/7d97761e9c67362c14023ea750105255e2ef6f32.png)

In my case, le is buckets(10, 25, 50, 100, 500, 1000, 5000 ms) and value fields is aggregated value for each bucket.

Prometheus supports percentile calculation for already bucket aggregated data using histogram\_quantile function  
[https://prometheus.io/docs/prometheus/latest/querying/functions/#histogram\_quantile](https://prometheus.io/docs/prometheus/latest/querying/functions/#histogram_quantile)

---

<div class="post-metadata">

**Author:** ![Nathan\_Reese](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nathan_reese/32/84829_2.png) [@Nathan\_Reese](https://discuss.elastic.co/u/Nathan_Reese)\
**Post date:** [October 4, 2019, 1:45pm UTC](https://discuss.elastic.co/t/how-to-calculate-quantile-from-histogram-bucket-metrics/201333/5 "2019-10-04T13:45:28Z")

</div>

Can you provide a few sample documents? Not sure I understand the data set or problem

---

<div class="post-metadata">

**Author:** ![surenraju](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/surenraju/32/62606_2.png) [@surenraju](https://discuss.elastic.co/u/surenraju)\
**Post date:** [October 4, 2019, 5:42pm UTC](https://discuss.elastic.co/t/how-to-calculate-quantile-from-histogram-bucket-metrics/201333/6 "2019-10-04T17:42:33Z")

</div>

I am measuring latency of "/employee/{id}" URI into five histogram buckets 10ms, 50ms, 100ms, 500ms, 1000ms using java library micrometer.

Following are the example of three scarpe with internal of 15 seconds. Within the 15 seconds interval, latency of "/employee/{id}" API calls are grouped into above mentioned five latency buckets and output metrics looks like the following

```
{"@timestamp": "2019-10-04T17:27:50.228Z", "name": "http_server_requests_histogram","method": "GET","outcome": "SUCCESS","status": "200","uri": "/employee/{id}","le": "10", "value": 1}
{"@timestamp": "2019-10-04T17:27:50.228Z","name": "http_server_requests_histogram","method": "GET","outcome": "SUCCESS","status": "200","uri": "/employee/{id}","le": "50", "value": 2}
{"@timestamp": "2019-10-04T17:27:50.228Z", "name": "http_server_requests_histogram","method": "GET","outcome": "SUCCESS","status": "200","uri": "/employee/{id}","le": "100", "value": 7}
{"@timestamp": "2019-10-04T17:27:50.228Z","name": "http_server_requests_histogram","method": "GET","outcome": "SUCCESS","status": "200","uri": "/employee/{id}","le": "500", "value": 125}
{"@timestamp": "2019-10-04T17:27:50.228Z","name": "http_server_requests_histogram","method": "GET","outcome": "SUCCESS","status": "200","uri": "/employee/{id}","le": "1000", "value": 1}

{"@timestamp": "2019-10-04T17:28:05.228Z", "name": "http_server_requests_histogram","method": "GET","outcome": "SUCCESS","status": "200","uri": "/employee/{id}","le": "10", "value": 0}
{"@timestamp": "2019-10-04T17:28:05.228Z","name": "http_server_requests_histogram","method": "GET","outcome": "SUCCESS","status": "200","uri": "/employee/{id}","le": "50", "value": 1}
{"@timestamp": "2019-10-04T17:28:05.228Z", "name": "http_server_requests_histogram","method": "GET","outcome": "SUCCESS","status": "200","uri": "/employee/{id}","le": "100", "value": 3}
{"@timestamp": "2019-10-04T17:28:05.228Z","name": "http_server_requests_histogram","method": "GET","outcome": "SUCCESS","status": "200","uri": "/employee/{id}","le": "500", "value": 134}
{"@timestamp": "2019-10-04T17:28:05.228Z","name": "http_server_requests_histogram","method": "GET","outcome": "SUCCESS","status": "200","uri": "/employee/{id}","le": "1000", "value": 2}

{"@timestamp": "2019-10-04T17:28:20.228Z", "name": "http_server_requests_histogram","method": "GET","outcome": "SUCCESS","status": "200","uri": "/employee/{id}","le": "10", "value": 2}
{"@timestamp": "2019-10-04T17:28:20.228Z","name": "http_server_requests_histogram","method": "GET","outcome": "SUCCESS","status": "200","uri": "/employee/{id}","le": "50", "value": 6}
{"@timestamp": "2019-10-04T17:28:20.228Z", "name": "http_server_requests_histogram","method": "GET","outcome": "SUCCESS","status": "200","uri": "/employee/{id}","le": "100", "value": 5}
{"@timestamp": "2019-10-04T17:28:20.228Z","name": "http_server_requests_histogram","method": "GET","outcome": "SUCCESS","status": "200","uri": "/employee/{id}","le": "500", "value": 214}
{"@timestamp": "2019-10-04T17:28:20.228Z","name": "http_server_requests_histogram","method": "GET","outcome": "SUCCESS","status": "200","uri": "/employee/{id}","le": "1000", "value": 10}

```

With this data, i would like to calculate 50%, 95%, 99% percentile latency. Just to explain the usecase, this can be achieved in prometheus using histogram\_quantile function [https://prometheus.io/docs/prometheus/latest/querying/functions/#histogram\_quantile](https://prometheus.io/docs/prometheus/latest/querying/functions/#histogram_quantile)

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [November 1, 2019, 5:52pm UTC](https://discuss.elastic.co/t/how-to-calculate-quantile-from-histogram-bucket-metrics/201333/7 "2019-11-01T17:52:44Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
