discourse/config/initializers/006-mini_profiler.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

136 lines
4.8 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
2013-02-05 14:16:51 -05:00
# If Mini Profiler is included via gem
if Rails.configuration.respond_to?(:load_mini_profiler) && Rails.configuration.load_mini_profiler &&
RUBY_ENGINE == "ruby"
require "rack-mini-profiler"
require "stackprof"
begin
require "memory_profiler"
rescue => e
STDERR.put "#{e} failed to require mini profiler"
end
2014-12-28 21:30:54 -05:00
# initialization is skipped so trigger it
Rack::MiniProfilerRails.initialize!(Rails.application)
end
if defined?(Rack::MiniProfiler) && defined?(Rack::MiniProfiler::Config)
2013-03-25 02:19:59 -04: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
Rack::MiniProfiler.config.storage_instance =
Rack::MiniProfiler::RedisStore.new(connection: DiscourseRedis.new(nil, namespace: false))
2013-02-05 14:16:51 -05:00
Rack::MiniProfiler.config.snapshot_every_n_requests = GlobalSetting.mini_profiler_snapshots_period
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
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/
DEV: Run QUnit tests for official Discourse themes (#24405) Why this change? As the number of themes which the Discourse team supports officially grows, we want to ensure that changes made to Discourse core do not break the plugins. As such, we are adding a step to our Github actions test job to run the QUnit tests for all official themes. What does this change do? This change adds a new job to our tests Github actions workflow to run the QUnit tests for all official plugins. This is achieved with the following changes: 1. Update `testem.js` to rely on the `THEME_TEST_PAGES` env variable to set the `test_page` option when running theme QUnit tests with testem. The `test_page` option [allows an array to be specified](https://github.com/testem/testem#multiple-test-pages) such that tests for multiple pages can be run at the same time. We are relying on a ENV variable because the `testem` CLI does not support passing a list of pages to the `--test_page` option. 2. Support a `/testem-theme-qunit/:testem_id/theme-qunit` Rails route in the development environment. This is done because testem prefixes the path with a unique ID to the configured `test_page` URL. This is problematic for us because we proxy all testem requests to the Rails server and testem's proxy configuration option does not allow us to easily rewrite the URL to remove the prefix. Therefore, we configure a proxy in testem to prefix `theme-qunit` requests with `/testem-theme-qunit` which can then be easily identified by the Rails server and routed accordingly. 3. Update `qunit:test` to support a `THEME_IDS` environment variable which will allow it to run QUnit tests for multiple themes at the same time. 4. Support `bin/rake themes:qunit[ids,"<theme_id>|<theme_id>"]` to run the QUnit tests for multiple themes at the same time. 5. Adds a `themes:qunit_all_official` Rake task which runs the QUnit tests for all the official themes.
2023-11-16 18:17:32 -05:00
].map { |path| "#{Discourse.base_path}#{path}" }.concat([/.*theme-qunit/])
2015-04-16 22:16:37 -04: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 11:42:20 -05:00
Rack::MiniProfiler.config.pre_authorize_cb =
lambda { |env| env["HTTP_USER_AGENT"] !~ /iPad|iPhone|Android/ }
2013-02-05 14:16:51 -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
# be stored associated with ip1 as the user and retrieved using ip2 causing 404s
Rack::MiniProfiler.config.user_provider =
lambda do |env|
2013-03-24 23:36:55 -04: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)
end
# Cookie path should be set to the base path so Discourse's session cookie path
# does not get clobbered.
Rack::MiniProfiler.config.cookie_path = Discourse.base_path.presence || "/"
Rack::MiniProfiler.config.position = "right"
2013-02-05 14:16:51 -05: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}
Rack::MiniProfiler.config.backtrace_includes = [%r{^/?(app|config|lib|test|plugins)}]
Rack::MiniProfiler.config.max_traces_to_show = 100 if Rails.env.development?
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
Rack::MiniProfiler.counter_method(Redis::Client, :call) { "redis" }
2013-08-30 02:44:17 -04:00
# Rack::MiniProfiler.counter_method(ActiveRecord::QueryMethods, 'build_arel')
# Rack::MiniProfiler.counter_method(Array, 'uniq')
2013-02-05 14:16:51 -05:00
# require "#{Rails.root}/vendor/backports/notification"
# 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 14:16:51 -05: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 02:58:25 -04: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