stethoscope 0.2.2 → 0.2.3

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -1,3 +1,9 @@
1
1
  pkg/*
2
2
  *.gem
3
- .bundle
3
+
4
+ /.bundle
5
+ bundle/
6
+ .DS_Store
7
+ /.env
8
+
9
+ .rvmrc
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- stethoscope (0.2.1)
4
+ stethoscope (0.2.3)
5
5
  dictionary (>= 1.0)
6
6
  rack (> 1.0)
7
7
  tilt (>= 1.0)
@@ -17,8 +17,7 @@ GEM
17
17
  rack-test (0.5.4)
18
18
  rack (>= 1.0)
19
19
  rake (0.8.7)
20
- rspec-core (2.0.0.beta.20)
21
- tilt (1.3.4)
20
+ tilt (1.3.5)
22
21
 
23
22
  PLATFORMS
24
23
  ruby
@@ -30,5 +29,4 @@ DEPENDENCIES
30
29
  nanotest_extensions
31
30
  rack-test
32
31
  rake
33
- rspec-core (>= 2.0.0.beta.20)
34
32
  stethoscope!
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # Stethoscope
2
2
 
3
- Stethoscope is Rack Middelware that provides heartbeats for your application. Heartbeats are used to check that your application is functioning correctly.
3
+ Stethoscope is Rack Middleware that provides heartbeats for your application. Heartbeats are used to check that your application is functioning correctly.
4
4
 
5
5
  Typically, a tool like Nagios will monitor a heartbeat URL which will return a 200 OK status if everything is ok, or a 500 response for any issues.
6
6
 
@@ -18,7 +18,7 @@ Typically, a tool like Nagios will monitor a heartbeat URL which will return a 2
18
18
 
19
19
  ### Rails 3
20
20
 
21
- Just require Stethoscope in your application. Stethoscope has a Railtie that will configure Stethoscope to work
21
+ Just require Stethoscope in your application. Stethoscope has a Railtie that will configure Stethoscope to work.
22
22
 
23
23
  ## Customizing Stethoscope
24
24
 
@@ -57,6 +57,24 @@ Returning a response _:status_ outside 200..299 will trigger Stethoscope to retu
57
57
 
58
58
  Any exceptions are caught and added to the response with the _:error_ key. The template can then handle them appropriately
59
59
 
60
+ Checks can be placed into buckets to target specific checks.
61
+
62
+ Stethoscope.check :database, :critical, :quick do |response|
63
+ ActiveRecord::Base.connection.execute("select 1")
64
+ end
65
+
66
+ Stethoscope.check :something_big, :critical, :expensive do |response|
67
+ do_something_expensive
68
+ end
69
+
70
+ Now you have the put into buckets, you can choose which checks you want to execute. By default, all check are performed, but if you want to check only one type of check (all the checks in a given bucket) then you can just append the bucket name onto the url.
71
+
72
+ #### Example
73
+
74
+ curl http://my.awesomeapp.com/heartbeat/critical.json # check the critical checks only
75
+ curl http://my.awesomeapp.com/heartbeat/quick.json # check the quick checks only
76
+ curl http://my.awesomeapp.com/heartbeat.json # check all the things
77
+
60
78
  #### Defaults
61
79
 
62
80
  * ActiveRecord
@@ -85,3 +103,21 @@ By default, Stethoscope provides a simple template to render the responses of th
85
103
  You can overwrite the template used:
86
104
 
87
105
  Stethoscope.template = Tilt.new("my_new_tempalte_file.haml")
106
+
107
+
108
+ ## Testing
109
+
110
+ _How do I run the project's automated tests?_
111
+
112
+ ### Run Tests
113
+
114
+ * `bundle exec rake test`
115
+
116
+ ## Contributing changes
117
+
118
+ * Fork it.
119
+ * Create a branch (git checkout -b my_changes)
120
+ * Commit your changes (git commit -am "Added Some Changes")
121
+ * Push to the branch (git push origin my_changes)
122
+ * Open a Pull Request
123
+ * Enjoy a refreshing Dr. Pepper© and wait
@@ -2,6 +2,7 @@ require 'dictionary'
2
2
  require 'tilt'
3
3
  require 'json'
4
4
  require 'stethoscope/rails'
5
+ require 'set'
5
6
 
6
7
  # Stethoscope is Rack middleware that provides a heartbeat function to an application.
7
8
  #
@@ -16,6 +17,22 @@ require 'stethoscope/rails'
16
17
  #
17
18
  # @see Rack
18
19
  class Stethoscope
20
+ class Buckets < Hash
21
+ def []=(key,val)
22
+ super(key.to_s, val)
23
+ end
24
+
25
+ def [](key)
26
+ super(key.to_s)
27
+ end
28
+ end
29
+
30
+ @buckets = Buckets.new { |h, k| h[k.to_s] = Set.new }
31
+
32
+ def self.buckets
33
+ @buckets
34
+ end
35
+
19
36
  # Set the url to check for the heartbeat in this application
20
37
  #
21
38
  # @example
@@ -40,7 +57,7 @@ class Stethoscope
40
57
  # @see Stethoscope.check
41
58
  # @api public
42
59
  def self.checks
43
- @checks ||= Dictionary.new
60
+ @checks ||= {}
44
61
  end
45
62
 
46
63
  # Add a check to Stethoscope
@@ -63,7 +80,10 @@ class Stethoscope
63
80
  #
64
81
  # @see Stethoscope.check
65
82
  # @api public
66
- def self.check(name, &blk)
83
+ def self.check(name, *_buckets_, &blk)
84
+ _buckets_.each do |bucket|
85
+ buckets[bucket] << name
86
+ end
67
87
  checks[name] = blk
68
88
  end
69
89
 
@@ -74,6 +94,9 @@ class Stethoscope
74
94
  # @see Stethoscope.check
75
95
  # @api public
76
96
  def self.remove_check(name)
97
+ buckets.each do |bucket, _checks_|
98
+ _checks_.delete(name)
99
+ end
77
100
  checks.delete(name)
78
101
  end
79
102
 
@@ -81,6 +104,7 @@ class Stethoscope
81
104
  # @see Stethoscope.check
82
105
  # @api public
83
106
  def self.clear_checks
107
+ buckets.clear
84
108
  checks.clear
85
109
  end
86
110
 
@@ -116,7 +140,9 @@ class Stethoscope
116
140
  h[k] = {:status => 200}
117
141
  end
118
142
 
119
- Stethoscope.checks.each do |name, check|
143
+ _checks_ = checks_to_run(request.path)
144
+
145
+ _checks_.each do |name, check|
120
146
  begin
121
147
  check.call(responses[name])
122
148
  rescue => e
@@ -142,12 +168,27 @@ class Stethoscope
142
168
  end
143
169
 
144
170
  private
171
+ def checks_to_run(path)
172
+ bucket = find_bucket_to_check(path)
173
+ return Stethoscope.checks if bucket.nil?
174
+ Stethoscope.buckets[bucket].inject({}){|h, name| h[name] = Stethoscope.checks[name]; h}
175
+ end
176
+
177
+ def find_bucket_to_check(path)
178
+ path =~ %r|#{self.class.url}\/([^\/\.]+)(\.(json))?$|
179
+ $1
180
+ end
181
+
145
182
  def check_heartbeat?(path)
146
- path == self.class.url || path == (self.class.url + '.json')
183
+ regexps = [ %r|#{self.class.url}(\.json)?| ]
184
+ self.class.buckets.each do |name, _|
185
+ regexps << %r|#{File.join(self.class.url, name)}(\.json)|
186
+ end
187
+ regexps.any?{|r| path =~ r}
147
188
  end
148
189
 
149
190
  def format(path)
150
- return :json if path == (self.class.url + '.json')
191
+ return :json if path =~ /\.json$/
151
192
  :html
152
193
  end
153
194
  end
@@ -1,7 +1,7 @@
1
1
  require 'stethoscope'
2
2
 
3
3
  # Provides a check for active record databases
4
- Stethoscope.check :database do |response|
4
+ Stethoscope.check :database, :critical do |response|
5
5
  query = "SELECT 1"
6
6
  response["query"] = query.inspect
7
7
  ActiveRecord::Base.connection.execute(query)
@@ -1,7 +1,7 @@
1
1
  require 'stethoscope'
2
2
 
3
3
  # Provides a check for datamapper databases
4
- Stethoscope.check :database do |response|
4
+ Stethoscope.check :database, :critical do |response|
5
5
  query = "SELECT 1"
6
6
  response["query"] = query.inspect
7
7
  DataMapper.repository.adapter.execute(query)
@@ -1,7 +1,7 @@
1
1
  require 'stethoscope'
2
2
 
3
3
  # Provides a check for mongoid databases
4
- Stethoscope.check :database do |response|
4
+ Stethoscope.check :database, :critical do |response|
5
5
  collection_names = Mongoid.database.collection_names
6
6
  response["collection count"] = collection_names.size
7
7
  response["Mongoid"] = "OK"
@@ -1,7 +1,7 @@
1
1
  require 'stethoscope'
2
2
 
3
3
  # Provides a check for mongoid databases
4
- Stethoscope.check :database do |response|
4
+ Stethoscope.check :database, :critical do |response|
5
5
  collections = Mongoid::Sessions.default.collections
6
6
  response["collection count"] = collections.size
7
7
  response["Mongoid"] = "OK"
@@ -1,3 +1,3 @@
1
1
  class Stethoscope
2
- VERSION = "0.2.2"
2
+ VERSION = "0.2.3"
3
3
  end
@@ -7,7 +7,7 @@ Gem::Specification.new do |s|
7
7
  s.platform = Gem::Platform::RUBY
8
8
  s.authors = ['Daniel Neighman']
9
9
  s.email = ['has.sox@gmail.com']
10
- s.homepage = "http://rubygems.org/gems/stethoscope"
10
+ s.homepage = "http://github.com/hassox/stethoscope"
11
11
  s.summary = "Heartbeat middleware for responding to heartbeat pings"
12
12
  s.description = "Heartbeat middleware for responding to heartbeat pings"
13
13
 
@@ -19,7 +19,6 @@ Gem::Specification.new do |s|
19
19
  s.add_dependency "tilt", ">= 1.0"
20
20
 
21
21
  s.add_development_dependency "bundler", ">= 1.0.0"
22
- s.add_development_dependency "rspec-core", ">= 2.0.0.beta.20"
23
22
  s.add_development_dependency "nanotest"
24
23
  s.add_development_dependency "nanotest_extensions"
25
24
  s.add_development_dependency "rake"
@@ -101,6 +101,29 @@ class TestStethoscope
101
101
  response = get "/heartbeat"
102
102
  assert { response.status == 500 }
103
103
  end
104
+
105
+ test do
106
+ Stethoscope.check(:foo, :foo, :bar){ |response| response[:status] = 200; response[:foo] = :test1 }
107
+ Stethoscope.check(:bar, :bar){ |response| response[:status] = 200; response[:bar] = :test2 }
108
+
109
+ response = get "/heartbeat/foo.json"
110
+ results = JSON.parse(response.body)
111
+ assert { response.status == 200 }
112
+ assert { results['checks']['foo']['foo'] == 'test1' }
113
+ assert { !results['checks'].key?('bar') }
114
+
115
+ response = get "/heartbeat/bar.json"
116
+ results = JSON.parse(response.body)
117
+ assert { response.status == 200 }
118
+ assert { results['checks']['foo']['foo'] == 'test1' }
119
+ assert { results['checks']['bar']['bar'] == 'test2' }
120
+
121
+ response = get "/heartbeat.json"
122
+ results = JSON.parse(response.body)
123
+ assert { response.status == 200 }
124
+ assert { results['checks']['foo']['foo'] == 'test1' }
125
+ assert { results['checks']['bar']['bar'] == 'test2' }
126
+ end
104
127
  end
105
128
  end
106
129
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: stethoscope
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.2.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-03-13 00:00:00.000000000 Z
12
+ date: 2013-03-14 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rack
@@ -75,22 +75,6 @@ dependencies:
75
75
  - - ! '>='
76
76
  - !ruby/object:Gem::Version
77
77
  version: 1.0.0
78
- - !ruby/object:Gem::Dependency
79
- name: rspec-core
80
- requirement: !ruby/object:Gem::Requirement
81
- none: false
82
- requirements:
83
- - - ! '>='
84
- - !ruby/object:Gem::Version
85
- version: 2.0.0.beta.20
86
- type: :development
87
- prerelease: false
88
- version_requirements: !ruby/object:Gem::Requirement
89
- none: false
90
- requirements:
91
- - - ! '>='
92
- - !ruby/object:Gem::Version
93
- version: 2.0.0.beta.20
94
78
  - !ruby/object:Gem::Dependency
95
79
  name: nanotest
96
80
  requirement: !ruby/object:Gem::Requirement
@@ -179,7 +163,6 @@ extensions: []
179
163
  extra_rdoc_files: []
180
164
  files:
181
165
  - .gitignore
182
- - .rvmrc
183
166
  - Gemfile
184
167
  - Gemfile.lock
185
168
  - LICENSE
@@ -197,7 +180,7 @@ files:
197
180
  - stethoscope.gemspec
198
181
  - test/test_helper.rb
199
182
  - test/test_stethoscope.rb
200
- homepage: http://rubygems.org/gems/stethoscope
183
+ homepage: http://github.com/hassox/stethoscope
201
184
  licenses: []
202
185
  post_install_message:
203
186
  rdoc_options: []
@@ -209,6 +192,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
209
192
  - - ! '>='
210
193
  - !ruby/object:Gem::Version
211
194
  version: '0'
195
+ segments:
196
+ - 0
197
+ hash: -4133514826087754687
212
198
  required_rubygems_version: !ruby/object:Gem::Requirement
213
199
  none: false
214
200
  requirements:
@@ -217,9 +203,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
217
203
  version: 1.3.6
218
204
  requirements: []
219
205
  rubyforge_project: stethoscope
220
- rubygems_version: 1.8.24
206
+ rubygems_version: 1.8.23
221
207
  signing_key:
222
208
  specification_version: 3
223
209
  summary: Heartbeat middleware for responding to heartbeat pings
224
210
  test_files: []
225
- has_rdoc:
data/.rvmrc DELETED
@@ -1,7 +0,0 @@
1
-
2
- if [[ -d "${rvm_path:-$HOME/.rvm}/environments" \
3
- && -s "${rvm_path:-$HOME/.rvm}/environments/ruby-1.8.7-p302@stethoscope" ]] ; then
4
- \. "${rvm_path:-$HOME/.rvm}/environments/ruby-1.8.7-p302@stethoscope"
5
- else
6
- rvm --create use "ruby-1.8.7-p302@stethoscope"
7
- fi