memory-monitoring 0.0.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +14 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +48 -0
- data/Rakefile +2 -0
- data/lib/memory-monitoring.rb +2 -0
- data/lib/memory_monitoring.rb +6 -0
- data/lib/memory_monitoring/injector.rb +46 -0
- data/lib/memory_monitoring/rack.rb +68 -0
- data/lib/memory_monitoring/simple_formatter.rb +22 -0
- data/lib/memory_monitoring/version.rb +5 -0
- data/memory-monitoring.gemspec +26 -0
- metadata +118 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 247d7bcb9aea01083c596d41d3fbea19b7f52615
|
4
|
+
data.tar.gz: 3bd494bab71aad5a7d189e3f66d9ba79fa392167
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 2c38447d7d7383b41a75f2c7876dae8de559c20900958b69928e50338bc138254d2cb38dc7c0e7c6caf52c052e8afee4dea94d87ce3d8900b436326b4bbd682a
|
7
|
+
data.tar.gz: 7c5ff2b53a852c40ce8542fcd1268dd9ba1fcdffc8f1c0431a0de2ead76f7caa3f5e17479e7383ae9118f0fb533de36e4c5cbcdb41055f2a827740eb2e8b70e5
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Howl王
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
# MemoryMonitoring Middleware
|
2
|
+
|
3
|
+
`MemoryMonitoring::Rack` provides support for Memory monitor for Rack compatible web applications.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'memory-monitoring'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install memory-monitoring
|
20
|
+
|
21
|
+
## Configuration
|
22
|
+
|
23
|
+
### Rack
|
24
|
+
|
25
|
+
In `config.ru`, configure `MemoryMonitoring::Rack` by passing a block to the `use` command:
|
26
|
+
|
27
|
+
```ruby
|
28
|
+
use MemoryMonitoring::Rack
|
29
|
+
```
|
30
|
+
|
31
|
+
Or Padrino, Sinatra:
|
32
|
+
```ruby
|
33
|
+
run MemoryMonitoring::Rack.new(Padrino.application)
|
34
|
+
```
|
35
|
+
### Rails
|
36
|
+
Put something like the code below in `config/application.rb` of your Rails application. For example, this will allow GET, POST or OPTIONS requests from any origin on any resource.
|
37
|
+
|
38
|
+
```ruby
|
39
|
+
module YourApp
|
40
|
+
class Application < Rails::Application
|
41
|
+
|
42
|
+
# ...
|
43
|
+
|
44
|
+
config.middleware.use MemoryMonitoring::Rack
|
45
|
+
|
46
|
+
end
|
47
|
+
end
|
48
|
+
```
|
data/Rakefile
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
require 'json' unless defined?(::JSON)
|
3
|
+
|
4
|
+
module MemoryMonitoring
|
5
|
+
class Injector # 注入
|
6
|
+
def initialize(messages)
|
7
|
+
@messages = messages
|
8
|
+
end
|
9
|
+
|
10
|
+
# 注入
|
11
|
+
def injection(type, body)
|
12
|
+
case type
|
13
|
+
when /text\/html/, '*/*'
|
14
|
+
html_injection(body)
|
15
|
+
when /application\/json/
|
16
|
+
json_injection(body)
|
17
|
+
when /text\/javascript/
|
18
|
+
js_injection(body)
|
19
|
+
else
|
20
|
+
puts type
|
21
|
+
puts '_'*88
|
22
|
+
puts body
|
23
|
+
puts '_'*88
|
24
|
+
body
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
private
|
29
|
+
|
30
|
+
def js_injection(body)
|
31
|
+
body = "console.log('#{@messages.join(', ')}');\n" + body
|
32
|
+
end
|
33
|
+
|
34
|
+
def html_injection(body)
|
35
|
+
body = "<!-- #{ @messages.join(', ') } -->\n" + body
|
36
|
+
end
|
37
|
+
|
38
|
+
def json_injection(body)
|
39
|
+
body = ::JSON.load(body)
|
40
|
+
@messages = @messages.map { |m| m.split(': ') }.flatten
|
41
|
+
body[:debug] = Hash[*@messages]
|
42
|
+
body = ::JSON.dump(body)
|
43
|
+
end
|
44
|
+
|
45
|
+
end
|
46
|
+
end
|
@@ -0,0 +1,68 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
require 'logger' unless defined?(::Logger)
|
3
|
+
|
4
|
+
module MemoryMonitoring
|
5
|
+
class Rack # 中间件 Middleware
|
6
|
+
def initialize(app)
|
7
|
+
@app = app
|
8
|
+
logger.formatter = SimpleFormatter.new
|
9
|
+
reset # 重置计数器
|
10
|
+
end
|
11
|
+
|
12
|
+
def call(env)
|
13
|
+
request_uri = env['HTTP_HOST'] + env['PATH_INFO']
|
14
|
+
@messages = []
|
15
|
+
# 除静态文件外,读取当前内存
|
16
|
+
unless request_uri =~ /\/(assets|uploads|images|fonts|js|css)|.(js|css|png|gif|jpg|jpeg|woff|txt|ico|eot|map|svg|ttf)/
|
17
|
+
@start_rss = current_memory
|
18
|
+
@start_at = Time.now
|
19
|
+
end
|
20
|
+
# 获取输出内容
|
21
|
+
status, headers, response = @app.call(env)
|
22
|
+
# rescue => e # 出错了
|
23
|
+
ensure
|
24
|
+
unless messages.empty?
|
25
|
+
@level = :error if status.nil? || status.between?(400, 600)
|
26
|
+
logger.send @level, (@messages + [ request_uri ]).join("\t")
|
27
|
+
unless response.nil?
|
28
|
+
body = (response.respond_to?(:body) ? response.body : response).first
|
29
|
+
injector = Injector.new(@messages)
|
30
|
+
return [status, headers, [injector.injection(env['HTTP_ACCEPT'], body)]]
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
private
|
36
|
+
|
37
|
+
def messages
|
38
|
+
if @start_rss > 0
|
39
|
+
@ended_rss = current_memory
|
40
|
+
@ended_at = Time.now
|
41
|
+
if distance > 0
|
42
|
+
@messages << "Response Time: #{(@ended_at - @start_at).round(3)} sec."
|
43
|
+
@messages << "Consume Memory: #{distance} MB."
|
44
|
+
@messages << "Now: #{@ended_rss} MB."
|
45
|
+
end
|
46
|
+
reset # 重置计数器
|
47
|
+
end
|
48
|
+
@messages
|
49
|
+
end
|
50
|
+
|
51
|
+
def reset
|
52
|
+
@level = :info
|
53
|
+
@start_rss = @ended_rss = 0
|
54
|
+
end
|
55
|
+
|
56
|
+
def distance # 内存差
|
57
|
+
(@ended_rss - @start_rss).round(2)
|
58
|
+
end
|
59
|
+
|
60
|
+
def current_memory
|
61
|
+
(`ps -o rss= -p #{$$}`.to_f / 1024).round(2)
|
62
|
+
end
|
63
|
+
|
64
|
+
def logger
|
65
|
+
@logger ||= ::Logger.new './log/memory.log', 'daily'
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
|
3
|
+
module MemoryMonitoring
|
4
|
+
class SimpleFormatter # 日志格式
|
5
|
+
SEVERITY_TO_TAG_MAP = { 'DEBUG'=>'meh', 'INFO'=>'fyi', 'WARN'=>'hmm', 'ERROR'=>'wtf', 'FATAL'=>'omg', 'UNKNOWN'=>'???' }
|
6
|
+
SEVERITY_TO_COLOR_MAP = { 'DEBUG'=>'0;37', 'INFO'=>'32', 'WARN'=>'33', 'ERROR'=>'31', 'FATAL'=>'31', 'UNKNOWN'=>'37' }
|
7
|
+
USE_HUMOROUS_SEVERITIES = true
|
8
|
+
|
9
|
+
def call(severity, time, progname, msg)
|
10
|
+
if USE_HUMOROUS_SEVERITIES
|
11
|
+
formatted_severity = sprintf("%-3s", SEVERITY_TO_TAG_MAP[severity])
|
12
|
+
else
|
13
|
+
formatted_severity = sprintf("%-5s", severity)
|
14
|
+
end
|
15
|
+
|
16
|
+
formatted_time = time.strftime("%Y-%m-%d %H:%M:%S.") << time.usec.to_s[0..2].rjust(3)
|
17
|
+
color = SEVERITY_TO_COLOR_MAP[severity]
|
18
|
+
|
19
|
+
"\033[0;37m#{formatted_time}\033[0m [\033[#{color}m#{formatted_severity}\033[0m] #{msg.strip} (pid:#{$$})\n"
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'memory_monitoring/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "memory-monitoring"
|
8
|
+
spec.version = MemoryMonitoring::VERSION
|
9
|
+
spec.authors = ["Howl王"]
|
10
|
+
spec.email = ["howl.wong@gmail.com"]
|
11
|
+
spec.summary = %q{ Memory Monitoring }
|
12
|
+
spec.description = %q{ Provides support for Memory monitor for Rack compatible web applications. }
|
13
|
+
spec.homepage = "https://github.com/mimosa/memory-monitoring"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_runtime_dependency 'json', '~> 1.8', '>= 1.8.0'
|
22
|
+
|
23
|
+
spec.add_development_dependency 'bundler', '~> 1.7'
|
24
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
25
|
+
spec.add_development_dependency 'awesome_print', '~> 1.2'
|
26
|
+
end
|
metadata
ADDED
@@ -0,0 +1,118 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: memory-monitoring
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.5
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Howl王
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-12-21 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: json
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.8'
|
20
|
+
- - ">="
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: 1.8.0
|
23
|
+
type: :runtime
|
24
|
+
prerelease: false
|
25
|
+
version_requirements: !ruby/object:Gem::Requirement
|
26
|
+
requirements:
|
27
|
+
- - "~>"
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '1.8'
|
30
|
+
- - ">="
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: 1.8.0
|
33
|
+
- !ruby/object:Gem::Dependency
|
34
|
+
name: bundler
|
35
|
+
requirement: !ruby/object:Gem::Requirement
|
36
|
+
requirements:
|
37
|
+
- - "~>"
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
version: '1.7'
|
40
|
+
type: :development
|
41
|
+
prerelease: false
|
42
|
+
version_requirements: !ruby/object:Gem::Requirement
|
43
|
+
requirements:
|
44
|
+
- - "~>"
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: '1.7'
|
47
|
+
- !ruby/object:Gem::Dependency
|
48
|
+
name: rake
|
49
|
+
requirement: !ruby/object:Gem::Requirement
|
50
|
+
requirements:
|
51
|
+
- - "~>"
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '10.0'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
requirements:
|
58
|
+
- - "~>"
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
version: '10.0'
|
61
|
+
- !ruby/object:Gem::Dependency
|
62
|
+
name: awesome_print
|
63
|
+
requirement: !ruby/object:Gem::Requirement
|
64
|
+
requirements:
|
65
|
+
- - "~>"
|
66
|
+
- !ruby/object:Gem::Version
|
67
|
+
version: '1.2'
|
68
|
+
type: :development
|
69
|
+
prerelease: false
|
70
|
+
version_requirements: !ruby/object:Gem::Requirement
|
71
|
+
requirements:
|
72
|
+
- - "~>"
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
version: '1.2'
|
75
|
+
description: " Provides support for Memory monitor for Rack compatible web applications. "
|
76
|
+
email:
|
77
|
+
- howl.wong@gmail.com
|
78
|
+
executables: []
|
79
|
+
extensions: []
|
80
|
+
extra_rdoc_files: []
|
81
|
+
files:
|
82
|
+
- ".gitignore"
|
83
|
+
- Gemfile
|
84
|
+
- LICENSE.txt
|
85
|
+
- README.md
|
86
|
+
- Rakefile
|
87
|
+
- lib/memory-monitoring.rb
|
88
|
+
- lib/memory_monitoring.rb
|
89
|
+
- lib/memory_monitoring/injector.rb
|
90
|
+
- lib/memory_monitoring/rack.rb
|
91
|
+
- lib/memory_monitoring/simple_formatter.rb
|
92
|
+
- lib/memory_monitoring/version.rb
|
93
|
+
- memory-monitoring.gemspec
|
94
|
+
homepage: https://github.com/mimosa/memory-monitoring
|
95
|
+
licenses:
|
96
|
+
- MIT
|
97
|
+
metadata: {}
|
98
|
+
post_install_message:
|
99
|
+
rdoc_options: []
|
100
|
+
require_paths:
|
101
|
+
- lib
|
102
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
103
|
+
requirements:
|
104
|
+
- - ">="
|
105
|
+
- !ruby/object:Gem::Version
|
106
|
+
version: '0'
|
107
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
108
|
+
requirements:
|
109
|
+
- - ">="
|
110
|
+
- !ruby/object:Gem::Version
|
111
|
+
version: '0'
|
112
|
+
requirements: []
|
113
|
+
rubyforge_project:
|
114
|
+
rubygems_version: 2.4.2
|
115
|
+
signing_key:
|
116
|
+
specification_version: 4
|
117
|
+
summary: Memory Monitoring
|
118
|
+
test_files: []
|