You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: blog/posts/2026-04-06-exploring-predict-linear.md
+10-9Lines changed: 10 additions & 9 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -5,13 +5,14 @@ kind: article
5
5
author_name: "Opeyemi Onikute (@Ope__O)"
6
6
---
7
7
8
-
You may want to know whether a negative situation will happen in the future based on the current trend. For example, whether a web server will run out of available disk space. This represents a shift in thinking about resiliency and reliability engineering -- from being reactive to proactive.
8
+
You may want to know whether a negative situation will happen in the future based on the current trend. For example, whether a web server will run out of available disk space. This represents a shift in thinking about resiliency and reliability engineering — from being reactive to proactive.
9
9
10
10
Not enough people know that you can accomplish this principle with Prometheus, which is why we are writing this quick refresher. PromQL's `predict_linear` function lets you project a time series into the future using linear regression, so you can build proactive alerts instead of relying only on static thresholds.
11
11
12
12
This post describes the syntax, how it works, practical examples, when to use it, and when not to.
13
13
14
-
## Syntax
14
+
<!-- more -->
15
+
## The syntax
15
16
16
17
`predict_linear` takes a range vector and a scalar number of seconds:
17
18
@@ -31,7 +32,7 @@ With these parameters, you can predict the future expected value of your metric
31
32
32
33
`predict_linear` implements linear regression by fitting a line to the samples in the given range. This line is given by *f(x) = slope × x + intercept*. The slope is derived from the data and represents the rate of change. The intercept is the value at the evaluation time.
33
34
34
-
Prometheus uses the [Kahan summation algorithm](https://en.wikipedia.org/wiki/Kahan_summation_algorithm) for numerical stability when computing. This improves the accuracy of the result by significantly reducing the numerical error of the computation.
35
+
Prometheus uses the [Kahan summation algorithm](https://en.wikipedia.org/wiki/Kahan_summation_algorithm) for numerical stability during computation. This improves the accuracy of the result by significantly reducing the numerical error of the computation.
35
36
36
37
With the slope and intercept obtained, the predicted value at time *now + t* is then *f(x) = f(now + t)*. *predict_linear* is essentially taking the slope and intercept it obtains, and applying it to the future time you've provided.
37
38
@@ -51,7 +52,7 @@ To predict free disk bytes 24 hours (86400 seconds) from now, using the trend of
If this predicted value is less than 0, it means that based on the current trajectory seen in the last 3 hours, the disk would be full one day from now. You can use that in an alert:
55
+
If this predicted value is less than 0, it means that, based on the current trajectory over the last 3 hours, the disk would be full within a day. You can use that in an alert:
55
56
56
57
```yaml
57
58
- alert: DiskFullSoon
@@ -63,11 +64,11 @@ If this predicted value is less than 0, it means that based on the current traje
63
64
summary: "Disk may be full within 24h"
64
65
```
65
66
66
-
You can explore this idea with different ranges and horizons. In practice, choosing a longer range (e.g. 6h or 1d) usually makes the prediction less sensitive to short-term noise. The trade-off however, is that the prediction reacts more slowly to a change in trend.
67
+
You can explore this idea with different ranges and horizons. In practice, choosing a longer range (e.g., 6h or 1d) usually makes the prediction less sensitive to short-term noise. The trade-off, however, is that the prediction reacts more slowly to a change in trend.
67
68
68
69
### Predict CPU usage (❌)
69
70
70
-
You may be tempted to attempt prediction for a metric that looks linear in some graphs you've seen. Be careful here as *predict_linear* becomes unsuitable once the metric is spiky. With CPU usage for instance, you may see a steady increase depending on the type of compute you're running, but a single consumer may run something intensive and make the trend non-linear.
71
+
You may be tempted to predict a metric that looks linear in some graphs you've seen. Be careful here, as *predict_linear* becomes unsuitable once the metric is spiky. With CPU usage, for instance, you may see a steady increase depending on the type of compute you're running, but a single process may run something intensive and make the trend non-linear.
71
72
72
73
```yaml
73
74
- alert: CPUHighSoon
@@ -87,11 +88,11 @@ For more complex forecasting, it is recommended to export your metrics and send
87
88
88
89
As mentioned in the example above, the caveats around *predict_linear* usage all concern the linearity of your metric. Consult the list below if you're ever in doubt:
89
90
90
-
- **Machine learning callout**: For more complex forecasting using machine learning, particular non-linear data, it's currently best to export your metrics and use a more suitable library for prediction. The function assumes the trend is roughly linear in the provided window. Overly pessimistic or optimistic predictions will be gotten if there are sudden drops or sawtooth patterns.
91
-
- **Not suitable for forecast graphs.** Using `predict_linear` for visualisation can be misleading. At each evaluation time the range window moves, so you get a new regression and a new slope. The function is only suitable for alerting or real-time evaluation. For discussion and alternatives, see [this Prometheus discussion](https://github.com/prometheus/prometheus/discussions/11705).
91
+
- **Machine learning callout**: For more complex forecasting using machine learning, particularly non-linear data, it's currently best to export your metrics and use a more suitable library for prediction. The function assumes the trend is roughly linear in the provided window. Sudden drops or sawtooth patterns will produce overly pessimistic or optimistic predictions.
92
+
- **Not suitable for forecast graphs.** Using `predict_linear` for visualisation can be misleading. At each evaluation time, the range window moves, so you get a new regression and a new slope. The function is only suitable for alerting or real-time evaluation. For discussion and alternatives, see [this Prometheus discussion](https://github.com/prometheus/prometheus/discussions/11705).
92
93
93
94
## Summary
94
95
95
-
With `predict_linear`, you have a simple, native way to turn the current trend into a value you can alert on. For example, "free space will hit zero in 24h if the trend continues". It is best suited for proactive capacity alerts. For richer forecasting or visualisation, consider exporting the metric data and using a more suitable tool.
96
+
With `predict_linear`, you have a simple, native way to turn the current trend into a value you can alert on. For example, "free space will hit zero in 24h if the trend continues." It is best suited for proactive capacity alerts. For richer forecasting or visualisation, consider exporting the metric data and using a more suitable tool.
96
97
97
98
For the full list of PromQL functions, see the [documentation](https://prometheus.io/docs/prometheus/latest/querying/functions/).
0 commit comments