Class: Stethoscope

Inherits:
Object
  • Object
show all
Defined in:
lib/stethoscope.rb,
lib/stethoscope/rails.rb,
lib/stethoscope/version.rb

Overview

Adds the heartbeat to the stack

Defined Under Namespace

Classes: Buckets, Railtie

Constant Summary collapse

VERSION =
"0.2.3"

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ Stethoscope

Returns a new instance of Stethoscope.



132
133
134
# File 'lib/stethoscope.rb', line 132

def initialize(app)
  @app = app
end

Class Method Details

.bucketsObject



32
33
34
# File 'lib/stethoscope.rb', line 32

def self.buckets
  @buckets
end

.check(name, *_buckets_, &blk) ⇒ Object

Add a check to Stethoscope

A check is a block that checks the health of some aspect of your application You add information to the response of the check, including a status (if not successful)

Any resonse that has a status outside 200..299 will cause the heartbeat to fail

Examples:

Stethoscope.check("My Database") do |response|
  if my_db_check
    response[:result] = "Success"
  else
    response[:result] = "Bad Bad Bad"
    response[:arbitrary] = "something else"
    response[:status] = 500 # <---- VERY IMPORTANT
  end
end

See Also:



83
84
85
86
87
88
# File 'lib/stethoscope.rb', line 83

def self.check(name, *_buckets_, &blk)
  _buckets_.each do |bucket|
    buckets[bucket] << name
  end
  checks[name] = blk
end

.checksObject

The collection of checks currently in place in Stethoscope

See Also:



59
60
61
# File 'lib/stethoscope.rb', line 59

def self.checks
  @checks ||= {}
end

.clear_checksObject

Clears all defined checks

See Also:



106
107
108
109
# File 'lib/stethoscope.rb', line 106

def self.clear_checks
  buckets.clear
  checks.clear
end

.remove_check(name) ⇒ Object

Removes a give check

Examples:

Stethoscope.remove_check("my check to remove")

See Also:



96
97
98
99
100
101
# File 'lib/stethoscope.rb', line 96

def self.remove_check(name)
  buckets.each do |bucket, _checks_|
    _checks_.delete(name)
  end
  checks.delete(name)
end

.templateObject

Getter for the Tilt template for heartbeat rendering By default, the Stethoscope default template is used. Overwrite this to use a custom template

See Also:



129
130
131
# File 'lib/stethoscope.rb', line 129

def self.template
  @template ||= Tilt.new(File.join(File.dirname(__FILE__), "stethoscope", "template.erb"))
end

.template=(template) ⇒ Object

Sets the Tilt template that will be used for heartbeat rendering

Parameters:

  • template
    • A Tilt template object

See Also:



118
119
120
# File 'lib/stethoscope.rb', line 118

def self.template=(template)
  @template = template
end

.urlObject

The current url that Stethoscope is setup to listen for

See Also:



52
53
54
# File 'lib/stethoscope.rb', line 52

def self.url
  @url ||= "/heartbeat"
end

.url=(url) ⇒ Object

Set the url to check for the heartbeat in this application

Examples:

Stethoscope.url = "/my/heartbeat/location"

GET "/my/heartbeat/location" <-- intercepted by stethoscope

See Also:



45
46
47
# File 'lib/stethoscope.rb', line 45

def self.url=(url)
  @url = url
end

Instance Method Details

#call(env) ⇒ Object



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/stethoscope.rb', line 136

def call(env)
  request = Rack::Request.new(env)
  return @app.call(env) unless check_heartbeat?(request.path)
  responses = Hash.new do |h,k|
    h[k] = {:status => 200}
  end

  _checks_ = checks_to_run(request.path)

  _checks_.each do |name, check|
    begin
      check.call(responses[name])
    rescue => e
      responses[name][:error]  = e
      responses[name][:status] = 500
    end
  end

  status = responses.any?{ |k,v| v[:status] && !((200..299).include?(v[:status])) } ? 500 : 200
  _format = format(request.path)

  headers = { 'Content-Type' => 'text/html' }

  case format(request.path)
  when :html
    result = Stethoscope.template.render(Object.new, :checks => responses)
  when :json
    result = {:checks => responses, :status => status}.to_json
    headers['Content-Type'] = 'application/json'
  end

  Rack::Response.new(result, status, headers).finish
end