Get last n logs in each label

Hi!

Can you tell me how to get the latest n logs or latest log for each label value using only one query?

Now I’m doing this by following a query and using a transformation.

{service_name=~“.+”}

“service_name” is label.

However, if one of the label values has the same number of logs as the line limit, the others will not be included in the query.

I have about 100 service_name label values. And I would not like to have a separate panel in dashboards for each value.

This is one log.

1 Like

I don’t know how this can be done in LogQL, but it is easy to do in LogsQL:

* | stats by (service_name) row_max(_time) as last_log

It uses the row_max stats function inside stats pipe .

This query returns log entry with the biggest timestamp per every service_name label.