Prometheus query quantile of pod memory usage performance

6/18/2019

I'd like to get the 0.95 percentile memory usage of my pods from the last x time. However this query start to take too long if I use a 'big' (7 / 10d) range.

The query that i'm using right now is:

quantile_over_time(0.95, container_memory_usage_bytes[10d])

Takes around 100s to complete

I removed extra namespace filters for brevity

What steps could I take to make this query more performant ? (except making the machine bigger)

I thought about calculating the 0.95 percentile every x time (let's say 30min) and label it p95_memory_usage and in the query use p95_memory_usage instead of container_memory_usage_bytes, so that i can reduce the amount of points the query has to go through.

However, would this not distort the values ?

-- RVandersteen
kubernetes
prometheus
quantile

1 Answer

6/19/2019

As you already observed, aggregating quantiles (over time or otherwise) doesn't really work.

You could try to build a histogram of memory usage over time using recording rules, looking like a "real" Prometheus histogram (consisting of _bucket, _count and _sum metrics) although doing it may be tedious. Something like:

- record: container_memory_usage_bytes_bucket
  labels:
    le: 100000.0
  expr: |
    container_memory_usage_bytes > bool 100000.0
      +
    (
      container_memory_usage_bytes_bucket{le="100000.0"}
        or ignoring(le)
      container_memory_usage_bytes * 0
    )

Repeat for all bucket sizes you're interested in, add _count and _sum metrics.

Histograms can be aggregated (over time or otherwise) without problems, so you can use a second set of recording rules that computes an increase of the histogram metrics, at much lower resolution (e.g. hourly or daily increase, at hourly or daily resolution). And finally, you can use histogram_quantile over your low resolution histogram (which has a lot fewer samples than the original time series) to compute your quantile.

It's a lot of work, though, and there will be a couple of downsides: you'll only get hourly/daily updates to your quantile and the accuracy may be lower, depending on how many histogram buckets you define.

Else (and this only came to me after writing all of the above) you could define a recording rule that runs at lower resolution (e.g. once an hour) and records the current value of container_memory_usage_bytes metrics. Then you could continue to use quantile_over_time over this lower resolution metric. You'll obviously lose precision (as you're throwing away a lot of samples) and your quantile will only update once an hour, but it's much simpler. And you only need to wait for 10 days to see if the result is close enough. (o:

-- Alin Sînpălean
Source: StackOverflow