Grafana Pattern Parser for Nginx Logs

We use Grafana and Loki to ingest and monitor Nginx access logs. I was trying to find the average response time for one specific URL; Grafana out of the box provides a number of helpful labels, but wasn’t parsing the Nginx logs into labels.

Here’s the pattern parser I came up with:

<ip> [<timestamp>] <host> "<method> <path> <_>" HTTP <response_code> <response_size> time:<duration>s "<referer>" "<user_agent>" <_>

You can read the docs, but basically you supply a pattern, and each set of angle brackets is a “capture group”; if you don’t care about part of the line, then you can use <_> to discard it.

And here’s the full query that breaks the logs apart into those labels, filters to just the one path, and then plots the duration as points on a graph:

sum by() (
  avg_over_time(
    {pod=~"app-.+", container="nginx"}
    | pattern `<ip> [<timestamp>] <host> "<method> <path> <_>" HTTP <response_code> <response_size> time:<duration>s "<referer>" "<user_agent>" <_>`
    | path = `/path/I/want/to/inspect`
    | unwrap duration [$__interval]
    )
)

Installing New Relic on RunCloud

Now that New Relic is offering a free tier, I’ve started using it to monitor some personal websites.

This particular server was configured using RunCloud, and the New Relic installation script didn’t automatically install New Relic monitoring for RunCloud’s PHP. Here are the steps I followed to get that working:

  1. Install New Relic APM for PHP following this documentation.
  2. Find the RunCloud PHP directories: /RunCloud/Packages/php7*rc/bin
  3. Set the NR_INSTALL_PATH variable to all the versions I had installed, as described here.
  4. Run newrelic-install and select all to install for all versions.
  5. Reload PHP: systemctl reload php7*rc-fpm

Bonus

I found this plugin that adds WordPress-specific knowledge to New Relic reporting.

Bonus 2

New Relic allows you to specify multiple app names to group together all the sites on a server, all of one client’s sites, etc. See their documentation for more details.

The WordPress plugin provides the wp_nr_app_name filter to modify the app name.