Class: Thin::Stats::Adapter

Inherits:
Object
  • Object
show all
Includes:
ERB::Util
Defined in:
lib/thin/stats.rb

Overview

Rack adapter to log stats about a Rack application.

Instance Method Summary collapse

Constructor Details

#initialize(app, path = '/stats') ⇒ Adapter

Returns a new instance of Adapter.



9
10
11
12
13
14
15
16
17
18
# File 'lib/thin/stats.rb', line 9

def initialize(app, path='/stats')
  @app  = app
  @path = path

  @template = ERB.new(File.read(File.dirname(__FILE__) + '/stats.html.erb'))
  
  @requests          = 0
  @requests_finished = 0
  @start_time        = Time.now
end

Instance Method Details

#call(env) ⇒ Object



20
21
22
23
24
25
26
# File 'lib/thin/stats.rb', line 20

def call(env)
  if env['PATH_INFO'].index(@path) == 0
    serve(env)
  else
    log(env) { @app.call(env) }
  end
end

#log(env) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/thin/stats.rb', line 28

def log(env)
  @requests += 1
  @last_request = Rack::Request.new(env)
  request_started_at = Time.now
  
  response = yield
  
  @requests_finished += 1
  @last_request_time = Time.now - request_started_at
  
  response
end

#serve(env) ⇒ Object



41
42
43
44
45
46
47
48
49
# File 'lib/thin/stats.rb', line 41

def serve(env)
  body = @template.result(binding)
  
  [
    200,
    { 'Content-Type' => 'text/html' },
    [body]
  ]
end