DEV: Allow raw PG tracing to be enabled only for sidekiq processes

This commit is contained in:
David Taylor 2020-02-17 18:14:14 +00:00
parent 42729e6a27
commit cd3fab9ccc
No known key found for this signature in database
GPG Key ID: 46904C18B1D3F434
1 changed files with 5 additions and 0 deletions

View File

@ -4,6 +4,9 @@
# to be streamed to files for debugging. The filenames are formatted
# like tmp/pgtrace/{{PID}}_{{CONNECTION_OBJECT_ID}}.txt
#
# Setting TRACE_PG_CONNECTIONS=SIDEKIQ will only trace connections
# on in sidekiq (safer, because there will be minimal user-facing perf impact)
#
# 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
@ -17,6 +20,7 @@ if ENV["TRACE_PG_CONNECTIONS"]
def initialize(*args)
super(*args).tap do
next if ENV["TRACE_PG_CONNECTIONS"] == "SIDEKIQ" && !Sidekiq.server?
FileUtils.mkdir_p(TRACE_DIR)
@trace_filename = "#{TRACE_DIR}/#{Process.pid}_#{self.object_id}.txt"
trace File.new(@trace_filename, "w")
@ -25,6 +29,7 @@ if ENV["TRACE_PG_CONNECTIONS"]
def close
super.tap do
next if ENV["TRACE_PG_CONNECTIONS"] == "SIDEKIQ" && !Sidekiq.server?
File.delete(@trace_filename)
end
end