2019-05-02 17:17:27 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2013-02-05 13:16:51 -06:00
|
|
|
# If Mini Profiler is included via gem
|
2022-05-04 20:50:02 -05:00
|
|
|
if Rails.configuration.respond_to?(:load_mini_profiler) && Rails.configuration.load_mini_profiler &&
|
|
|
|
RUBY_ENGINE == "ruby"
|
2013-04-11 01:24:08 -05:00
|
|
|
require "rack-mini-profiler"
|
2020-12-28 20:54:41 -06:00
|
|
|
require "stackprof"
|
2016-06-30 12:21:40 -05:00
|
|
|
|
|
|
|
begin
|
|
|
|
require "memory_profiler"
|
|
|
|
rescue => e
|
|
|
|
STDERR.put "#{e} failed to require mini profiler"
|
|
|
|
end
|
2014-12-28 20:30:54 -06:00
|
|
|
|
2013-04-11 01:24:08 -05:00
|
|
|
# initialization is skipped so trigger it
|
|
|
|
Rack::MiniProfilerRails.initialize!(Rails.application)
|
|
|
|
end
|
|
|
|
|
2018-12-09 21:29:20 -06:00
|
|
|
if defined?(Rack::MiniProfiler) && defined?(Rack::MiniProfiler::Config)
|
2013-03-25 01:19:59 -05:00
|
|
|
# note, we may want to add some extra security here that disables mini profiler in a multi hosted env unless user global admin
|
|
|
|
# raw_connection means results are not namespaced
|
|
|
|
#
|
|
|
|
# namespacing gets complex, cause mini profiler is in the rack chain way before multisite
|
2017-08-02 00:32:01 -05:00
|
|
|
Rack::MiniProfiler.config.storage_instance =
|
|
|
|
Rack::MiniProfiler::RedisStore.new(connection: DiscourseRedis.new(nil, namespace: false))
|
2013-02-05 13:16:51 -06:00
|
|
|
|
2020-09-17 10:18:35 -05:00
|
|
|
Rack::MiniProfiler.config.snapshot_every_n_requests = GlobalSetting.mini_profiler_snapshots_period
|
2020-10-21 11:37:28 -05:00
|
|
|
Rack::MiniProfiler.config.snapshots_transport_destination_url =
|
|
|
|
GlobalSetting.mini_profiler_snapshots_transport_url
|
|
|
|
Rack::MiniProfiler.config.snapshots_transport_auth_key =
|
|
|
|
GlobalSetting.mini_profiler_snapshots_transport_auth_key
|
2023-03-06 05:39:15 -06:00
|
|
|
Rack::MiniProfiler.config.skip_paths =
|
|
|
|
%w[
|
|
|
|
/assets/
|
|
|
|
/cdn_asset/
|
|
|
|
/extra-locales/
|
|
|
|
/favicon/proxied
|
|
|
|
/highlight-js/
|
|
|
|
/images/
|
|
|
|
/javascripts/
|
|
|
|
/letter_avatar_proxy/
|
|
|
|
/letter_avatar/
|
|
|
|
/logs
|
|
|
|
/manifest.webmanifest
|
|
|
|
/message-bus/
|
|
|
|
/opensearch.xml
|
|
|
|
/presence/
|
|
|
|
/secure-media-uploads/
|
|
|
|
/secure-uploads/
|
|
|
|
/srv/status
|
|
|
|
/stylesheets/
|
|
|
|
/svg-sprite/
|
|
|
|
/theme-javascripts
|
|
|
|
/topics/timings
|
|
|
|
/uploads/
|
|
|
|
/user_avatar/
|
2023-11-16 17:17:32 -06:00
|
|
|
].map { |path| "#{Discourse.base_path}#{path}" }.concat([/.*theme-qunit/])
|
2015-04-16 21:16:37 -05:00
|
|
|
|
2016-03-15 18:54:40 -05:00
|
|
|
# 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
|
2013-02-25 10:42:20 -06:00
|
|
|
Rack::MiniProfiler.config.pre_authorize_cb =
|
2020-09-17 10:18:35 -05:00
|
|
|
lambda { |env| env["HTTP_USER_AGENT"] !~ /iPad|iPhone|Android/ }
|
2013-02-05 13:16:51 -06:00
|
|
|
|
2013-03-24 22:09:28 -05:00
|
|
|
# without a user provider our results will use the ip address for namespacing
|
|
|
|
# with a load balancer in front this becomes really bad as some results can
|
2013-06-19 14:06:23 -05:00
|
|
|
# be stored associated with ip1 as the user and retrieved using ip2 causing 404s
|
2013-03-24 21:52:03 -05:00
|
|
|
Rack::MiniProfiler.config.user_provider =
|
|
|
|
lambda do |env|
|
2013-03-24 22:36:55 -05:00
|
|
|
request = Rack::Request.new(env)
|
|
|
|
id = request.cookies["_t"] || request.ip || "unknown"
|
|
|
|
id = id.to_s
|
|
|
|
# some security, lets not have these tokens floating about
|
|
|
|
Digest::MD5.hexdigest(id)
|
2013-03-24 21:52:03 -05:00
|
|
|
end
|
|
|
|
|
2022-02-23 08:42:57 -06:00
|
|
|
# Cookie path should be set to the base path so Discourse's session cookie path
|
|
|
|
# does not get clobbered.
|
2022-02-23 13:18:58 -06:00
|
|
|
Rack::MiniProfiler.config.cookie_path = Discourse.base_path.presence || "/"
|
2022-02-23 08:42:57 -06:00
|
|
|
|
2022-07-14 06:03:43 -05:00
|
|
|
Rack::MiniProfiler.config.position = "right"
|
|
|
|
|
2013-02-05 13:16:51 -06:00
|
|
|
Rack::MiniProfiler.config.backtrace_ignores ||= []
|
|
|
|
Rack::MiniProfiler.config.backtrace_ignores << %r{lib/rack/message_bus.rb}
|
|
|
|
Rack::MiniProfiler.config.backtrace_ignores << %r{config/initializers/silence_logger}
|
|
|
|
Rack::MiniProfiler.config.backtrace_ignores << %r{config/initializers/quiet_logger}
|
|
|
|
|
2019-08-19 00:47:53 -05:00
|
|
|
Rack::MiniProfiler.config.backtrace_includes = [%r{^/?(app|config|lib|test|plugins)}]
|
|
|
|
|
2021-06-10 01:22:47 -05:00
|
|
|
Rack::MiniProfiler.config.max_traces_to_show = 100 if Rails.env.development?
|
|
|
|
|
2024-02-16 05:16:54 -06:00
|
|
|
Rack::MiniProfiler.config.content_security_policy_nonce =
|
|
|
|
Proc.new do |env, headers|
|
|
|
|
if csp = headers["Content-Security-Policy"]
|
|
|
|
csp[/script-src[^;]+'nonce-([^']+)'/, 1]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-10-30 11:11:22 -05:00
|
|
|
Rack::MiniProfiler.counter_method(Redis::Client, :call) { "redis" }
|
2013-08-30 01:44:17 -05:00
|
|
|
# Rack::MiniProfiler.counter_method(ActiveRecord::QueryMethods, 'build_arel')
|
|
|
|
# Rack::MiniProfiler.counter_method(Array, 'uniq')
|
2013-02-05 13:16:51 -06:00
|
|
|
# require "#{Rails.root}/vendor/backports/notification"
|
|
|
|
|
2013-03-24 22:09:28 -05:00
|
|
|
# inst = Class.new
|
|
|
|
# class << inst
|
|
|
|
# def start(name,id,payload)
|
|
|
|
# if Rack::MiniProfiler.current && name !~ /(process_action.action_controller)|(render_template.action_view)/
|
|
|
|
# @prf ||= {}
|
|
|
|
# @prf[id] ||= []
|
|
|
|
# @prf[id] << Rack::MiniProfiler.start_step("#{payload[:serializer] if name =~ /serialize.serializer/} #{name}")
|
|
|
|
# end
|
|
|
|
# end
|
|
|
|
|
|
|
|
# def finish(name,id,payload)
|
|
|
|
# if Rack::MiniProfiler.current && name !~ /(process_action.action_controller)|(render_template.action_view)/
|
|
|
|
# t = @prf[id].pop
|
|
|
|
# @prf.delete id unless t
|
|
|
|
# Rack::MiniProfiler.finish_step t
|
|
|
|
# end
|
|
|
|
# end
|
|
|
|
# end
|
2013-02-05 13:16:51 -06:00
|
|
|
# disabling for now cause this slows stuff down too much
|
|
|
|
# ActiveSupport::Notifications.subscribe(/.*/, inst)
|
|
|
|
|
|
|
|
# Rack::MiniProfiler.profile_method ActionView::PathResolver, 'find_templates'
|
|
|
|
end
|
2015-08-19 01:58:25 -05:00
|
|
|
|
|
|
|
if ENV["PRINT_EXCEPTIONS"]
|
|
|
|
trace =
|
|
|
|
TracePoint.new(:raise) do |tp|
|
|
|
|
puts tp.raised_exception
|
|
|
|
puts tp.raised_exception.backtrace.join("\n")
|
|
|
|
puts
|
|
|
|
end
|
|
|
|
trace.enable
|
|
|
|
end
|