Title: Prometheus: Count metric value over a period of time
Tags: prometheus
Question: I don't speak English very well, but I need some advice. I have Prometheus.
How can I calculate the number of downtime for a service over a period of time?
It's my function
irate(ALERTS{job="blackbox", alertstate="firing"}[2h])


Here is another answer: From the image you provided, it seems like the value of the metric (say metric_x) is 1 when the server is down, otherwise 0.
You can use sum_over_time(range-vector) query function to calculate, how many times the value was equal to 1 at a given interval.
The following query calculates the sum of metric_x (ie. how many times the mertic_x was equal to 1) within the last 5 minutes.
sum_over_time(metric_x{job="xxx"}[5m])
Similarly, the following query calculates the sum of metric_x of a day had a week ago. It's like a range, starting time to ending time.
sum_over_time(metric_x{job="xxx"}[1d] offset 1w)
References:
Range Vector Selectors
Offset modifier
aggregation_over_time()
Update:
Well If I were you, I would create a recording rule for the query and perform sum_over_time() on the new metric that the rule created.
groups:
- name: rules
rules:
- record: blakbox:ALERTS:irate
expr:  irate(ALERTS{job="blackbox", alertstate="firing"}[2h])
sum_over_time(blakbox:ALERTS:irate{job="blackbox", alertstate="firing"}[1d] offset 1w)
Comment for this answer: @ВладимирWolf Updated the answer, take a look.
Comment for this answer: @ВладимирWolf , it won't work, because `sum_over_time()` takes `range_vector` as input. Have you tried the process I mentioned?
Comment for this answer: @ВладимирWolf, the process looks good to me. what's is result of query `blakbox:ALERTS:irate` ? and `blakbox:ALERTS:irate{job="blackbox", alertstate="firing"}`? and `blakbox:ALERTS:irate{job="blackbox", alertstate="firing"}[90d]` ? are they showing the value as expected?
Comment for this answer: Thank you. Sorry, I gave worng information. I don't need a total idle time. I want to calculate how many times the service did not work. It is my function - >>
irate(ALERTS{job="blackbox", alertstate="firing"}[2h])
Comment for this answer: If I understand correctly, I create a new metric and then count it. But this does not work for me.          sum_over_time(irate(ALERTS{job="blackbox", alertstate="firing", instance="https://example.com"}[2h]))
Comment for this answer: Maybe I'm doing it wrong. I just started learning Prometheus
1) I change file prometheus.yml and add in block rule_files:
rule_files:
- count.rules.yml
2) I add in file count.rules.yml
groups:
- name: rules
rules:
- record: blakbox:ALERTS:irate
expr:  irate(ALERTS{job="blackbox", alertstate="firing"}[2h])
3)Reload prometheus and made a request
sum_over_time(blakbox:ALERTS:irate{job="blackbox", alertstate="firing"}[90d])
Comment for this answer: Sorry, I forgot to answer, but I could not get a positive response from Prometheus

Here is another answer: If the metric can have either 0 or 1 values, then the sum_over_time(metric[d]) calculates the number of 1 values on the specified lookbehind window d. For example, sum_over_time(up[1h]) returns the number of up samples with 1 value during the last hour. The number of 0 values then can be calculates as count_over_time(up[1h]) - sum_over_time(up[1h]).
If the metric can have other values than 0 and 1, then Prometheus doesn't provide functions for counting the number of samples with a particular value yet :(
There is another Prometheus-like system, which allows counting the number of raw samples with the given value on the specified lookback window - VictoriaMetrics (I'm the core developer of this system). It provides count_eq_over_time function for this task. For example, the following MetricsQL query returns the number of samples for some_metric time series with the value 42 over the last hour:
count_eq_over_time(some_metric[1h], 42)

Here is another answer: It looks like you are using Grafana as a visualisation tool. There is the possibility to add an expression to a panel. This would result in a second Metric (in my example below B). This is located in the Query Tab on Edit. You ma need a newer Grafana version (AFAIK it was introduced with v6.x)

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 3, 'CommentCount': 0, 'ContentLicense': 'CC BY-SA 4.0', 'CreationDate': '2020:00:05/06/20', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<prometheus>', 'Title': 'Prometheus: Count metric value over a period of time', 'ViewCount': 12930, 'id': 61630701, 'user_AboutMe': None, 'user_AccountId': 18437988.0, 'user_DisplayName': 'Владимир Wolf', 'user_Reputation': 21.0, 'user_Views': 2.0, 'user_id': 13481057.0}}