Group by regex Loki

  • What Grafana version and what operating system are you using?
    Grafana Cloud 9.0.5

  • What are you trying to achieve?
    Create a bar chart that shows # request per path, grouped by parent path, based on the access logs of an nginx container.

  • How are you trying to achieve it?
    A query in Grafana, based on a Loki data source.

  • What happened?
    I don’t know how to write this query.

  • What did you expect to happen?
    The results are grouped by parent path. For example /path/subpath and /path/othersubpath are grouped under /path.

For example, given these fake logs:

GET /foo/bar
GET /foo/baz 
GET /quux/
GET /foo
GET /baz

I would expect to get a table like the following:

/foo 3 
/quux 1 
/baz 1
  • Can you copy/paste the configuration(s) that you are having problems with?

Query:

sum by(path) 
  (count_over_time(
    {compose_project="deployment"} 
    | pattern `<_> - - <_> "<method> <path> <_>" <status> <_> <_> "<_>" <_>`   
    | path!=`` [$__range]))

One thing I did try was creating a column for each label of each series. This results in each row having a column that contains its path. If all I could do now was calculate a column that calculates the parent path with a regex or something, I’d be set.