FEATURE: anon cache reports data to loggers

FEATURE: anon cache reports data to loggers

This allows custom plugins such as prometheus exporter to log how many requests are stored in the anon cache vs used by the anon cache.

This metric allows us to fine tune cache behaviors

diff --git a/lib/middleware/anonymous_cache.rb b/lib/middleware/anonymous_cache.rb
index 118d47c..5df0a89 100644
--- a/lib/middleware/anonymous_cache.rb
+++ b/lib/middleware/anonymous_cache.rb
@@ -182,6 +182,8 @@ module Middleware
 
           $redis.setex(cache_key_body,  cache_duration, parts.join)
           $redis.setex(cache_key_other, cache_duration, [status, headers_stripped].to_json)
+
+          headers["X-Discourse-Cached"] = "store"
         else
           parts = response
         end
diff --git a/lib/middleware/request_tracker.rb b/lib/middleware/request_tracker.rb
index 2a75ada..607912e 100644
--- a/lib/middleware/request_tracker.rb
+++ b/lib/middleware/request_tracker.rb
@@ -105,7 +105,7 @@ class Middleware::RequestTracker
     track_view &&= env_track_view || (request.get? && !request.xhr? && headers["Content-Type"] =~ /text\/html/)
     track_view = !!track_view
 
-    {
+    h = {
       status: status,
       is_crawler: helper.is_crawler?,
       has_auth_cookie: helper.has_auth_cookie?,
@@ -114,9 +114,16 @@ class Middleware::RequestTracker
       track_view: track_view,
       timing: timing,
       queue_seconds: env['REQUEST_QUEUE_SECONDS']
-    }.tap do |h|
-      h[:user_agent] = env['HTTP_USER_AGENT'] if h[:is_crawler]
+    }
+
+    if h[:is_crawler]
+      h[:user_agent] = env['HTTP_USER_AGENT']
+    end
+
+    if cache = headers["X-Discourse-Cached"]
+      h[:cache] = cache
     end
+    h
   end
 
   def log_request_info(env, result, info)
diff --git a/spec/components/middleware/request_tracker_spec.rb b/spec/components/middleware/request_tracker_spec.rb
index ae73bba..35f9921 100644
--- a/spec/components/middleware/request_tracker_spec.rb
+++ b/spec/components/middleware/request_tracker_spec.rb
@@ -272,6 +272,19 @@ describe Middleware::RequestTracker do
       Middleware::RequestTracker.unregister_detailed_request_logger(logger)
     end
 
+    it "can report data from anon cache" do
+      cache = Middleware::AnonymousCache.new(app([200, {}, ["i am a thing"]]))
+      tracker = Middleware::RequestTracker.new(cache)
+
+      uri = "/path?#{SecureRandom.hex}"
+      tracker.call(env("REQUEST_URI" => uri, "ANON_CACHE_DURATION" => 60))
+
+      expect(@data[:cache]).to eq("store")
+
+      tracker.call(env("REQUEST_URI" => uri, "ANON_CACHE_DURATION" => 60))
+      expect(@data[:cache]).to eq("true")
+    end
+
     it "can correctly log detailed data" do
 
       global_setting :enable_performance_http_headers, true

GitHub sha: 08743e8a

1 Like