DEV: Add ENV variable for enabling MiniProfiler snapshots (#10690)

DEV: Add ENV variable for enabling MiniProfiler snapshots (#10690)

  • DEV: Add ENV variable for enabling MiniProfiler snapshots

  • MiniProfiler is not loaded in test env

diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb
index f626119..1d4a19e 100644
--- a/app/controllers/application_controller.rb
+++ b/app/controllers/application_controller.rb
@@ -31,6 +31,7 @@ class ApplicationController < ActionController::Base
   before_action :check_readonly_mode
   before_action :handle_theme
   before_action :set_current_user_for_logs
+  before_action :set_mp_snapshot_fields
   before_action :clear_notifications
   around_action :with_resolved_locale
   before_action :set_mobile_view
@@ -295,6 +296,12 @@ class ApplicationController < ActionController::Base
     response.headers["X-Discourse-Route"] = "#{controller_name}/#{action_name}"
   end
 
+  def set_mp_snapshot_fields
+    if defined?(Rack::MiniProfiler)
+      Rack::MiniProfiler.add_snapshot_custom_field("application version", Discourse.git_version)
+    end
+  end
+
   def clear_notifications
     if current_user && !@readonly_mode
 
diff --git a/config/discourse_defaults.conf b/config/discourse_defaults.conf
index 8dadac2..c725035 100644
--- a/config/discourse_defaults.conf
+++ b/config/discourse_defaults.conf
@@ -95,6 +95,10 @@ smtp_openssl_verify_mode =
 # load MiniProfiler in production, to be used by developers
 load_mini_profiler = true
 
+# Every how many requests should MP profile a request (aka take snapshot)
+# Default is never
+mini_profiler_snapshots_period = 0
+
 # recommended, cdn used to access assets
 cdn_url =
 
diff --git a/config/initializers/006-mini_profiler.rb b/config/initializers/006-mini_profiler.rb
index ce3443e..6bd862f 100644
--- a/config/initializers/006-mini_profiler.rb
+++ b/config/initializers/006-mini_profiler.rb
@@ -24,7 +24,8 @@ if defined?(Rack::MiniProfiler) && defined?(Rack::MiniProfiler::Config)
     connection:  DiscourseRedis.new(nil, namespace: false)
   )
 
-  skip = [
+  Rack::MiniProfiler.config.snapshot_every_n_requests = GlobalSetting.mini_profiler_snapshots_period
+  Rack::MiniProfiler.config.skip_paths = [
     /^\/message-bus/,
     /^\/extra-locales/,
     /topics\/timings/,
@@ -51,10 +52,7 @@ if defined?(Rack::MiniProfiler) && defined?(Rack::MiniProfiler::Config)
   # we DO NOT WANT mini-profiler loading on anything but real desktops and laptops
   # so let's rule out all handheld, tablet, and mobile devices
   Rack::MiniProfiler.config.pre_authorize_cb = lambda do |env|
-    path = env['PATH_INFO']
-
-    (env['HTTP_USER_AGENT'] !~ /iPad|iPhone|Android/) &&
-    !skip.any? { |re| re =~ path }
+    env['HTTP_USER_AGENT'] !~ /iPad|iPhone|Android/
   end
 
   # without a user provider our results will use the ip address for namespacing

GitHub sha: a92d8874

This commit appears in #10690 which was merged by OsamaSayegh.