From a92d88747e39811afee9a439f96eb2c6e227638e Mon Sep 17 00:00:00 2001 From: Osama Sayegh Date: Thu, 17 Sep 2020 18:18:35 +0300 Subject: [PATCH] DEV: Add ENV variable for enabling MiniProfiler snapshots (#10690) * DEV: Add ENV variable for enabling MiniProfiler snapshots * MiniProfiler is not loaded in test env --- app/controllers/application_controller.rb | 7 +++++++ config/discourse_defaults.conf | 4 ++++ config/initializers/006-mini_profiler.rb | 8 +++----- 3 files changed, 14 insertions(+), 5 deletions(-) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index f626119901e..1d4a19ebe7a 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 8dadac203e4..c7250354b46 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 ce3443e051b..6bd862f14dc 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