Skip to content

A platform for community discussion. Free, open, simple.

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

David Taylorcd3fab9ccc1d
diff --git a/config/initializers/000-trace_pg_connections.rb b/config/initializers/000-trace_pg_connections.rb
index add1990ba09..fcda5bb611f 100644
--- a/config/initializers/000-trace_pg_connections.rb
+++ b/config/initializers/000-trace_pg_connections.rb
@@ -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