Search code examples
grafanagrafana-loki

Removing duplicate entries with Grafana and Loki


I am using Grafana to visualize metrics from Prometheus and logs from Loki about an app. There are log statements such as:

{"action": "action_a", "username": "user_1", "ts": 1012}
{"action": "action_a", "username": "user_2", "ts": 1008}
{"action": "action_a", "username": "user_1", "ts": 1005}
{"action": "action_a", "username": "user_1", "ts": 1000}

and I have a query to get a list of the "recently active users", using the Grafana Logs panel:

{job="my-app"} | json | username != "" | line_format "{{.username}}"

I have tried all values of Deduplication, and this mostly works fine, except in the (common) case where users are making actions in between each other (as above), then I get logs like this:

user_1
user_2
user_1

How can I make it so it only shows each user one time?, eg:

user_1
user_2

Solution

  • Try something like this:

    count by (username) (count_over_time({job="my-app"} | json | username != "" [$__range]))
    

    You can show this data using, for example, a pie chart on Grafana. See below how to configure this, using a different log file and a different label ("branch" instead of "username"):

    enter image description here