2019-05-03 06:17:27 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
# If Mini Profiler is included via gem
|
2014-07-17 06:34:30 +08:00
|
|
|
if Rails.configuration.respond_to?(:load_mini_profiler) && Rails.configuration.load_mini_profiler
|
2013-04-11 14:24:08 +08:00
|
|
|
require 'rack-mini-profiler'
|
2020-12-29 10:54:41 +08:00
|
|
|
require 'stackprof'
|
2016-07-01 01:21:40 +08:00
|
|
|
|
|
|
|
begin
|
|
|
|
require 'memory_profiler'
|
|
|
|
rescue => e
|
|
|
|
STDERR.put "#{e} failed to require mini profiler"
|
|
|
|
end
|
2014-12-29 10:30:54 +08:00
|
|
|
|
2013-04-11 14:24:08 +08:00
|
|
|
# initialization is skipped so trigger it
|
|
|
|
Rack::MiniProfilerRails.initialize!(Rails.application)
|
|
|
|
end
|
|
|
|
|
2018-12-10 11:29:20 +08:00
|
|
|
if defined?(Rack::MiniProfiler) && defined?(Rack::MiniProfiler::Config)
|
2013-03-25 14:19:59 +08: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 13:32:01 +08:00
|
|
|
Rack::MiniProfiler.config.storage_instance = Rack::MiniProfiler::RedisStore.new(
|
|
|
|
connection: DiscourseRedis.new(nil, namespace: false)
|
|
|
|
)
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2020-09-17 23:18:35 +08:00
|
|
|
Rack::MiniProfiler.config.snapshot_every_n_requests = GlobalSetting.mini_profiler_snapshots_period
|
2020-10-22 00:37:28 +08: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
|
2020-09-17 23:18:35 +08:00
|
|
|
Rack::MiniProfiler.config.skip_paths = [
|
2015-04-17 10:16:37 +08:00
|
|
|
/^\/message-bus/,
|
2018-01-09 08:30:59 +08:00
|
|
|
/^\/extra-locales/,
|
2015-04-17 10:16:37 +08:00
|
|
|
/topics\/timings/,
|
|
|
|
/assets/,
|
|
|
|
/\/user_avatar\//,
|
|
|
|
/\/letter_avatar\//,
|
2015-12-09 13:40:49 +08:00
|
|
|
/\/letter_avatar_proxy\//,
|
2015-04-17 10:16:37 +08:00
|
|
|
/\/highlight-js\//,
|
2018-11-27 05:49:57 +08:00
|
|
|
/\/svg-sprite\//,
|
2015-04-17 10:16:37 +08:00
|
|
|
/qunit/,
|
|
|
|
/srv\/status/,
|
|
|
|
/commits-widget/,
|
|
|
|
/^\/cdn_asset/,
|
|
|
|
/^\/logs/,
|
|
|
|
/^\/site_customizations/,
|
2015-05-04 14:11:27 +08:00
|
|
|
/^\/uploads/,
|
2020-02-28 10:11:30 +08:00
|
|
|
/^\/secure-media-uploads/,
|
2015-05-04 14:11:27 +08:00
|
|
|
/^\/javascripts\//,
|
2015-05-05 13:50:13 +08:00
|
|
|
/^\/images\//,
|
2015-08-25 09:54:23 +08:00
|
|
|
/^\/stylesheets\//,
|
|
|
|
/^\/favicon\/proxied/
|
2015-04-17 10:16:37 +08:00
|
|
|
]
|
|
|
|
|
2016-03-16 07:54:40 +08: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-26 00:42:20 +08:00
|
|
|
Rack::MiniProfiler.config.pre_authorize_cb = lambda do |env|
|
2020-09-17 23:18:35 +08:00
|
|
|
env['HTTP_USER_AGENT'] !~ /iPad|iPhone|Android/
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
2013-03-25 11:09:28 +08: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-20 03:06:23 +08:00
|
|
|
# be stored associated with ip1 as the user and retrieved using ip2 causing 404s
|
2013-03-25 10:52:03 +08:00
|
|
|
Rack::MiniProfiler.config.user_provider = lambda do |env|
|
2013-03-25 11:36:55 +08: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-25 10:52:03 +08:00
|
|
|
end
|
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
Rack::MiniProfiler.config.position = 'left'
|
|
|
|
Rack::MiniProfiler.config.backtrace_ignores ||= []
|
|
|
|
Rack::MiniProfiler.config.backtrace_ignores << /lib\/rack\/message_bus.rb/
|
|
|
|
Rack::MiniProfiler.config.backtrace_ignores << /config\/initializers\/silence_logger/
|
|
|
|
Rack::MiniProfiler.config.backtrace_ignores << /config\/initializers\/quiet_logger/
|
|
|
|
|
2019-08-19 13:47:53 +08:00
|
|
|
Rack::MiniProfiler.config.backtrace_includes = [/^\/?(app|config|lib|test|plugins)/]
|
|
|
|
|
2020-10-31 00:11:22 +08:00
|
|
|
Rack::MiniProfiler.counter_method(Redis::Client, :call) { 'redis' }
|
2013-08-30 14:44:17 +08:00
|
|
|
# Rack::MiniProfiler.counter_method(ActiveRecord::QueryMethods, 'build_arel')
|
|
|
|
# Rack::MiniProfiler.counter_method(Array, 'uniq')
|
2013-02-06 03:16:51 +08:00
|
|
|
# require "#{Rails.root}/vendor/backports/notification"
|
|
|
|
|
2013-03-25 11:09:28 +08: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-06 03:16:51 +08: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 14:58:25 +08: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
|