Friday, 6 June 2025

How to Predict Future Metric Values Using the predict_linear() Function in Prometheus?

The predict_linear() function in Prometheus helps you to predict the future value of a gauge metric based on its historical trend over a given time range. This function works by analyzing the metric’s previous pattern of change and estimate that into the future.

 

Gauge metrics are metrics that can increase or decrease over time (like disk space usage, memory usage, etc.). predict_linear() estimates where the value of a metric might be in the future by using linear regression based on the time series in the given time range.

 

For example, Let’s say we want to predict how many bytes the system will read from the disk in the next 2 minutes based on the last 5 minutes of data.

 

Here’s how we can use the predict_linear() function:

 

predict_linear(node_disk_read_bytes_total[5m], 2 * 60)

 

Here,

·      node_disk_read_bytes_total: Metric that tracks the total number of bytes read from the disk.

·      [5m]: We’re looking at the last 5 minutes of data to understand the pattern.

·      2 * 60: We’re predicting the future value for the next 2 minutes (2 minutes * 60 seconds).

 

Sample Response

{
  "status": "success",
  "data": {
    "resultType": "vector",
    "result": [
      {
        "metric": {
          "device": "disk0",
          "instance": "localhost:9100",
          "job": "node_exporter"
        },
        "value": [1744433310.999, "348916387607.1669"]
      }
    ]
  }
}

In the response:

 

·      Timestamp: 1744433310.999, represents the moment when the prediction was made.

·      Predicted Value: 348916387607.1669, This is the predicted number of bytes that will be read from the disk in the next 2 minutes.

 

So, based on the previous 5 minutes of data, Prometheus is predicting that the disk read bytes will be around 348.92 GB in the next 2 minutes.

 

Convert the Memory Prediction to GB:

We can also convert this predicted value from bytes to gigabytes (GB) to make it easier to understand.

 

Here’s how you do it:

predict_linear(node_disk_read_bytes_total[5m], 2 * 60) / (1024 * 1024 * 1024)

 

The formula divides the byte value by (1024 * 1024 * 1024) to convert from bytes to GB.

 

{
  "status": "success",
  "data": {
    "resultType": "vector",
    "result": [
      {
        "metric": {
          "device": "disk0",
          "instance": "localhost:9100",
          "job": "node_exporter"
        },
        "value": [1744433345.750, "325.2838164785548"]
      }
    ]
  }
}

Predicted Value in GB: 325.2838164785548, This is the predicted disk read value in gigabytes for the next 2 minutes.

 

So, the disk is expected to read about 325.28 GB in the next 2 minutes, based on the previous 5-minute trend.

 

 

In summary, The predict_linear() function is used to forecast future values based on past data. It’s useful in scenarios like:

 

·      Predicting future disk usage for capacity planning.

·      Estimating future memory consumption.

·      Forecasting network traffic or other resources that change over time.

 

Note

·      The accuracy of the prediction depends on how consistent the trend is in the historical data. If the pattern fluctuates a lot, the prediction may not be very accurate.

 

·      This function uses a simple linear regression model, so it works best when the metric’s behaviour follows a predictable trend.

 

  

Previous                                                    Next                                                    Home

No comments:

Post a Comment