Friday, 6 June 2025

How to Track Value Changes Over Time with the changes() Function?

If you’re new to Prometheus and wondering how to check how many times a value has changed over time, the changes() function is your tool.

 

In this post, let’s understand what it does, how to use it.

 

What does changes() do?

The changes() function in Prometheus counts how many times a value changes within a given time window. It’s often used with metrics that track process states or system behaviour like when something restarts or a status flag flips.

 

Let’s say we want to know how many times Prometheus job has restarted in the last 24 hours. We can use this metric query:

 

changes(process_start_time_seconds{job="prometheus"}[24h])

 

Here’s what’s happening:

 

·      process_start_time_seconds{job="prometheus"} is a metric that shows when the job prometheus started.

·      [24h] tells Prometheus to look at the data from the past 24 hours.

·      changes() counts how many times that value changed, which usually means the process restarted.

 

Sample Response 

{
  "status": "success",
  "data": {
    "resultType": "vector",
    "result": [
      {
        "metric": {
          "instance": "localhost:9090",
          "job": "prometheus"
        },
        "value": [1744430954.807, "7"]
      }
    ]
  }
}

·      The value "7" in the response means that the Prometheus process restarted 7 times in the last 24 hours. So, this query gives you a quick summary of how often a metric value has changed.

 

·      1744430954.807 is a timestamp representing the time when the change occurred. The timestamp 1744430954.807 corresponds to Saturday, April 12, 2025 4:09:14.807 AM GMT.

 

In summary, use changes() when you want a count of how many times something changed, not what it changed to. Perfect for monitoring restarts, on/off states, or flag switches.

 

Previous                                                    Next                                                    Home

No comments:

Post a Comment