corn 0.4.6 → 0.5.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/corn/post.rb +3 -6
- data/lib/corn/rack/request_env.rb +13 -14
- data/lib/corn/rack/slow_request_profiler.rb +33 -22
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e4be49268a392f4c4cbdaedf31da0808ac05329e
|
4
|
+
data.tar.gz: 645775af9946dcefb266f85f3bcc3da4ea572735
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
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
|
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(
|
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
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
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
|
17
|
-
|
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
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
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
|
19
|
-
@
|
37
|
+
def initialize(app, *args)
|
38
|
+
@app = Corn.configured? ? ProfilingApp.new(app, *args) : app
|
20
39
|
end
|
21
40
|
|
22
|
-
def
|
23
|
-
@
|
24
|
-
@post.terminate
|
41
|
+
def call(env)
|
42
|
+
@app.call(env)
|
25
43
|
end
|
26
44
|
|
27
|
-
def
|
28
|
-
|
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
|
+
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-
|
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
|
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: []
|