DEV: Add more debugging information to AR query logs on GitHub actions (#25237)

Why this change?

We have been chasing a problem with our flaky system test where the user
is logged out when it should never be.

What does this change do?

1. Logs the request path when lookup a user auth token.
2. Logs the request path and also the current thread's object id in
   ActiveRecord query logs.
This commit is contained in:
Alan Guo Xiang Tan 2024-01-12 13:06:29 +08:00 committed by GitHub
parent 80b93e06f7
commit c76ca876a6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 1 deletions

View File

@ -122,13 +122,19 @@ class UserAuthToken < ActiveRecord::Base
expire_before = SiteSetting.maximum_session_age.hours.ago expire_before = SiteSetting.maximum_session_age.hours.ago
user_token = user_token =
find_by( where(
"(auth_token = :token OR "(auth_token = :token OR
prev_auth_token = :token) AND rotated_at > :expire_before", prev_auth_token = :token) AND rotated_at > :expire_before",
token: token, token: token,
expire_before: expire_before, expire_before: expire_before,
) )
if SiteSetting.verbose_auth_token_logging && path = opts.dig(:path)
user_token = user_token.annotate("path:#{path}")
end
user_token = user_token.first
if !user_token if !user_token
log_verbose( log_verbose(
action: "miss token", action: "miss token",

View File

@ -53,6 +53,16 @@ Discourse::Application.configure do
config.active_record.verbose_query_logs = true config.active_record.verbose_query_logs = true
config.active_record.query_log_tags_enabled = true config.active_record.query_log_tags_enabled = true
config.active_record.query_log_tags = [
:application,
:controller,
:action,
{
request_path: ->(context) { context[:controller]&.request&.path },
thread_id: ->(context) { Thread.current.object_id },
},
]
config.after_initialize do config.after_initialize do
ActiveRecord::LogSubscriber.backtrace_cleaner.add_silencer do |line| ActiveRecord::LogSubscriber.backtrace_cleaner.add_silencer do |line|
line =~ %r{lib/freedom_patches} line =~ %r{lib/freedom_patches}