Class: Proceso::Middleware
- Inherits:
-
Object
- Object
- Proceso::Middleware
- Defined in:
- lib/proceso/middleware.rb
Constant Summary collapse
- SUBSCRIPTION =
'proceso.usage'
Instance Attribute Summary collapse
-
#app ⇒ Object
readonly
Returns the value of attribute app.
-
#notifier ⇒ Object
readonly
Returns the value of attribute notifier.
-
#pid ⇒ Object
readonly
Returns the value of attribute pid.
Class Method Summary collapse
Instance Method Summary collapse
- #call(env) ⇒ Object
-
#initialize(app) ⇒ Middleware
constructor
A new instance of Middleware.
- #measure_process_usage(env) ⇒ Object
- #process ⇒ Object
- #process_info(env, mem_used, cpu_used, resp_time) ⇒ Object
Constructor Details
#initialize(app) ⇒ Middleware
Returns a new instance of Middleware.
29 30 31 32 33 |
# File 'lib/proceso/middleware.rb', line 29 def initialize(app) @app = app @notifier ||= ActiveSupport::Notifications if defined?(ActiveSupport::Notifications) @pid = Process.pid end |
Instance Attribute Details
#app ⇒ Object (readonly)
Returns the value of attribute app.
27 28 29 |
# File 'lib/proceso/middleware.rb', line 27 def app @app end |
#notifier ⇒ Object (readonly)
Returns the value of attribute notifier.
27 28 29 |
# File 'lib/proceso/middleware.rb', line 27 def notifier @notifier end |
#pid ⇒ Object (readonly)
Returns the value of attribute pid.
27 28 29 |
# File 'lib/proceso/middleware.rb', line 27 def pid @pid end |
Class Method Details
.report ⇒ Object
8 9 10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/proceso/middleware.rb', line 8 def self.report subscribe do |name, start, finish, id, payload| puts puts "******* PROCESO INFORMATION *******" mem_used = (payload[:mem_used].to_f / 1024.0).round(1) cpu_used = payload[:cpu_used].to_f.round(1) path = payload[:request].path_info resp_time = payload[:resp_time] puts "MEM: #{mem_used}KB CPU: #{cpu_used} RESP: #{resp_time}ms PATH: #{path}" puts "******* END OF PROCESO INFORMATION *******" puts end end |
.subscribe(&blk) ⇒ Object
22 23 24 25 |
# File 'lib/proceso/middleware.rb', line 22 def self.subscribe(&blk) return unless defined?(ActiveSupport::Notifications) ActiveSupport::Notifications.subscribe(SUBSCRIPTION, &blk) end |
Instance Method Details
#call(env) ⇒ Object
35 36 37 38 39 |
# File 'lib/proceso/middleware.rb', line 35 def call(env) measure_process_usage(env) do @app.call(env) end end |
#measure_process_usage(env) ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/proceso/middleware.rb', line 41 def measure_process_usage(env) mem_1 = process.mem_size cpu_1 = process.user_cpu_times resp_1 = Time.now.to_i response = yield mem_2 = process.mem_size cpu_2 = process.user_cpu_times resp_2 = Time.now.to_i process_usage = process_info(env, (mem_2 - mem_1), (cpu_2 - cpu_1), (resp_2 - resp_1)) notifier.instrument(SUBSCRIPTION, process_usage) response end |
#process ⇒ Object
65 66 67 |
# File 'lib/proceso/middleware.rb', line 65 def process @process ||= Proceso::PID.new(pid) end |
#process_info(env, mem_used, cpu_used, resp_time) ⇒ Object
54 55 56 57 58 59 60 61 62 63 |
# File 'lib/proceso/middleware.rb', line 54 def process_info(env, mem_used, cpu_used, resp_time) req = Rack::Request.new(env) { pid: process.pid, mem_used: mem_used, cpu_used: cpu_used, resp_time: resp_time, request: req } end |