"Fossies" - the Fresh Open Source Software Archive

Member "discourse-2.8.3/config/initializers/006-mini_profiler.rb" (14 Apr 2022, 4401 Bytes) of package /linux/www/discourse-2.8.3.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Ruby source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. See also the last Fossies "Diffs" side-by-side code changes report for "006-mini_profiler.rb": 2.7.13_vs_2.8.0.

    1 # frozen_string_literal: true
    2 
    3 # If Mini Profiler is included via gem
    4 if Rails.configuration.respond_to?(:load_mini_profiler) && Rails.configuration.load_mini_profiler
    5   require 'rack-mini-profiler'
    6   require 'stackprof'
    7 
    8   begin
    9     require 'memory_profiler'
   10   rescue => e
   11     STDERR.put "#{e} failed to require mini profiler"
   12   end
   13 
   14   # initialization is skipped so trigger it
   15   Rack::MiniProfilerRails.initialize!(Rails.application)
   16 end
   17 
   18 if defined?(Rack::MiniProfiler) && defined?(Rack::MiniProfiler::Config)
   19   # note, we may want to add some extra security here that disables mini profiler in a multi hosted env unless user global admin
   20   #   raw_connection means results are not namespaced
   21   #
   22   # namespacing gets complex, cause mini profiler is in the rack chain way before multisite
   23   Rack::MiniProfiler.config.storage_instance = Rack::MiniProfiler::RedisStore.new(
   24     connection:  DiscourseRedis.new(nil, namespace: false)
   25   )
   26 
   27   Rack::MiniProfiler.config.snapshot_every_n_requests = GlobalSetting.mini_profiler_snapshots_period
   28   Rack::MiniProfiler.config.snapshots_transport_destination_url = GlobalSetting.mini_profiler_snapshots_transport_url
   29   Rack::MiniProfiler.config.snapshots_transport_auth_key = GlobalSetting.mini_profiler_snapshots_transport_auth_key
   30   Rack::MiniProfiler.config.skip_paths = [
   31     /^\/message-bus/,
   32     /^\/extra-locales/,
   33     /topics\/timings/,
   34     /assets/,
   35     /\/user_avatar\//,
   36     /\/letter_avatar\//,
   37     /\/letter_avatar_proxy\//,
   38     /\/highlight-js\//,
   39     /\/svg-sprite\//,
   40     /qunit/,
   41     /srv\/status/,
   42     /commits-widget/,
   43     /^\/cdn_asset/,
   44     /^\/logs/,
   45     /^\/site_customizations/,
   46     /^\/uploads/,
   47     /^\/secure-media-uploads/,
   48     /^\/javascripts\//,
   49     /^\/images\//,
   50     /^\/stylesheets\//,
   51     /^\/favicon\/proxied/
   52   ]
   53 
   54   # we DO NOT WANT mini-profiler loading on anything but real desktops and laptops
   55   # so let's rule out all handheld, tablet, and mobile devices
   56   Rack::MiniProfiler.config.pre_authorize_cb = lambda do |env|
   57     env['HTTP_USER_AGENT'] !~ /iPad|iPhone|Android/
   58   end
   59 
   60   # without a user provider our results will use the ip address for namespacing
   61   #  with a load balancer in front this becomes really bad as some results can
   62   #  be stored associated with ip1 as the user and retrieved using ip2 causing 404s
   63   Rack::MiniProfiler.config.user_provider = lambda do |env|
   64     request = Rack::Request.new(env)
   65     id = request.cookies["_t"] || request.ip || "unknown"
   66     id = id.to_s
   67     # some security, lets not have these tokens floating about
   68     Digest::MD5.hexdigest(id)
   69   end
   70 
   71   Rack::MiniProfiler.config.position = 'left'
   72   Rack::MiniProfiler.config.backtrace_ignores ||= []
   73   Rack::MiniProfiler.config.backtrace_ignores << /lib\/rack\/message_bus.rb/
   74   Rack::MiniProfiler.config.backtrace_ignores << /config\/initializers\/silence_logger/
   75   Rack::MiniProfiler.config.backtrace_ignores << /config\/initializers\/quiet_logger/
   76 
   77   Rack::MiniProfiler.config.backtrace_includes = [/^\/?(app|config|lib|test|plugins)/]
   78 
   79   Rack::MiniProfiler.config.max_traces_to_show = 100 if Rails.env.development?
   80 
   81   Rack::MiniProfiler.counter_method(Redis::Client, :call) { 'redis' }
   82   # Rack::MiniProfiler.counter_method(ActiveRecord::QueryMethods, 'build_arel')
   83   # Rack::MiniProfiler.counter_method(Array, 'uniq')
   84   # require "#{Rails.root}/vendor/backports/notification"
   85 
   86   # inst = Class.new
   87   # class << inst
   88   #   def start(name,id,payload)
   89   #     if Rack::MiniProfiler.current && name !~ /(process_action.action_controller)|(render_template.action_view)/
   90   #       @prf ||= {}
   91   #       @prf[id] ||= []
   92   #       @prf[id] << Rack::MiniProfiler.start_step("#{payload[:serializer] if name =~ /serialize.serializer/} #{name}")
   93   #     end
   94   #   end
   95 
   96   #   def finish(name,id,payload)
   97   #     if Rack::MiniProfiler.current && name !~ /(process_action.action_controller)|(render_template.action_view)/
   98   #       t = @prf[id].pop
   99   #       @prf.delete id unless t
  100   #       Rack::MiniProfiler.finish_step t
  101   #     end
  102   #   end
  103   # end
  104   # disabling for now cause this slows stuff down too much
  105   # ActiveSupport::Notifications.subscribe(/.*/, inst)
  106 
  107   # Rack::MiniProfiler.profile_method ActionView::PathResolver, 'find_templates'
  108 end
  109 
  110 if ENV["PRINT_EXCEPTIONS"]
  111   trace = TracePoint.new(:raise) do |tp|
  112     puts tp.raised_exception
  113     puts tp.raised_exception.backtrace.join("\n")
  114     puts
  115   end
  116   trace.enable
  117 end