corn 0.4.0 → 0.4.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
- ---
2
- SHA1:
3
- metadata.gz: f6f36171dd4b5964f2fd405ee3d8bb32ec9903dc
4
- data.tar.gz: c6a0ae40a7cc39fbe9de79bc94ab1544052e41d8
5
- SHA512:
6
- metadata.gz: 8ee868d1f6c7ead8146b24d82f5df7d53771ebc50ddefe554483528885c963e472ff2ec01392745f7e6516baf70230c3dc83ad72cac7411b66335f4158b5f06d
7
- data.tar.gz: e9509c5b8a8925db14fbb7c6ba634e8acc560519fd15acad8361d1fc5db4b76672fc6917815da342eb3a5f591880f17c142be44d1d9b3cbae84c5a0e8d3df77f
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 6b6b345c60ef516a140324b6b3dd8eb3a88eee84
4
+ data.tar.gz: 0f6a5d0a10a48c6435b3b3bd3066e97d0fbe67fb
5
+ SHA512:
6
+ metadata.gz: 75be86a19958acc4d0052f291ce091f4ac98fdd9690c62756eb65c78b3f818eb2839a0ad7fe896830731c41ca69688874b519b071389cfd497d59f1ae0f912fd
7
+ data.tar.gz: 0490385b78fca2ab7cc5aac0dcc07bf70eed6c9787f3b2515de0f16eff8bb994274c29872da3028564441b1729c1a2fc5875c942b28ae70156502b0f2d0b4a12
data/lib/corn.rb CHANGED
@@ -3,10 +3,15 @@ require 'sampling_prof'
3
3
  require 'net/http'
4
4
  require 'net/https'
5
5
  require 'corn/rack'
6
+ require 'logger'
6
7
 
7
8
  module Corn
8
9
  module_function
9
10
 
11
+ def rack_slow_request_profiler
12
+ Rack::SlowRequestProfiler
13
+ end
14
+
10
15
  def host
11
16
  ENV['CORN_HOST']
12
17
  end
@@ -19,39 +24,15 @@ module Corn
19
24
  !!(host && client_id)
20
25
  end
21
26
 
22
- def profiler(report_name, sampling_interval=0.1, output_interval=nil)
23
- if !configured?
24
- log("No CORN_CLIENT_ID or CORN_HOST configured, profiling data is not submitted")
25
- return
26
- end
27
- SamplingProf.new(sampling_interval, true) do |data|
28
- post(data, report_name)
29
- end.tap do |prof|
30
- if output_interval
31
- prof.output_interval = output_interval
32
- end
33
- end
34
- end
35
-
36
27
  def submit_url
37
28
  File.join(host, 'profile_data')
38
29
  end
39
30
 
40
- def log(msg)
41
- $stderr.puts msg
31
+ def logger=(l)
32
+ @logger = l
42
33
  end
43
34
 
44
- def post(data, name)
45
- url = URI.parse(submit_url)
46
- req = Net::HTTP::Post.new(url.path)
47
- req.set_form_data("data" => data, 'client_id' => client_id, 'name' => name)
48
- res = Net::HTTP.start(url.host, url.port) do |http|
49
- http.use_ssl = url.scheme == 'https'
50
- http.request(req)
51
- end
52
- log("Corn report submitted to #{submit_url}")
53
- rescue Exception => e
54
- log("post to #{submit_url} failed: #{e.message}")
55
- log(e.backtrace.join("\n"))
35
+ def logger
36
+ @logger ||= Logger.new(STDOUT)
56
37
  end
57
38
  end
data/lib/corn/post.rb ADDED
@@ -0,0 +1,53 @@
1
+ require 'thread'
2
+
3
+ module Corn
4
+ class Post
5
+ def initialize
6
+ @queue = Queue.new
7
+ @thread = start_post_thread
8
+ end
9
+
10
+ def terminate
11
+ @thread.terminate rescue nil
12
+ end
13
+
14
+ def start_post_thread
15
+ Thread.start do
16
+ begin
17
+ loop do
18
+ http_post(*@queue.pop)
19
+ sleep 1
20
+ end
21
+ rescue => e
22
+ log("Corn post thread stopped by error #{e.message}\n#{e.backtrace.join("\n")}")
23
+ end
24
+ end
25
+ end
26
+
27
+ def enqueue(data, name)
28
+ @queue << [data, name]
29
+ end
30
+
31
+ def http_post(data, name)
32
+ url = URI.parse(submit_url)
33
+ req = Net::HTTP::Post.new(url.path)
34
+ req.set_form_data("data" => data, 'client_id' => Corn.client_id, 'name' => name)
35
+ res = Net::HTTP.start(url.host, url.port) do |http|
36
+ http.use_ssl = url.scheme == 'https'
37
+ http.request(req)
38
+ end
39
+ log("Corn report submitted to #{submit_url}")
40
+ rescue Exception => e
41
+ log("post to #{submit_url} failed: #{e.message}")
42
+ log(e.backtrace.join("\n"))
43
+ end
44
+
45
+ def submit_url
46
+ Corn.submit_url
47
+ end
48
+
49
+ def log(msg)
50
+ Corn.logger.info msg
51
+ end
52
+ end
53
+ end
data/lib/corn/rack.rb CHANGED
@@ -1,26 +1 @@
1
- module Corn
2
- class Rack
3
- def initialize(app,
4
- report_name="Corn::Rack created at #{Time.now}",
5
- sampling_interval=0.1,
6
- output_interval=nil)
7
- @app = app
8
- @prof = Corn.profiler(report_name, sampling_interval, output_interval)
9
- at_exit { terminate }
10
- end
11
-
12
- def call(env)
13
- if Corn.configured?
14
- @prof.profile do
15
- @app.call(env)
16
- end
17
- else
18
- @app.call(env)
19
- end
20
- end
21
-
22
- def terminate
23
- @prof.terminate
24
- end
25
- end
26
- end
1
+ require 'corn/rack/slow_request_profiler'
@@ -0,0 +1,34 @@
1
+ require 'sampling_prof'
2
+ require 'corn/post'
3
+
4
+ module Corn
5
+ module Rack
6
+ class SlowRequestProfiler
7
+ def initialize(app,
8
+ slow_request=5,
9
+ sampling_interval=0.1)
10
+ @app = app
11
+ @post = Post.new
12
+ @prof = SamplingProf.new(sampling_interval) do |data|
13
+ t = Thread.current
14
+ if (Time.now - t['corn_start_time']) > slow_request
15
+ name = [t['corn_start_time'], t['corn_path_info']].join(',')
16
+ @post.enqueue(data, name)
17
+ end
18
+ end
19
+ at_exit { terminate }
20
+ end
21
+
22
+ def call(env)
23
+ Thread.current['corn_path_info'] = env['PATH_INFO']
24
+ Thread.current['corn_start_time'] = Time.now
25
+ @prof.profile { @app.call(env) }
26
+ end
27
+
28
+ def terminate
29
+ @prof.terminate rescue nil
30
+ @post.terminate
31
+ end
32
+ end
33
+ end
34
+ end
metadata CHANGED
@@ -1,66 +1,64 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: corn
3
- version: !ruby/object:Gem::Version
4
- version: 0.4.0
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.4.1
5
5
  platform: ruby
6
- authors:
7
- - Xiao Li
8
- autorequire:
6
+ authors:
7
+ - Xiao Li
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
-
12
- date: 2014-04-16 00:00:00 Z
13
- dependencies:
14
- - !ruby/object:Gem::Dependency
15
- name: sampling_prof
16
- prerelease: false
17
- requirement: &id001 !ruby/object:Gem::Requirement
18
- requirements:
19
- - - ">="
20
- - !ruby/object:Gem::Version
21
- version: "0.3"
22
- type: :runtime
23
- version_requirements: *id001
11
+ date: 2014-06-15 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: sampling_prof
15
+ version_requirements: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0.4'
20
+ requirement: !ruby/object:Gem::Requirement
21
+ requirements:
22
+ - - '>='
23
+ - !ruby/object:Gem::Version
24
+ version: '0.4'
25
+ prerelease: false
26
+ type: :runtime
24
27
  description: |
25
28
  Corn collects your application's profiling data by sampling_prof gem, and submits the result to server, so that you can merge multiple server's profiling data and do analysis together.
26
-
27
- email:
28
- - swing1979@gmail.com
29
+ email:
30
+ - swing1979@gmail.com
29
31
  executables: []
30
-
31
32
  extensions: []
32
-
33
33
  extra_rdoc_files: []
34
-
35
- files:
36
- - README.md
37
- - lib/corn.rb
38
- - lib/corn/rack.rb
34
+ files:
35
+ - README.md
36
+ - lib/corn.rb
37
+ - lib/corn/post.rb
38
+ - lib/corn/rack.rb
39
+ - lib/corn/rack/slow_request_profiler.rb
39
40
  homepage: https://github.com/xli/corn
40
- licenses:
41
- - MIT
41
+ licenses:
42
+ - MIT
42
43
  metadata: {}
43
-
44
- post_install_message:
44
+ post_install_message:
45
45
  rdoc_options: []
46
-
47
- require_paths:
48
- - lib
49
- required_ruby_version: !ruby/object:Gem::Requirement
50
- requirements:
51
- - &id002
52
- - ">="
53
- - !ruby/object:Gem::Version
54
- version: "0"
55
- required_rubygems_version: !ruby/object:Gem::Requirement
56
- requirements:
57
- - *id002
46
+ require_paths:
47
+ - lib
48
+ required_ruby_version: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - '>='
51
+ - !ruby/object:Gem::Version
52
+ version: '0'
53
+ required_rubygems_version: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - '>='
56
+ - !ruby/object:Gem::Version
57
+ version: '0'
58
58
  requirements: []
59
-
60
- rubyforge_project:
59
+ rubyforge_project:
61
60
  rubygems_version: 2.1.9
62
- signing_key:
61
+ signing_key:
63
62
  specification_version: 4
64
63
  summary: Corn submits profiling data to Corn server.
65
64
  test_files: []
66
-