rack-healthcheck 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/.rspec +2 -0
- data/.travis.yml +5 -0
- data/Gemfile +9 -0
- data/LICENSE.txt +21 -0
- data/README.md +142 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/rack/healthcheck/action.rb +30 -0
- data/lib/rack/healthcheck/actions/base.rb +23 -0
- data/lib/rack/healthcheck/actions/complete.rb +42 -0
- data/lib/rack/healthcheck/actions/load_balancer.rb +41 -0
- data/lib/rack/healthcheck/checks/active_record.rb +27 -0
- data/lib/rack/healthcheck/checks/base.rb +43 -0
- data/lib/rack/healthcheck/checks/http_request.rb +42 -0
- data/lib/rack/healthcheck/checks/mongo_db.rb +27 -0
- data/lib/rack/healthcheck/checks/rabbit_mq.rb +35 -0
- data/lib/rack/healthcheck/checks/redis.rb +33 -0
- data/lib/rack/healthcheck/configuration.rb +21 -0
- data/lib/rack/healthcheck/middleware.rb +20 -0
- data/lib/rack/healthcheck/type.rb +13 -0
- data/lib/rack/healthcheck/version.rb +5 -0
- data/lib/rack/healthcheck.rb +15 -0
- data/rack-healthcheck.gemspec +27 -0
- metadata +139 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA1:
|
|
3
|
+
metadata.gz: 4bb98532f3467956bca043943021093ccb905a53
|
|
4
|
+
data.tar.gz: d3fc8bffe52e13ad8a2054534589aa154856e4aa
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 2ab2c3970cb11515bee1ccb6451d2316e001537de14e3b562c0d6d83e79909a5b402e51368c9a564c049f89e84f5aeaf352ebdc6b864a0cce23ab4e6d5013746
|
|
7
|
+
data.tar.gz: 1c367ecca4c550fc084d5010673b6e486e50cb868db47a104c72bdc9d5a0dd90b332b4f9266f1209956a412fc1c4d66f820a24c0f6a8ace77bbefe496cbcb814
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2016 Leandro Maduro
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
|
13
|
+
all copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
[](https://travis-ci.org/downgba/rack-healthcheck)
|
|
2
|
+
[](https://coveralls.io/github/downgba/rack-healthcheck?branch=master)
|
|
3
|
+
|
|
4
|
+
# Rack::Healthcheck
|
|
5
|
+
|
|
6
|
+
Is a middleware that verifies if your app and all resources that it needs is available.
|
|
7
|
+
|
|
8
|
+
## Installation
|
|
9
|
+
|
|
10
|
+
Add this line to your application's Gemfile:
|
|
11
|
+
|
|
12
|
+
```ruby
|
|
13
|
+
gem 'rack-healthcheck'
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
And then execute:
|
|
17
|
+
|
|
18
|
+
$ bundle
|
|
19
|
+
|
|
20
|
+
Or install it yourself as:
|
|
21
|
+
|
|
22
|
+
$ gem install rack-healthcheck
|
|
23
|
+
|
|
24
|
+
## Usage
|
|
25
|
+
|
|
26
|
+
###### Rails
|
|
27
|
+
```ruby
|
|
28
|
+
# config/application.rb
|
|
29
|
+
config.middleware.use Rack::Healthcheck::Middleware
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
###### Sinatra
|
|
33
|
+
```ruby
|
|
34
|
+
# config.ru
|
|
35
|
+
use Rack::Healthcheck::Middleware
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
### Setting up
|
|
39
|
+
|
|
40
|
+
Create an initializer file, where you'll setup all services that you use.
|
|
41
|
+
|
|
42
|
+
```ruby
|
|
43
|
+
Rack::Healthcheck.configure do |config|
|
|
44
|
+
config.app_name = "Your Application"
|
|
45
|
+
config.app_version = 1.0
|
|
46
|
+
config.checks = [
|
|
47
|
+
Rack::Healthcheck::Checks::ActiveRecord.new("Database"),
|
|
48
|
+
Rack::Healthcheck::Checks::RabbitMQ.new(name, config = {
|
|
49
|
+
hosts: [localhost],
|
|
50
|
+
port: 5672,
|
|
51
|
+
user: guest,
|
|
52
|
+
pass: guest
|
|
53
|
+
}),
|
|
54
|
+
Rack::Healthcheck::Checks::Redis.new(name, config = {
|
|
55
|
+
url: "redis://localhost:6379",
|
|
56
|
+
password: "pass"
|
|
57
|
+
})
|
|
58
|
+
Rack::Healthcheck::Checks::HTTPRequest.new(name, config = {
|
|
59
|
+
url: localhost,
|
|
60
|
+
headers: {"Host" => "something"},
|
|
61
|
+
service_type: "INTERNAL_SERVICE",
|
|
62
|
+
expected_result: "LIVE"
|
|
63
|
+
})
|
|
64
|
+
]
|
|
65
|
+
end
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
##### Available checks
|
|
69
|
+
|
|
70
|
+
* Rack::Healthcheck::Checks::ActiveRecord.new(name, config = {})
|
|
71
|
+
* Rack::Healthcheck::Checks::MongoDB.new(name, config = {})
|
|
72
|
+
* Rack::Healthcheck::Checks::RabbitMQ.new(name, config = {})
|
|
73
|
+
* Rack::Healthcheck::Checks::Redis.new(name, config = {})
|
|
74
|
+
* Rack::Healthcheck::Checks::HTTPRequest.new(name, config = {})
|
|
75
|
+
|
|
76
|
+
You can inform if one of your checks is optional, so this check will be disregarded in final result.
|
|
77
|
+
To do that you just need to pass a hash with `:optional` key.
|
|
78
|
+
|
|
79
|
+
```ruby
|
|
80
|
+
Rack::Healthcheck::Checks::ActiveRecord.new("Test", {optional: true})
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
If you want to display the service url in final result, you need to pass it in hash config too.
|
|
84
|
+
|
|
85
|
+
```ruby
|
|
86
|
+
Rack::Healthcheck::Checks::ActiveRecord.new("Test", {optional: true, url: "http://myservice.com/healthcheck"})
|
|
87
|
+
```
|
|
88
|
+
### Routes
|
|
89
|
+
|
|
90
|
+
By default this gem creates two routes `/healthcheck` and `/healthcheck/complete`.
|
|
91
|
+
|
|
92
|
+
The `/healthcheck` route doesn't verify the services used by application. This is the fastest way to tell to load balancer if it should or not keep the machine in the pool.
|
|
93
|
+
These are the available HTTP methods for this route:
|
|
94
|
+
|
|
95
|
+
* GET -> Returns the current status (LIVE|DEAD)
|
|
96
|
+
* POST -> Changes the current status to LIVE
|
|
97
|
+
* DELETE -> Changes the current status to DEAD
|
|
98
|
+
|
|
99
|
+
The `/healthcheck/complete` performs all configured checks and returns a JSON with a lot of informations (elapsed time, status for each service, global status considering only required services).
|
|
100
|
+
Example:
|
|
101
|
+
```json
|
|
102
|
+
{
|
|
103
|
+
"name": "My application",
|
|
104
|
+
"status": true,
|
|
105
|
+
"version": 1.0,
|
|
106
|
+
"checks": [
|
|
107
|
+
{
|
|
108
|
+
"name": "Oracle database",
|
|
109
|
+
"type": "DATABASE",
|
|
110
|
+
"status": true,
|
|
111
|
+
"optional": false,
|
|
112
|
+
"time": 0.036443
|
|
113
|
+
}
|
|
114
|
+
]
|
|
115
|
+
}
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
##### Changing default routes
|
|
119
|
+
|
|
120
|
+
If you want to change the default route, you just need to pass the new route to the middleware
|
|
121
|
+
|
|
122
|
+
```ruby
|
|
123
|
+
# For rails
|
|
124
|
+
# config/application.rb
|
|
125
|
+
config.middleware.use Rack::Healthcheck::Middleware, "/myroute"
|
|
126
|
+
# or
|
|
127
|
+
# For sinatra
|
|
128
|
+
# config.ru
|
|
129
|
+
use Rack::Healthcheck::Middleware, "/myroute"
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
## Contributing
|
|
133
|
+
|
|
134
|
+
1. Fork it ( https://github.com/downgba/rack-healthcheck/fork )
|
|
135
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
|
136
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
|
137
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
|
138
|
+
5. Create a new Pull Request
|
|
139
|
+
|
|
140
|
+
## License
|
|
141
|
+
|
|
142
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/bin/console
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
#!/usr/bin/env ruby
|
|
2
|
+
|
|
3
|
+
require "bundler/setup"
|
|
4
|
+
require "rack/healthcheck"
|
|
5
|
+
|
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
|
8
|
+
|
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
|
10
|
+
# require "pry"
|
|
11
|
+
# Pry.start
|
|
12
|
+
|
|
13
|
+
require "irb"
|
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
require "rack/healthcheck/actions/load_balancer"
|
|
2
|
+
require "rack/healthcheck/actions/complete"
|
|
3
|
+
|
|
4
|
+
module Rack::Healthcheck
|
|
5
|
+
class Action
|
|
6
|
+
class InvalidAction < Exception; end;
|
|
7
|
+
|
|
8
|
+
@mount_at = "healthcheck"
|
|
9
|
+
|
|
10
|
+
class << self
|
|
11
|
+
attr_accessor :mount_at
|
|
12
|
+
|
|
13
|
+
def get(path, request_method)
|
|
14
|
+
raise InvalidAction.new("Unknown action") unless available_actions.has_key?(path)
|
|
15
|
+
|
|
16
|
+
available_actions[path].send(:new, path, request_method)
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def available_actions
|
|
20
|
+
route = @mount_at.gsub(/^\//, "")
|
|
21
|
+
{
|
|
22
|
+
"/#{route}" => Rack::Healthcheck::Actions::LoadBalancer,
|
|
23
|
+
"/#{route}/complete" => Rack::Healthcheck::Actions::Complete
|
|
24
|
+
}
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
private_class_method :new
|
|
29
|
+
end
|
|
30
|
+
end
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
module Rack::Healthcheck::Actions
|
|
2
|
+
class Base
|
|
3
|
+
class InvalidRequestMethod < Exception; end;
|
|
4
|
+
|
|
5
|
+
VALID_REQUEST_METHODS = [:get].freeze
|
|
6
|
+
|
|
7
|
+
attr_accessor :path, :request_method
|
|
8
|
+
|
|
9
|
+
def initialize(path, request_method)
|
|
10
|
+
method = request_method.downcase.to_sym
|
|
11
|
+
raise InvalidRequestMethod.new("Method not allowed") unless valid_request_method?(method)
|
|
12
|
+
|
|
13
|
+
@path = path
|
|
14
|
+
@request_method = method
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
protected
|
|
18
|
+
|
|
19
|
+
def valid_request_method?(method)
|
|
20
|
+
self.class::VALID_REQUEST_METHODS.include?(method)
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
end
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
require "json"
|
|
2
|
+
require "rack/healthcheck/actions/base"
|
|
3
|
+
require "rack/healthcheck/checks/active_record"
|
|
4
|
+
require "rack/healthcheck/checks/mongo_db"
|
|
5
|
+
require "rack/healthcheck/checks/rabbit_mq"
|
|
6
|
+
require "rack/healthcheck/checks/redis"
|
|
7
|
+
require "rack/healthcheck/checks/http_request"
|
|
8
|
+
|
|
9
|
+
module Rack::Healthcheck::Actions
|
|
10
|
+
class Complete < Base
|
|
11
|
+
def get
|
|
12
|
+
perform
|
|
13
|
+
['200', {'Content-Type' => 'application/json'}, [result.to_json]]
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
private
|
|
17
|
+
|
|
18
|
+
def result
|
|
19
|
+
results = []
|
|
20
|
+
status = true
|
|
21
|
+
Rack::Healthcheck.configuration.checks.each do |check|
|
|
22
|
+
status = (status == true && check.keep_in_pool?)
|
|
23
|
+
results << check.to_hash
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
{
|
|
27
|
+
name: Rack::Healthcheck.configuration.app_name,
|
|
28
|
+
status: status,
|
|
29
|
+
version: Rack::Healthcheck.configuration.app_version,
|
|
30
|
+
checks: results
|
|
31
|
+
}
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
def perform
|
|
35
|
+
threads = Rack::Healthcheck.configuration.checks.map do |check|
|
|
36
|
+
Thread.new { check.run }
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
threads.each(&:join)
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
end
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
require "rack/healthcheck/actions/base"
|
|
2
|
+
|
|
3
|
+
module Rack::Healthcheck::Actions
|
|
4
|
+
class LoadBalancer < Base
|
|
5
|
+
LIVE = "LIVE"
|
|
6
|
+
DEAD = "DEAD"
|
|
7
|
+
VALID_REQUEST_METHODS = [:get, :post, :delete].freeze
|
|
8
|
+
|
|
9
|
+
@status = LIVE
|
|
10
|
+
class << self
|
|
11
|
+
attr_accessor :status
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
# Returns the current status to load balancer
|
|
15
|
+
# When status is LIVE the load balancer keeps the machine in pool
|
|
16
|
+
# When status is DEAD the load balancer removes the machine from pool
|
|
17
|
+
def get
|
|
18
|
+
response
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
# Change current status to LIVE
|
|
22
|
+
# This method is used to add the machine in pool
|
|
23
|
+
def post
|
|
24
|
+
self.class.status = LIVE
|
|
25
|
+
response
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
# Change current status to DEAD
|
|
29
|
+
# This method is used to remove the machine from pool
|
|
30
|
+
def delete
|
|
31
|
+
self.class.status = DEAD
|
|
32
|
+
response
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
private
|
|
36
|
+
|
|
37
|
+
def response
|
|
38
|
+
['200', {'Content-Type' => 'text/plain'}, [self.class.status]]
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
end
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
require "rack/healthcheck/checks/base"
|
|
2
|
+
require "rack/healthcheck/type"
|
|
3
|
+
|
|
4
|
+
module Rack::Healthcheck::Checks
|
|
5
|
+
class ActiveRecord < Base
|
|
6
|
+
# @param name [String]
|
|
7
|
+
# @param config [Hash<Symbol, Object>] Hash with optional configs
|
|
8
|
+
# @example
|
|
9
|
+
# name = Database
|
|
10
|
+
# config {
|
|
11
|
+
# optional: false,
|
|
12
|
+
# url: "mydatabase.com"
|
|
13
|
+
# }
|
|
14
|
+
def initialize(name, config = {})
|
|
15
|
+
super(name, Rack::Healthcheck::Type::DATABASE, config[:optional], config[:url])
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
private
|
|
19
|
+
|
|
20
|
+
def check
|
|
21
|
+
::ActiveRecord::Migrator.current_version
|
|
22
|
+
@status = true
|
|
23
|
+
rescue Exception => e
|
|
24
|
+
@status = false
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
end
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
module Rack::Healthcheck::Checks
|
|
2
|
+
class Base
|
|
3
|
+
class InvalidType < Exception; end;
|
|
4
|
+
|
|
5
|
+
attr_accessor :name, :optional, :url
|
|
6
|
+
attr_reader :type, :status, :elapsed_time
|
|
7
|
+
|
|
8
|
+
def initialize(name, type, optional, url)
|
|
9
|
+
raise InvalidType.new("Type must be one of these options #{Rack::Healthcheck::Type::ALL.join(", ")}") unless Rack::Healthcheck::Type::ALL.include?(type)
|
|
10
|
+
|
|
11
|
+
@name = name
|
|
12
|
+
@optional = optional || false
|
|
13
|
+
@url = url
|
|
14
|
+
@type = type
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def run
|
|
18
|
+
start = Time.now
|
|
19
|
+
check
|
|
20
|
+
@elapsed_time = Time.now - start
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def to_hash
|
|
24
|
+
{
|
|
25
|
+
name: name,
|
|
26
|
+
type: type,
|
|
27
|
+
status: status,
|
|
28
|
+
optional: optional,
|
|
29
|
+
time: elapsed_time,
|
|
30
|
+
url: url
|
|
31
|
+
}.reject{ |key, value| value.nil? }
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
def keep_in_pool?
|
|
35
|
+
(!optional && status == true) || optional
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
private
|
|
39
|
+
|
|
40
|
+
def check
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
end
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
require "rack/healthcheck/checks/base"
|
|
2
|
+
require "rack/healthcheck/type"
|
|
3
|
+
require "uri"
|
|
4
|
+
require "net/http"
|
|
5
|
+
|
|
6
|
+
module Rack::Healthcheck::Checks
|
|
7
|
+
class HTTPRequest < Base
|
|
8
|
+
class InvalidURL < Exception; end;
|
|
9
|
+
|
|
10
|
+
attr_reader :config
|
|
11
|
+
|
|
12
|
+
# @param name [String]
|
|
13
|
+
# @param config [Hash<Symbol, Object>] Hash with configs
|
|
14
|
+
# @example
|
|
15
|
+
# name = Ceph or Another system
|
|
16
|
+
# config = {
|
|
17
|
+
# url: localhost,
|
|
18
|
+
# headers: {"Host" => "something"},
|
|
19
|
+
# service_type: "INTERNAL_SERVICE",
|
|
20
|
+
# expected_result: "LIVE",
|
|
21
|
+
# optional: true
|
|
22
|
+
# }
|
|
23
|
+
# @see Rack::Healthcheck::Type
|
|
24
|
+
def initialize(name, config)
|
|
25
|
+
raise InvalidURL.new("Expected :url to be a http or https endpoint") if config[:url].match(/^(http:\/\/|https:\/\/)/).nil?
|
|
26
|
+
|
|
27
|
+
super(name, config[:service_type], config[:optional], config[:url])
|
|
28
|
+
@config = config
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
private
|
|
32
|
+
|
|
33
|
+
def check
|
|
34
|
+
uri = URI(url)
|
|
35
|
+
http = Net::HTTP.new(uri.host)
|
|
36
|
+
response = http.get(uri.path, config[:headers])
|
|
37
|
+
@status = response.body.gsub(/\n/, '') == config[:expected_result]
|
|
38
|
+
rescue Exception => e
|
|
39
|
+
@status = false
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
end
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
require "rack/healthcheck/checks/base"
|
|
2
|
+
require "rack/healthcheck/type"
|
|
3
|
+
|
|
4
|
+
module Rack::Healthcheck::Checks
|
|
5
|
+
class MongoDB < Base
|
|
6
|
+
# @param name [String]
|
|
7
|
+
# @param config [Hash<Symbol, Object>] Hash with optional configs
|
|
8
|
+
# @example
|
|
9
|
+
# name = Database
|
|
10
|
+
# config {
|
|
11
|
+
# optional: false,
|
|
12
|
+
# url: "mymongodb.com"
|
|
13
|
+
# }
|
|
14
|
+
def initialize(name, config = {})
|
|
15
|
+
super(name, Rack::Healthcheck::Type::DATABASE, config[:optional], config[:url])
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
private
|
|
19
|
+
|
|
20
|
+
def check
|
|
21
|
+
Mongoid::Sessions.with_name(:default).command(dbStats: 1)["db"]
|
|
22
|
+
@status = true
|
|
23
|
+
rescue Exception => e
|
|
24
|
+
@status = false
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
end
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
require "rack/healthcheck/checks/base"
|
|
2
|
+
require "rack/healthcheck/type"
|
|
3
|
+
|
|
4
|
+
module Rack::Healthcheck::Checks
|
|
5
|
+
class RabbitMQ < Base
|
|
6
|
+
attr_reader :config
|
|
7
|
+
|
|
8
|
+
# @param name [String]
|
|
9
|
+
# @param config [Hash<Symbol,Object>] Hash with configs
|
|
10
|
+
# @example
|
|
11
|
+
# name = RabbitMQ
|
|
12
|
+
# config = {
|
|
13
|
+
# hosts: [localhost],
|
|
14
|
+
# port: 5672,
|
|
15
|
+
# user: guest,
|
|
16
|
+
# pass: guest,
|
|
17
|
+
# optional: true
|
|
18
|
+
# }
|
|
19
|
+
def initialize(name, config)
|
|
20
|
+
super(name, Rack::Healthcheck::Type::MESSAGING, config[:optional], config[:hosts])
|
|
21
|
+
@config = config
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
private
|
|
25
|
+
|
|
26
|
+
def check
|
|
27
|
+
connection = Bunny.new(config)
|
|
28
|
+
connection.start
|
|
29
|
+
connection.close
|
|
30
|
+
@status = true
|
|
31
|
+
rescue Exception => e
|
|
32
|
+
@status = false
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
end
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
require "rack/healthcheck/checks/base"
|
|
2
|
+
require "rack/healthcheck/type"
|
|
3
|
+
|
|
4
|
+
module Rack::Healthcheck::Checks
|
|
5
|
+
class Redis < Base
|
|
6
|
+
attr_reader :config
|
|
7
|
+
|
|
8
|
+
# @param name [String]
|
|
9
|
+
# @param config [Hash<Symbol,String>] Hash with configs
|
|
10
|
+
# @param optional [Boolean] Flag used to inform if this service is optional
|
|
11
|
+
# @example
|
|
12
|
+
# name = Redis
|
|
13
|
+
# config = {
|
|
14
|
+
# url: "redis://localhost:6379",
|
|
15
|
+
# password: "pass",
|
|
16
|
+
# optional: true
|
|
17
|
+
# }
|
|
18
|
+
def initialize(name, config)
|
|
19
|
+
super(name, Rack::Healthcheck::Type::CACHE, config[:optional], config[:url])
|
|
20
|
+
@config = config
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
private
|
|
24
|
+
|
|
25
|
+
def check
|
|
26
|
+
redis = ::Redis.new(config)
|
|
27
|
+
redis.info
|
|
28
|
+
@status = true
|
|
29
|
+
rescue Exception => e
|
|
30
|
+
@status = false
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
end
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
module Rack::Healthcheck
|
|
2
|
+
# This class is responsible by keep all services that the healthcheck needs to verify
|
|
3
|
+
#
|
|
4
|
+
# app_name [String] This value is used only in complete check result
|
|
5
|
+
# app_version [String] This value is used only in complete check result
|
|
6
|
+
# checks [Array] Array that contains all services that healthcheck needs to verify
|
|
7
|
+
# @example
|
|
8
|
+
# app_name = "Rack Healthcheck"
|
|
9
|
+
# app_version = 1.0
|
|
10
|
+
# checks = [
|
|
11
|
+
# Rack::Healthcheck::Checks::ActiveRecord.new("database"),
|
|
12
|
+
# Rack::Healthcheck::Checks::Redis.new("redis", {})
|
|
13
|
+
# ]
|
|
14
|
+
class Configuration
|
|
15
|
+
attr_accessor :app_name, :app_version, :checks
|
|
16
|
+
|
|
17
|
+
def checks
|
|
18
|
+
@checks || {}
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
end
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
require "rack/healthcheck/action"
|
|
2
|
+
|
|
3
|
+
module Rack::Healthcheck
|
|
4
|
+
class Middleware
|
|
5
|
+
def initialize(app, mount_at = nil)
|
|
6
|
+
@app = app
|
|
7
|
+
Rack::Healthcheck::Action.mount_at = mount_at unless mount_at.nil?
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
def call(env)
|
|
11
|
+
path = env["PATH_INFO"]
|
|
12
|
+
request_method = env["REQUEST_METHOD"]
|
|
13
|
+
|
|
14
|
+
action = Rack::Healthcheck::Action.get(path, request_method)
|
|
15
|
+
action.send(request_method.downcase)
|
|
16
|
+
rescue Rack::Healthcheck::Action::InvalidAction, Rack::Healthcheck::Actions::Base::InvalidRequestMethod
|
|
17
|
+
@app.call(env)
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
end
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
module Rack::Healthcheck
|
|
2
|
+
class Type
|
|
3
|
+
ALL = [
|
|
4
|
+
CACHE = "CACHE".freeze,
|
|
5
|
+
EXTERNAL_SERVICE = "EXTERNAL_SERVICE".freeze,
|
|
6
|
+
INTERNAL_SERVICE = "INTERNAL_SERVICE".freeze,
|
|
7
|
+
STORAGE = "STORAGE".freeze,
|
|
8
|
+
MESSAGING = "MESSAGING".freeze,
|
|
9
|
+
DATABASE = "DATABASE".freeze,
|
|
10
|
+
FILE = "FILE".freeze
|
|
11
|
+
].freeze
|
|
12
|
+
end
|
|
13
|
+
end
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
require "rack/healthcheck/version"
|
|
2
|
+
require "rack/healthcheck/middleware"
|
|
3
|
+
require "rack/healthcheck/configuration"
|
|
4
|
+
|
|
5
|
+
module Rack::Healthcheck
|
|
6
|
+
class << self
|
|
7
|
+
def configuration
|
|
8
|
+
@configuration ||= Configuration.new
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
def configure
|
|
12
|
+
yield(configuration)
|
|
13
|
+
end
|
|
14
|
+
end
|
|
15
|
+
end
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
# coding: utf-8
|
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
|
4
|
+
require 'rack/healthcheck/version'
|
|
5
|
+
|
|
6
|
+
Gem::Specification.new do |spec|
|
|
7
|
+
spec.name = "rack-healthcheck"
|
|
8
|
+
spec.version = Rack::Healthcheck::VERSION
|
|
9
|
+
spec.authors = ["Leandro Maduro"]
|
|
10
|
+
spec.email = ["leandromaduro1@gmail.com"]
|
|
11
|
+
|
|
12
|
+
spec.summary = "A healthcheck interface for Sinatra and Rails framework"
|
|
13
|
+
spec.description = "A healthcheck interface for Sinatra and Rails framework"
|
|
14
|
+
spec.homepage = ""
|
|
15
|
+
spec.license = "MIT"
|
|
16
|
+
|
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
|
18
|
+
spec.bindir = "exe"
|
|
19
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
|
20
|
+
spec.require_paths = ["lib"]
|
|
21
|
+
|
|
22
|
+
spec.add_development_dependency "bundler", "~> 1.12"
|
|
23
|
+
spec.add_development_dependency "coveralls"
|
|
24
|
+
spec.add_development_dependency "simplecov"
|
|
25
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
|
26
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
|
27
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: rack-healthcheck
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Leandro Maduro
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: exe
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2016-09-25 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.12'
|
|
20
|
+
type: :development
|
|
21
|
+
prerelease: false
|
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
23
|
+
requirements:
|
|
24
|
+
- - "~>"
|
|
25
|
+
- !ruby/object:Gem::Version
|
|
26
|
+
version: '1.12'
|
|
27
|
+
- !ruby/object:Gem::Dependency
|
|
28
|
+
name: coveralls
|
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
|
30
|
+
requirements:
|
|
31
|
+
- - ">="
|
|
32
|
+
- !ruby/object:Gem::Version
|
|
33
|
+
version: '0'
|
|
34
|
+
type: :development
|
|
35
|
+
prerelease: false
|
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
37
|
+
requirements:
|
|
38
|
+
- - ">="
|
|
39
|
+
- !ruby/object:Gem::Version
|
|
40
|
+
version: '0'
|
|
41
|
+
- !ruby/object:Gem::Dependency
|
|
42
|
+
name: simplecov
|
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
|
44
|
+
requirements:
|
|
45
|
+
- - ">="
|
|
46
|
+
- !ruby/object:Gem::Version
|
|
47
|
+
version: '0'
|
|
48
|
+
type: :development
|
|
49
|
+
prerelease: false
|
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
51
|
+
requirements:
|
|
52
|
+
- - ">="
|
|
53
|
+
- !ruby/object:Gem::Version
|
|
54
|
+
version: '0'
|
|
55
|
+
- !ruby/object:Gem::Dependency
|
|
56
|
+
name: rake
|
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
|
58
|
+
requirements:
|
|
59
|
+
- - "~>"
|
|
60
|
+
- !ruby/object:Gem::Version
|
|
61
|
+
version: '10.0'
|
|
62
|
+
type: :development
|
|
63
|
+
prerelease: false
|
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
65
|
+
requirements:
|
|
66
|
+
- - "~>"
|
|
67
|
+
- !ruby/object:Gem::Version
|
|
68
|
+
version: '10.0'
|
|
69
|
+
- !ruby/object:Gem::Dependency
|
|
70
|
+
name: rspec
|
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
|
72
|
+
requirements:
|
|
73
|
+
- - "~>"
|
|
74
|
+
- !ruby/object:Gem::Version
|
|
75
|
+
version: '3.0'
|
|
76
|
+
type: :development
|
|
77
|
+
prerelease: false
|
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
79
|
+
requirements:
|
|
80
|
+
- - "~>"
|
|
81
|
+
- !ruby/object:Gem::Version
|
|
82
|
+
version: '3.0'
|
|
83
|
+
description: A healthcheck interface for Sinatra and Rails framework
|
|
84
|
+
email:
|
|
85
|
+
- leandromaduro1@gmail.com
|
|
86
|
+
executables: []
|
|
87
|
+
extensions: []
|
|
88
|
+
extra_rdoc_files: []
|
|
89
|
+
files:
|
|
90
|
+
- ".gitignore"
|
|
91
|
+
- ".rspec"
|
|
92
|
+
- ".travis.yml"
|
|
93
|
+
- Gemfile
|
|
94
|
+
- LICENSE.txt
|
|
95
|
+
- README.md
|
|
96
|
+
- Rakefile
|
|
97
|
+
- bin/console
|
|
98
|
+
- bin/setup
|
|
99
|
+
- lib/rack/healthcheck.rb
|
|
100
|
+
- lib/rack/healthcheck/action.rb
|
|
101
|
+
- lib/rack/healthcheck/actions/base.rb
|
|
102
|
+
- lib/rack/healthcheck/actions/complete.rb
|
|
103
|
+
- lib/rack/healthcheck/actions/load_balancer.rb
|
|
104
|
+
- lib/rack/healthcheck/checks/active_record.rb
|
|
105
|
+
- lib/rack/healthcheck/checks/base.rb
|
|
106
|
+
- lib/rack/healthcheck/checks/http_request.rb
|
|
107
|
+
- lib/rack/healthcheck/checks/mongo_db.rb
|
|
108
|
+
- lib/rack/healthcheck/checks/rabbit_mq.rb
|
|
109
|
+
- lib/rack/healthcheck/checks/redis.rb
|
|
110
|
+
- lib/rack/healthcheck/configuration.rb
|
|
111
|
+
- lib/rack/healthcheck/middleware.rb
|
|
112
|
+
- lib/rack/healthcheck/type.rb
|
|
113
|
+
- lib/rack/healthcheck/version.rb
|
|
114
|
+
- rack-healthcheck.gemspec
|
|
115
|
+
homepage: ''
|
|
116
|
+
licenses:
|
|
117
|
+
- MIT
|
|
118
|
+
metadata: {}
|
|
119
|
+
post_install_message:
|
|
120
|
+
rdoc_options: []
|
|
121
|
+
require_paths:
|
|
122
|
+
- lib
|
|
123
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
124
|
+
requirements:
|
|
125
|
+
- - ">="
|
|
126
|
+
- !ruby/object:Gem::Version
|
|
127
|
+
version: '0'
|
|
128
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
129
|
+
requirements:
|
|
130
|
+
- - ">="
|
|
131
|
+
- !ruby/object:Gem::Version
|
|
132
|
+
version: '0'
|
|
133
|
+
requirements: []
|
|
134
|
+
rubyforge_project:
|
|
135
|
+
rubygems_version: 2.4.8
|
|
136
|
+
signing_key:
|
|
137
|
+
specification_version: 4
|
|
138
|
+
summary: A healthcheck interface for Sinatra and Rails framework
|
|
139
|
+
test_files: []
|