David
David

Reputation: 10738

how to aggregate prometheus counters during a specific time period

This is what my metrics endpoint looks like:

app_ui_card_open{host="foo.bar.com",card_id="listing_tickets",username="smiller"} 2 app_ui_card_open{host="foo.bar.com",card_id="listing_companies",username="smiller"} 1 app_ui_card_open{host="foo.bar.com",card_id="listing_contacts",username="smiller"} 1

There are other similar lines with other usernames but these are all of them for the "smiller" username.

I've just started collecting this data over the past week. I'm trying to understand how i can a single count of all of the app_ui_card_open counts per username.

This is the query i came up with:

sum(increase(app_ui_card_open{instance="foo.bar.com"}[1y])) by (username)

However, the produces a value of only 1 for the "smiller" username. I would expect to see the value of 4; the sum of all those metrics listed above since 1y would contain the entire dataset.

Is my query wrong? Is prometheus just doing it's estimating even though the data set is really small?

Upvotes: 1

Views: 5777

Answers (3)

valyala
valyala

Reputation: 17784

The following query should return the number of cards opened by every user during the last 6 hours:

sum(increase(app_ui_card_open[6h])) by (username)

The query assumes that the app_ui_card_open is a counter metric, otherwise the query will return unexpected results.

Prometheus may return unexpected fractional results for integer-only app_ui_card_open metric because of extrapolation. If you need the exact integer results, then try this query in VictoriaMetrics - this is Prometheus-like monitoring solution I work on. It implements the increase() function without extrapolation. See this article for more details.

Upvotes: 0

Just use the following query:

sum by (username) (app_ui_card_open{host="foo.bar.com"})

Upvotes: 0

Felipe
Felipe

Reputation: 7563

Using increase you will get how much the values increased over a period. In your case they increased just by 1. i.e.: 1 -> 1 -> 2. Use the sum_over_time as the docs say.

sum_over_time(range-vector): the sum of all values in the specified interval.

after that you group by username

sum(
    sum_over_time(app_ui_card_open{instance="foo.bar.com"}[1y])
) ​by (username)

check this demo as an example in case your query does not work.

Upvotes: 1

Related Questions