rack-ecg 0.1.0 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/dependabot.yml +9 -0
- data/.github/workflows/{main.yml → test.yml} +3 -4
- data/.rubocop.yml +2 -0
- data/.ruby-version +1 -1
- data/CHANGELOG.md +30 -3
- data/Gemfile +2 -1
- data/README.md +123 -61
- data/Rakefile +9 -4
- data/docker-compose.yml +18 -0
- data/examples/basic.ru +3 -2
- data/examples/checks.ru +3 -2
- data/examples/custom_failure_status.ru +11 -0
- data/examples/git_revision_check_replacement.ru +28 -0
- data/examples/hook.ru +3 -3
- data/examples/mounted_path.ru +3 -2
- data/examples/parameters.ru +7 -6
- data/examples/stand_alone.ru +2 -1
- data/lib/rack/ecg/check/active_record_connection.rb +1 -0
- data/lib/rack/ecg/check/error.rb +12 -3
- data/lib/rack/ecg/check/git_revision.rb +5 -0
- data/lib/rack/ecg/check/http.rb +12 -3
- data/lib/rack/ecg/check/migration_version.rb +1 -0
- data/lib/rack/ecg/check/redis_connection.rb +16 -7
- data/lib/rack/ecg/check/sequel_connection.rb +2 -1
- data/lib/rack/ecg/check/static.rb +52 -0
- data/lib/rack/ecg/check.rb +3 -1
- data/lib/rack/ecg/check_factory.rb +1 -0
- data/lib/rack/ecg/check_registry.rb +10 -7
- data/lib/rack/ecg/version.rb +2 -1
- data/lib/rack/ecg.rb +11 -4
- data/lib/rack-ecg.rb +2 -1
- data/rack-ecg.gemspec +9 -9
- metadata +34 -29
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d9102753e910fc1dc1008ff19d2a1d3bd23db4fdb6f0ac24f322d0750294b429
|
4
|
+
data.tar.gz: 189b4f010c85128da3d317b813ab829023b2db2793e1df4ce01bfbd9d6b67bfb
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6c7698889a12a7c2263b4f70c366423691d5d4a3cd7f9d74bbb94a3e974087143f2c9906bc9cdf0e234d3dc2ece5ea840458b0f60d2f94cb250288b1a86f6073
|
7
|
+
data.tar.gz: 96fc9feb30b537fccf7cd377716078c9bfd6a9c33f33f1ac9d9cf1375863e6b751f48ea2670ee7c1d4b03b9ddf4be9f4d1e63be258fe7936b70824bf59b16c0b
|
@@ -1,4 +1,4 @@
|
|
1
|
-
name:
|
1
|
+
name: test
|
2
2
|
|
3
3
|
on: [push,pull_request]
|
4
4
|
|
@@ -6,17 +6,16 @@ jobs:
|
|
6
6
|
test:
|
7
7
|
strategy:
|
8
8
|
matrix:
|
9
|
-
ruby: [2.
|
9
|
+
ruby: ["2.7", "3.0", "3.1", "3.2"]
|
10
10
|
runs-on: ubuntu-latest
|
11
11
|
name: Test (Ruby ${{ matrix.ruby }})
|
12
12
|
steps:
|
13
|
-
- uses: actions/checkout@
|
13
|
+
- uses: actions/checkout@v3
|
14
14
|
- name: Set up Ruby
|
15
15
|
uses: ruby/setup-ruby@v1
|
16
16
|
with:
|
17
17
|
ruby-version: ${{ matrix.ruby }}
|
18
18
|
- name: Run the default task
|
19
19
|
run: |
|
20
|
-
gem install bundler -v 2.2.1
|
21
20
|
bundle install
|
22
21
|
bundle exec rake
|
data/.rubocop.yml
CHANGED
data/.ruby-version
CHANGED
@@ -1 +1 @@
|
|
1
|
-
2.
|
1
|
+
3.2.2
|
data/CHANGELOG.md
CHANGED
@@ -1,10 +1,36 @@
|
|
1
1
|
# Changelog
|
2
|
+
|
2
3
|
All notable changes to this project will be documented in this file.
|
3
4
|
|
4
5
|
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
5
6
|
|
6
7
|
## [Unreleased]
|
7
8
|
|
9
|
+
## [0.3.0] - 2023-09-08
|
10
|
+
|
11
|
+
### Added
|
12
|
+
|
13
|
+
- (#53) Add a configuration option for the unhealthy status code - thanks @floere!
|
14
|
+
|
15
|
+
### Removed
|
16
|
+
|
17
|
+
- **Breaking:** Dropped support for Ruby versions < 2.7.0
|
18
|
+
|
19
|
+
## [0.2.0] - 2022-02-21
|
20
|
+
|
21
|
+
### Added
|
22
|
+
|
23
|
+
- The `static` check, which uses the provided check parameters to return the same result every time.
|
24
|
+
|
25
|
+
### Changed
|
26
|
+
|
27
|
+
- **Breaking**: The Redis check now requires being configured with an instance of the Redis client, via the named `instance` parameter
|
28
|
+
- **Deprecated**: The `git_revision` check will be removed in rack-ecg version 1.0.0. For a suggested replacement, [see the GitRevision Check Replacement example](./examples/gitrevision_check_replacement.ru), which uses the `static` check to memoize the value.
|
29
|
+
|
30
|
+
### Removed
|
31
|
+
|
32
|
+
- **Breaking:** Dropped support for Ruby versions < 2.6.0
|
33
|
+
|
8
34
|
## [0.1.0] - 2020-12-16
|
9
35
|
|
10
36
|
### Added
|
@@ -59,11 +85,12 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
|
59
85
|
- `git_revision` check to return the current git revision
|
60
86
|
- `migration_version` check to return the current ActiveRecord migration version
|
61
87
|
|
62
|
-
[Unreleased]: https://github.com/envato/rack-ecg/compare/v0.
|
63
|
-
[0.
|
88
|
+
[Unreleased]: https://github.com/envato/rack-ecg/compare/v0.3.0...HEAD
|
89
|
+
[0.3.0]: https://github.com/envato/rack-ecg/compare/v0.2.0...v0.3.0
|
90
|
+
[0.2.0]: https://github.com/envato/rack-ecg/compare/v0.1.0...v0.2.0
|
91
|
+
[0.1.0]: https://github.com/envato/rack-ecg/compare/v0.0.5...v0.1.0
|
64
92
|
[0.0.5]: https://github.com/envato/rack-ecg/compare/v0.0.4...v0.0.5
|
65
93
|
[0.0.4]: https://github.com/envato/rack-ecg/compare/v0.0.3...v0.0.4
|
66
94
|
[0.0.3]: https://github.com/envato/rack-ecg/compare/v0.0.2...v0.0.3
|
67
95
|
[0.0.2]: https://github.com/envato/rack-ecg/compare/v0.0.1...v0.0.2
|
68
96
|
[0.0.1]: https://github.com/envato/rack-ecg/releases/tag/v0.0.1
|
69
|
-
|
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -17,7 +17,7 @@ check endpoint, with minimal configuration.
|
|
17
17
|
|
18
18
|
## Development Status
|
19
19
|
|
20
|
-
[![Build Status](https://github.com/envato/rack-ecg/workflows/
|
20
|
+
[![Build Status](https://github.com/envato/rack-ecg/workflows/test/badge.svg?branch=main)](https://github.com/envato/rack-ecg/actions?query=branch%3Amain)
|
21
21
|
|
22
22
|
`Rack::ECG` is extracted from production code in use at
|
23
23
|
[Envato](http://envato.com). However, it is undergoing early development, and
|
@@ -28,7 +28,7 @@ APIs and features are almost certain to be in flux.
|
|
28
28
|
Add this to your application's `Gemfile`:
|
29
29
|
|
30
30
|
```ruby
|
31
|
-
gem 'rack-ecg', '~> 0.0
|
31
|
+
gem 'rack-ecg', '~> 0.3.0`
|
32
32
|
```
|
33
33
|
|
34
34
|
Then run `bundle install`.
|
@@ -73,99 +73,149 @@ status if any of the checks fail.
|
|
73
73
|
|
74
74
|
## Configuration
|
75
75
|
|
76
|
-
There are options that can be passed to `use Rack::ECG` to customise how it
|
77
|
-
works.
|
76
|
+
There are options that can be passed to `use Rack::ECG` to customise how it works.
|
78
77
|
|
79
|
-
###
|
80
|
-
Out of the box `Rack::ECG` doesn't do much and just checks that
|
81
|
-
HTTP responses can be returned. There are a number of built in checks that
|
82
|
-
`Rack::ECG` can be told to do (more to come)
|
83
|
-
- `:git_revision` - this assumes your code is deployed via git and exists in a
|
84
|
-
git repo, and that the `git` command can access it
|
85
|
-
- `:migration_version` - this assumes you are using ActiveRecord migrations. It
|
86
|
-
queries the `schema_versions` table and tells you what version the database is
|
87
|
-
at.
|
88
|
-
- `:active_record` - this checks if an ActiveRecord connection is active.
|
89
|
-
- `:redis` - this checks if a Redis connection is active.
|
78
|
+
### Checks
|
90
79
|
|
91
|
-
|
80
|
+
By default, `Rack::ECG` indicates that the service is reponsive via a `http` check. Additional checks are included in this gem, and can be enabled by passing their configuration to the `checks` parameter. To enable a check, add its name, and optionally configuration, to the `checks` array:
|
92
81
|
|
93
82
|
```ruby
|
94
|
-
use Rack::ECG, checks: [
|
83
|
+
use Rack::ECG, checks: [
|
84
|
+
# no configuration required, or allowed
|
85
|
+
:http,
|
86
|
+
# passing configuration options
|
87
|
+
[:static, { name: "app", value: "my-cool-app" }],
|
88
|
+
# some checks can be used multiple times
|
89
|
+
[:static, { name: "env", value: Rails.env }],
|
90
|
+
]
|
91
|
+
```
|
92
|
+
|
93
|
+
#### `active_record`
|
94
|
+
|
95
|
+
Requires a configured ActiveRecord connection. Does not support configuration. Indicates whether the connection to the default database is currently open. On success, returns something in the following format:
|
96
|
+
|
97
|
+
```json
|
98
|
+
{
|
99
|
+
"active_record": {
|
100
|
+
"status": "ok",
|
101
|
+
"value": true
|
102
|
+
}
|
103
|
+
}
|
95
104
|
```
|
96
105
|
|
106
|
+
#### `error`
|
107
|
+
|
108
|
+
Does not support configuration. Always returns the following:
|
109
|
+
|
110
|
+
```json
|
111
|
+
{
|
112
|
+
"error": {
|
113
|
+
"status": "error",
|
114
|
+
"value": "PC LOAD ERROR"
|
115
|
+
}
|
116
|
+
}
|
97
117
|
```
|
98
|
-
|
118
|
+
|
119
|
+
#### `git_revision`
|
120
|
+
|
121
|
+
**Deprecated**: will be removed in version 1.0.0. [See the GitRevision Check Replacement example](./examples/git_revision_check_replacement.ru), which uses the `static` check to memoize the value.
|
122
|
+
|
123
|
+
Requires the `git` executable on path, and that the application's working directory is within a Git repository. Does not support configuration. On success, returns something in the following format:
|
124
|
+
|
125
|
+
```json
|
126
|
+
{
|
127
|
+
"git_revision": {
|
128
|
+
"status": "ok",
|
129
|
+
"value": "dc840f9d5563e6e5a8b34da29c298764e3046039"
|
130
|
+
}
|
131
|
+
}
|
132
|
+
```
|
133
|
+
|
134
|
+
#### `http`
|
135
|
+
|
136
|
+
Automatically included, and does not support configuration. Always returns the following:
|
137
|
+
|
138
|
+
```json
|
99
139
|
{
|
100
140
|
"http": {
|
101
141
|
"status": "ok",
|
102
142
|
"value": "online"
|
103
|
-
}
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
143
|
+
}
|
144
|
+
}
|
145
|
+
```
|
146
|
+
|
147
|
+
#### `migration_version`
|
148
|
+
|
149
|
+
Requires a configured ActiveRecord connection, and that ActiveRecord migrations are in use. Does not support configuration. Queries the `schema_versions` table on the default database to report the current migration version. On success, returns something in the following format:
|
150
|
+
|
151
|
+
```json
|
152
|
+
{
|
108
153
|
"migration_version": {
|
109
154
|
"status": "ok",
|
110
|
-
"value": "
|
111
|
-
}
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
155
|
+
"value": "20210506024055"
|
156
|
+
}
|
157
|
+
}
|
158
|
+
```
|
159
|
+
|
160
|
+
#### `redis`
|
161
|
+
|
162
|
+
Requires the Redis gem. Requires configuration, an instance of a Redis client. Indicates whether the Redis client passed in is currently connected to the Redis database. On success, returns something in the following format:
|
163
|
+
|
164
|
+
```json
|
165
|
+
{
|
116
166
|
"redis": {
|
117
167
|
"status": "ok",
|
118
|
-
"value":
|
168
|
+
"value": true
|
119
169
|
}
|
120
170
|
}
|
121
171
|
```
|
122
172
|
|
123
|
-
####
|
124
|
-
|
173
|
+
#### `sequel`
|
174
|
+
|
175
|
+
Requires the Sequel gem. Requires configuration, and can be configured multiple times. Indicates whether a (new) connection can be established to the configured Sequel database.
|
176
|
+
|
177
|
+
Given the following configuration:
|
125
178
|
|
126
179
|
```ruby
|
127
|
-
|
180
|
+
{
|
181
|
+
connection: "sqlite://events.db",
|
182
|
+
name: "events", # must be unique per sequel check
|
183
|
+
}
|
128
184
|
```
|
129
185
|
|
130
|
-
|
131
|
-
|
186
|
+
Returns the something in the following format on success:
|
187
|
+
|
188
|
+
```json
|
132
189
|
{
|
133
|
-
"
|
134
|
-
"status": "ok",
|
135
|
-
"value": "online"
|
136
|
-
},
|
137
|
-
"sequel": {
|
190
|
+
"sequel_events": {
|
138
191
|
"status": "ok",
|
139
|
-
"value":
|
192
|
+
"value": true
|
140
193
|
}
|
141
194
|
}
|
142
195
|
```
|
143
196
|
|
144
|
-
|
197
|
+
#### `static`
|
198
|
+
|
199
|
+
Returns the same value every time. Requires configuration, and can be configured multiple times.
|
200
|
+
|
201
|
+
Given the following configuration:
|
145
202
|
|
146
203
|
```ruby
|
147
|
-
|
148
|
-
:
|
149
|
-
|
150
|
-
|
151
|
-
]
|
204
|
+
{
|
205
|
+
name: "image_build_url", # must be unique per static check
|
206
|
+
success: true, # default value
|
207
|
+
status: Rack::ECG::Check::Status::OK, # optional, overrides `success`
|
208
|
+
value: ENV["IMAGE_BUILD_URL"],
|
209
|
+
}
|
152
210
|
```
|
153
211
|
|
154
|
-
|
155
|
-
$ curl http://localhost:9292/_ecg
|
212
|
+
Returns the something in the following format:
|
156
213
|
|
214
|
+
```json
|
157
215
|
{
|
158
|
-
"
|
216
|
+
"image_build_url": {
|
159
217
|
"status": "ok",
|
160
|
-
"value": "
|
161
|
-
},
|
162
|
-
"sequel_events": {
|
163
|
-
"status": "ok",
|
164
|
-
"value": "true"
|
165
|
-
},
|
166
|
-
"sequel_projections": {
|
167
|
-
"status": "ok",
|
168
|
-
"value": "true"
|
218
|
+
"value": "https://example.com/pipelines/my-cool-app/builds/1234"
|
169
219
|
}
|
170
220
|
}
|
171
221
|
```
|
@@ -191,17 +241,28 @@ use Rack::ECG, hook: Proc.new { |success, _checks| puts "Is healthy? #{success}"
|
|
191
241
|
- `success`: whether the response will indicate success
|
192
242
|
- `checks`: an array of the check names and values
|
193
243
|
|
194
|
-
|
244
|
+
### `failure_status`
|
245
|
+
|
246
|
+
By default, check failures result in a HTTP status code `500` (Internal Server
|
247
|
+
Error). You can change this code by setting the `failure_status` option. e.g.
|
248
|
+
|
249
|
+
```ruby
|
250
|
+
use Rack::ECG, checks: [:error], failure_status: 503
|
251
|
+
```
|
195
252
|
|
196
253
|
## Requirements
|
197
254
|
|
198
|
-
- Ruby >= 2.
|
255
|
+
- Ruby >= 2.6
|
199
256
|
- Rack
|
200
257
|
- To use optional `git_revision` check, your deployed code needs to be in a git repo, and
|
201
258
|
`git` command must be accessible on the server
|
202
259
|
- To use optional `migration_version` check, you must be using ActiveRecord and
|
203
260
|
migrations stored in `schema_versions` table
|
204
261
|
|
262
|
+
## Examples
|
263
|
+
|
264
|
+
Some configuration examples are provided in [/examples](https://github.com/envato/rack-ecg/tree/main/examples).
|
265
|
+
|
205
266
|
## Contact
|
206
267
|
|
207
268
|
- [github project](https://github.com/envato/rack-ecg)
|
@@ -212,6 +273,7 @@ migrations stored in `schema_versions` table
|
|
212
273
|
## Maintainers
|
213
274
|
|
214
275
|
- [Liam Dawson](https://github.com/liamdawson)
|
276
|
+
- [Andrew Davis](https://github.com/andyjdavis)
|
215
277
|
|
216
278
|
## Contributors
|
217
279
|
|
data/Rakefile
CHANGED
@@ -1,9 +1,14 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
|
-
require "bundler/gem_tasks"
|
3
2
|
|
4
|
-
|
5
|
-
require
|
6
|
-
|
3
|
+
begin
|
4
|
+
require "bundler/gem_tasks"
|
5
|
+
rescue LoadError
|
6
|
+
# not a significant issue
|
7
|
+
end
|
8
|
+
|
9
|
+
require "rspec/core/rake_task"
|
10
|
+
require "rubocop/rake_task"
|
11
|
+
require "yard"
|
7
12
|
|
8
13
|
RSpec::Core::RakeTask.new(:spec)
|
9
14
|
RuboCop::RakeTask.new(:rubocop)
|
data/docker-compose.yml
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
# for running tests across different versions locally
|
2
|
+
|
3
|
+
version: "3"
|
4
|
+
services:
|
5
|
+
ruby30: &ruby30
|
6
|
+
image: ruby:3.0
|
7
|
+
volumes:
|
8
|
+
- ./:/app
|
9
|
+
command: bash -c 'gem update --system && cd /app && { ! [ -f Gemfile.lock ] || rm Gemfile.lock; } && bundle install && bundle exec rake'
|
10
|
+
ruby27:
|
11
|
+
<<: *ruby30
|
12
|
+
image: ruby:2.7
|
13
|
+
ruby26:
|
14
|
+
<<: *ruby30
|
15
|
+
image: ruby:2.6
|
16
|
+
ruby25:
|
17
|
+
<<: *ruby30
|
18
|
+
image: ruby:2.5
|
data/examples/basic.ru
CHANGED
data/examples/checks.ru
CHANGED
@@ -0,0 +1,28 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "rack/ecg"
|
4
|
+
|
5
|
+
# This example behaves just like the deprecated GitRevision check, except that the value is memoized.
|
6
|
+
# i.e. "Fetching the git revision" shouldn't show up for every `GET /_ecg` request.
|
7
|
+
#
|
8
|
+
# Also consider writing the git revision to a file, or storing it in an environment variable, so it can found more
|
9
|
+
# efficiently and with fewer dependencies.
|
10
|
+
|
11
|
+
def git_revision
|
12
|
+
puts "Fetching the git revision"
|
13
|
+
|
14
|
+
_stdin, stdout, stderr, wait_thread = Open3.popen3("git rev-parse HEAD")
|
15
|
+
|
16
|
+
success = wait_thread.value.success?
|
17
|
+
|
18
|
+
status = success ? Rack::ECG::Check::Status::OK : Rack::ECG::Check::Status::ERROR
|
19
|
+
|
20
|
+
value = success ? stdout.read : stderr.read
|
21
|
+
value = value.strip
|
22
|
+
|
23
|
+
{ name: :git_revision, status: status, value: value }
|
24
|
+
end
|
25
|
+
|
26
|
+
use(Rack::ECG, checks: [[:static, git_revision]])
|
27
|
+
|
28
|
+
run(->(_env) { [200, {}, ["Hello, World"]] })
|
data/examples/hook.ru
CHANGED
@@ -1,12 +1,12 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require
|
3
|
+
require "rack/ecg"
|
4
4
|
|
5
5
|
log_check_results = proc do |success, checks|
|
6
6
|
next if success
|
7
7
|
|
8
8
|
checks.each do |check_name, check_status|
|
9
|
-
next unless check_status[:status] ==
|
9
|
+
next unless check_status[:status] == "error"
|
10
10
|
|
11
11
|
puts "Check #{check_name} failed: #{check_status[:value]}"
|
12
12
|
end
|
@@ -14,4 +14,4 @@ end
|
|
14
14
|
|
15
15
|
use(Rack::ECG, checks: [:git_revision, :migration_version], hook: log_check_results)
|
16
16
|
|
17
|
-
run(->(_env) { [200, {}, [
|
17
|
+
run(->(_env) { [200, {}, ["Hello, World"]] })
|
data/examples/mounted_path.ru
CHANGED
data/examples/parameters.ru
CHANGED
@@ -1,12 +1,13 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require
|
4
|
-
require
|
2
|
+
|
3
|
+
require "rack/ecg"
|
4
|
+
require "sequel"
|
5
|
+
require "sqlite3"
|
5
6
|
|
6
7
|
use(Rack::ECG, checks: [
|
7
8
|
:http,
|
8
|
-
[:sequel, { connection:
|
9
|
-
[:sequel, { connection:
|
9
|
+
[:sequel, { connection: "sqlite://events.db", name: "events" }],
|
10
|
+
[:sequel, { connection: "sqlite://projections.db", name: "projections" }],
|
10
11
|
])
|
11
12
|
|
12
|
-
run(->
|
13
|
+
run(->(_env) { [200, {}, ["Hello, World"]] })
|
data/examples/stand_alone.ru
CHANGED
data/lib/rack/ecg/check/error.rb
CHANGED
@@ -1,12 +1,21 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "static"
|
4
|
+
|
2
5
|
module Rack
|
3
6
|
class ECG
|
4
7
|
module Check
|
5
8
|
# @!method initialize
|
6
9
|
# Always returns a basic error for testing purposes.
|
7
|
-
class Error
|
8
|
-
|
9
|
-
|
10
|
+
class Error < Static
|
11
|
+
STATIC_PARAMETERS = {
|
12
|
+
name: :error,
|
13
|
+
success: false,
|
14
|
+
value: "PC LOAD LETTER",
|
15
|
+
}.freeze
|
16
|
+
|
17
|
+
def initialize
|
18
|
+
super(STATIC_PARAMETERS)
|
10
19
|
end
|
11
20
|
end
|
12
21
|
|
@@ -1,7 +1,12 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
|
+
|
2
3
|
module Rack
|
3
4
|
class ECG
|
4
5
|
module Check
|
6
|
+
# @deprecated This check requires the presence of the git executable, and executes it every time to determine the
|
7
|
+
# current revision. Consider checking the revision at initialization time, and returning it via a {Static} check
|
8
|
+
# instead.
|
9
|
+
#
|
5
10
|
# @!method initialize
|
6
11
|
# Returns the SHA1 of the current commit, as reported by the git
|
7
12
|
# executable.
|
data/lib/rack/ecg/check/http.rb
CHANGED
@@ -1,12 +1,21 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "static"
|
4
|
+
|
2
5
|
module Rack
|
3
6
|
class ECG
|
4
7
|
module Check
|
5
8
|
# @!method initialize
|
6
9
|
# Always returns a success.
|
7
|
-
class Http
|
8
|
-
|
9
|
-
|
10
|
+
class Http < Static
|
11
|
+
STATIC_PARAMETERS = {
|
12
|
+
name: :http,
|
13
|
+
success: true,
|
14
|
+
value: "online",
|
15
|
+
}.freeze
|
16
|
+
|
17
|
+
def initialize
|
18
|
+
super(STATIC_PARAMETERS)
|
10
19
|
end
|
11
20
|
end
|
12
21
|
|
@@ -1,19 +1,28 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
|
+
|
2
3
|
module Rack
|
3
4
|
class ECG
|
4
5
|
module Check
|
5
|
-
# @!method initialize
|
6
|
-
# Checks whether the global Redis client is currently connected to the
|
7
|
-
# database.
|
8
|
-
#
|
9
|
-
# Does not take any options.
|
10
6
|
class RedisConnection
|
7
|
+
attr_reader :redis_instance
|
8
|
+
|
9
|
+
# Checks whether the given Redis client is currently connected to the
|
10
|
+
# database as identified by the ++instance++ option.
|
11
|
+
#
|
12
|
+
# @option parameters instance [Redis] The Redis client
|
13
|
+
def initialize(parameters = {})
|
14
|
+
@redis_instance = parameters[:instance]
|
15
|
+
end
|
16
|
+
|
11
17
|
def result
|
12
18
|
value = ""
|
13
19
|
status = Status::OK
|
14
20
|
begin
|
15
|
-
if
|
16
|
-
|
21
|
+
if redis_instance.nil?
|
22
|
+
status = Status::ERROR
|
23
|
+
value = "Redis instance parameters not found"
|
24
|
+
elsif defined?(::Redis)
|
25
|
+
value = redis_instance.connected?
|
17
26
|
status = value ? Status::OK : Status::ERROR
|
18
27
|
else
|
19
28
|
status = Status::ERROR
|
@@ -1,4 +1,5 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
|
+
|
2
3
|
module Rack
|
3
4
|
class ECG
|
4
5
|
module Check
|
@@ -41,7 +42,7 @@ module Rack
|
|
41
42
|
|
42
43
|
def result_key
|
43
44
|
if name
|
44
|
-
"sequel #{name.downcase}".gsub(/\W+/,
|
45
|
+
"sequel #{name.downcase}".gsub(/\W+/, "_")
|
45
46
|
else
|
46
47
|
"sequel"
|
47
48
|
end
|
@@ -0,0 +1,52 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Rack
|
4
|
+
class ECG
|
5
|
+
module Check
|
6
|
+
class Static
|
7
|
+
# Always returns the provided ++value++ under the ++name++ key, with the result set by ++status++.
|
8
|
+
#
|
9
|
+
# @example Return "Hello, world!" under ++static++
|
10
|
+
# use(Rack::ECG, { checks: [[:static, { value: "Hello, world!" }]] })
|
11
|
+
#
|
12
|
+
# @example Return "Paper jam in tray 2" as an error under ++printer_status++
|
13
|
+
# use(Rack::ECG, {
|
14
|
+
# checks: [
|
15
|
+
# [
|
16
|
+
# :static,
|
17
|
+
# {
|
18
|
+
# value: "Paper jam in tray 2",
|
19
|
+
# success: false, # or status: Rack::ECG::Check::Status::ERROR
|
20
|
+
# name: :printer_status,
|
21
|
+
# },
|
22
|
+
# ],
|
23
|
+
# ],
|
24
|
+
# })
|
25
|
+
#
|
26
|
+
# @option parameters value [Object] (nil) Result value
|
27
|
+
# @option parameters status [Status::ERROR, Status::OK, nil] (nil) Result status (takes precedence over
|
28
|
+
# ++success++)
|
29
|
+
# @option parameters success [Boolean] (true) Whether the result is successful
|
30
|
+
# @option parameters name [Symbol, #to_sym] (:static) Key for the check result in the response
|
31
|
+
def initialize(parameters)
|
32
|
+
parameters ||= {}
|
33
|
+
|
34
|
+
@name = parameters.fetch(:name, :static).to_sym
|
35
|
+
@value = parameters.fetch(:value, nil)
|
36
|
+
|
37
|
+
@status = if parameters.key?(:status)
|
38
|
+
parameters[:status]
|
39
|
+
else
|
40
|
+
parameters.fetch(:success, true) ? Status::OK : Status::ERROR
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
def result
|
45
|
+
Result.new(@name, @status, @value)
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
CheckRegistry.instance.register(:static, Static)
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
data/lib/rack/ecg/check.rb
CHANGED
@@ -1,12 +1,14 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
|
+
|
2
3
|
require "rack/ecg/check_registry"
|
4
|
+
require "rack/ecg/check/active_record_connection"
|
3
5
|
require "rack/ecg/check/error"
|
4
6
|
require "rack/ecg/check/git_revision"
|
5
7
|
require "rack/ecg/check/http"
|
6
8
|
require "rack/ecg/check/migration_version"
|
7
|
-
require "rack/ecg/check/active_record_connection"
|
8
9
|
require "rack/ecg/check/redis_connection"
|
9
10
|
require "rack/ecg/check/sequel_connection"
|
11
|
+
require "rack/ecg/check/static"
|
10
12
|
|
11
13
|
module Rack
|
12
14
|
class ECG
|
@@ -1,4 +1,5 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
|
+
|
2
3
|
require "singleton"
|
3
4
|
|
4
5
|
module Rack
|
@@ -29,14 +30,16 @@ module Rack
|
|
29
30
|
@registry.fetch(name) { raise CheckNotRegistered, "Check '#{name}' is not registered" }
|
30
31
|
end
|
31
32
|
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
33
|
+
class << self
|
34
|
+
# (see #lookup)
|
35
|
+
def lookup(name)
|
36
|
+
instance.lookup(name)
|
37
|
+
end
|
36
38
|
|
37
|
-
|
38
|
-
|
39
|
-
|
39
|
+
# (see #register)
|
40
|
+
def register(name, check_class)
|
41
|
+
instance.register(name, check_class)
|
42
|
+
end
|
40
43
|
end
|
41
44
|
end
|
42
45
|
end
|
data/lib/rack/ecg/version.rb
CHANGED
data/lib/rack/ecg.rb
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
|
+
|
2
3
|
require "rack/ecg/version"
|
3
4
|
require "json"
|
4
5
|
require "open3"
|
@@ -10,6 +11,8 @@ module Rack
|
|
10
11
|
DEFAULT_MOUNT_AT = "/_ecg"
|
11
12
|
# Checks enabled by default.
|
12
13
|
DEFAULT_CHECKS = [:http]
|
14
|
+
# Default failure response status.
|
15
|
+
DEFAULT_FAILURE_STATUS = 500
|
13
16
|
|
14
17
|
# Constructs an instance of ECG Rack middleware with the specified
|
15
18
|
# options.
|
@@ -21,7 +24,9 @@ module Rack
|
|
21
24
|
# @param at [String, nil] Path which this ECG instance handles.
|
22
25
|
# @param hook [#call, nil] Callable which receives the success status and
|
23
26
|
# check results
|
24
|
-
|
27
|
+
# @param failure_status [Integer] Status code to return on check failure
|
28
|
+
def initialize(app = nil, checks: DEFAULT_CHECKS, at: DEFAULT_MOUNT_AT, hook: nil,
|
29
|
+
failure_status: DEFAULT_FAILURE_STATUS)
|
25
30
|
@app = app
|
26
31
|
|
27
32
|
check_configuration = checks || []
|
@@ -29,6 +34,8 @@ module Rack
|
|
29
34
|
@mount_at = at || DEFAULT_MOUNT_AT
|
30
35
|
|
31
36
|
@result_hook = hook
|
37
|
+
|
38
|
+
@failure_response_status = failure_status
|
32
39
|
end
|
33
40
|
|
34
41
|
# Rack compatible call method. Not intended for direct usage.
|
@@ -40,13 +47,13 @@ module Rack
|
|
40
47
|
|
41
48
|
success = check_results.none? { |check| check[1][:status] == Check::Status::ERROR }
|
42
49
|
|
43
|
-
response_status = success ? 200 :
|
50
|
+
response_status = success ? 200 : @failure_response_status
|
44
51
|
|
45
52
|
@result_hook&.call(success, check_results)
|
46
53
|
|
47
54
|
response_headers = {
|
48
|
-
"
|
49
|
-
"
|
55
|
+
"x-rack-ecg-version" => Rack::ECG::VERSION,
|
56
|
+
"content-type" => "application/json",
|
50
57
|
}
|
51
58
|
|
52
59
|
response_body = JSON.pretty_generate(check_results)
|
data/lib/rack-ecg.rb
CHANGED
data/rack-ecg.gemspec
CHANGED
@@ -8,7 +8,7 @@ Gem::Specification.new do |spec|
|
|
8
8
|
spec.version = Rack::ECG::VERSION
|
9
9
|
spec.authors = ["Envato", "Julian Doherty"]
|
10
10
|
spec.email = ["julian@envato.com"]
|
11
|
-
spec.summary =
|
11
|
+
spec.summary = "Rack middleware serving a health check page"
|
12
12
|
spec.description = <<-EOF
|
13
13
|
rack-ecg allows you to serve a page that shows you facts about your deployed
|
14
14
|
app to allow you to check that everything is running as it should: git
|
@@ -30,16 +30,16 @@ Gem::Specification.new do |spec|
|
|
30
30
|
spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
|
31
31
|
spec.require_paths = ["lib"]
|
32
32
|
|
33
|
-
spec.required_ruby_version = Gem::Requirement.new(">= 2.
|
33
|
+
spec.required_ruby_version = Gem::Requirement.new(">= 2.7.0")
|
34
34
|
|
35
35
|
spec.add_runtime_dependency("rack")
|
36
36
|
|
37
|
+
spec.add_development_dependency("pry", "~> 0.14.2")
|
38
|
+
spec.add_development_dependency("rack-test", "~> 2.1.0")
|
39
|
+
spec.add_development_dependency("rackup", "~> 2.1.0")
|
37
40
|
spec.add_development_dependency("rake", "~> 13.0")
|
38
|
-
spec.add_development_dependency("
|
39
|
-
spec.add_development_dependency("rspec", "~> 3.
|
40
|
-
spec.add_development_dependency("
|
41
|
-
spec.add_development_dependency("
|
42
|
-
spec.add_development_dependency("rubocop-shopify", "~> 1.0.0")
|
43
|
-
spec.add_development_dependency("yard", "~> 0.9.24")
|
44
|
-
spec.add_development_dependency("redcarpet", "~> 3.5.0")
|
41
|
+
spec.add_development_dependency("redcarpet", "~> 3.6.0")
|
42
|
+
spec.add_development_dependency("rspec", "~> 3.12.0")
|
43
|
+
spec.add_development_dependency("rubocop-shopify", "~> 2.14")
|
44
|
+
spec.add_development_dependency("yard", "~> 0.9.34")
|
45
45
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rack-ecg
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Envato
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: exe
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2023-09-08 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rack
|
@@ -26,117 +26,117 @@ dependencies:
|
|
26
26
|
- !ruby/object:Gem::Version
|
27
27
|
version: '0'
|
28
28
|
- !ruby/object:Gem::Dependency
|
29
|
-
name:
|
29
|
+
name: pry
|
30
30
|
requirement: !ruby/object:Gem::Requirement
|
31
31
|
requirements:
|
32
32
|
- - "~>"
|
33
33
|
- !ruby/object:Gem::Version
|
34
|
-
version:
|
34
|
+
version: 0.14.2
|
35
35
|
type: :development
|
36
36
|
prerelease: false
|
37
37
|
version_requirements: !ruby/object:Gem::Requirement
|
38
38
|
requirements:
|
39
39
|
- - "~>"
|
40
40
|
- !ruby/object:Gem::Version
|
41
|
-
version:
|
41
|
+
version: 0.14.2
|
42
42
|
- !ruby/object:Gem::Dependency
|
43
|
-
name:
|
43
|
+
name: rack-test
|
44
44
|
requirement: !ruby/object:Gem::Requirement
|
45
45
|
requirements:
|
46
46
|
- - "~>"
|
47
47
|
- !ruby/object:Gem::Version
|
48
|
-
version: 2.
|
48
|
+
version: 2.1.0
|
49
49
|
type: :development
|
50
50
|
prerelease: false
|
51
51
|
version_requirements: !ruby/object:Gem::Requirement
|
52
52
|
requirements:
|
53
53
|
- - "~>"
|
54
54
|
- !ruby/object:Gem::Version
|
55
|
-
version: 2.
|
55
|
+
version: 2.1.0
|
56
56
|
- !ruby/object:Gem::Dependency
|
57
|
-
name:
|
57
|
+
name: rackup
|
58
58
|
requirement: !ruby/object:Gem::Requirement
|
59
59
|
requirements:
|
60
60
|
- - "~>"
|
61
61
|
- !ruby/object:Gem::Version
|
62
|
-
version:
|
62
|
+
version: 2.1.0
|
63
63
|
type: :development
|
64
64
|
prerelease: false
|
65
65
|
version_requirements: !ruby/object:Gem::Requirement
|
66
66
|
requirements:
|
67
67
|
- - "~>"
|
68
68
|
- !ruby/object:Gem::Version
|
69
|
-
version:
|
69
|
+
version: 2.1.0
|
70
70
|
- !ruby/object:Gem::Dependency
|
71
|
-
name:
|
71
|
+
name: rake
|
72
72
|
requirement: !ruby/object:Gem::Requirement
|
73
73
|
requirements:
|
74
74
|
- - "~>"
|
75
75
|
- !ruby/object:Gem::Version
|
76
|
-
version:
|
76
|
+
version: '13.0'
|
77
77
|
type: :development
|
78
78
|
prerelease: false
|
79
79
|
version_requirements: !ruby/object:Gem::Requirement
|
80
80
|
requirements:
|
81
81
|
- - "~>"
|
82
82
|
- !ruby/object:Gem::Version
|
83
|
-
version:
|
83
|
+
version: '13.0'
|
84
84
|
- !ruby/object:Gem::Dependency
|
85
|
-
name:
|
85
|
+
name: redcarpet
|
86
86
|
requirement: !ruby/object:Gem::Requirement
|
87
87
|
requirements:
|
88
88
|
- - "~>"
|
89
89
|
- !ruby/object:Gem::Version
|
90
|
-
version:
|
90
|
+
version: 3.6.0
|
91
91
|
type: :development
|
92
92
|
prerelease: false
|
93
93
|
version_requirements: !ruby/object:Gem::Requirement
|
94
94
|
requirements:
|
95
95
|
- - "~>"
|
96
96
|
- !ruby/object:Gem::Version
|
97
|
-
version:
|
97
|
+
version: 3.6.0
|
98
98
|
- !ruby/object:Gem::Dependency
|
99
|
-
name:
|
99
|
+
name: rspec
|
100
100
|
requirement: !ruby/object:Gem::Requirement
|
101
101
|
requirements:
|
102
102
|
- - "~>"
|
103
103
|
- !ruby/object:Gem::Version
|
104
|
-
version:
|
104
|
+
version: 3.12.0
|
105
105
|
type: :development
|
106
106
|
prerelease: false
|
107
107
|
version_requirements: !ruby/object:Gem::Requirement
|
108
108
|
requirements:
|
109
109
|
- - "~>"
|
110
110
|
- !ruby/object:Gem::Version
|
111
|
-
version:
|
111
|
+
version: 3.12.0
|
112
112
|
- !ruby/object:Gem::Dependency
|
113
|
-
name:
|
113
|
+
name: rubocop-shopify
|
114
114
|
requirement: !ruby/object:Gem::Requirement
|
115
115
|
requirements:
|
116
116
|
- - "~>"
|
117
117
|
- !ruby/object:Gem::Version
|
118
|
-
version:
|
118
|
+
version: '2.14'
|
119
119
|
type: :development
|
120
120
|
prerelease: false
|
121
121
|
version_requirements: !ruby/object:Gem::Requirement
|
122
122
|
requirements:
|
123
123
|
- - "~>"
|
124
124
|
- !ruby/object:Gem::Version
|
125
|
-
version:
|
125
|
+
version: '2.14'
|
126
126
|
- !ruby/object:Gem::Dependency
|
127
|
-
name:
|
127
|
+
name: yard
|
128
128
|
requirement: !ruby/object:Gem::Requirement
|
129
129
|
requirements:
|
130
130
|
- - "~>"
|
131
131
|
- !ruby/object:Gem::Version
|
132
|
-
version:
|
132
|
+
version: 0.9.34
|
133
133
|
type: :development
|
134
134
|
prerelease: false
|
135
135
|
version_requirements: !ruby/object:Gem::Requirement
|
136
136
|
requirements:
|
137
137
|
- - "~>"
|
138
138
|
- !ruby/object:Gem::Version
|
139
|
-
version:
|
139
|
+
version: 0.9.34
|
140
140
|
description: |2
|
141
141
|
rack-ecg allows you to serve a page that shows you facts about your deployed
|
142
142
|
app to allow you to check that everything is running as it should: git
|
@@ -147,7 +147,8 @@ executables: []
|
|
147
147
|
extensions: []
|
148
148
|
extra_rdoc_files: []
|
149
149
|
files:
|
150
|
-
- ".github/
|
150
|
+
- ".github/dependabot.yml"
|
151
|
+
- ".github/workflows/test.yml"
|
151
152
|
- ".gitignore"
|
152
153
|
- ".rspec"
|
153
154
|
- ".rubocop.yml"
|
@@ -161,8 +162,11 @@ files:
|
|
161
162
|
- Rakefile
|
162
163
|
- bin/console
|
163
164
|
- bin/setup
|
165
|
+
- docker-compose.yml
|
164
166
|
- examples/basic.ru
|
165
167
|
- examples/checks.ru
|
168
|
+
- examples/custom_failure_status.ru
|
169
|
+
- examples/git_revision_check_replacement.ru
|
166
170
|
- examples/hook.ru
|
167
171
|
- examples/mounted_path.ru
|
168
172
|
- examples/parameters.ru
|
@@ -177,6 +181,7 @@ files:
|
|
177
181
|
- lib/rack/ecg/check/migration_version.rb
|
178
182
|
- lib/rack/ecg/check/redis_connection.rb
|
179
183
|
- lib/rack/ecg/check/sequel_connection.rb
|
184
|
+
- lib/rack/ecg/check/static.rb
|
180
185
|
- lib/rack/ecg/check_factory.rb
|
181
186
|
- lib/rack/ecg/check_registry.rb
|
182
187
|
- lib/rack/ecg/version.rb
|
@@ -196,14 +201,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
196
201
|
requirements:
|
197
202
|
- - ">="
|
198
203
|
- !ruby/object:Gem::Version
|
199
|
-
version: 2.
|
204
|
+
version: 2.7.0
|
200
205
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
201
206
|
requirements:
|
202
207
|
- - ">="
|
203
208
|
- !ruby/object:Gem::Version
|
204
209
|
version: '0'
|
205
210
|
requirements: []
|
206
|
-
rubygems_version: 3.
|
211
|
+
rubygems_version: 3.4.10
|
207
212
|
signing_key:
|
208
213
|
specification_version: 4
|
209
214
|
summary: Rack middleware serving a health check page
|