Sam
02/13/2025, 2:01 AMrate method from Prometheus to calculate the rate per minute of a counter value while handing the counter resets?Sam
02/13/2025, 4:49 AMrate method, but I am not sure the performance will be okay in scale
WITH incremental_values AS (
SELECT
__time,
TIME_FLOOR(__time, 'PT1M') AS minute_bucket,
counter_value,
LAG(counter_value) OVER w AS prev_value
FROM "cumulative_data"
WINDOW w AS (ORDER BY __time)
)
SELECT
minute_bucket,
SUM(CASE WHEN counter_value > prev_value THEN counter_value - prev_value ELSE counter_value END) AS rate_per_minute
FROM incremental_values
GROUP BY minute_bucket
ORDER BY minute_bucket
Data
{"timestamp": "2024-03-11T12:00:05Z", "counter_value": 100}
{"timestamp": "2024-03-11T12:00:30Z", "counter_value": 150}
{"timestamp": "2024-03-11T12:00:55Z", "counter_value": 200}
{"timestamp": "2024-03-11T12:01:10Z", "counter_value": 210}
{"timestamp": "2024-03-11T12:01:40Z", "counter_value": 50}
{"timestamp": "2024-03-11T12:01:55Z", "counter_value": 100}
{"timestamp": "2024-03-11T12:02:15Z", "counter_value": 5}
{"timestamp": "2024-03-11T12:02:50Z", "counter_value": 30}