Live users count based on logs

Hi!

I’ve been trying to get the number of distinct users currently using my application using Loki.
I’ve got a user_id field in my logs, and the idea is to count the number of distinct values within a time period.

I came across multiple topics related to the subject, but most of them were closed without a proper answer (Creating log-based metric to count unique field values in logs, Count number of unique values for a key, and others I can’t link because the forum blocks me :smile:). I have found one potential solution to this problem, but I’m not sure if it gives me the correct result, and it seems to be awfully inefficient as the query can’t seem to work for time ranges longer than one minute.

count(count by(user_id) (rate({service_name="my-app"} [1m])))

Are there, by any chance, better solutions for that? I considered “Unwrapped range aggregations” by my lack of experience in LogQL seem to prevent me from using them correctly :smiling_face_with_tear:

Thanks in advance!

Maybe try this:

count by (user_id) (
  count_over_time({<SELECTOR>} [$__auto])
)

Thanks for the reply, but this is not exactly what I want :no_mouth:
This gives me the number of logs over a period, grouped by user_id. What I want if the number of distinct user_id over this period. If I wrap it into a count like this:

count(
  count by (user_id) (
    count_over_time({<SELECTOR>} [$__auto])
  )
)

I think it gives me the result I want, but the query fails if there are too many users, and I don’t quite understand why. I get this error: maximum of series (500) reached for a single query