mirror of
https://github.com/discourse/discourse.git
synced 2024-11-22 14:38:17 +08:00
DEV: Add raw PG connection tracing behind an environment variable
This should be useful for debugging connection problems. Warning: this will generate some large files, and will likely impact performance
This commit is contained in:
parent
72b6f908f4
commit
0c6f2892c6
33
config/initializers/000-trace_pg_connections.rb
Normal file
33
config/initializers/000-trace_pg_connections.rb
Normal file
|
@ -0,0 +1,33 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
# Setting TRACE_PG_CONNECTIONS=1 will cause all pg connections
|
||||
# to be streamed to files for debugging. The filenames are formatted
|
||||
# like tmp/pgtrace/{{PID}}_{{CONNECTION_OBJECT_ID}}.txt
|
||||
#
|
||||
# Files will be automatically deleted when the connection is closed gracefully
|
||||
# (e.g. when activerecord closes it after a period of inactivity)
|
||||
# Files will not be automatically deleted when closed abruptly
|
||||
# (e.g. terminating/restarting the app process)
|
||||
#
|
||||
# Warning: this could create some very large files!
|
||||
|
||||
if ENV["TRACE_PG_CONNECTIONS"]
|
||||
PG::Connection.prepend(Module.new do
|
||||
TRACE_DIR = "tmp/pgtrace"
|
||||
|
||||
def initialize(*args)
|
||||
super(*args).tap do
|
||||
FileUtils.mkdir_p(TRACE_DIR)
|
||||
@trace_filename = "#{TRACE_DIR}/#{Process.pid}_#{self.object_id}.txt"
|
||||
trace File.new(@trace_filename, "w")
|
||||
end
|
||||
end
|
||||
|
||||
def close
|
||||
super.tap do
|
||||
File.delete(@trace_filename)
|
||||
end
|
||||
end
|
||||
|
||||
end)
|
||||
end
|
Loading…
Reference in New Issue
Block a user