Do I Understand Prometheus's Rate Vs Increase Functions Correctly?

Better Stack Team
Updated on December 2, 2024

Understanding the rate and increase functions in Prometheus is crucial for accurately analyzing time-series data. Here's a detailed breakdown of both functions and their differences to ensure you have a solid grasp of how they work.

Rate Function

  • Definition: The rate function calculates the average per-second rate of increase of a counter over a specified time range.
  • Usage: It is primarily used for counters, which are metrics that only increase (or reset). The function is written as:

     
    rate(metric_name[time_range])
    
  • Example: If you have a counter that tracks the number of HTTP requests, you could use:

     
    rate(http_requests_total[5m])
    

    This returns the average number of requests per second over the last 5 minutes.

  • Key Points:

    • The result is a rate (requests per second) based on the specified time interval.
    • It handles counter resets automatically (e.g., when the service restarts).
    • Useful for generating smooth graphs over time.

Increase Function

  • Definition: The increase function calculates the total increase in a counter over a specified time range.
  • Usage: This function also works with counters, and it’s written as:

     
    increase(metric_name[time_range])
    
  • Example: Continuing with the HTTP request example, you could use:

     
    increase(http_requests_total[5m])
    

    This returns the total number of requests that occurred over the last 5 minutes.

  • Key Points:

    • The result is an absolute count of the metric increase over the specified time.
    • It also handles counter resets automatically.
    • Useful for calculating total occurrences (e.g., total requests) over a defined period.

Key Differences

  • Output:
    • rate: Returns the per-second rate of increase (e.g., requests/second).
    • increase: Returns the total increase over the specified time (e.g., total requests).
  • Use Cases:
    • Use rate when you are interested in the speed of change or performance (e.g., how fast requests are coming in).
    • Use increase when you need to know the total amount of something that occurred (e.g., total requests over a time period).

Make your mark

Join the writer's program

Are you a developer and love writing and sharing your knowledge with the world? Join our guest writing program and get paid for writing amazing technical guides. We'll get them to the right readers that will appreciate them.

Write for us
Writer of the month
Marin Bezhanov
Marin is a software engineer and architect with a broad range of experience working...
Build on top of Better Stack

Write a script, app or project on top of Better Stack and share it with the world. Make a public repository and share it with us at our email.

community@betterstack.com

or submit a pull request and help us build better products for everyone.

See the full list of amazing projects on github