"Fossies" - the Fresh Open Source Software Archive

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