How to imitate the SELECT COUNT DISTINCT on PromQL in Grafana

39 Views Asked by At

I've been hours trying to imitate the SELECT COUNT DISTINCT on PromQL in Grafana but I'm not able to achieve it.

Let me introduce the following scenario (fictitious, to give an example):

  • Imagine I have one metric called api_interaction that is incremented each time an external application interacts with my api. Imagine also that this metric has a tag to save the clientId that interacts with this api.So I have:
    api_interaction{app="foo", instance="server-1", clientId="123"}  20
    api_interaction{app="foo", instance="server-1", clientId="456"}  20
    api_interaction{app="foo", instance="server-2", clientId="789"}  30
    api_interaction{app="foo", instance="server-2", clientId="123"}  10
  • Imagine I have another metric called outside_interaction that is incremented each time an external application that has interacted with my api requires to access to an external service so my api acts as a bridge. I also have a tag to save the clientId.
    outside_interaction{app="foo", instance="server-1", clientId="123"}  2
    outside_interaction{app="foo", instance="server-1", clientId="456"}  1
    outside_interaction{app="foo", instance="server-2", clientId="789"}  3

At the end what I want to do is a rate connections to my api / requires external service but but I find that if I simply divide one metric by the other I will be messing up the data since maybe a customer connects 20 times a day but only one of them requires the external service. So really what I want to do is to do it based on the clientId tag and count for each client tag value only once.

So given the examples above I should have that: clients 123, 456 and 789 at least connected once and at least required external access once so 3/3 = rate of 100%.

I tried using count_values("client_id, api_interaction) promQL method but is not working as expected. It keeps showing lots of values with clientIds I do not recognize and setting them to 1.

Also checked this documentation but wasn't able to find what I'm looking for.

Thanks in advance!

1

There are 1 best solutions below

3
mark04321 On

count_over_time(api_interaction{app="foo"} == on(clientId) group_left outside_interaction{app="foo"}[1d])