Skip to content

Commit a4b22bc

Browse files
donrestaronedonrestarone
andauthored
[feature] enable profiling in production (#1449)
* [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 <shashikejayatunge@gmail.co> --------- Co-authored-by: donrestarone <shashikejayatunge@gmail.co>
1 parent 96d21fc commit a4b22bc

File tree

10 files changed

+50
-13
lines changed

10 files changed

+50
-13
lines changed

Gemfile

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,16 +62,20 @@ gem 'net-ssh', '>= 6.0.2'
6262
gem 'ed25519', '>= 1.2', '< 2.0'
6363
gem 'bcrypt_pbkdf', '>= 1.0', '< 2.0'
6464

65+
# Display performance information such as SQL time and flame graphs for each request in your browser.
66+
# Can be configured to work on production as well see: https://github.com/MiniProfiler/rack-mini-profiler/blob/master/README.md
67+
gem 'rack-mini-profiler', '~> 3.0', require: false
68+
gem 'flamegraph'
69+
gem 'stackprof'
70+
gem 'memory_profiler'
71+
6572
group :development, :test do
6673
# Call 'byebug' anywhere in the code to stop execution and get a debugger console
6774
gem 'byebug', platforms: [:mri, :mingw, :x64_mingw]
6875
gem 'pry'
6976
end
7077

7178
group :development do
72-
# Display performance information such as SQL time and flame graphs for each request in your browser.
73-
# Can be configured to work on production as well see: https://github.com/MiniProfiler/rack-mini-profiler/blob/master/README.md
74-
gem 'rack-mini-profiler', '~> 2.0'
7579
gem 'listen', '~> 3.3'
7680
# Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
7781
gem 'spring'

Gemfile.lock

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,7 @@ GEM
216216
faker (2.21.0)
217217
i18n (>= 1.8.11, < 2)
218218
ffi (1.15.5)
219+
flamegraph (0.9.5)
219220
font-awesome-sass (5.15.1)
220221
sassc (>= 1.11)
221222
friendly_id (5.4.2)
@@ -276,6 +277,7 @@ GEM
276277
mailgun-ruby (1.2.4)
277278
rest-client (>= 2.0.2)
278279
marcel (1.0.2)
280+
memory_profiler (1.0.1)
279281
meta-tags (2.14.0)
280282
actionpack (>= 3.2.0, < 6.2)
281283
method_source (1.0.0)
@@ -317,7 +319,7 @@ GEM
317319
rack (>= 0.4)
318320
rack-cors (1.1.1)
319321
rack (>= 2.0.0)
320-
rack-mini-profiler (2.3.1)
322+
rack-mini-profiler (3.0.0)
321323
rack (>= 1.2.0)
322324
rack-protection (2.2.0)
323325
rack
@@ -439,6 +441,7 @@ GEM
439441
sshkit (1.21.2)
440442
net-scp (>= 1.1.2)
441443
net-ssh (>= 2.8.0)
444+
stackprof (0.2.23)
442445
stripe (7.1.0)
443446
stripe-rails (2.3.5)
444447
rails (>= 5.1)
@@ -519,6 +522,7 @@ DEPENDENCIES
519522
ember-cli-rails (= 0.10.0)
520523
exception_notification (~> 4.5)
521524
faker
525+
flamegraph
522526
friendly_id
523527
graphiql-rails
524528
graphql (~> 2.0)
@@ -531,14 +535,15 @@ DEPENDENCIES
531535
listen (~> 3.3)
532536
local_time
533537
mailgun-ruby
538+
memory_profiler
534539
meta-tags
535540
mocha
536541
net-ssh (>= 6.0.2)
537542
pg (~> 1.1)
538543
pry
539544
puma (~> 5.6)
540545
rack-cors
541-
rack-mini-profiler (~> 2.0)
546+
rack-mini-profiler (~> 3.0)
542547
rack-timeout (~> 0.6)
543548
rails (~> 6.1.5)
544549
rails-controller-testing
@@ -554,6 +559,7 @@ DEPENDENCIES
554559
sinatra
555560
sitemap_generator
556561
spring
562+
stackprof
557563
stripe-rails
558564
turbo-rails (~> 1.1)
559565
turnout (~> 2.5)

app/controllers/application_controller.rb

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
class ApplicationController < ActionController::Base
22
include ActiveStorage::SetCurrent
33

4-
before_action :prepare_exception_notifier
54
before_action :store_user_location!, if: :storable_location?
6-
before_action :set_current_user
5+
before_action :prepare_profiler,:prepare_exception_notifier , :set_current_user
76

87
def after_sign_in_path_for(resource)
98
if session[:user_return_to] then return session[:user_return_to] end
@@ -41,6 +40,12 @@ def prepare_exception_notifier
4140
}
4241
end
4342

43+
def prepare_profiler
44+
if current_user && current_user.can_access_admin? & current_user.show_profiler?
45+
Rack::MiniProfiler.authorize_request
46+
end
47+
end
48+
4449
# 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
4550
def storable_location?
4651
request.get? && is_navigational_format? && !devise_controller? && !request.xhr?

app/controllers/comfy/admin/users_controller.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ def update_params
8888
:can_manage_subdomain_settings,
8989
:can_access_admin,
9090
:deliver_error_notifications,
91+
:show_profiler,
9192
category_ids: [],
9293
api_accessibility: {}
9394
)

app/models/user.rb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,14 @@ class User < ApplicationRecord
4343
:invitation_limit ,
4444
:invited_by_type,
4545
:invited_by_id,
46-
:invitations_count
46+
:invitations_count,
47+
:global_admin,
48+
:show_profiler,
4749
]
4850

4951
FULL_PERMISSIONS = {
5052
can_access_admin: true,
53+
can_access_forum: true,
5154
can_manage_web: true,
5255
can_manage_analytics: true,
5356
can_manage_files: true,
@@ -57,7 +60,8 @@ class User < ApplicationRecord
5760
can_manage_subdomain_settings: true,
5861
api_accessibility: {api_namespaces: {all_namespaces: {full_access: 'true'}}, api_keys: {full_access: 'true'}},
5962
can_view_restricted_pages: true,
60-
moderator: true
63+
moderator: true,
64+
deliver_error_notifications: true,
6165
}
6266

6367
SESSION_TIMEOUT = [

app/views/comfy/admin/users/_form.haml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,11 @@
5656
.form-group
5757
= f.check_box :deliver_error_notifications
5858
%label
59-
Deliver error notifications
59+
Deliver error notifications
60+
.form-group
61+
= f.check_box :show_profiler
62+
%label
63+
Show Profiler
6064
.form-group
6165
= f.check_box :can_manage_users
6266
%label

config/environments/development.rb

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,4 @@
8484

8585
# Uncomment if you wish to allow Action Cable access from any origin.
8686
# config.action_cable.disable_request_forgery_protection = true
87-
88-
Rack::MiniProfiler.config.position = 'bottom-right'
8987
end
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# frozen_string_literal: true
2+
require "rack-mini-profiler"
3+
4+
# initialization is skipped so trigger it
5+
Rack::MiniProfilerRails.initialize!(Rails.application)
6+
Rack::MiniProfiler.config.position = 'bottom-right'
7+
Rack::MiniProfiler.config.authorization_mode = :allow_authorized
8+
Rack::MiniProfiler.config.enable_hotwire_turbo_drive_support = true
9+
Rack::MiniProfiler.config.enable_advanced_debugging_tools = true
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class AddShowProfilerToUsers < ActiveRecord::Migration[6.1]
2+
def change
3+
add_column :users, :show_profiler, :boolean, default: false
4+
end
5+
end

db/schema.rb

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)