Combing two metrics to get a new one. (Calculate a ratio by comparing two metrics arithmetically)

114 views
Skip to first unread message

Dave Southwell

unread,
Aug 6, 2019, 8:39:59 PM8/6/19
to Prometheus Users
Hello,

I've done some searching, but haven't found anything definitive.  What I'd like to do is create a query against Prometheus that will extract two metrics as sum's, and then perform some arithmetic on them.  The use-case here is to compare two histogram metrics of status code returns.

The metric is: http_status_codes. Here's some pseudo code of what I'd like to do.

sum( sum(irate(http_status_codes{host=~".*mysite.*",code="429"}[1m])) / sum(irate(http_status_codes{host=~".*mysite.*",code="200"}[1m])) )

I want to calculate a ratio of the number of 429's to 200's.

Is this possible with a prometheus query?

-Dave

Matt Haag

unread,
Aug 7, 2019, 1:16:58 PM8/7/19
to Prometheus Users
Hey Dave,

You would want to use a recording_rule for this expression. 
https://prometheus.io/docs/prometheus/latest/configuration/recording_rules/

I believe you would want a recording rule for the rate of 429s, the rate of 200s, and then if you'd like. You could have a 3rd recording rule that is performs the arithmetic on the first two recording rules we defined.

Matt Haag
Reply all
Reply to author
Forward
0 new messages