rails_performance 0.9.9 → 1.0.0.beta1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (47) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +32 -3
  3. data/app/controllers/rails_performance/rails_performance_controller.rb +47 -35
  4. data/app/helpers/rails_performance/application_helper.rb +25 -7
  5. data/app/views/rails_performance/javascripts/app.js +2 -2
  6. data/app/views/rails_performance/rails_performance/_summary.html.erb +1 -1
  7. data/app/views/rails_performance/rails_performance/custom.html.erb +83 -0
  8. data/app/views/rails_performance/rails_performance/delayed_job.html.erb +74 -0
  9. data/app/views/rails_performance/rails_performance/grape.html.erb +64 -0
  10. data/app/views/rails_performance/rails_performance/rake.html.erb +55 -0
  11. data/app/views/rails_performance/rails_performance/recent.html.erb +3 -1
  12. data/app/views/rails_performance/rails_performance/{jobs.html.erb → sidekiq.html.erb} +5 -4
  13. data/app/views/rails_performance/rails_performance/summary.js.erb +1 -1
  14. data/app/views/rails_performance/rails_performance/trace.js.erb +1 -1
  15. data/app/views/rails_performance/shared/_header.html.erb +9 -1
  16. data/app/views/rails_performance/stylesheets/style.css +5 -0
  17. data/config/routes.rb +5 -1
  18. data/lib/rails_performance.rb +29 -6
  19. data/lib/rails_performance/data_source.rb +52 -13
  20. data/lib/rails_performance/engine.rb +20 -3
  21. data/lib/rails_performance/extensions/{capture_everything.rb → trace.rb} +2 -2
  22. data/lib/rails_performance/gems/custom_ext.rb +33 -0
  23. data/lib/rails_performance/gems/delayed_job_ext.rb +54 -0
  24. data/lib/rails_performance/gems/grape_ext.rb +35 -0
  25. data/lib/rails_performance/gems/rake_ext.rb +40 -0
  26. data/lib/rails_performance/gems/{sidekiq.rb → sidekiq_ext.rb} +13 -12
  27. data/lib/rails_performance/instrument/metrics_collector.rb +3 -2
  28. data/lib/rails_performance/models/base_record.rb +12 -0
  29. data/lib/rails_performance/models/custom_record.rb +48 -0
  30. data/lib/rails_performance/models/delayed_job_record.rb +62 -0
  31. data/lib/rails_performance/models/grape_record.rb +61 -0
  32. data/lib/rails_performance/models/rake_record.rb +49 -0
  33. data/lib/rails_performance/models/request_record.rb +98 -0
  34. data/lib/rails_performance/models/sidekiq_record.rb +66 -0
  35. data/lib/rails_performance/models/trace_record.rb +19 -0
  36. data/lib/rails_performance/rails/middleware.rb +42 -16
  37. data/lib/rails_performance/rails/query_builder.rb +1 -1
  38. data/lib/rails_performance/reports/breakdown_report.rb +4 -16
  39. data/lib/rails_performance/reports/crash_report.rb +4 -15
  40. data/lib/rails_performance/reports/recent_requests_report.rb +7 -44
  41. data/lib/rails_performance/reports/trace_report.rb +1 -1
  42. data/lib/rails_performance/{models → thread}/current_request.rb +9 -4
  43. data/lib/rails_performance/utils.rb +15 -28
  44. data/lib/rails_performance/version.rb +1 -1
  45. metadata +79 -10
  46. data/lib/rails_performance/models/job_record.rb +0 -48
  47. data/lib/rails_performance/models/record.rb +0 -68
@@ -0,0 +1,49 @@
1
+ module RailsPerformance
2
+ module Models
3
+ class RakeRecord < BaseRecord
4
+ attr_accessor :task, :duration, :datetime, :datetimei, :status
5
+
6
+ # rake|task|["task3"]|datetime|20210416T1254|datetimei|1618602843|status|error|END|1.0.0
7
+ # {"duration":0.00012442}
8
+ def RakeRecord.from_db(key, value)
9
+ items = key.split("|")
10
+
11
+ RakeRecord.new(
12
+ task: JSON.parse(items[2]),
13
+ datetime: items[4],
14
+ datetimei: items[6],
15
+ status: items[8],
16
+ json: value
17
+ )
18
+ end
19
+
20
+ def initialize(task:, duration: nil, datetime:, datetimei:, status:, json: '{}')
21
+ @task = Array.wrap(task)
22
+ @datetime = datetime
23
+ @datetimei = datetimei.to_i
24
+ @status = status
25
+ @duration = duration
26
+ @json = json
27
+
28
+ @duration ||= value['duration']
29
+ end
30
+
31
+ def record_hash
32
+ {
33
+ task: task,
34
+ datetime: Time.at(datetimei),
35
+ datetimei: datetimei,
36
+ duration: duration,
37
+ status: status,
38
+ }
39
+ end
40
+
41
+ def save
42
+ key = "rake|task|#{task.to_json}|datetime|#{datetime}|datetimei|#{datetimei}|status|#{status}|END|#{RailsPerformance::VERSION}"
43
+ value = { duration: duration }
44
+ Utils.save_to_redis(key, value)
45
+ end
46
+
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,98 @@
1
+ module RailsPerformance
2
+ module Models
3
+ class RequestRecord < BaseRecord
4
+ attr_accessor :controller, :action, :format, :status, :datetime, :datetimei, :method, :path, :request_id, :json
5
+ attr_accessor :view_runtime, :db_runtime, :duration, :http_referer
6
+
7
+ def RequestRecord.find_by(request_id:)
8
+ keys, values = RP::Utils.fetch_from_redis("performance|*|request_id|#{request_id}|*")
9
+
10
+ return nil if keys.blank?
11
+ return nil if values.blank?
12
+
13
+ RP::Models::RequestRecord.from_db(keys[0], values[0])
14
+ end
15
+
16
+ # key = performance|
17
+ # controller|HomeController|
18
+ # action|index|
19
+ # format|html|
20
+ # status|200|
21
+ # datetime|20200124T0523|
22
+ # datetimei|1579861423|
23
+ # method|GET|
24
+ # path|/|
25
+ # request_id|454545454545454545|
26
+ # END|1.0.0
27
+ # = {"view_runtime":8.444603008683771,"db_runtime":0,"duration":9.216095000000001}
28
+ # value = JSON
29
+ def RequestRecord.from_db(key, value)
30
+ items = key.split("|")
31
+
32
+ RequestRecord.new(
33
+ controller: items[2],
34
+ action: items[4],
35
+ format: items[6],
36
+ status: items[8],
37
+ datetime: items[10],
38
+ datetimei: items[12],
39
+ method: items[14],
40
+ path: items[16],
41
+ request_id: items[18],
42
+ json: value
43
+ )
44
+ end
45
+
46
+ def initialize(controller:, action:, format:, status:, datetime:, datetimei:, method:, path:, request_id:, view_runtime: nil, db_runtime: nil, duration: nil, http_referer: nil, json: '{}')
47
+ @controller = controller
48
+ @action = action
49
+ @format = format
50
+ @status = status
51
+ @datetime = datetime
52
+ @datetimei = datetimei.to_i
53
+ @method = method
54
+ @path = path
55
+ @request_id = request_id
56
+
57
+ @view_runtime = view_runtime
58
+ @db_runtime = db_runtime
59
+ @duration = duration
60
+ @http_referer = http_referer
61
+
62
+ @json = json
63
+ end
64
+
65
+ def controller_action
66
+ "#{controller}##{action}"
67
+ end
68
+
69
+ def controller_action_format
70
+ "#{controller}##{action}|#{format}"
71
+ end
72
+
73
+ def record_hash
74
+ {
75
+ controller: self.controller,
76
+ action: self.action,
77
+ format: self.format,
78
+ status: self.status,
79
+ method: self.method,
80
+ path: self.path,
81
+ request_id: self.request_id,
82
+ datetime: Time.at(self.datetimei.to_i),
83
+ datetimei: datetimei,
84
+ duration: self.value['duration'],
85
+ db_runtime: self.value['db_runtime'],
86
+ view_runtime: self.value['view_runtime'],
87
+ }
88
+ end
89
+
90
+ def save
91
+ value = { view_runtime: view_runtime, db_runtime: db_runtime, duration: duration, http_referer: http_referer }
92
+ key = "performance|controller|#{controller}|action|#{action}|format|#{format}|status|#{status}|datetime|#{datetime}|datetimei|#{datetimei}|method|#{method}|path|#{path}|request_id|#{request_id}|END|#{RailsPerformance::VERSION}"
93
+ Utils.save_to_redis(key, value)
94
+ end
95
+
96
+ end
97
+ end
98
+ end
@@ -0,0 +1,66 @@
1
+ module RailsPerformance
2
+ module Models
3
+ class SidekiqRecord < BaseRecord
4
+ attr_accessor :queue, :worker, :jid, :datetimei, :enqueued_ati, :datetime, :start_timei, :status, :duration, :message
5
+
6
+ # key = job-performance
7
+ # |queue|default
8
+ # |worker|SimpleWorker
9
+ # |jid|7d48fbf20976c224510dbc60
10
+ # |datetime|20200124T0523
11
+ # |datetimei|1583146613
12
+ # |enqueued_ati|1583146613
13
+ # |start_timei|1583146614
14
+ # |status|success|END|1.0.0
15
+ # value = JSON
16
+ def SidekiqRecord.from_db(key, value)
17
+ items = key.split("|")
18
+
19
+ SidekiqRecord.new(
20
+ queue: items[2],
21
+ worker: items[4],
22
+ jid: items[6],
23
+ datetime: items[8],
24
+ datetimei: items[10],
25
+ enqueued_ati: items[12],
26
+ start_timei: items[14],
27
+ status: items[16],
28
+ json: value
29
+ )
30
+ end
31
+
32
+ def initialize(queue:, worker:, jid:, datetime:, datetimei:, enqueued_ati:, start_timei:, status: nil, duration: nil, json: "{}")
33
+ @queue = queue
34
+ @worker = worker
35
+ @jid = jid
36
+ @datetime = datetime
37
+ @datetimei = datetimei.to_i
38
+ @enqueued_ati = enqueued_ati
39
+ @start_timei = start_timei
40
+ @status = status
41
+ @duration = duration
42
+ @json = json
43
+ end
44
+
45
+ def record_hash
46
+ {
47
+ worker: self.worker,
48
+ queue: self.queue,
49
+ jid: self.jid,
50
+ status: self.status,
51
+ datetimei: datetimei,
52
+ datetime: Time.at(self.start_timei.to_i),
53
+ duration: self.value['duration'],
54
+ message: value['message']
55
+ }
56
+ end
57
+
58
+ def save
59
+ key = "sidekiq|queue|#{queue}|worker|#{worker}|jid|#{jid}|datetime|#{datetime}|datetimei|#{datetimei}|enqueued_ati|#{enqueued_ati}|start_timei|#{start_timei}|status|#{status}|END|#{RailsPerformance::VERSION}"
60
+ value = { message: message, duration: duration }
61
+ Utils.save_to_redis(key, value)
62
+ end
63
+
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,19 @@
1
+ module RailsPerformance
2
+ module Models
3
+ class TraceRecord < BaseRecord
4
+ attr_accessor :request_id, :value
5
+
6
+ def initialize(request_id:, value:)
7
+ @request_id = request_id
8
+ @value = value
9
+ end
10
+
11
+ def save
12
+ return if value.empty?
13
+
14
+ Utils.save_to_redis("trace|#{request_id}|END|#{RailsPerformance::VERSION}", value, RailsPerformance::Reports::RecentRequestsReport::TIME_WINDOW.to_i)
15
+ end
16
+
17
+ end
18
+ end
19
+ end
@@ -1,5 +1,34 @@
1
1
  module RailsPerformance
2
2
  module Rails
3
+ class MiddlewareTraceStorerAndCleanup
4
+ def initialize(app)
5
+ @app = app
6
+ end
7
+
8
+ def call(env)
9
+ dup.call!(env)
10
+ end
11
+
12
+ def call!(env)
13
+ if %r{#{RailsPerformance.mount_at}}.match?(env["PATH_INFO"])
14
+ RailsPerformance.skip = true
15
+ end
16
+
17
+ @status, @headers, @response = @app.call(env)
18
+
19
+ if !RailsPerformance.skip
20
+ RailsPerformance::Models::TraceRecord.new(
21
+ request_id: CurrentRequest.current.request_id,
22
+ value: CurrentRequest.current.tracings
23
+ ).save
24
+ end
25
+
26
+ CurrentRequest.cleanup
27
+
28
+ [@status, @headers, @response]
29
+ end
30
+ end
31
+
3
32
  class Middleware
4
33
  def initialize(app)
5
34
  @app = app
@@ -13,24 +42,21 @@ module RailsPerformance
13
42
  @status, @headers, @response = @app.call(env)
14
43
 
15
44
  #t = Time.now
16
- if record = CurrentRequest.current.record
17
- begin
18
- record[:status] ||= @status # for 500 errors
19
- record[:request_id] = CurrentRequest.current.request_id
20
-
21
- # capture referer from where this page was opened
22
- if record[:status] == 404
23
- record[:HTTP_REFERER] = env["HTTP_REFERER"]
24
- end
45
+ if !RailsPerformance.skip
46
+ if !CurrentRequest.current.ignore.include?(:performance) # grape is executed first, and than ignore regular future storage of "controller"-like request
47
+ if data = CurrentRequest.current.data
48
+ record = RailsPerformance::Models::RequestRecord.new(**data.merge({request_id: CurrentRequest.current.request_id}))
25
49
 
26
- # store for section "recent requests"
27
- RP::Utils.log_trace_in_redis(CurrentRequest.current.request_id, CurrentRequest.current.storage)
50
+ # for 500 errors
51
+ record.status ||= @status
28
52
 
29
- # store request information
30
- RP::Utils.log_request_in_redis(record)
31
- ensure
32
- # we don't want to have a memory leak
33
- CurrentRequest.cleanup
53
+ # capture referer from where this page was opened
54
+ record.http_referer = env["HTTP_REFERER"] if record.status == 404
55
+
56
+ # store for section "recent requests"
57
+ # store request information (regular rails request)
58
+ record.save
59
+ end
34
60
  end
35
61
  end
36
62
  #puts "==> store performance data: #{(Time.now - t).round(3)}ms"
@@ -1,5 +1,5 @@
1
1
  module RailsPerformance
2
- module Rails
2
+ module Rails
3
3
  class QueryBuilder
4
4
 
5
5
  def QueryBuilder.compose_from(params)
@@ -3,25 +3,13 @@ module RailsPerformance
3
3
  class BreakdownReport < BaseReport
4
4
 
5
5
  def set_defaults
6
- @sort ||= :datetime
6
+ @sort ||= :datetimei
7
7
  end
8
8
 
9
9
  def data
10
- db.data.collect do |record|
11
- {
12
- controller: record.controller,
13
- action: record.action,
14
- format: record.format,
15
- status: record.status,
16
- method: record.method,
17
- path: record.path,
18
- datetime: Time.at(record.datetimei.to_i),
19
- duration: record.value['duration'],
20
- db_runtime: record.value['db_runtime'],
21
- view_runtime: record.value['view_runtime'],
22
- }
23
- end
24
- .sort{|a, b| b[sort] <=> a[sort]}
10
+ db.data
11
+ .collect{|e| e.record_hash}
12
+ .sort{|a, b| b[sort] <=> a[sort]}
25
13
  end
26
14
  end
27
15
 
@@ -2,24 +2,13 @@ module RailsPerformance
2
2
  module Reports
3
3
  class CrashReport < BaseReport
4
4
  def set_defaults
5
- @sort ||= :datetime
5
+ @sort ||= :datetimei
6
6
  end
7
7
 
8
8
  def data
9
- db.data.collect do |record|
10
- {
11
- controller: record.controller,
12
- action: record.action,
13
- format: record.format,
14
- status: record.status,
15
- method: record.method,
16
- path: record.path,
17
- datetime: Time.parse(record.datetime),
18
- duration: record.value['duration'],
19
- db_runtime: record.value['db_runtime'],
20
- view_runtime: record.value['view_runtime'],
21
- }
22
- end.sort{|a, b| b[sort] <=> a[sort]}
9
+ db.data
10
+ .collect{|e| e.record_hash}
11
+ .sort{|a, b| b[sort] <=> a[sort]}
23
12
  end
24
13
  end
25
14
 
@@ -4,53 +4,16 @@ module RailsPerformance
4
4
  TIME_WINDOW = 60.minutes
5
5
 
6
6
  def set_defaults
7
- @sort ||= :datetime
7
+ @sort ||= :datetimei
8
8
  end
9
9
 
10
- def data(type = :requests) # most popular type
11
- db.data.collect do |record|
12
- case type
13
- when :requests
14
- record_hash(record)
15
- when :jobs
16
- job_hash(record)
17
- end
18
- end
19
- .select{|e| e if e[:datetime] >= TIME_WINDOW.ago}
20
- .sort{|a, b| b[sort] <=> a[sort]}
21
- end
22
-
23
- private
24
-
25
- def record_hash(record)
26
- {
27
- controller: record.controller,
28
- action: record.action,
29
- format: record.format,
30
- status: record.status,
31
- method: record.method,
32
- path: record.path,
33
- request_id: record.request_id,
34
- datetime: Time.at(record.datetimei.to_i),
35
- duration: record.value['duration'],
36
- db_runtime: record.value['db_runtime'],
37
- view_runtime: record.value['view_runtime'],
38
- }
39
- end
40
-
41
- def job_hash(record)
42
- {
43
- worker: record.worker,
44
- queue: record.queue,
45
-
46
- jid: record.jid,
47
- status: record.status,
48
- datetime: Time.at(record.start_timei.to_i),
49
- duration: record.value['duration'],
50
- }
10
+ def data
11
+ time_agoi = TIME_WINDOW.ago.to_i
12
+ db.data
13
+ .collect{|e| e.record_hash}
14
+ .select{|e| e if e[sort] >= time_agoi}
15
+ .sort{|a, b| b[sort] <=> a[sort]}
51
16
  end
52
17
  end
53
-
54
-
55
18
  end
56
19
  end
@@ -8,7 +8,7 @@ module RailsPerformance
8
8
  end
9
9
 
10
10
  def data
11
- key = "trace|#{request_id}"
11
+ key = "trace|#{request_id}|END|#{RailsPerformance::VERSION}"
12
12
  JSON.parse(RP.redis.get(key).presence || '[]')
13
13
  end
14
14
  end