Class: RackStatsD::RequestStatus

Inherits:
Object
  • Object
show all
Defined in:
lib/rack-statsd.rb

Overview

Simple middleware to add a quick status URL for tools like Nagios.

Constant Summary collapse

REQUEST_METHOD =
'REQUEST_METHOD'.freeze
GET =
'GET'.freeze
PATH_INFO =
'PATH_INFO'.freeze
STATUS_PATH =
'/status'
HEADERS =
{"Content-Type" => "text/plain"}.freeze

Instance Method Summary collapse

Constructor Details

#initialize(app, callback_or_response, status_path = nil) ⇒ RequestStatus

Initializes the middleware.

# Responds with "OK" on /status
use RequestStatus, "OK"

You can change what URL to look for:

use RequestStatus, "OK", "/ping"

You can also check internal systems and return something more informative.

use RequestStatus, lambda {
  status = MyApp.status # A Hash of some live counters or something
  [200, {"Content-Type" => "application/json"}, status.to_json]
}

app - The next Rack app in the pipeline. callback_or_response - Either a Proc or a Rack response. status_path - Optional String path that returns the status.

Default: "/status"

Returns nothing.



34
35
36
37
38
# File 'lib/rack-statsd.rb', line 34

def initialize(app, callback_or_response, status_path = nil)
  @app = app
  @status_path = (status_path || STATUS_PATH).freeze
  @callback = callback_or_response
end

Instance Method Details

#call(env) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/rack-statsd.rb', line 40

def call(env)
  if env[REQUEST_METHOD] == GET
    if env[PATH_INFO] == @status_path
      if @callback.respond_to?(:call)
        return @callback.call
      else
        return [200, HEADERS, [@callback.to_s]]
      end
    end
  end

  @app.call env
end