Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I calculate the number of metrics by unique labels without specifying a label in Prometheus/Grafana?

Because all URLs are dynamic, I cannot specify a specific URL when I group labels. Because every time a unique URL can appear.

Here's an example from metrics.

16:00:00
http_trace{method="GET",status="404",uri="/actuator/conditions/777",} 1.0
http_trace{method="GET",status="200",uri="/actuator/conditions",} 3.0
http_trace{method="GET",status="200",uri="/actuator/test",} 3.0
http_trace{method="GET",status="200",uri="/actuator/beans",} 3.0

16:00:30
http_trace{method="GET",status="404",uri="/actuator/conditions/777",} 4.0
http_trace{method="GET",status="200",uri="/actuator/conditions",} 2.0
http_trace{method="GET",status="200",uri="/actuator/test",} 6.0
http_trace{method="GET",status="200",uri="/actuator/beans123",} 7.0

How can I make Promethus or Grafana Query - So that I get that number.?

uri="/actuator/conditions/777" -> 5.0
uri="/actuator/conditions" -> 5.0
uri="/actuator/test" -> 9.0
uri="/actuator/beans" -> 3.0
uri="/actuator/beans123" -> 7.0

Thanks.

like image 995
JDev Avatar asked Nov 24 '25 11:11

JDev


1 Answers

If you want to calculate a sum of metric values grouped by some label:

sum(http_trace) by (uri)

This will give you the following result:

{uri="/actuator/conditions/777"} 5.0
{uri="/actuator/conditions"} 5.0
{uri="/actuator/test"} 9.0
{uri="/actuator/beans"} 3.0
{uri="/actuator/beans123"} 7.0

If you want to get the number of time-series (=how many metrics are there), grouped by uri :

count(http_trace) by (uri)

And the result will be:

{uri="/actuator/conditions/777"} 1.0
{uri="/actuator/conditions"} 1.0
{uri="/actuator/test"} 1.0
{uri="/actuator/beans"} 1.0
{uri="/actuator/beans123"} 1.0

This shows how many metrics with unique label sets are there, grouped by uri label.

like image 54
anemyte Avatar answered Nov 27 '25 09:11

anemyte



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!