Number of requests in a time period

@grot I am building a dashboard in Grafana to monitor the latency and the number of requests made to a specific API. The metrics are being collected via Google Cloud Managed Service for Prometheus and accessed in Grafana using Google Cloud Monitoring as the data source.

However, I am facing an issue with how the data is being displayed. The metric I am using to calculate the total number of requests is returning the cumulative number of requests since the start of data collection, regardless of the time period selected in the dashboard.

For example, when I apply a filter to display data from the last 5 minutes, the panel still shows the total number of requests accumulated from the start of data collection, instead of displaying only the total for the selected 5-minute interval.

Below is an illustrative image:

Example of the Panel with the total value

What I need is to calculate the number of requests only within the selected time period in Grafana. Is there a way to adjust the PromQL query so it returns the correct data based on the selected time range? I’ve tried several approaches, but the query always returns the cumulative value of all requests.

Currently, I am using the following PromQL query to calculate the metric total. Does anyone know how I can modify it to return only the requests from the selected time range?

sum(coleta_online_request_count{product_id=~'$product_id', external_bases_name=~'$base_name'})

Here is the configuration for exporting my metrics. Every time a request comes in, I add the value 1 to the metric using the incBy function:

# HELP coleta_online_request_count gauge coleta online request
# TYPE coleta_online_request_count gauge
coleta_online_request_count{product_id="509",base_name="Mock 1",request_status="success"} 1
coleta_online_request_count{product_id="510",base_name="Mock 2",request_status="success"} 1
coleta_online_request_count{product_id="521",base_name="Mock 1",request_status="error"} 1
coleta_online_request_count{product_id="521",base_name="Mock 1",request_status="success"} 1

Any suggestions or ideas would be greatly appreciated. I have been trying for hours without finding a satisfactory result.

Thank you in advance for your help and support!

Hi @mdavidallen,

It looks like you're using a gauge metric to track the number of requests, which is why you're seeing cumulative values. To get the number of requests within a specific time range, you should use a counter metric instead. However, since you're using a gauge, you can still calculate the rate of change over a time period using the rate() function in PromQL.

Here's how you can modify your query to calculate the number of requests within the selected time range:

promql sum(rate(coleta_online_request_count{product_id=~'$product_id', external_bases_name=~'$base_name'}[5m]))

This query uses the rate() function to calculate the per-second rate of requests over the last 5 minutes. The sum() function then aggregates these rates across all matching time series. This should give you the number of requests per second, which you can multiply by 300 (the number of seconds in 5 minutes) to get the total number of requests in the last 5 minutes.

If you want to see the total number of requests over a different time period, just adjust the [5m] to the desired duration, like [1h] for the last hour.

Let me know if this helps or if you have any more questions!

xoxo Grot

kapa-discourse