0

I have the following counter metrics

9:00 AM message_counter{deviceid="1", messagetype="event1"}
9:01 AM message_counter{deviceid="1", messagetype="event2"}
9:02 AM message_counter{deviceid="2", messagetype="event1"}
9:03 AM message_counter{deviceid="3", messagetype="event2"}
9:04 AM message_counter{deviceid="4", messagetype="event1"}
9:05 AM message_counter{deviceid="3", messagetype="event2"}

How do I count the number of active devices for the last 2minutes which is 2(device id 3 and 4)?

Rockstart
  • 2,337
  • 5
  • 30
  • 59
  • This looks like distinct logged events. Prometheus data is, typically, increasing metrics. You may be using the wrong tool for this. – Jason Crease Jul 20 '20 at 11:15
  • Also, we wary of 'series explosion'. Prometheus will create a new series for every unique set of labels. If you have say, 10 000 devices and 100 messagetypes, you'll end up with a million series. – Jason Crease Jul 20 '20 at 11:18

1 Answers1

0

This is a similar question to Prometheus query to count unique label values. But here you only want metrics that have increased over the last two minutes.

You'll need to look at items with non-zero rates over the past 2 minutes:

count(count by (deviceid) (rate(message_counter[2m]) > 0))
Jason Crease
  • 1,896
  • 17
  • 17