Skip to content

Commit

Permalink
[feature] enable profiling in production (#1449)
Browse files Browse the repository at this point in the history
* [feature] enable profiling in production (#1448)

## Shows application profiling
When enabled, shows profiling information to admin users
<img width="1728" alt="Screen Shot 2023-03-10 at 6 27 18 PM" src="https://user-images.githubusercontent.com/35935196/224448480-6cf1f7ec-e48d-4c9b-8a88-df1de8b5571a.png">

## Permissions
Ensure user has `show profiler` permission
<img width="1728" alt="Screen Shot 2023-03-10 at 6 28 13 PM" src="https://user-images.githubusercontent.com/35935196/224448545-ea9afe62-e68d-4681-a332-7c98fc5d251c.png">

## Profiling configuration
Append `?pp=help` to access the profiling help page
<img width="1728" alt="Screen Shot 2023-03-10 at 6 27 41 PM" src="https://user-images.githubusercontent.com/35935196/224448618-c8eebc9f-b845-43c8-931f-0d9f25218a76.png">

## Improve app performance!
Use instrumentation to track down performance bottlenecks and improve app performance 
<img width="1728" alt="Screen Shot 2023-03-10 at 6 29 32 PM" src="https://user-images.githubusercontent.com/35935196/224448636-d6d0bb5e-3597-4b9e-8b6d-f7487eb88d04.png">

* add permissions (#1450)

Co-authored-by: donrestarone <[email protected]>

---------

Co-authored-by: donrestarone <[email protected]>
  • Loading branch information
donrestarone and donrestarone authored Mar 11, 2023
1 parent 96d21fc commit a4b22bc
Show file tree
Hide file tree
Showing 10 changed files with 50 additions and 13 deletions.
10 changes: 7 additions & 3 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -62,16 +62,20 @@ gem 'net-ssh', '>= 6.0.2'
gem 'ed25519', '>= 1.2', '< 2.0'
gem 'bcrypt_pbkdf', '>= 1.0', '< 2.0'

# Display performance information such as SQL time and flame graphs for each request in your browser.
# Can be configured to work on production as well see: https://github.com/MiniProfiler/rack-mini-profiler/blob/master/README.md
gem 'rack-mini-profiler', '~> 3.0', require: false
gem 'flamegraph'
gem 'stackprof'
gem 'memory_profiler'

group :development, :test do
# Call 'byebug' anywhere in the code to stop execution and get a debugger console
gem 'byebug', platforms: [:mri, :mingw, :x64_mingw]
gem 'pry'
end

group :development do
# Display performance information such as SQL time and flame graphs for each request in your browser.
# Can be configured to work on production as well see: https://github.com/MiniProfiler/rack-mini-profiler/blob/master/README.md
gem 'rack-mini-profiler', '~> 2.0'
gem 'listen', '~> 3.3'
# Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
gem 'spring'
Expand Down
10 changes: 8 additions & 2 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ GEM
faker (2.21.0)
i18n (>= 1.8.11, < 2)
ffi (1.15.5)
flamegraph (0.9.5)
font-awesome-sass (5.15.1)
sassc (>= 1.11)
friendly_id (5.4.2)
Expand Down Expand Up @@ -276,6 +277,7 @@ GEM
mailgun-ruby (1.2.4)
rest-client (>= 2.0.2)
marcel (1.0.2)
memory_profiler (1.0.1)
meta-tags (2.14.0)
actionpack (>= 3.2.0, < 6.2)
method_source (1.0.0)
Expand Down Expand Up @@ -317,7 +319,7 @@ GEM
rack (>= 0.4)
rack-cors (1.1.1)
rack (>= 2.0.0)
rack-mini-profiler (2.3.1)
rack-mini-profiler (3.0.0)
rack (>= 1.2.0)
rack-protection (2.2.0)
rack
Expand Down Expand Up @@ -439,6 +441,7 @@ GEM
sshkit (1.21.2)
net-scp (>= 1.1.2)
net-ssh (>= 2.8.0)
stackprof (0.2.23)
stripe (7.1.0)
stripe-rails (2.3.5)
rails (>= 5.1)
Expand Down Expand Up @@ -519,6 +522,7 @@ DEPENDENCIES
ember-cli-rails (= 0.10.0)
exception_notification (~> 4.5)
faker
flamegraph
friendly_id
graphiql-rails
graphql (~> 2.0)
Expand All @@ -531,14 +535,15 @@ DEPENDENCIES
listen (~> 3.3)
local_time
mailgun-ruby
memory_profiler
meta-tags
mocha
net-ssh (>= 6.0.2)
pg (~> 1.1)
pry
puma (~> 5.6)
rack-cors
rack-mini-profiler (~> 2.0)
rack-mini-profiler (~> 3.0)
rack-timeout (~> 0.6)
rails (~> 6.1.5)
rails-controller-testing
Expand All @@ -554,6 +559,7 @@ DEPENDENCIES
sinatra
sitemap_generator
spring
stackprof
stripe-rails
turbo-rails (~> 1.1)
turnout (~> 2.5)
Expand Down
9 changes: 7 additions & 2 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
class ApplicationController < ActionController::Base
include ActiveStorage::SetCurrent

before_action :prepare_exception_notifier
before_action :store_user_location!, if: :storable_location?
before_action :set_current_user
before_action :prepare_profiler,:prepare_exception_notifier , :set_current_user

def after_sign_in_path_for(resource)
if session[:user_return_to] then return session[:user_return_to] end
Expand Down Expand Up @@ -41,6 +40,12 @@ def prepare_exception_notifier
}
end

def prepare_profiler
if current_user && current_user.can_access_admin? & current_user.show_profiler?
Rack::MiniProfiler.authorize_request
end
end

# Reference: https://github.com/heartcombo/devise/wiki/How-To:-Redirect-back-to-current-page-after-sign-in,-sign-out,-sign-up,-update#storelocation-to-the-rescue
def storable_location?
request.get? && is_navigational_format? && !devise_controller? && !request.xhr?
Expand Down
1 change: 1 addition & 0 deletions app/controllers/comfy/admin/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ def update_params
:can_manage_subdomain_settings,
:can_access_admin,
:deliver_error_notifications,
:show_profiler,
category_ids: [],
api_accessibility: {}
)
Expand Down
8 changes: 6 additions & 2 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,14 @@ class User < ApplicationRecord
:invitation_limit ,
:invited_by_type,
:invited_by_id,
:invitations_count
:invitations_count,
:global_admin,
:show_profiler,
]

FULL_PERMISSIONS = {
can_access_admin: true,
can_access_forum: true,
can_manage_web: true,
can_manage_analytics: true,
can_manage_files: true,
Expand All @@ -57,7 +60,8 @@ class User < ApplicationRecord
can_manage_subdomain_settings: true,
api_accessibility: {api_namespaces: {all_namespaces: {full_access: 'true'}}, api_keys: {full_access: 'true'}},
can_view_restricted_pages: true,
moderator: true
moderator: true,
deliver_error_notifications: true,
}

SESSION_TIMEOUT = [
Expand Down
6 changes: 5 additions & 1 deletion app/views/comfy/admin/users/_form.haml
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,11 @@
.form-group
= f.check_box :deliver_error_notifications
%label
Deliver error notifications
Deliver error notifications
.form-group
= f.check_box :show_profiler
%label
Show Profiler
.form-group
= f.check_box :can_manage_users
%label
Expand Down
2 changes: 0 additions & 2 deletions config/environments/development.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,4 @@

# Uncomment if you wish to allow Action Cable access from any origin.
# config.action_cable.disable_request_forgery_protection = true

Rack::MiniProfiler.config.position = 'bottom-right'
end
9 changes: 9 additions & 0 deletions config/initializers/rack_profiler.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# frozen_string_literal: true
require "rack-mini-profiler"

# initialization is skipped so trigger it
Rack::MiniProfilerRails.initialize!(Rails.application)
Rack::MiniProfiler.config.position = 'bottom-right'
Rack::MiniProfiler.config.authorization_mode = :allow_authorized
Rack::MiniProfiler.config.enable_hotwire_turbo_drive_support = true
Rack::MiniProfiler.config.enable_advanced_debugging_tools = true
5 changes: 5 additions & 0 deletions db/migrate/20230310222034_add_show_profiler_to_users.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddShowProfilerToUsers < ActiveRecord::Migration[6.1]
def change
add_column :users, :show_profiler, :boolean, default: false
end
end
3 changes: 2 additions & 1 deletion db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit a4b22bc

Please sign in to comment.