corn 0.4.6 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 44550a060755050d6930b2921ba78abb6abe6553
4
- data.tar.gz: 964e84445c59fe3631c4a96c163c1dae2e3aaad0
3
+ metadata.gz: e4be49268a392f4c4cbdaedf31da0808ac05329e
4
+ data.tar.gz: 645775af9946dcefb266f85f3bcc3da4ea572735
5
5
  SHA512:
6
- metadata.gz: 213d49a2eff2d13f557b46e66bed4a9eea5492f508e29f4488a677d2a2b3bd3dc60267e6ddef0ef52d1459abebb74f329cf949152d673486161c5b231d3bf83e
7
- data.tar.gz: c390d9e3b98b03e562a1867dcee8a406ba7fc5f45e5b1d4dac3c537dc87283bdd40c836d0fc9f6f8264453b20e97ddf831288245af53fd1ecd682cc45237033b
6
+ metadata.gz: dc4055efcb92c7c0b14faefdced0800786bfd6804a3bca82466507e60578638fa95f03deb9dfe6bb480a8069b5fb022025dba771d8dce2791cc5cc1c4316e7b9
7
+ data.tar.gz: 48b103523e4a1f684524ae625c9dab66600eae78927463411b7eb748a54ee76adb82f0459ab925d5b2ce7603c44b6547671994d488ebb272d77f20413138aa8a
data/lib/corn/post.rb CHANGED
@@ -11,7 +11,7 @@ module Corn
11
11
  end
12
12
 
13
13
  def terminate
14
- @thread.terminate rescue nil
14
+ @thread.terminate
15
15
  end
16
16
 
17
17
  def start_post_thread
@@ -31,13 +31,10 @@ module Corn
31
31
  @queue << args
32
32
  end
33
33
 
34
- def http_post(data, name, start_time)
34
+ def http_post(data)
35
35
  uri = URI.parse(submit_url)
36
36
  req = Net::HTTP::Post.new(uri.path)
37
- req.set_form_data("data" => data,
38
- 'client_id' => Corn.client_id,
39
- 'name' => name,
40
- 'start_time' => start_time.iso8601)
37
+ req.set_form_data(data.merge('client_id' => Corn.client_id))
41
38
 
42
39
  http = Net::HTTP.new(uri.host, uri.port)
43
40
  if uri.scheme == 'https'
@@ -1,25 +1,24 @@
1
1
  module Corn
2
2
  module Rack
3
3
  class RequestEnv
4
- attr_reader :start_time
5
- def initialize(env, threshold)
6
- @path_info = env['PATH_INFO']
7
- @http_host = env['HTTP_HOST']
8
- @threshold = threshold
9
- @start_time = Time.now
4
+ def initialize(env, start_time=Time.now)
5
+ @env = [:path_info, :http_host, :query_string].inject({}) do |memo, k|
6
+ v = env[k.to_s.upcase]
7
+ if v.nil? || v.empty?
8
+ memo
9
+ else
10
+ memo.merge(k => v)
11
+ end
12
+ end
13
+ @env.merge!(:start_time => start_time)
10
14
  end
11
15
 
12
16
  def time
13
- Time.now - start_time
17
+ Time.now - @env[:start_time]
14
18
  end
15
19
 
16
- def slow_request?
17
- time > @threshold
18
- end
19
-
20
- def report_name
21
- File.join(*[@http_host,
22
- @path_info].compact)
20
+ def to_h
21
+ @env.dup
23
22
  end
24
23
  end
25
24
  end
@@ -5,34 +5,45 @@ require 'sampling_prof'
5
5
  module Corn
6
6
  module Rack
7
7
  class SlowRequestProfiler
8
- def initialize(app,
9
- slow_request_threshold=5,
10
- sampling_interval=0.1)
11
- @app = app
12
- @slow_request_threshold = slow_request_threshold
13
- @post = Post.new
14
- @prof = SamplingProf.new(sampling_interval)
15
- at_exit { terminate }
8
+ class ProfilingApp
9
+ def initialize(app, slow_request_threshold=5, sampling_interval=0.1)
10
+ @app = app
11
+ @slow_request_threshold = slow_request_threshold
12
+ @sampling_interval = sampling_interval
13
+ @post = Post.new
14
+ @prof = SamplingProf.new(@sampling_interval)
15
+ at_exit { terminate }
16
+ end
17
+
18
+ def call(env)
19
+ @prof.profile(output_handler(env)) { @app.call(env) }
20
+ end
21
+
22
+ def terminate
23
+ @prof.terminate rescue nil
24
+ @post.terminate rescue nil
25
+ end
26
+
27
+ def output_handler(env)
28
+ request_env = RequestEnv.new(env)
29
+ lambda do |data|
30
+ if request_env.time > @slow_request_threshold
31
+ @post.enqueue(request_env.to_h.merge("data" => data))
32
+ end
33
+ end
34
+ end
16
35
  end
17
36
 
18
- def call(env)
19
- @prof.profile(output_handler(env)) { @app.call(env) }
37
+ def initialize(app, *args)
38
+ @app = Corn.configured? ? ProfilingApp.new(app, *args) : app
20
39
  end
21
40
 
22
- def terminate
23
- @prof.terminate rescue nil
24
- @post.terminate
41
+ def call(env)
42
+ @app.call(env)
25
43
  end
26
44
 
27
- def output_handler(env)
28
- request_env = RequestEnv.new(env, @slow_request_threshold)
29
- lambda do |data|
30
- if request_env.slow_request?
31
- @post.enqueue(data,
32
- request_env.report_name,
33
- request_env.start_time)
34
- end
35
- end
45
+ def terminate
46
+ @app.terminate if @app.respond_to?(:terminate)
36
47
  end
37
48
  end
38
49
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: corn
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.6
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Xiao Li
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-06-19 00:00:00.000000000 Z
11
+ date: 2014-06-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: sampling_prof
@@ -25,7 +25,7 @@ dependencies:
25
25
  prerelease: false
26
26
  type: :runtime
27
27
  description: |
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.
28
+ Corn collects your application's profiling data by sampling_prof gem, and submits the result to Corn server for analysis.
29
29
  email:
30
30
  - swing1979@gmail.com
31
31
  executables: []