Hash High Cardinality Labels


We have switched to Loki for more than two years, and we are happy with it. However, one feature we still miss is being able to search across all services with a user id like this issue suggests we would like to be able to search with the user id as a label.

We had an Idea to reduce the cardinality of the user id label, we can use a modular hashing function to hash the user id values to reduce to X(number of buckets), we will hash the value before we store the log, and then we can query for the user id by using this query {container="x", user_id_hash=${<user-id> % <number-of-buckets} | json | user_id = <user-id> so the results will be first filtered using the label(to the hash bucket) then it will be filtered again using the field to that specific user.

If this is a good idea, then how can hash the value in Grafana? we would like to have a dashboard to enter a user id and it will be hashed to be used in this query {container="x", user_id_hash=${<user-id> % <number-of-buckets} | json | user_id = <user-id>.

Thank you