cogent-healthcheck 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +14 -0
- data/.rspec +2 -0
- data/.travis.yml +3 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +44 -0
- data/Rakefile +2 -0
- data/healthcheck.gemspec +24 -0
- data/lib/healthcheck.rb +26 -0
- data/lib/healthcheck/check.rb +60 -0
- data/lib/healthcheck/checks/alive.rb +31 -0
- data/lib/healthcheck/configuration.rb +28 -0
- data/lib/healthcheck/version.rb +3 -0
- data/spec/healthcheck_spec.rb +38 -0
- data/spec/spec_helper.rb +2 -0
- metadata +103 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 1ecbf7b77655855e459bb6977bde06dd0657d273
|
4
|
+
data.tar.gz: 337646fd147f89d13b878bf20bbd89b0df54df01
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 8b141840f06a806fc0e3a85d5466dfb24b746234891857f2fab320ad42b3fdefb15794556dd2244e66aa9c6ac887c9b3cfa3764f600b2907da9f3b8f93ea0fdd
|
7
|
+
data.tar.gz: 0b4e45a9738b0b43344086e2a2575d227b1001e95a9551c1ba4690bc4a413f5c2df03253a84c2b562db45ed3cf152583f664f141548160b3f0272da0f7fef466
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2015 Rob Mitchell
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
# Healthcheck
|
2
|
+
|
3
|
+
Provides a mechanism to add healthchecks to your application as a Rack endpoint.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'cogent-healthcheck', require: 'healthcheck'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install cogent-healthcheck
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
Add the gem as a rack app to your config.ru file as follows:
|
23
|
+
|
24
|
+
map '/healthcheck' do
|
25
|
+
use Cogent::Healthcheck::Check
|
26
|
+
end
|
27
|
+
using any endpoint you'd like.
|
28
|
+
The default endpoint responds to checks, returning a json list of available checks and their urls. Calling each check will either respond with a 200 if everything is ok, or a 500 with a json representation of the errors.
|
29
|
+
|
30
|
+
##Configuration
|
31
|
+
Implementing your own checks is easy. Simple create a class that has name and description class methods. The name method is used for the endpoint and is required. Implement an execute method, that either returns a valid response or raises an exception in the case of an error.
|
32
|
+
|
33
|
+
Add the check to healthcheck as follows in any config file:
|
34
|
+
Cogent::Healthcheck.configure do |config|
|
35
|
+
config.add_check(Salesforce::Check)
|
36
|
+
end
|
37
|
+
|
38
|
+
## Contributing
|
39
|
+
|
40
|
+
1. Fork it ( https://github.com/[my-github-username]/healthcheck/fork )
|
41
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
42
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
43
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
44
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
data/healthcheck.gemspec
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'healthcheck/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "cogent-healthcheck"
|
8
|
+
spec.version = Healthcheck::VERSION
|
9
|
+
spec.authors = ["Rob Mitchell"]
|
10
|
+
spec.email = ["robm.mitchell@gmail.com"]
|
11
|
+
spec.summary = %q{Healthcheck gem to expose the health of specific parts of your app}
|
12
|
+
spec.description = %q{}
|
13
|
+
spec.homepage = ""
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.7"
|
22
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
23
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
24
|
+
end
|
data/lib/healthcheck.rb
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
require "healthcheck/version"
|
2
|
+
require 'healthcheck/configuration'
|
3
|
+
require 'healthcheck/check'
|
4
|
+
Dir.glob(File.join(File.dirname(__FILE__), '/healthcheck/checks/**/*.rb')).each { |r| require r}
|
5
|
+
|
6
|
+
module Cogent
|
7
|
+
module Healthcheck
|
8
|
+
|
9
|
+
def self.configure(&block)
|
10
|
+
block.call(self.config)
|
11
|
+
end
|
12
|
+
|
13
|
+
def self.initialize_config
|
14
|
+
return if @config
|
15
|
+
@config = Cogent::Healthcheck::Configuration.new
|
16
|
+
@config.add_check(Cogent::Healthcheck::Checks::Alive)
|
17
|
+
end
|
18
|
+
|
19
|
+
def self.config
|
20
|
+
initialize_config
|
21
|
+
@config
|
22
|
+
end
|
23
|
+
|
24
|
+
initialize_config
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,60 @@
|
|
1
|
+
module Cogent
|
2
|
+
|
3
|
+
module Healthcheck
|
4
|
+
|
5
|
+
class Check
|
6
|
+
|
7
|
+
def initialize(app)
|
8
|
+
@app = app
|
9
|
+
end
|
10
|
+
|
11
|
+
def call(env)
|
12
|
+
path_info = env['PATH_INFO']
|
13
|
+
if path_info == '/checks'
|
14
|
+
response('200', checks(env).to_json, env)
|
15
|
+
elsif path_info =~ /\/checks\//
|
16
|
+
check_name = path_info.split('/').last
|
17
|
+
check(check_name, env)
|
18
|
+
else
|
19
|
+
@app.call(env)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
private
|
24
|
+
def check(name, env)
|
25
|
+
check = Cogent::Healthcheck.config.checks.find{|check| check.name == name}
|
26
|
+
begin
|
27
|
+
check.new.execute
|
28
|
+
rescue => e
|
29
|
+
response('500', {error: e.backtrace}.to_json, env)
|
30
|
+
end
|
31
|
+
response('200', '', env)
|
32
|
+
end
|
33
|
+
|
34
|
+
def checks(env)
|
35
|
+
base_uri = [env['rack.url_scheme'], '://',env['HTTP_HOST'], env['REQUEST_PATH']].join
|
36
|
+
checks = Cogent::Healthcheck.config.checks.map do |check|
|
37
|
+
uri = [base_uri, '/', check.name].join
|
38
|
+
{
|
39
|
+
name: check.name,
|
40
|
+
description: check.description,
|
41
|
+
href: uri
|
42
|
+
}
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
def response(status_code, body, env)
|
47
|
+
content_type = 'application/json'
|
48
|
+
if env['QUERY_STRING'] && env['QUERY_STRING'] =~ /callback/
|
49
|
+
query_string = env['QUERY_STRING'].gsub(/callback=/, '')
|
50
|
+
callback = query_string.split('&')[0]
|
51
|
+
body = "#{callback}(#{body})"
|
52
|
+
content_type = 'application/javascript'
|
53
|
+
end
|
54
|
+
[status_code, {'Content-Type' => content_type}, [body]]
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
end
|
59
|
+
|
60
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
module Cogent
|
2
|
+
|
3
|
+
module Healthcheck
|
4
|
+
|
5
|
+
module Checks
|
6
|
+
|
7
|
+
class Alive
|
8
|
+
|
9
|
+
class << self
|
10
|
+
|
11
|
+
def name
|
12
|
+
'alive'
|
13
|
+
end
|
14
|
+
|
15
|
+
def description
|
16
|
+
'Will always return a 200 to check that the site is alive'
|
17
|
+
end
|
18
|
+
|
19
|
+
end
|
20
|
+
|
21
|
+
def execute
|
22
|
+
true
|
23
|
+
end
|
24
|
+
|
25
|
+
end
|
26
|
+
|
27
|
+
end
|
28
|
+
|
29
|
+
end
|
30
|
+
|
31
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
module Cogent
|
2
|
+
|
3
|
+
module Healthcheck
|
4
|
+
|
5
|
+
class Configuration
|
6
|
+
|
7
|
+
attr_reader :checks
|
8
|
+
|
9
|
+
def initialize
|
10
|
+
@checks = []
|
11
|
+
end
|
12
|
+
|
13
|
+
def add_check(check)
|
14
|
+
@checks << check if is_valid?(check)
|
15
|
+
end
|
16
|
+
|
17
|
+
private
|
18
|
+
def is_valid?(check)
|
19
|
+
class_methods = [:name, :description]
|
20
|
+
valid = check.methods & class_methods == class_methods && check.instance_methods.include?(:execute)
|
21
|
+
raise StandardError.new('Check must have name and description class methods as well as an execute instance method') unless valid
|
22
|
+
valid
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
end
|
27
|
+
|
28
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Healthcheck do
|
4
|
+
it 'has a version number' do
|
5
|
+
expect(Healthcheck::VERSION).not_to be nil
|
6
|
+
end
|
7
|
+
|
8
|
+
it "has an alive check by default" do
|
9
|
+
expect(Cogent::Healthcheck.config.checks.first.name).to eql('alive')
|
10
|
+
end
|
11
|
+
|
12
|
+
describe 'configuration' do
|
13
|
+
let(:check) {
|
14
|
+
class Check
|
15
|
+
|
16
|
+
def self.name
|
17
|
+
end
|
18
|
+
|
19
|
+
def self.description
|
20
|
+
end
|
21
|
+
|
22
|
+
def execute
|
23
|
+
end
|
24
|
+
end
|
25
|
+
Check
|
26
|
+
}
|
27
|
+
before do
|
28
|
+
Cogent::Healthcheck.configure do |config|
|
29
|
+
config.add_check(check)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
it "allows new checks to be added" do
|
34
|
+
expect(Cogent::Healthcheck.config.checks).to include(check)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,103 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: cogent-healthcheck
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Rob Mitchell
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-03-10 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.7'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.7'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.0'
|
55
|
+
description: ''
|
56
|
+
email:
|
57
|
+
- robm.mitchell@gmail.com
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- ".gitignore"
|
63
|
+
- ".rspec"
|
64
|
+
- ".travis.yml"
|
65
|
+
- Gemfile
|
66
|
+
- LICENSE.txt
|
67
|
+
- README.md
|
68
|
+
- Rakefile
|
69
|
+
- healthcheck.gemspec
|
70
|
+
- lib/healthcheck.rb
|
71
|
+
- lib/healthcheck/check.rb
|
72
|
+
- lib/healthcheck/checks/alive.rb
|
73
|
+
- lib/healthcheck/configuration.rb
|
74
|
+
- lib/healthcheck/version.rb
|
75
|
+
- spec/healthcheck_spec.rb
|
76
|
+
- spec/spec_helper.rb
|
77
|
+
homepage: ''
|
78
|
+
licenses:
|
79
|
+
- MIT
|
80
|
+
metadata: {}
|
81
|
+
post_install_message:
|
82
|
+
rdoc_options: []
|
83
|
+
require_paths:
|
84
|
+
- lib
|
85
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
91
|
+
requirements:
|
92
|
+
- - ">="
|
93
|
+
- !ruby/object:Gem::Version
|
94
|
+
version: '0'
|
95
|
+
requirements: []
|
96
|
+
rubyforge_project:
|
97
|
+
rubygems_version: 2.2.2
|
98
|
+
signing_key:
|
99
|
+
specification_version: 4
|
100
|
+
summary: Healthcheck gem to expose the health of specific parts of your app
|
101
|
+
test_files:
|
102
|
+
- spec/healthcheck_spec.rb
|
103
|
+
- spec/spec_helper.rb
|