public
Description: A very fast & simple Ruby web server
Homepage: http://code.macournoyer.com/thin/
Clone URL: git://github.com/macournoyer/thin.git
Search Repo:
Add a very basic stats page through Stats adapter, load w/ --stats and 
browse to /stats.
macournoyer (author)
Mon Jan 21 21:30:03 -0800 2008
commit  98d3e8f4e08e46f73bb2012b0869a819d1cb2a5f
tree    c813b8c5cd1765f0872b372ef2f073ca60c34aa6
parent  2b8482fd1dc3fe7c0539ddce9299dfb96bb8d88d
...
1
 
2
3
4
...
1
2
3
4
5
0
@@ -1,4 +1,5 @@
0
 == 0.5.5 Pony release
0
+ * Add a very basic stats page through Stats adapter, load w/ --stats and browse to /stats.
0
  * Add --trace (-V) option to trace request/response and get backtrace w/out all Ruby debug stuff.
0
  * Add --config (-C) option to load options from a config file in thin script [Matt Todd].
0
  * Alter response headers to output directly to a string.
...
46
47
48
 
49
50
51
...
94
95
96
 
 
 
97
98
99
...
46
47
48
49
50
51
52
...
95
96
97
98
99
100
101
102
103
0
@@ -46,6 +46,7 @@ opts = OptionParser.new do |opts|
0
   opts.on("-g", "--group NAME", "Group to run daemon as (use with -u)") { |group| options[:group] = group }
0
   opts.on( "--prefix PATH", "Mount the app under PATH (start with /)") { |path| options[:prefix] = path }
0
   opts.on("-C", "--config PATH", "Load option from a config file") { |file| options[:config] = file }
0
+ opts.on( "--stats", "Install the Stats adapter at /stats") { options[:stats] = true }
0
   
0
   opts.separator ""
0
   opts.separator "Common options:"
0
@@ -94,6 +95,9 @@ def start(options)
0
     # If a prefix is required, wrap in Rack URL mapper
0
     server.app = Rack::URLMap.new(options[:prefix] => server.app) if options[:prefix]
0
     
0
+ # If a stats are required, wrap in Stats adapter
0
+ server.app = Thin::Stats::Adapter.new(server.app) if options[:stats]
0
+
0
     server.start!
0
   end
0
 end
...
22
23
24
 
25
26
27
...
22
23
24
25
26
27
28
0
@@ -22,6 +22,7 @@ module Thin
0
   autoload :Request, 'thin/request'
0
   autoload :Response, 'thin/response'
0
   autoload :Server, 'thin/server'
0
+ autoload :Stats, 'thin/stats'
0
 end
0
 
0
 require 'rack'
...
66
67
68
69
70
71
72
...
66
67
68
 
69
70
71
0
@@ -66,7 +66,6 @@ module Thin
0
       end
0
     end
0
     
0
-
0
     def stop
0
       EventMachine.stop_event_loop
0
     rescue
...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
0
@@ -0,0 +1,64 @@
0
+module Thin
0
+ # Rack adapter to log stats to a Rack application
0
+ module Stats
0
+ class Adapter
0
+ def initialize(app, path='/stats')
0
+ @app = app
0
+ @path = path
0
+
0
+ @requests = 0
0
+ @requests_finished = 0
0
+ @start_time = Time.now
0
+ end
0
+
0
+ def call(env)
0
+ if env['PATH_INFO'].index(@path) == 0
0
+ serve(env)
0
+ else
0
+ log(env) { @app.call(env) }
0
+ end
0
+ end
0
+
0
+ def log(env)
0
+ @requests += 1
0
+ @server = env['SERVER_SOFTWARE']
0
+ request_started_at = Time.now
0
+
0
+ response = yield
0
+
0
+ @requests_finished += 1
0
+ @last_request_path = env['PATH_INFO']
0
+ @last_request_time = Time.now - request_started_at
0
+
0
+ response
0
+ end
0
+
0
+ def serve(env)
0
+ body = '<html><body>'
0
+ body << '<h1>Server stats</h1>'
0
+ body << '<ul>'
0
+ body << "<li>#{@requests} requests</li>"
0
+ body << "<li>#{@requests_finished} requests finished</li>"
0
+ body << "<li>#{@requests - @requests_finished} errors</li>"
0
+ body << "<li>#{Time.now - @start_time} uptime</li>"
0
+ body << "<li>Running on #{@server}</li>"
0
+ body << '</ul>'
0
+ body << '<h2>Last request</h2>'
0
+ body << '<ul>'
0
+ body << "<li>#{@last_request_path}</li>"
0
+ body << "<li>Took #{@last_request_time} sec</li>"
0
+ body << '</ul>'
0
+ body << '</body></html>'
0
+
0
+ [
0
+ 200,
0
+ {
0
+ 'Content-Type' => 'text/html',
0
+ 'Content-Length' => body.size.to_s
0
+ },
0
+ body
0
+ ]
0
+ end
0
+ end
0
+ end
0
+end
0
\ No newline at end of file

Comments

    No one has commented yet.