Skip to content

Commit fe73094

Browse files
OpeOnikutenwanduka
andauthored
Apply suggestions from code review
Co-authored-by: Victoria Nduka <122698422+nwanduka@users.noreply.github.com> Signed-off-by: Opeyemi Onikute <opeyemionikute@yahoo.com>
1 parent bd993cc commit fe73094

1 file changed

Lines changed: 10 additions & 9 deletions

File tree

blog/posts/2026-04-06-exploring-predict-linear.md

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,14 @@ kind: article
55
author_name: "Opeyemi Onikute (@Ope__O)"
66
---
77

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.
99

1010
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.
1111

1212
This post describes the syntax, how it works, practical examples, when to use it, and when not to.
1313

14-
## Syntax
14+
<!-- more -->
15+
## The syntax
1516

1617
`predict_linear` takes a range vector and a scalar number of seconds:
1718

@@ -31,7 +32,7 @@ With these parameters, you can predict the future expected value of your metric
3132

3233
`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.
3334

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.
3536

3637
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.
3738

@@ -51,7 +52,7 @@ To predict free disk bytes 24 hours (86400 seconds) from now, using the trend of
5152
predict_linear(node_filesystem_free_bytes[3h], 86400)
5253
```
5354

54-
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:
5556

5657
```yaml
5758
- alert: DiskFullSoon
@@ -63,11 +64,11 @@ If this predicted value is less than 0, it means that based on the current traje
6364
summary: "Disk may be full within 24h"
6465
```
6566
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.
6768
6869
### Predict CPU usage (❌)
6970
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.
7172
7273
```yaml
7374
- alert: CPUHighSoon
@@ -87,11 +88,11 @@ For more complex forecasting, it is recommended to export your metrics and send
8788
8889
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:
8990
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).
9293

9394
## Summary
9495

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.
9697

9798
For the full list of PromQL functions, see the [documentation](https://prometheus.io/docs/prometheus/latest/querying/functions/).

0 commit comments

Comments
 (0)