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
This commit is contained in:
Sam Saffron 2019-09-02 18:45:35 +10:00
parent afeb7e4b55
commit 08743e8ac0
3 changed files with 25 additions and 3 deletions

View File

@ -182,6 +182,8 @@ module Middleware
$redis.setex(cache_key_body, cache_duration, parts.join) $redis.setex(cache_key_body, cache_duration, parts.join)
$redis.setex(cache_key_other, cache_duration, [status, headers_stripped].to_json) $redis.setex(cache_key_other, cache_duration, [status, headers_stripped].to_json)
headers["X-Discourse-Cached"] = "store"
else else
parts = response parts = response
end end

View File

@ -105,7 +105,7 @@ class Middleware::RequestTracker
track_view &&= env_track_view || (request.get? && !request.xhr? && headers["Content-Type"] =~ /text\/html/) track_view &&= env_track_view || (request.get? && !request.xhr? && headers["Content-Type"] =~ /text\/html/)
track_view = !!track_view track_view = !!track_view
{ h = {
status: status, status: status,
is_crawler: helper.is_crawler?, is_crawler: helper.is_crawler?,
has_auth_cookie: helper.has_auth_cookie?, has_auth_cookie: helper.has_auth_cookie?,
@ -114,9 +114,16 @@ class Middleware::RequestTracker
track_view: track_view, track_view: track_view,
timing: timing, timing: timing,
queue_seconds: env['REQUEST_QUEUE_SECONDS'] 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 end
if cache = headers["X-Discourse-Cached"]
h[:cache] = cache
end
h
end end
def log_request_info(env, result, info) def log_request_info(env, result, info)

View File

@ -272,6 +272,19 @@ describe Middleware::RequestTracker do
Middleware::RequestTracker.unregister_detailed_request_logger(logger) Middleware::RequestTracker.unregister_detailed_request_logger(logger)
end 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 it "can correctly log detailed data" do
global_setting :enable_performance_http_headers, true global_setting :enable_performance_http_headers, true