Get Total Requests in a Period of Time

Better Stack Team
Updated on December 2, 2024

To calculate the total number of requests over a specified period of time using Prometheus, you can utilize the increase function. This function is ideal for summing up the total count of a counter metric over a given time interval.

Example Scenario

Let's say you have a counter metric called http_requests_total, which counts the total number of HTTP requests received by your application. To get the total requests over a specific time period, you would write a PromQL query using the increase function.

PromQL Query

Here’s how you can construct the query:

 
increase(http_requests_total[5m])

In this example:

  • http_requests_total is the name of your counter metric.
  • [5m] specifies the time range over which you want to calculate the increase—in this case, the last 5 minutes.

Breakdown of the Query

  • Function: increase(...) calculates the total increase in the http_requests_total counter over the specified time range.
  • Time Range: You can adjust the time range as needed. For instance:
    • For the last 10 minutes: increase(http_requests_total[10m])
    • For the last hour: increase(http_requests_total[1h])
    • For the last day: increase(http_requests_total[1d])

Viewing the Results

To view the results:

  1. Open your Prometheus dashboard or any compatible visualization tool like Grafana.
  2. Enter the query in the query editor and execute it.
  3. You should see the total number of HTTP requests received in the specified time period.

Additional Considerations

  1. Counter Resets: The increase function automatically handles cases where the counter resets (e.g., due to application restarts), ensuring that your total count remains accurate.
  2. Rate of Change: If you're interested in the rate of requests per second over that period, you can use the rate function instead:

     
    rate(http_requests_total[5m])
    

    This gives you the average requests per second over the last 5 minutes.

  3. Multiple Instances: If you have multiple instances of your application (e.g., in a microservices architecture), you might want to sum the requests across all instances:

     
    sum(increase(http_requests_total[5m])) by (instance)
    

Conclusion

Using the increase function in Prometheus allows you to effectively measure the total requests made over a specified time period. This is a valuable metric for monitoring application performance and understanding traffic patterns. Adjust the time range as needed based on your analysis requirements, and consider using visualization tools for better insights.

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