diff --git a/lib/middleware/anonymous_cache.rb b/lib/middleware/anonymous_cache.rb index 118d47c9230..5df0a896d39 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 2a75adaf2e9..607912ee28b 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 ae73bbaa034..35f9921f3c8 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