From 3a263aaacc54de52bdf10ebfb60e59654fc0da89 Mon Sep 17 00:00:00 2001
From: Kristian Klausen <kristian@klausen.dk>
Date: Sat, 24 Aug 2024 18:44:48 +0200
Subject: [PATCH] loki/nginx: Add recording rule for tracking upstream cache
 status[1]

This enables us to calculate the cache hit ratio. Please note that the
variable is only set when caching is enabled (e.g. proxy_cache is
configured), so

FIXME: the sum of the two metrics are too high..

correlate

[1] http://nginx.org/en/docs/http/ngx_http_upstream_module.html#var_upstream_cache_status
---
 roles/loki/files/rules.yaml         | 2 ++
 roles/nginx/templates/nginx.conf.j2 | 7 +++++--
 2 files changed, 7 insertions(+), 2 deletions(-)

diff --git a/roles/loki/files/rules.yaml b/roles/loki/files/rules.yaml
index c9ea6e547..935d1b2b7 100644
--- a/roles/loki/files/rules.yaml
+++ b/roles/loki/files/rules.yaml
@@ -4,3 +4,5 @@ groups:
     rules:
       - record: nginx:requests:rate1m
         expr: 'sum by (instance, server_protocol, ssl_protocol, ssl_cipher) (rate({job="nginx"}[1m] | json))'
+      - record: nginx:requests:rate1m
+        expr: 'sum by (instance, upstream_cache_status) (rate({job="nginx"}[1m] | json))'
diff --git a/roles/nginx/templates/nginx.conf.j2 b/roles/nginx/templates/nginx.conf.j2
index bb1344f3c..d6da832b7 100644
--- a/roles/nginx/templates/nginx.conf.j2
+++ b/roles/nginx/templates/nginx.conf.j2
@@ -28,13 +28,14 @@ http {
         '$remote_addr $host $remote_user [$time_local] "$request" '
         '$status $body_bytes_sent "$http_referer" '
         '"$http_user_agent" "$http_x_forwarded_for" $request_time '
-        '$server_protocol $ssl_protocol $ssl_cipher';
+        '$server_protocol $ssl_protocol $ssl_cipher '
+        '$upstream_cache_status';
 
     log_format reduced
         '$host [$time_local] "$request" '
         '$status $body_bytes_sent "$http_referer" '
         '"$http_user_agent" $server_protocol $ssl_protocol '
-        '$ssl_cipher';
+        '$ssl_cipher $upstream_cache_status';
 
     log_format json_main escape=json
         '{'
@@ -53,6 +54,7 @@ http {
         '"server_protocol":"$server_protocol",'
         '"ssl_protocol":"$ssl_protocol",'
         '"ssl_cipher":"$ssl_cipher",'
+        '"upstream_cache_status":"$upstream_cache_status",'
         # This was added to keep every log line unique as Loki drops
         # log line with the same timestamp and log text:
         # https://grafana.com/docs/loki/latest/overview/#timestamp-ordering
@@ -73,6 +75,7 @@ http {
         '"server_protocol":"$server_protocol",'
         '"ssl_protocol":"$ssl_protocol",'
         '"ssl_cipher":"$ssl_cipher",'
+        '"upstream_cache_status":"$upstream_cache_status",'
         # This was added to keep every log line unique as Loki drops
         # log line with the same timestamp and log text:
         # https://grafana.com/docs/loki/latest/overview/#timestamp-ordering
-- 
GitLab