Is it possible to aggregate Loki logs by day on Grafana?
Asked Answered
P

2

6

I have a set of logs like this:

{"action": "action_a", "username": "user_1", "ts": "2021-09-10T02:18:14.103Z"}
{"action": "action_a", "username": "user_2", "ts": "2021-09-10T02:17:14.103Z"}
{"action": "action_a", "username": "user_1", "ts": "2021-09-09T02:16:14.103Z"}
{"action": "action_a", "username": "user_1", "ts": "2021-09-08T02:15:14.103Z"}

Is it possible to group the logs by date and username to get a count of unique users per day?

I currently have the following query:

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

This effectively gives me a pie chart of unique users for the current dashboard range. Instead, I would like a time-series to show the number of unique users per day (for the past 60 days, for example). In other words, the Daily Active Users (DAU).

With the above logs, I need something like:

{"2021-09-10": 2}
{"2021-09-09": 1}
{"2021-09-08": 1}

Is this possible with Loki or should I look to something like Elasticsearch instead?

Pub answered 10/9, 2021 at 2:43 Comment(0)
M
2

To aggregate by day with LogQL, you can replace $__range with your desired time grouping interval.

E.g.

sum by (username) (
  count_over_time(
    {job="my-app"} | json | username != ""
  [1d]) # <-- put your desired time interval here instead of $__range
)

You can then use a time series visualization to show you your data:

a Grafana dashboard showing user signup per day. It uses the Time Series visualization

Useful links:

Mawkin answered 20/7, 2022 at 22:1 Comment(0)
S
0

Maybe creating a new label using label_format would do the trick? Labels format expression

sum by (day) (
  count_over_time({job="my-app"} | json | label_format day=`{{.ts| substr 0 10}}` | username != ""[$__range])
)
Shebashebang answered 17/6, 2022 at 9:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.