proceso-rails 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: d0932393e7e50f1cf3c47d1b22e1b899f69379c8
4
+ data.tar.gz: 2a5b660eeb65c1b4de77a9837080ea0433acbc6c
5
+ SHA512:
6
+ metadata.gz: df5f58f5981ae148771f7761a250a28edb0d1b33c3afb24a0b5ebe2c953dce1b216192de858a2e83425fb89183b5cbfdb4e052875205e31009565a8da6475ee2
7
+ data.tar.gz: c24c0ff17d03f11e7585fbfc4ad0b14e4108fdb50cf470dbcdf78bff20a6add88462a50eb22f44f002cb5e3c64d5677bd952078bab54339ae717fe8b1c7ecb1a
@@ -0,0 +1,92 @@
1
+ require 'rack'
2
+
3
+ module Proceso
4
+ class Middleware
5
+
6
+ SUBSCRIPTION = 'proceso.usage'
7
+
8
+ class << self
9
+
10
+ def start_instrument!
11
+ logger.info "[Proceso #{Proceso::VERSION}] Proceso Middleware is activated. (#{Rails.env.to_s} mode)"
12
+ subscribe do |name, start, finish, id, payload|
13
+ mem_used = (payload[:mem_used].to_f / 1024.0).round(1)
14
+ cpu_used = payload[:cpu_used].to_f.round(1)
15
+ path = payload[:request].path_info
16
+ resp_time = payload[:resp_time]
17
+ logger.debug "[PROCESO] MEM: #{mem_used}KB\tCPU: #{cpu_used}\tRESP: #{resp_time}ms\tPATH: #{path}"
18
+ end
19
+ end
20
+
21
+ def subscribe(&blk)
22
+ ActiveSupport::Notifications.subscribe(SUBSCRIPTION, &blk)
23
+ end
24
+
25
+ def logger
26
+ @@logger ||= begin
27
+ Rails.configuration.logger || ActiveSupport::Logger.new(STDOUT)
28
+ end
29
+ end
30
+
31
+ end
32
+
33
+ attr_reader :app, :pid, :notifier, :config
34
+
35
+ def initialize(app, options = {})
36
+ @app = app
37
+ @notifier = ActiveSupport::Notifications
38
+ @pid = Process.pid
39
+ @config = ActiveSupport::InheritableOptions.new(options[:config])
40
+ end
41
+
42
+ def call(env)
43
+ return @app.call(env) if path_excluded?(env["PATH_INFO"])
44
+ capture_process_usage(env) do
45
+ @app.call(env)
46
+ end
47
+ end
48
+
49
+ def capture_process_usage(env)
50
+ mem_1, cpu_1, resp_1 = build_process_payload
51
+ response = yield
52
+ mem_2, cpu_2, resp_2 = build_process_payload
53
+ process_payload = calculate_process_times(env, mem_1, mem_2, cpu_1, cpu_2, resp_1, resp_2)
54
+ notifier.instrument(SUBSCRIPTION, process_payload)
55
+ response
56
+ end
57
+
58
+ def build_process_payload
59
+ mem = process.mem_size
60
+ cpu = process.user_cpu_times
61
+ resp = Time.now.to_i
62
+ [mem, cpu, resp]
63
+ end
64
+
65
+ def calculate_process_times(env, m1, m2, c1, c2, r1, r2)
66
+ mem_used = m2 - m1
67
+ cpu_used = c2 - c2
68
+ resp_time = r2 - r1
69
+ req = Rack::Request.new(env)
70
+ {
71
+ pid: process.pid,
72
+ mem_used: mem_used,
73
+ cpu_used: cpu_used,
74
+ resp_time: resp_time,
75
+ request: req
76
+ }
77
+ end
78
+
79
+ def process
80
+ @process ||= Proceso::PID.new(pid)
81
+ end
82
+
83
+ def exclusions
84
+ config.exclusions || []
85
+ end
86
+
87
+ def path_excluded?(path)
88
+ exclusions.any? {|e| path =~ e }
89
+ end
90
+
91
+ end
92
+ end
@@ -0,0 +1,29 @@
1
+ require 'rails'
2
+ require 'proceso'
3
+ require 'proceso/middleware'
4
+
5
+ module Proceso
6
+ class Railtie < Rails::Railtie
7
+
8
+ config.proceso = ActiveSupport::OrderedOptions.new
9
+
10
+ config.proceso.environments = ['development']
11
+ config.proceso.exclusions = [/\.(js|wot|jpg|jpeg|gif|png|css)$/]
12
+
13
+ initializer 'proceso.initializer' do |app|
14
+ if environment_enabled?
15
+ app.middleware.insert 0, Proceso::Middleware, config: config.proceso
16
+ Proceso::Middleware.start_instrument!
17
+ end
18
+ end
19
+
20
+ def environments
21
+ config.proceso.environments.uniq.map(&:to_s).compact
22
+ end
23
+
24
+ def environment_enabled?
25
+ environments.include?(Rails.env.to_s)
26
+ end
27
+
28
+ end
29
+ end
metadata ADDED
@@ -0,0 +1,94 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: proceso-rails
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.1
5
+ platform: ruby
6
+ authors:
7
+ - Bryan Goines
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-05-09 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: proceso
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0.2'
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 0.2.0
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - "~>"
28
+ - !ruby/object:Gem::Version
29
+ version: '0.2'
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: 0.2.0
33
+ - !ruby/object:Gem::Dependency
34
+ name: rake
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ type: :development
41
+ prerelease: false
42
+ version_requirements: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ - !ruby/object:Gem::Dependency
48
+ name: rspec
49
+ requirement: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: '0'
61
+ description: Proceso Middleware for Rails
62
+ email:
63
+ - bryann83@gmail.com
64
+ executables: []
65
+ extensions: []
66
+ extra_rdoc_files: []
67
+ files:
68
+ - lib/proceso-rails.rb
69
+ - lib/proceso/middleware.rb
70
+ homepage: https://github.com/bry4n/proceso
71
+ licenses:
72
+ - MIT
73
+ metadata: {}
74
+ post_install_message:
75
+ rdoc_options: []
76
+ require_paths:
77
+ - lib
78
+ required_ruby_version: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ required_rubygems_version: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - ">="
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
88
+ requirements: []
89
+ rubyforge_project:
90
+ rubygems_version: 2.2.0
91
+ signing_key:
92
+ specification_version: 4
93
+ summary: Proceso Middleware for Rails 4
94
+ test_files: []