Class: Stethoscope
- Inherits:
-
Object
- Object
- Stethoscope
- Defined in:
- lib/stethoscope.rb,
lib/stethoscope/rails.rb,
lib/stethoscope/version.rb
Overview
Adds the heartbeat to the stack
Defined Under Namespace
Constant Summary collapse
- VERSION =
"0.2.3"
Class Method Summary collapse
- .buckets ⇒ Object
-
.check(name, *_buckets_, &blk) ⇒ Object
Add a check to Stethoscope.
-
.checks ⇒ Object
The collection of checks currently in place in Stethoscope.
-
.clear_checks ⇒ Object
Clears all defined checks.
-
.remove_check(name) ⇒ Object
Removes a give check.
-
.template ⇒ Object
Getter for the Tilt template for heartbeat rendering By default, the Stethoscope default template is used.
-
.template=(template) ⇒ Object
Sets the Tilt template that will be used for heartbeat rendering.
-
.url ⇒ Object
The current url that Stethoscope is setup to listen for.
-
.url=(url) ⇒ Object
Set the url to check for the heartbeat in this application.
Instance Method Summary collapse
- #call(env) ⇒ Object
-
#initialize(app) ⇒ Stethoscope
constructor
A new instance of Stethoscope.
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
.buckets ⇒ Object
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
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 |
.checks ⇒ Object
The collection of checks currently in place in Stethoscope
59 60 61 |
# File 'lib/stethoscope.rb', line 59 def self.checks @checks ||= {} end |
.clear_checks ⇒ Object
Clears all defined checks
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
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 |
.template ⇒ Object
Getter for the Tilt template for heartbeat rendering By default, the Stethoscope default template is used. Overwrite this to use a custom template
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
118 119 120 |
# File 'lib/stethoscope.rb', line 118 def self.template=(template) @template = template end |
.url ⇒ Object
The current url that Stethoscope is setup to listen for
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
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 |