nginxgrafanagrafana-lokipromtaillogql

How to handle dynamic values in request_uri from nginx access logs


I have a PLG stack which extracts the Nginx logs (which is in json format) and visualize it in Grafana.

I want to create a dashboard in Grafana, where I get latency for different routes.

For this I am using the query:

min_over_time({job="nginx"}
| json
| unwrap request_time [1m])
by (request_uri) 

But I noticed I have some routes which have dynamic values, for example /route1/?name=adarsh. So this route is also popping up in my dashboard which I don't want: I want it to get aggregated in into /route1/.


Solution

  • Using | label_format expression and regexReplaceAll template function it is possible to introduce an additional label sanitized_request_uri with removed parameters:

    min_over_time({job="nginx"}
    | json
    | label_format sanitized_request_uri="{{ regexReplaceAll  \"([^?]*).*\" .request_uri \"$1\" }}"
    | unwrap request_time [1m])
    by (sanitized_request_uri) 
    

    If you experiencing issues with trailing / in request URIs, preventing proper grouping, they can be dropped with addition of following expression

     | label_format sanitized_request_uri="{{ regexReplaceAll  \"(.*)/\" .sanitized_request_uri\"$1\" }}"
    

    after first label_format expression.